-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTunesModuleSource.m
199 lines (155 loc) · 6.61 KB
/
iTunesModuleSource.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
//
// iTunesModuleSource.m
// iTunesModule
//
// Created by James on 6/5/11.
// Copyright university of md college park 2011. All rights reserved.
//
#import "iTunesModuleSource.h"
@implementation iTunesModuleSource
#pragma mark -
#pragma mark Load iTunes Module objects.
// Load iTunesModuleRating objects.
-(NSArray *) ratings {
NSMutableArray *objects = [ NSMutableArray arrayWithCapacity:5 ];
QSObject *object = [ QSObject objectWithName:@"5 Stars" ];
[ object setObject:[ NSNumber numberWithInt:100 ] forType:iTunesModuleRating ];
//[ object setEnabled ];??
//[[ QSLibrarian sharedInstance ] setItem:object isOmitted:YES ];
// set identifier?
// [ object setIcon:[ QSResourceManager imageNamed:@"" ]];
[ objects addObject:object ];
object = [ QSObject objectWithName:@"4 Stars" ];
[ object setObject:[ NSNumber numberWithInt:80 ] forType:iTunesModuleRating ];
// set identifier?
// [ object setIcon:[ QSResourceManager imageNamed:@"" ]];
[ objects addObject:object ];
object = [ QSObject objectWithName:@"3 Stars" ];
[ object setObject:[ NSNumber numberWithInt:60 ] forType:iTunesModuleRating ];
// set identifier?
// [ object setIcon:[ QSResourceManager imageNamed:@"" ]];
[ objects addObject:object ];
object = [ QSObject objectWithName:@"2 Stars" ];
[ object setObject:[ NSNumber numberWithInt:40 ] forType:iTunesModuleRating ];
// set identifier?
// [ object setIcon:[ QSResourceManager imageNamed:@"" ]];
[ objects addObject:object ];
object = [ QSObject objectWithName:@"1 Star" ];
[ object setObject:[ NSNumber numberWithInt:20 ] forType:iTunesModuleRating ];
// set identifier?
// [ object setIcon:[ QSResourceManager imageNamed:@"" ]];
[ objects addObject:object ];
return objects;
}
// Load iTunes songs. Referenced "https://github.com/quicksilver/Plugins/blob/master/DeliciousLibrary/QSDeliciousLibraryModule_Source.m".
-(NSArray *) iTunesSongsWithLocation:(NSString *) location {
//detach thread?
// Parse iTunes library xml file.
/* http://stackoverflow.com/questions/4299840/parsing-large-plist-and-memory-footprint
NSData *data = [ NSData dataWithContentsOfFile:location ];
NSXMLParser *parser = [[ NSXMLParser alloc ] initWithData:data ];
iTunesParserDelegate *delegate = [[ iTunesParserDelegate alloc ] init ];
[ parser setDelegate:delegate ];
[ parser parse ];
[ parser release ];
[ delegate autorelease ];
return [ delegate objects ];//retain this?
*/
// For now:
NSDictionary *library = [ NSDictionary dictionaryWithContentsOfFile:location ];
NSMutableArray *objects = [ NSMutableArray arrayWithCapacity:100 ];
for ( NSDictionary *track in [[ library objectForKey:@"Tracks" ] allValues ]) {
NSDictionary *newTrack = [ NSDictionary dictionaryWithObjectsAndKeys:
[ track objectForKey:@"Name" ], @"Name",
[ track objectForKey:@"Track ID" ], @"Track ID",
[ track objectForKey:@"Artist" ], @"Artist",
[ track objectForKey:@"Genre" ], @"Genre",
// file location
nil ];
QSObject *object = [ QSObject objectWithName:[ track objectForKey:@"Name" ]];
[ object setObject:newTrack forType:iTunesModuleSong ];
[ object setDetails: [ track objectForKey:@"Artist" ]];
// set text, url type?
[ object setIdentifier:[ iTunesModuleSong stringByAppendingFormat:@"%d", [ track objectForKey:@"Track ID" ]]];
[ objects addObject:object ];
}
// add current song proxy
return objects;
}
// Get iTunes library location.
-(NSString *) iTunesLibraryLocation {
/*NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[ defaults addSuiteNamed:@"com.apple.iTunes" ];
NSString *location = [[ defaults stringForKey:@"NSNavLastRootDirectory" ] stringByAppendingString:@"/iTunes Music Library.xml" ];
[ defaults removeSuiteNamed:@"com.apple.iTunes" ];
*/
//for now:
NSString *location = @"/users/james/Music/iTunes/iTunes Music Library.xml";
return location;
}
#pragma mark -
#pragma mark iTunes Module source.
// Referenced "https://github.com/pjrobertson/1Password-Plugin/blob/master/OnePasswordSource.m".
- (BOOL)indexIsValidFromDate:(NSDate *)indexDate forEntry:(NSDictionary *)theEntry{
// NSLog(@"indexIsValidFromDate: called\n%@", [ theEntry description ]);
return NO;
if ( [[ theEntry objectForKey:@"name" ] isEqualToString:@"Ratings" ])
return YES;// isFirstRun/isStartingUp?
else if ( [ theEntry objectForKey:@"iTunes item" ]) { //[[ theEntry objectForKey:@"name" ] isEqualToString:@"Songs" ]) {
// Compare last modified date of iTunes library.
NSString *location = [ self iTunesLibraryLocation ];
NSError *err = nil;
NSFileManager *mgmt = [[ NSFileManager alloc ] init ];
NSDictionary *xmlAttributes = [ mgmt attributesOfItemAtPath:location error:&err ]; // retain this?
if ( err) {
[ mgmt release ];
return NO;
}
NSDate *modificationDate = [ xmlAttributes objectForKey:NSFileModificationDate ];
Boolean modified = ([ modificationDate compare:indexDate ] == NSOrderedAscending );
[ mgmt release ];
return modified;
}
return YES;
}
- (NSImage *) iconForEntry:(NSDictionary *)dict{
return nil;
}
// Return a unique identifier for an object (if you haven't assigned one before)
//- (NSString *)identifierForObject:(id <QSObject>)object{
// return nil;
//}
- (NSArray *) objectsForEntry:(NSDictionary *)theEntry{
// NSLog(@"objectsForEntry: called\n%@", [ theEntry description ]);
NSArray *objects = nil;
NSString *name = [ theEntry objectForKey:@"name" ];
/*if ( [[ theEntry objectForKey:@"name" ] isEqualToString:@"iTunes" ])
return [ theEntry objectForKey:@"children" ];
else*/
if ([ name isEqualToString:@"Ratings" ]) {
objects = [ self ratings ];
}
else if ([ name isEqualToString:@"Songs" ]) {
// NSLog(@"in Songs");
NSString *location = [ self iTunesLibraryLocation ];
// NSLog(@"%@", location);
objects = [ self iTunesSongsWithLocation:location ];
}
return objects;
}
- (BOOL)objectHasChildren:(QSObject *)object {
return NO;
}
// Object Handler Methods
/*
- (void)setQuickIconForObject:(QSObject *)object{
[object setIcon:nil]; // An icon that is either already in memory or easy to load
}
- (BOOL)loadIconForObject:(QSObject *)object{
return NO;
id data=[object objectForType:iTunesModuleType];
[object setIcon:nil];
return YES;
}
*/
@end