Mass Bug Fixes by The Mastar of L33t, Joe. Ph34r m3.
[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 - (int)currentSongIndex
169 {
170     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
171 }
172
173 - (NSString *)currentSongTitle
174 {
175     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
176 }
177
178 - (NSString *)currentSongArtist
179 {
180     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
181 }
182
183 - (NSString *)currentSongAlbum
184 {
185     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
186 }
187
188 - (NSString *)currentSongGenre
189 {
190     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
191 }
192
193 - (NSString *)currentSongLength
194 {
195     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
196 }
197
198 - (NSString *)currentSongRemaining
199 {
200     long duration = [[ITAppleEventCenter sharedCenter]
201                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
202     long current = [[ITAppleEventCenter sharedCenter]
203                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
204
205     return [[NSNumber numberWithLong:duration - current] stringValue];
206 }
207
208 - (float)currentSongRating
209 {
210     return [[ITAppleEventCenter sharedCenter]
211                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN] / 100;
212 }
213
214 - (BOOL)setCurrentSongRating:(float)rating
215 {
216     [[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];
217     return YES;
218 }
219
220 - (BOOL)equalizerEnabled
221 {
222     return [[ITAppleEventCenter sharedCenter]
223                         sendAEWithRequestedKeyForNumber:@"pEQ " eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
224 }
225
226 - (BOOL)setEqualizerEnabled:(BOOL)enabled
227 {
228 [[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];
229 }
230
231 - (NSArray *)eqPresets
232 {
233     int i;
234     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
235     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
236     
237     for (i = 1; i <= numPresets; i++) {
238         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];
239         if (theObj) {
240             [presets addObject:theObj];
241         }
242     }
243     return [presets autorelease];
244 }
245
246 - (int)currentEQPresetIndex
247 {
248     int result;
249     result = [[ITAppleEventCenter sharedCenter]
250                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
251     return result;
252 }
253
254 - (float)volume
255 {
256     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
257     return vol / 100;
258 }
259
260 - (BOOL)setVolume:(float)volume
261 {
262     [[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];
263     return NO;
264 }
265
266 - (BOOL)shuffleEnabled
267 {
268     int result = [[ITAppleEventCenter sharedCenter]
269                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
270     return result;
271 }
272
273 - (BOOL)setShuffleEnabled:(BOOL)enabled
274 {
275     [[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];
276 }
277
278 - (ITMTRemotePlayerRepeatMode)repeatMode
279 {
280     FourCharCode m00f;
281     int result;
282     m00f = [[ITAppleEventCenter sharedCenter]
283                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
284
285     switch (m00f)
286            {
287            case 'kRp0':
288                   result = ITMTRemotePlayerRepeatOff;
289                   break;
290            case 'kRp1':
291                   result = ITMTRemotePlayerRepeatOne;
292                   break;
293            case 'kRpA':
294                   result = ITMTRemotePlayerRepeatAll;
295                   break;
296            }
297     
298     return result;
299 }
300
301 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
302 {
303     FourCharCode m00f;
304     switch (repeatMode)
305            {
306            case ITMTRemotePlayerRepeatOff:
307                   m00f = 'kRp0';
308                   break;
309            case ITMTRemotePlayerRepeatOne:
310                   m00f = 'kRp1';
311                   break;
312            case ITMTRemotePlayerRepeatAll:
313                   m00f = 'kRpA';
314                   break;
315            }
316
317     [[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];
318
319 }
320
321 - (BOOL)play
322 {
323     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
324     return YES;
325 }
326
327 - (BOOL)pause
328 {
329     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
330     return YES;
331 }
332
333 - (BOOL)goToNextSong
334 {
335     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
336     return YES;
337 }
338
339 - (BOOL)goToPreviousSong
340 {
341     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
342     return YES;
343 }
344
345 - (BOOL)forward
346 {
347     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
348     return YES;
349 }
350
351 - (BOOL)rewind
352 {
353     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
354     return YES;
355 }
356
357 - (BOOL)switchToPlaylistAtIndex:(int)index
358 {
359     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
360     return YES;
361 }
362
363 - (BOOL)switchToSongAtIndex:(int)index
364 {
365     [[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];
366     return YES;
367 }
368
369 - (BOOL)switchToEQAtIndex:(int)index
370 {
371     // index should count from 0, but itunes counts from 1, so let's add 1.
372     [[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];
373     return YES;
374 }
375
376 - (ProcessSerialNumber)iTunesPSN
377 {
378     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
379     ProcessSerialNumber number;
380     int i;
381     int count = [apps count];
382     
383     number.highLongOfPSN = kNoProcess;
384     
385     for (i = 0; i < count; i++)
386     {
387         NSDictionary *curApp = [apps objectAtIndex:i];
388         
389         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
390         {
391             number.highLongOfPSN = [[curApp objectForKey:
392                 @"NSApplicationProcessSerialNumberHigh"] intValue];
393             number.lowLongOfPSN = [[curApp objectForKey:
394                 @"NSApplicationProcessSerialNumberLow"] intValue];
395         }
396     }
397     return number;
398 }
399
400 - (void)applicationLaunched:(NSNotification *)note
401 {
402     NSDictionary *info = [note userInfo];
403
404     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
405         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
406         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
407
408         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
409     }
410 }
411
412 - (void)applicationTerminated:(NSNotification *)note
413 {
414     NSDictionary *info = [note userInfo];
415
416     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
417         iTunesPSN.highLongOfPSN = kNoProcess;
418         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
419     }
420 }
421
422 @end