Quick Remote API update for an enumeration of all supported playlist classes.
[MenuTunes.git] / iTunesRemote.m
1 #import "iTunesRemote.h"
2
3 @implementation iTunesRemote
4
5 + (id)remote
6 {
7     return [[[iTunesRemote alloc] init] autorelease];
8 }
9
10 - (NSString *)remoteTitle
11 {
12     return @"iTunes Remote";
13 }
14
15 - (NSString *)remoteInformation
16 {
17     return @"Default MenuTunes plugin to control iTunes, by iThink Software.";
18 }
19
20 - (NSImage *)remoteIcon
21 {
22     return nil;
23 }
24
25 - (BOOL)begin
26 {
27     iTunesPSN = [self iTunesPSN];
28     
29     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
30     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
31     
32     return YES;
33 }
34
35 - (BOOL)halt
36 {
37     iTunesPSN.highLongOfPSN = kNoProcess;
38
39     //Unregister for application termination in NSWorkspace
40     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
41
42     return YES;
43 }
44
45 - (NSString *)playerFullName
46 {
47     return @"iTunes";
48 }
49
50 - (NSString *)playerSimpleName
51 {
52     return @"iTunes";
53 }
54
55 - (NSDictionary *)capabilities
56 {
57     return [NSDictionary dictionaryWithObjectsAndKeys:
58                 [NSNumber numberWithBool: YES], @"Remote",
59                 [NSNumber numberWithBool: YES], @"Basic Track Control",
60                 [NSNumber numberWithBool: YES], @"Track Information",
61                 [NSNumber numberWithBool: YES], @"Track Navigation",
62                 [NSNumber numberWithBool: YES], @"Upcoming Songs",
63                 [NSNumber numberWithBool: YES], @"Playlists",
64                 [NSNumber numberWithBool: YES], @"Volume",
65                 [NSNumber numberWithBool: YES], @"Shuffle",
66                 [NSNumber numberWithBool: YES], @"Repeat Modes",
67                 [NSNumber numberWithBool: YES], @"Equalizer",
68                 [NSNumber numberWithBool: YES], @"Track Rating",
69                 nil];
70 }
71
72 - (ITMTRemotePlayerRunningState)playerRunningState
73 {
74     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
75     int i;
76     int count = [apps count];
77     
78     for (i = 0; i < count; i++) {
79         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
80             return ITMTRemotePlayerRunning;
81         }
82     }
83     return ITMTRemotePlayerNotRunning;
84 }
85
86 - (ITMTRemotePlayerPlayingState)playerPlayingState
87 {
88     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
89     
90     switch (result)
91     {
92         default:
93         case 'kPSS':
94             return ITMTRemotePlayerStopped;
95         case 'kPSP':
96             return ITMTRemotePlayerPlaying;
97         case 'kPSp':
98             return ITMTRemotePlayerPaused;
99         case 'kPSR':
100             return ITMTRemotePlayerRewinding;
101         case 'kPSF':
102             return ITMTRemotePlayerForwarding;
103     }
104     
105     return ITMTRemotePlayerStopped;
106 }
107
108 - (NSArray *)playlists
109 {
110     long i = 0;
111     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
112     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
113     
114     for (i = 1; i <= numPlaylists; i++) {
115         const long j = i;
116         NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() } }",(unsigned long)j];
117         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
118         [playlists addObject:theObj];
119     }
120     return [playlists autorelease];
121 }
122
123 - (int)numberOfSongsInPlaylistAtIndex:(int)index
124 {
125     return [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:[NSString stringWithFormat:@"kocl:type('cTrk'), '----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
126 }
127
128 - (ITMTRemotePlayerPlaylistClass)classOfPlaylistAtIndex:(int)index
129 {
130     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     
132     // ADD SUPPORT FOR RETURNING A ITMTRemotePlayerLibraryPlaylist WHEN PLAYLIST IS LIBRARY.
133
134     switch (realResult)
135            {
136            case 'cRTP':
137                   return ITMTRemotePlayerRadioPlaylist;
138                   break;
139            default:
140                   return ITMTRemotePlayerPlaylist;
141            }
142 }
143
144 - (int)currentPlaylistIndex
145 {
146     int result;
147     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
148     return result;
149 }
150
151 - (NSString *)songTitleAtIndex:(int)index
152 {
153     return [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",index] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
154 }
155
156 - (int)currentSongIndex
157 {
158     int result;
159     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
160     return result;
161 }
162
163 - (NSString *)currentSongTitle
164 {
165     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
166 }
167
168 - (NSString *)currentSongArtist
169 {
170     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
171 }
172
173 - (NSString *)currentSongAlbum
174 {
175     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
176 }
177
178 - (NSString *)currentSongGenre
179 {
180     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
181 }
182
183 - (NSString *)currentSongLength
184 {
185     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
186 }
187
188 - (NSString *)currentSongRemaining
189 {
190     long duration = [[ITAppleEventCenter sharedCenter]
191                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
192     long current = [[ITAppleEventCenter sharedCenter]
193                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
194
195     return [[NSNumber numberWithLong:duration - current] stringValue];
196 }
197
198 - (float)currentSongRating
199 {
200     int realResult = [[ITAppleEventCenter sharedCenter]
201                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
202
203     return realResult / 100;
204 }
205
206 - (BOOL)setCurrentSongRating:(float)rating
207 {
208     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), ----:obj { form:'prop', want:type('prop'), seld:type('pRte'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() } } }",(long)rating*100,[self currentSongIndex],[self currentPlaylistIndex]] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
209     return YES;
210 }
211
212 - (BOOL)equalizerEnabled
213 {
214     return NO;
215 }
216
217 - (BOOL)setEqualizerEnabled:(BOOL)enabled
218 {
219     return NO;
220 }
221
222 - (NSArray *)eqPresets
223 {
224     int i;
225     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
226     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
227     
228     for (i = 1; i <= numPresets; i++) {
229         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() } }",i] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
230         if (theObj) {
231             [presets addObject:theObj];
232         }
233     }
234     return [presets autorelease];
235 }
236
237 - (int)currentEQPresetIndex
238 {
239     int result;
240     result = [[ITAppleEventCenter sharedCenter]
241                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
242     return result;
243 }
244
245 - (float)volume
246 {
247     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
248     return vol / 100;
249 }
250
251 - (BOOL)setVolume:(float)volume
252 {
253     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), ----:obj { form:'prop', want:type('prop'), seld:type('pVol'), from:'null'() }",(long)volume*100] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
254     return NO;
255 }
256
257 - (BOOL)shuffleEnabled
258 {
259     int result = [[ITAppleEventCenter sharedCenter]
260                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
261     return result;
262 }
263
264 - (BOOL)setShuffleEnabled:(BOOL)enabled
265 {
266     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
267 }
268
269 - (ITMTRemotePlayerRepeatMode)repeatMode
270 {
271     FourCharCode m00f;
272     int result;
273     m00f = [[ITAppleEventCenter sharedCenter]
274                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
275
276     switch (m00f)
277            {
278            case 'kRp0':
279                   result = ITMTRemotePlayerRepeatOff;
280                   break;
281            case 'kRp1':
282                   result = ITMTRemotePlayerRepeatOne;
283                   break;
284            case 'kRpA':
285                   result = ITMTRemotePlayerRepeatAll;
286                   break;
287            }
288     
289     return result;
290 }
291
292 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
293 {
294     FourCharCode m00f;
295     switch (repeatMode)
296            {
297            case ITMTRemotePlayerRepeatOff:
298                   m00f = 'kRp0';
299                   break;
300            case ITMTRemotePlayerRepeatOne:
301                   m00f = 'kRp1';
302                   break;
303            case ITMTRemotePlayerRepeatAll:
304                   m00f = 'kRpA';
305                   break;
306            }
307
308     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('pRpt'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",m00f] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
309
310 }
311
312 - (BOOL)play
313 {
314     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
315     return YES;
316 }
317
318 - (BOOL)pause
319 {
320     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
321     return YES;
322 }
323
324 - (BOOL)goToNextSong
325 {
326     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
327     return YES;
328 }
329
330 - (BOOL)goToPreviousSong
331 {
332     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
333     return YES;
334 }
335
336 - (BOOL)forward
337 {
338     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
339     return YES;
340 }
341
342 - (BOOL)rewind
343 {
344     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
345     return YES;
346 }
347
348 - (BOOL)switchToPlaylistAtIndex:(int)index
349 {
350     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
351     return YES;
352 }
353
354 - (BOOL)switchToSongAtIndex:(int)index
355 {
356     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:() } }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
357     return YES;
358 }
359
360 - (BOOL)switchToEQAtIndex:(int)index
361 {
362     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'ID  ', want:type('cEQP'), seld:long(%lu), from:'null'() }, ----:obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }",index] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
363     return YES;
364 }
365
366 - (ProcessSerialNumber)iTunesPSN
367 {
368     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
369     ProcessSerialNumber number;
370     int i;
371     int count = [apps count];
372     
373     number.highLongOfPSN = kNoProcess;
374     
375     for (i = 0; i < count; i++)
376     {
377         NSDictionary *curApp = [apps objectAtIndex:i];
378         
379         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
380         {
381             number.highLongOfPSN = [[curApp objectForKey:
382                 @"NSApplicationProcessSerialNumberHigh"] intValue];
383             number.lowLongOfPSN = [[curApp objectForKey:
384                 @"NSApplicationProcessSerialNumberLow"] intValue];
385         }
386     }
387     return number;
388 }
389
390 - (void)applicationLaunched:(NSNotification *)note
391 {
392     NSDictionary *info = [note userInfo];
393
394     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
395         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
396         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
397
398         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
399     }
400 }
401
402 - (void)applicationTerminated:(NSNotification *)note
403 {
404     NSDictionary *info = [note userInfo];
405
406     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
407         iTunesPSN.highLongOfPSN = kNoProcess;
408         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
409     }
410 }
411
412 @end