-
Notifications
You must be signed in to change notification settings - Fork 1
/
IFSiTunesFrameworkLibrary.m
343 lines (284 loc) · 9.46 KB
/
IFSiTunesFrameworkLibrary.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
/*
Copyright (c) 2007-2021, Marcus Müller <[email protected]>.
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 <iTunesLibrary/iTunesLibrary.h>
#import "IFSiTunesFrameworkLibrary.h"
#import "IFSiTunesLibrary.h"
#import "NSObject+FUSEOFS.h"
#import "NSString+Extensions.h"
#import "FUSEOFSMemoryContainer.h"
#import "IFSiTunesTrack.h"
#import "IFSiTunesPlaylist.h"
#import "IFSM3UPlaylist.h"
@interface IFSiTunesTrack (ITLib)
- (id)initWithITLibMediaItem:(ITLibMediaItem *)_item;
@end
@interface IFSiTunesPlaylist (ITLib)
- (id)initWithITLibPlaylist:(ITLibPlaylist *)_playlist
lib:(IFSiTunesLibrary *)_lib;
@end
// FIXME
@interface IFSiTunesLibrary (Private)
- (BOOL)doDebug;
- (BOOL)useM3UPlaylists;
@end
@implementation IFSiTunesFrameworkLibrary
- (id)init {
self = [super init];
if (self) {
NSError * error = nil;
self->lib = [[ITLibrary alloc] initWithAPIVersion:@"1.0" error:&error];
if (error) {
NSLog(@"ERROR: %@", error);
[self autorelease];
return nil;
}
[self reload];
}
return self;
}
- (void)dealloc {
[self->lib release];
self->lib = nil;
[super dealloc];
}
- (void)reload {
if (!self->lib)
return;
if ([self doDebug])
NSLog(@"%s", __PRETTY_FUNCTION__);
[self->plMap removeAllObjects];
[self->m3uMap removeAllObjects];
[self->trackMap removeAllObjects];
for (ITLibMediaItem *item in [self->lib allMediaItems]) {
IFSiTunesTrack *track = [[IFSiTunesTrack alloc]
initWithITLibMediaItem:item];
if ([track isUsable])
[self->trackMap setObject:track forKey:[item persistentID]];
[track release];
}
NSArray *allPlaylists = [self->lib allPlaylists];
NSUInteger count = [allPlaylists count];
NSMutableDictionary *idPlMap = [[NSMutableDictionary alloc]
initWithCapacity:count];
for (ITLibPlaylist *playlist in allPlaylists) {
IFSiTunesPlaylist *pl = [[IFSiTunesPlaylist alloc]
initWithITLibPlaylist:playlist
lib:self];
// only record top-level playlist, if playlist isn't a folder itself
if (![pl parentId]) {
[self->plMap setItem:pl
forName:[self burnFolderNameFromFolderName:[pl name]]];
}
id plId = [pl persistentId];
if (plId)
[idPlMap setObject:pl forKey:plId];
[pl release];
}
// connect children to their parents
if ([idPlMap count]) {
NSArray *ids = [idPlMap allKeys];
for (id plId in ids) {
IFSiTunesPlaylist *pl = [idPlMap objectForKey:plId];
id parentId = [pl parentId];
if (parentId) {
IFSiTunesPlaylist *parent = [idPlMap objectForKey:parentId];
if (parent) {
[parent addChild:pl
withName:[self burnFolderNameFromFolderName:[pl name]]];
}
else {
NSLog(@"ERROR: didn't find parent playlist of '%@'?!", pl);
}
}
}
}
if ([self useM3UPlaylists]) {
for (IFSiTunesPlaylist *pl in [idPlMap allValues]) {
if (![[pl allTracks] count])
continue;
IFSM3UPlaylist *m3uPl = [[IFSM3UPlaylist alloc]
initWithPlaylist:pl
useRelativePaths:NO];
[self->m3uMap setItem:m3uPl forName:[m3uPl fileName]];
[m3uPl release];
}
}
[idPlMap release];
[self reloadVirtualMaps];
}
- (void)close {
}
- (NSString *)name {
return @"iTunes Framework";
}
/* debugging */
- (NSString *)description {
return [NSString stringWithFormat:@"<%s %p: name:%@>",
object_getClassName(self), self,
[self name]];
}
/* FIXME */
- (NSString *)mountPoint {
return nil;
}
@end
@interface IFSiTunesTrack (Private)
- (void)setAttributes:(NSDictionary *)_attributes;
- (void)setPrettyName:(NSString *)_prettyName;
- (void)setTrackNumber:(NSUInteger)_trackNumber;
- (void)setUrl:(NSURL *)_url;
@end
@implementation IFSiTunesTrack (ITLib)
- (id)initWithITLibMediaItem:(ITLibMediaItem *)_item {
self = [super init];
if (self) {
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSString *locationReplacePrefix = [ud stringForKey:@"LocationReplacePrefix"];
NSString *locationDestinationPrefix = [ud stringForKey:@"LocationDestinationPrefix"];
BOOL useSymbolicLinks = [ud boolForKey:@"SymbolicLinks"];
ITLibAlbum *_album = [_item album];
ITLibMediaItemVideoInfo *_info = [_item videoInfo];
NSString *name = [[_item artist] name];
if (name && [name length] > 0)
self->artist = [[name properlyEscapedFSRepresentation] copy];
name = [_album title];
if (name && [name length] > 0)
self->album = [[name properlyEscapedFSRepresentation] copy];
name = [_album albumArtist];
if (name && [name length] > 0)
self->albumArtist = [[name properlyEscapedFSRepresentation] copy];
name = [_item composer];
if (name && [name length] > 0)
self->composer = [[name properlyEscapedFSRepresentation] copy];
name = [_item genre];
if (name && [name length] > 0)
self->genre = [[name properlyEscapedFSRepresentation] copy];
self->grouping = [[[_item grouping] properlyEscapedFSRepresentation] copy];
// FIXME
self->series = [[[_info series] properlyEscapedFSRepresentation] copy];
self->comments = [[[_item comments] properlyEscapedFSRepresentation] copy];
self->rating = [_item rating];
self->discNumber = [_album discNumber];
if (self->discNumber == 0)
self->discNumber = 1;
self->discCount = [_album discCount];
if (self->discCount == 0)
self->discCount = 1;
self->playCount = [_item playCount];
self->year = [_item year];
self->bitRate = [_item bitrate];
self->sampleRate = [_item sampleRate];
self->seasonNumber = [_info season];
self->episodeNumber = [_info episode];
[self setTrackNumber:[_item trackNumber]];
name = [_item title];
if (name && [name length] > 0) {
[self setPrettyName:[name properlyEscapedFSRepresentation]];
}
else {
NSLog(@"WARN: item without name! %@", _item);
[self setPrettyName:@"Empty"];
}
NSString *location = [[_item location] absoluteString];
if (location) {
if ([location hasPrefix:@"file"]) {
self->ext = [[location pathExtension] copy];
}
else {
/* http:// stream address... */
self->ext = @"webloc";
}
if (locationReplacePrefix) {
NSRange r;
r = [location rangeOfString:locationReplacePrefix];
if (r.location != NSNotFound) {
location = [location substringFromIndex:NSMaxRange(r)];
location = [locationDestinationPrefix
stringByAppendingString:location];
}
}
[self setUrl:[NSURL URLWithString:location]];
}
NSMutableDictionary *attrs = [[NSMutableDictionary alloc]
initWithCapacity:3];
if ([[self url] isFileURL]) {
[attrs setObject:[NSNumber numberWithLongLong:[_item fileSize]] forKey:NSFileSize];
}
id tmp = [_item addedDate];
if (tmp)
[attrs setObject:tmp forKey:NSFileCreationDate];
tmp = [_item modifiedDate];
if (tmp) {
[attrs setObject:tmp forKey:NSFileModificationDate];
}
else {
tmp = [_item lastPlayedDate];
if (tmp)
[attrs setObject:tmp forKey:NSFileModificationDate];
}
if (useSymbolicLinks)
[attrs setObject:NSFileTypeSymbolicLink forKey:NSFileType];
else
[attrs setObject:NSFileTypeRegular forKey:NSFileType];
[self setAttributes:attrs];
[attrs release];
}
return self;
}
@end
@interface IFSiTunesPlaylist (Private)
- (void)generatePrettyTrackNames;
- (void)setName:(NSString *)_name;
@end
@implementation IFSiTunesPlaylist (ITLib)
- (id)initWithITLibPlaylist:(ITLibPlaylist *)_playlist
lib:(IFSiTunesLibrary *)_lib
{
self = [self init];
if (self) {
[self setName:[_playlist name]];
self->persistentId = [[_playlist persistentID] copy];
self->parentId = [[_playlist parentID] copy];
BOOL isFolder = ([_playlist kind] == ITLibPlaylistKindFolder);
if (!isFolder) {
for (ITLibMediaItem *item in [_playlist items]) {
NSNumber *trackID = [item persistentID];
IFSiTunesTrack *trk = [_lib trackWithID:trackID];
if (!trk) {
if ([_lib doDebug])
NSLog(@"Playlist[%@]: found no track item for #%@",
self->name, trackID);
continue;
}
[self->savedTracks addObject:trk];
}
[self generatePrettyTrackNames];
}
}
return self;
}
@end