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