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