-
Notifications
You must be signed in to change notification settings - Fork 0
/
UKITunesLibrary.m
249 lines (194 loc) · 6.55 KB
/
UKITunesLibrary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// UKITunesLibrary.m
// TrackStorer
//
// Created by Uli Kusterer on 25.03.06.
// Copyright 2006 Uli Kusterer. All rights reserved.
//
#import "UKITunesLibrary.h"
#import "UKITunesPlaylist.h"
@implementation UKITunesLibrary
+(id) defaultLibrary
{
static UKITunesLibrary* sharedInstance = nil;
if( !sharedInstance )
sharedInstance = [[[self class] alloc] init];
return sharedInstance;
}
-(id) init
{
self = [super init];
if( self )
{
NSPropertyListFormat format = NSPropertyListXMLFormat_v1_0;
NSString* errorString = nil;
NSData* fileData = [NSData dataWithContentsOfFile: [self libraryPath]];
libraryFileDictionary = [[NSPropertyListSerialization propertyListFromData: fileData
mutabilityOption: NSPropertyListMutableContainers
format: &format errorDescription: &errorString] retain];
if( errorString )
[errorString release];
}
return( self );
}
-(void) dealloc
{
[libraryFileDictionary release];
libraryFileDictionary = nil;
[playlistsByID release];
playlistsByID = nil;
[playlistIDs release];
playlistIDs = nil;
[playlistHierarchy release];
playlistHierarchy = nil;
// libraryPlaylist is stored nonretained, so no need to release here.
libraryPlaylist = nil;
[super dealloc];
}
-(NSString*) libraryPath
{
return [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
}
-(UKITunesPlaylist*) playlistAtIndex: (int)n
{
NSDictionary* dict = [[libraryFileDictionary objectForKey: @"Playlists"] objectAtIndex: n];
UKITunesPlaylist* pl = [[[UKITunesPlaylist alloc] initWithPlaylistDictionary: dict owner: self] autorelease];
if( !libraryPlaylist && [pl isMainLibrary] )
libraryPlaylist = dict; // Also initialises the "library" playlist while it's at it. No need to retain, as the libraryFileDictionary already does that for us.
return pl;
}
-(UKITunesPlaylist*) playlistWithID: (NSString*)nme
{
[self setUpLookupTables];
// Look up playlist by persistent ID based on look-up-table:
NSDictionary* foundPL = [playlistsByID objectForKey: nme];
if( !foundPL )
{
NSLog(@"foundPL = %@", foundPL);
return nil;
}
return [[[UKITunesPlaylist alloc] initWithPlaylistDictionary: foundPL owner: self] autorelease];
}
-(NSArray*) playlistIDs
{
if( !playlistIDs )
[self setUpLookupTables];
return playlistIDs;
}
-(int) playlistCount
{
NSArray* playLists = [libraryFileDictionary objectForKey: @"Playlists"];
return [playLists count];
}
-(NSArray*) playlistHierarchy
{
if( playlistHierarchy )
return playlistHierarchy;
NSArray* playLists = [libraryFileDictionary objectForKey: @"Playlists"];
NSEnumerator* enny = [playLists objectEnumerator];
int plCount = [playLists count];
NSMutableDictionary* playlistsByPersistentIDs = [NSMutableDictionary dictionaryWithCapacity: plCount];
NSMutableArray* rootLevel = [[NSMutableArray alloc] initWithCapacity: plCount];
NSDictionary* currPL = nil;
while(( currPL = [enny nextObject] ))
{
NSString* persistentID = [currPL objectForKey: @"Playlist Persistent ID"];
NSDictionary* currEntry = [NSMutableDictionary dictionaryWithObjectsAndKeys:
/*currPL, @"playlistDictionary",*/
persistentID, @"persistentID",
[NSMutableArray arrayWithCapacity: plCount], @"subItems",
nil];
[playlistsByPersistentIDs setObject: currEntry forKey: persistentID];
NSString* parentPersistentID = [currPL objectForKey: @"Parent Persistent ID"];
if( parentPersistentID )
{
NSMutableDictionary* parent = [playlistsByPersistentIDs objectForKey: parentPersistentID];
NSMutableArray* parentSubItems = [parent objectForKey: @"subItems"];
[parentSubItems addObject: currEntry];
}
else
[rootLevel addObject: currEntry];
}
playlistHierarchy = rootLevel;
}
-(NSDictionary*) trackDictionaryByID: (NSNumber*)trackID
{
NSDictionary* tracksDict = [libraryFileDictionary objectForKey: @"Tracks"];
if( !tracksDict )
{
NSLog(@"tracksDict == NULL");
NSLog(@"libraryFileDictionary = %@", libraryFileDictionary);
return nil;
}
NSDictionary* dict = [tracksDict objectForKey: [trackID stringValue]];
if( !dict )
{
NSLog(@"!dict");
return nil;
}
return dict;
}
-(BOOL) synchronize
{
[libraryFileDictionary release];
libraryFileDictionary = nil;
[playlistHierarchy release];
playlistHierarchy = nil;
NSPropertyListFormat format = NSPropertyListXMLFormat_v1_0;
NSString* errorString = nil;
NSData* fileData = [NSData dataWithContentsOfFile: [self libraryPath]];
libraryFileDictionary = [[NSPropertyListSerialization propertyListFromData: fileData
mutabilityOption: NSPropertyListMutableContainers
format: &format errorDescription: &errorString] retain];
if( errorString )
[errorString release];
[self rebuildLookupTables];
}
-(void) rebuildLookupTables
{
libraryPlaylist = nil; // Causes rebuild in setUpLookupTables.
[self setUpLookupTables];
}
-(void) setUpLookupTables
{
// This *must* rebuild if libraryPlaylist is NIL and all others have already
// been loaded because that's the trick rebuildLookupTables uses.
if( playlistsByID && playlistIDs && libraryPlaylist )
return; // Already did our work, no need to burn cycles.
NSArray* playLists = [libraryFileDictionary objectForKey: @"Playlists"];
NSEnumerator* enny = [playLists objectEnumerator];
if( playlistsByID )
{
[playlistsByID release];
playlistsByID = nil;
}
if( playlistIDs )
{
[playlistIDs release];
playlistIDs = nil;
}
playlistsByID = [[NSMutableDictionary alloc] init];
playlistIDs = [[NSMutableArray alloc] initWithCapacity: [playLists count]];
NSDictionary* currPL = nil;
while(( currPL = [enny nextObject] ))
{
[playlistsByID setObject: currPL forKey: [currPL objectForKey: @"Playlist Persistent ID"]]; // Initialise by-id lookup table.
[playlistIDs addObject: [currPL objectForKey: @"Playlist Persistent ID"]]; // Initialise ordered list of persistent ids.
NSNumber* isMasterLibraryObj = [currPL objectForKey: @"Master"];
BOOL isMasterLibrary = isMasterLibraryObj != nil && [isMasterLibraryObj boolValue];
if( isMasterLibrary )
libraryPlaylist = currPL; // Also initialises the "library" playlist while it's at it. No need to retain, as the libraryFileDictionary already does that for us.
}
}
-(UKITunesPlaylist*) mainLibraryPlaylist
{
if( !libraryPlaylist )
[self setUpLookupTables];
return [[[UKITunesPlaylist alloc] initWithPlaylistDictionary: libraryPlaylist owner: self] autorelease];
}
-(NSURL*) musicFolderURL
{
NSString* urlStr = [libraryFileDictionary objectForKey: @"Music Folder"];
return [NSURL URLWithString: urlStr];
}
@end