-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFSiTunesPlaylist.m
421 lines (359 loc) · 12.9 KB
/
IFSiTunesPlaylist.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
Copyright (c) 2007-2015, Marcus Müller <znek@mulle-kybernetik.com>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Mulle kybernetiK nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#import "common.h"
#import "IFSiTunesPlaylist.h"
#import "IFSM3UPlaylist.h"
#import "IFSiTunesLibrary.h"
#import "IFSiTunesTrack.h"
#import "IFSFormatter.h"
#import "NSObject+FUSEOFS.h"
#import "FUSEOFSMemoryContainer.h"
#import "IFSFormatFile.h"
@interface IFSiTunesPlaylist (Private)
- (BOOL)hasTrackFormatFile;
- (BOOL)showTrackFormatFile;
- (NSString *)trackFormatFileName;
- (void)generatePrettyTrackNames;
- (void)setName:(NSString *)_name;
- (void)addTrack:(IFSiTunesTrack *)_track withName:(NSString *)_name;
@end
@implementation IFSiTunesPlaylist
static BOOL doDebug = NO;
static BOOL showPersistentID = NO;
static BOOL showTrackFormatFile = YES;
static BOOL showM3UPlaylistFile = NO;
static NSString *trackFormatFileName = @"PlaylistsTrackFormat.txt";
+ (void)initialize {
static BOOL didInit = NO;
NSUserDefaults *ud;
if (didInit) return;
didInit = YES;
ud = [NSUserDefaults standardUserDefaults];
doDebug = [ud boolForKey:@"iTunesFileSystemDebugEnabled"];
showPersistentID = [ud boolForKey:@"ShowPersistentIDs"];
showTrackFormatFile = [ud boolForKey:@"ShowFormatFiles"];
showM3UPlaylistFile = [ud boolForKey:@"ShowM3UPlaylistFiles"];
}
- (id)init {
self = [super init];
if (self) {
self->savedTracks = [[NSMutableArray alloc] initWithCapacity:10];
self->tracks = [[NSMutableArray alloc] initWithCapacity:10];
self->trackNames = [[NSMutableArray alloc] initWithCapacity:10];
self->childrenMap = [[NSMutableDictionary alloc] initWithCapacity:5];
// NOTE: cannot initialize trackFormatFile here, needs lazy init
self->shadowFolder = [[FUSEOFSMemoryContainer alloc] init];
self->modificationDate = [[NSDate date] retain];
}
return self;
}
- (id)initWithLibraryRepresentation:(NSDictionary *)_list
lib:(IFSiTunesLibrary *)_lib
{
self = [self init];
if (self) {
BOOL isFolder;
self->persistentId = [[_list objectForKey:kPlaylistPersistentID] copy];
self->parentId = [[_list objectForKey:kPlaylistParentPersistentID] copy];
[self setName:[_list objectForKey:kPlaylistName]];
isFolder = [[_list objectForKey:kPlaylistIsFolder] boolValue];
if (!isFolder) {
BOOL itemIsDictionary = YES;
NSArray *items = [_list objectForKey:kPlaylistItems];
if (!items) {
items = [_list objectForKey:kPlaylistTrackIDs];
itemIsDictionary = NO;
}
NSUInteger i, count = [items count];
for (i = 0; i < count; i++) {
id item = [items objectAtIndex:i];
NSString *trackID;
if (itemIsDictionary)
trackID = [[item objectForKey:kTrackID] description];
else
trackID = (NSString *)item;
IFSiTunesTrack *trk = [_lib trackWithID:trackID];
if (!trk) {
/* NOTE: Rolf's library really sports these effects, seems to be
* limited to Podcasts only.
*/
if (doDebug)
NSLog(@"Playlist[%@]: found no track item for #%@",
self->name, trackID);
continue;
}
[self->savedTracks addObject:trk];
}
[self generatePrettyTrackNames];
}
}
return self;
}
- (void)dealloc {
[self->persistentId release];
[self->parentId release];
[self->name release];
[self->savedTracks release];
[self->tracks release];
[self->trackNames release];
[self->childrenMap release];
[self->shadowFolder release];
[self->trackFormatFile release];
[self->m3uPlaylist release];
[self->modificationDate release];
[super dealloc];
}
/* private */
- (BOOL)hasTrackFormatFile {
return self->persistentId != nil && self->trackFormatFile;
}
- (BOOL)showTrackFormatFile {
if (![self hasTrackFormatFile])
return NO;
return showTrackFormatFile;
}
- (void)generatePrettyTrackNames {
if (!self->trackFormatFile) {
self->trackFormatFile = [[IFSFormatFile alloc]
initWithDefaultTemplate:@"PlaylistsTrackFormat"
templateId:self->persistentId];
}
[self->tracks removeAllObjects];
[self->trackNames removeAllObjects];
[self->modificationDate release];
self->modificationDate = [[NSDate date] retain];
NSArray *childrenKeys = [self->childrenMap allKeys];
NSUInteger i, count = [childrenKeys count];
for (i = 0; i < count; i++) {
NSString *childKey = [childrenKeys objectAtIndex:i];
IFSiTunesPlaylist *child = [self->childrenMap objectForKey:childKey];
if (![child persistentId]) {
// remove virtual child
[self->childrenMap removeObjectForKey:childKey];
}
}
IFSFormatter *formatter = [self->trackFormatFile getFormatter];
if ([formatter isPathFormat]) {
// formatter describes a path, which can lead to a whole hierarchy
// of virtual playlists.
// for every track in this current playlist we need to traverse its
// formatter path and possibly create and add any virtual playlists
// necessary in that process.
NSUInteger i, count = [self->savedTracks count];
for (i = 0; i < count; i++) {
IFSiTunesTrack *trk = [self->savedTracks objectAtIndex:i];
NSUInteger trkIndex = i + 1;
[trk setPlaylistNumber:trkIndex];
NSArray *pathComponents = [formatter
pathComponentsByFormattingObject:trk];
IFSiTunesPlaylist *pl = self;
NSString *pc;
NSUInteger k, pcCount = [pathComponents count];
for (k = 0; k < (pcCount - 1); k++) {
pc = [pathComponents objectAtIndex:k];
IFSiTunesPlaylist *nextPl = [pl lookupPathComponent:pc inContext:nil];
if (!nextPl || ![nextPl isContainer]) {
nextPl = [[IFSiTunesPlaylist alloc] init];
[nextPl setName:pc];
[pl addChild:nextPl withName:pc];
[nextPl release];
}
pl = nextPl;
}
[pl addTrack:trk withName:[pathComponents objectAtIndex:k]];
}
}
else {
NSUInteger i, count = [self->savedTracks count];
for (i = 0; i < count; i++) {
IFSiTunesTrack *trk = [self->savedTracks objectAtIndex:i];
NSUInteger trkIndex = i + 1;
[trk setPlaylistNumber:trkIndex];
NSString *tn = [formatter stringValueByFormattingObject:trk];
[self addTrack:trk withName:tn];
}
}
}
/* accessors */
- (void)setName:(NSString *)_name {
_name = [[_name properlyEscapedFSRepresentation] copy];
[self->name release];
self->name = _name;
}
- (NSString *)name {
if (!showPersistentID || !self->persistentId)
return self->name;
return [NSString stringWithFormat:@"%@[%@]", self->name, self->persistentId];
}
- (NSDate *)modificationDate {
return self->modificationDate;
}
- (id)persistentId {
return self->persistentId;
}
- (id)parentId {
return self->parentId;
}
- (NSArray *)allTracks {
return self->savedTracks;
}
- (void)addTrack:(IFSiTunesTrack *)_track withName:(NSString *)_name {
[self->tracks addObject:_track];
[self->trackNames addObject:_name];
if (showM3UPlaylistFile && !self->m3uPlaylist) {
self->m3uPlaylist = [[IFSM3UPlaylist alloc] initWithPlaylist:self
useRelativePaths:YES];
}
}
- (NSArray *)tracks {
return self->tracks;
}
- (NSUInteger)count {
return [self->tracks count];
}
- (IFSiTunesTrack *)trackAtIndex:(NSUInteger)_idx {
return [self->tracks objectAtIndex:_idx];
}
- (NSArray *)trackNames {
return self->trackNames;
}
- (void)addChild:(IFSiTunesPlaylist *)_child withName:(NSString *)_name {
[self->childrenMap setObject:_child
forKey:[_name properlyEscapedFSRepresentation]];
}
- (NSArray *)children {
return [self->childrenMap allValues];
}
/* FUSEOFS */
- (id)lookupPathComponent:(NSString *)_pc inContext:(id)_ctx {
if ([_pc isEqualToString:trackFormatFileName] &&
[self hasTrackFormatFile])
{
return self->trackFormatFile;
}
if (self->m3uPlaylist && [_pc isEqualToString:[self->m3uPlaylist fileName]])
{
return self->m3uPlaylist;
}
id result = [self->childrenMap objectForKey:_pc];
if (result)
return result;
NSUInteger idx = [[self trackNames] indexOfObject:_pc];
if (idx != NSNotFound)
result = [self trackAtIndex:idx];
if (!result && [self hasTrackFormatFile])
result = [self->shadowFolder lookupPathComponent:_pc inContext:_ctx];
return result;
}
- (NSArray *)containerContents {
if (![self showTrackFormatFile] && !self->m3uPlaylist) {
// in this case, we can possibly eliminate an unnecessary copy
if ([self->childrenMap count] && ![[self trackNames] count])
return [self->childrenMap allKeys];
else if (![self->childrenMap count] && [[self trackNames] count])
return [self trackNames];
}
NSMutableArray *names = [[NSMutableArray alloc]
initWithArray:self->trackNames];
[names addObjectsFromArray:[self->childrenMap allKeys]];
if ([self showTrackFormatFile]) {
[names addObject:trackFormatFileName];
#if 0
[names addObjectsFromArray:[self->shadowFolder containerContents]];
#endif
}
if (self->m3uPlaylist)
[names addObject:[self->m3uPlaylist fileName]];
return [names autorelease];
}
- (BOOL)isContainer {
return YES;
}
- (BOOL)isMutable {
return YES;
}
#if 0
// NOTE: in theory this should do the trick of hiding the .fpbf extension,
// however this flag has been removed from Finder.h altogether and it doesn't
// work in practice (tested on 10.5.6)
- (NSDictionary *)finderAttributes {
NSNumber *finderFlags = [NSNumber numberWithLong:0x0010];
return [NSDictionary dictionaryWithObject:finderFlags
forKey:kGMUserFileSystemFinderFlagsKey];
}
#endif
- (NSDictionary *)fileAttributes {
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithCapacity:5];
[attrs setObject:self->modificationDate forKey:NSFileCreationDate];
[attrs setObject:self->modificationDate forKey:NSFileModificationDate];
[attrs setObject:NSFileTypeDirectory forKey:NSFileType];
[attrs setObject:[NSNumber numberWithBool:YES] forKey:NSFileExtensionHidden];
NSNumber *perm = [NSNumber numberWithInt:[self isMutable] ? 0777 : 0555];
[attrs setObject:perm forKey:NSFilePosixPermissions];
return attrs;
}
#if 0
- (BOOL)createFileNamed:(NSString *)_name
withAttributes:(NSDictionary *)_attrs
{
if (![self hasTrackFormatFile])
return NO;
if (![_name isEqualTo:trackFormatFileName])
return NO;
return YES;
}
#endif
- (BOOL)createContainerNamed:(NSString *)_name
withAttributes:(NSDictionary *)_attrs
{
return [self->shadowFolder createContainerNamed:_name withAttributes:_attrs];
}
- (BOOL)writeFileNamed:(NSString *)_name withData:(NSData *)_data {
if (![self hasTrackFormatFile])
return NO;
if (![_name isEqualToString:trackFormatFileName])
return [self->shadowFolder writeFileNamed:_name withData:_data];
[self->trackFormatFile setFileContents:_data];
[self generatePrettyTrackNames];
return YES;
}
- (BOOL)removeItemNamed:(NSString *)_name {
if (![self hasTrackFormatFile])
return NO;
if ([_name isEqualToString:trackFormatFileName]) {
[self->trackFormatFile remove];
[self generatePrettyTrackNames];
return YES;
}
return [self->shadowFolder removeItemNamed:_name];
}
/* debugging */
- (NSString *)description {
return [NSString stringWithFormat:@"<%s %p: name:%@ #tracks:%ld>",
object_getClassName(self), self,
[self name], (unsigned long)[self count]];
}
@end /* IFSiTunesPlaylist */