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