Quick API addition for Unique IDs, to reduce unneeded MT queries.
[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 - (BOOL)showPrimaryInterface
73 {
74     // Make this into AppleEvents... shouldn't be too hard, I'm just too tired to do it right now.
75     //tell application "iTunes"
76     //  set frontmost to true
77     //  set visible of browser window 1 to true
78     //  set minimized of browser window 1 to false
79     //  set view of browser window 1 to (playlist (index of current playlist))
80     //end tell
81     return NO;
82 }
83
84 - (ITMTRemotePlayerRunningState)playerRunningState
85 {
86     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
87     int i;
88     int count = [apps count];
89     
90     for (i = 0; i < count; i++) {
91         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
92             return ITMTRemotePlayerRunning;
93         }
94     }
95     return ITMTRemotePlayerNotRunning;
96 }
97
98 - (ITMTRemotePlayerPlayingState)playerPlayingState
99 {
100     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
101     
102     switch (result)
103     {
104         default:
105         case 'kPSS':
106             return ITMTRemotePlayerStopped;
107         case 'kPSP':
108             return ITMTRemotePlayerPlaying;
109         case 'kPSp':
110             return ITMTRemotePlayerPaused;
111         case 'kPSR':
112             return ITMTRemotePlayerRewinding;
113         case 'kPSF':
114             return ITMTRemotePlayerForwarding;
115     }
116     
117     return ITMTRemotePlayerStopped;
118 }
119
120 - (NSArray *)playlists
121 {
122     long i = 0;
123     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
124     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
125     
126     for (i = 1; i <= numPlaylists; i++) {
127         const long j = i;
128         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];
129         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
130         [playlists addObject:theObj];
131     }
132     return [playlists autorelease];
133 }
134
135 - (int)numberOfSongsInPlaylistAtIndex:(int)index
136 {
137     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];
138 }
139
140 - (ITMTRemotePlayerPlaylistClass)classOfPlaylistAtIndex:(int)index
141 {
142     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
143     
144
145     switch (realResult)
146            {
147            case 'cLiP':
148                   return ITMTRemotePlayerLibraryPlaylist;
149                   break;
150            case 'cRTP':
151                   return ITMTRemotePlayerRadioPlaylist;
152                   break;
153            default:
154                   return ITMTRemotePlayerPlaylist;
155            }
156 }
157
158 - (int)currentPlaylistIndex
159 {
160     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
161 }
162
163 - (NSString *)songTitleAtIndex:(int)index
164 {
165     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];
166 }
167
168 - (NSString *)currentSongUniqueIdentifier
169 {
170     return @"BBQ";
171 }
172
173 - (int)currentSongIndex
174 {
175     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
176 }
177
178 - (NSString *)currentSongTitle
179 {
180     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
181 }
182
183 - (NSString *)currentSongArtist
184 {
185     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
186 }
187
188 - (NSString *)currentSongAlbum
189 {
190     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
191 }
192
193 - (NSString *)currentSongGenre
194 {
195     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
196 }
197
198 - (NSString *)currentSongLength
199 {
200     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
201 }
202
203 - (NSString *)currentSongRemaining
204 {
205     long duration = [[ITAppleEventCenter sharedCenter]
206                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
207     long current = [[ITAppleEventCenter sharedCenter]
208                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
209
210     return [[NSNumber numberWithLong:duration - current] stringValue];
211 }
212
213 - (float)currentSongRating
214 {
215     return [[ITAppleEventCenter sharedCenter]
216                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN] / 100;
217 }
218
219 - (BOOL)setCurrentSongRating:(float)rating
220 {
221     [[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:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",(long)(rating*100),[self currentSongIndex]] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
222     return YES;
223 }
224
225 - (BOOL)equalizerEnabled
226 {
227     return [[ITAppleEventCenter sharedCenter]
228                         sendAEWithRequestedKeyForNumber:@"pEQ " eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
229 }
230
231 - (BOOL)setEqualizerEnabled:(BOOL)enabled
232 {
233 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
234 }
235
236 - (NSArray *)eqPresets
237 {
238     int i;
239     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
240     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
241     
242     for (i = 1; i <= numPresets; i++) {
243         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];
244         if (theObj) {
245             [presets addObject:theObj];
246         }
247     }
248     return [presets autorelease];
249 }
250
251 - (int)currentEQPresetIndex
252 {
253     int result;
254     result = [[ITAppleEventCenter sharedCenter]
255                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
256     return result;
257 }
258
259 - (float)volume
260 {
261     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
262     return vol / 100;
263 }
264
265 - (BOOL)setVolume:(float)volume
266 {
267     [[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];
268     return NO;
269 }
270
271 - (BOOL)shuffleEnabled
272 {
273     int result = [[ITAppleEventCenter sharedCenter]
274                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
275     return result;
276 }
277
278 - (BOOL)setShuffleEnabled:(BOOL)enabled
279 {
280     [[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];
281 }
282
283 - (ITMTRemotePlayerRepeatMode)repeatMode
284 {
285     FourCharCode m00f;
286     int result;
287     m00f = [[ITAppleEventCenter sharedCenter]
288                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
289
290     switch (m00f)
291            {
292            case 'kRp0':
293                   result = ITMTRemotePlayerRepeatOff;
294                   break;
295            case 'kRp1':
296                   result = ITMTRemotePlayerRepeatOne;
297                   break;
298            case 'kRpA':
299                   result = ITMTRemotePlayerRepeatAll;
300                   break;
301            }
302     
303     return result;
304 }
305
306 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
307 {
308     FourCharCode m00f;
309     switch (repeatMode)
310            {
311            case ITMTRemotePlayerRepeatOff:
312                   m00f = 'kRp0';
313                   break;
314            case ITMTRemotePlayerRepeatOne:
315                   m00f = 'kRp1';
316                   break;
317            case ITMTRemotePlayerRepeatAll:
318                   m00f = 'kRpA';
319                   break;
320            }
321
322     [[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];
323
324 }
325
326 - (BOOL)play
327 {
328     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
329     return YES;
330 }
331
332 - (BOOL)pause
333 {
334     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
335     return YES;
336 }
337
338 - (BOOL)goToNextSong
339 {
340     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
341     return YES;
342 }
343
344 - (BOOL)goToPreviousSong
345 {
346     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
347     return YES;
348 }
349
350 - (BOOL)forward
351 {
352     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
353     return YES;
354 }
355
356 - (BOOL)rewind
357 {
358     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
359     return YES;
360 }
361
362 - (BOOL)switchToPlaylistAtIndex:(int)index
363 {
364     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
365     return YES;
366 }
367
368 - (BOOL)switchToSongAtIndex:(int)index
369 {
370     [[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];
371     return YES;
372 }
373
374 - (BOOL)switchToEQAtIndex:(int)index
375 {
376     // index should count from 0, but itunes counts from 1, so let's add 1.
377     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }, data:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() }",(index+1)] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
378     return YES;
379 }
380
381 - (ProcessSerialNumber)iTunesPSN
382 {
383     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
384     ProcessSerialNumber number;
385     int i;
386     int count = [apps count];
387     
388     number.highLongOfPSN = kNoProcess;
389     
390     for (i = 0; i < count; i++)
391     {
392         NSDictionary *curApp = [apps objectAtIndex:i];
393         
394         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
395         {
396             number.highLongOfPSN = [[curApp objectForKey:
397                 @"NSApplicationProcessSerialNumberHigh"] intValue];
398             number.lowLongOfPSN = [[curApp objectForKey:
399                 @"NSApplicationProcessSerialNumberLow"] intValue];
400         }
401     }
402     return number;
403 }
404
405 - (void)applicationLaunched:(NSNotification *)note
406 {
407     NSDictionary *info = [note userInfo];
408
409     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
410         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
411         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
412
413         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
414     }
415 }
416
417 - (void)applicationTerminated:(NSNotification *)note
418 {
419     NSDictionary *info = [note userInfo];
420
421     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
422         iTunesPSN.highLongOfPSN = kNoProcess;
423         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
424     }
425 }
426
427 @end