iTunesRemote updated to fit new ITMTRemote API changes.
[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 nil;
58 }
59
60 - (ITMTRemotePlayerRunningStatus)playerRunningState
61 {
62     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
63     int i;
64     int count = [apps count];
65     
66     for (i = 0; i < count; i++) {
67         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
68             return ITMTRemotePlayerRunning;
69         }
70     }
71     return ITMTRemotePlayerNotRunning;
72 }
73
74 - (ITMTRemotePlayerPlayingState)playerPlayingState
75 {
76     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
77     
78     switch (result)
79     {
80         default:
81         case 'kPSS':
82             return ITMTRemotePlayerStopped;
83         case 'kPSP':
84             return ITMTRemotePlayerPlaying;
85         case 'kPSp':
86             return ITMTRemotePlayerPaused;
87         case 'kPSR':
88             return ITMTRemotePlayerRewinding;
89         case 'kPSF':
90             return ITMTRemotePlayerForwarding;
91     }
92     
93     return ITMTRemotePlayerStopped;
94 }
95
96 - (NSArray *)playlists
97 {
98     long i = 0;
99     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
100     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
101     
102     for (i = 1; i <= numPlaylists; i++) {
103         const long j = i;
104         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];
105         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
106         [playlists addObject:theObj];
107     }
108     return [playlists autorelease];
109 }
110
111 - (int)numberOfSongsInPlaylistAtIndex:(int)index
112 {
113     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];
114 }
115
116 - (NSString *)classOfPlaylistAtIndex:(int)index
117 {
118     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
119     
120     if (realResult == 'cRTP') {
121         return @"radio tuner playlist";
122     } else {
123         return @"playlist";
124     }
125 }
126
127 - (int)currentPlaylistIndex
128 {
129     int result;
130     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     return result;
132 }
133
134 - (NSString *)songTitleAtIndex:(int)index
135 {
136     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];
137 }
138
139 - (int)currentSongIndex
140 {
141     int result;
142     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
143     return result;
144 }
145
146 - (NSString *)currentSongTitle
147 {
148     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
149 }
150
151 - (NSString *)currentSongArtist
152 {
153     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
154 }
155
156 - (NSString *)currentSongAlbum
157 {
158     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
159 }
160
161 - (NSString *)currentSongGenre
162 {
163     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
164 }
165
166 - (NSString *)currentSongLength
167 {
168     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
169 }
170
171 - (NSString *)currentSongRemaining
172 {
173     long duration = [[ITAppleEventCenter sharedCenter]
174                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
175     long current = [[ITAppleEventCenter sharedCenter]
176                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
177
178     return [[NSNumber numberWithLong:duration - current] stringValue];
179 }
180
181 - (float)currentSongRating
182 {
183     int realResult = [[ITAppleEventCenter sharedCenter]
184                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
185
186     return realResult / 100;
187 }
188
189 - (BOOL)setCurrentSongRating:(float)rating
190 {
191     [[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];
192     return YES;
193 }
194
195 - (BOOL)equalizerEnabled
196 {
197     return NO;
198 }
199
200 - (BOOL)setEqualizerEnabled:(BOOL)enabled
201 {
202     return NO;
203 }
204
205 - (NSArray *)eqPresets
206 {
207     int i;
208     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
209     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
210     
211     for (i = 1; i <= numPresets; i++) {
212         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];
213         if (theObj) {
214             [presets addObject:theObj];
215         }
216     }
217     return [presets autorelease];
218 }
219
220 - (int)currentEQPresetIndex
221 {
222     int result;
223     result = [[ITAppleEventCenter sharedCenter]
224                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
225     return result;
226 }
227
228 - (float)volume
229 {
230     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
231     return vol / 100;
232 }
233
234 - (BOOL)setVolume:(float)volume
235 {
236     [[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];
237     return NO;
238 }
239
240 - (BOOL)play
241 {
242     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
243     return YES;
244 }
245
246 - (BOOL)pause
247 {
248     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
249     return YES;
250 }
251
252 - (BOOL)goToNextSong
253 {
254     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
255     return YES;
256 }
257
258 - (BOOL)goToPreviousSong
259 {
260     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
261     return YES;
262 }
263
264 - (BOOL)forward
265 {
266     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
267     return YES;
268 }
269
270 - (BOOL)rewind
271 {
272     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
273     return YES;
274 }
275
276 - (BOOL)switchToPlaylistAtIndex:(int)index
277 {
278     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
279     return YES;
280 }
281
282 - (BOOL)switchToSongAtIndex:(int)index
283 {
284     [[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];
285     return YES;
286 }
287
288 - (BOOL)switchToEQAtIndex:(int)index
289 {
290     [[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];
291     return YES;
292 }
293
294 - (ProcessSerialNumber)iTunesPSN
295 {
296     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
297     ProcessSerialNumber number;
298     int i;
299     int count = [apps count];
300     
301     number.highLongOfPSN = kNoProcess;
302     
303     for (i = 0; i < count; i++)
304     {
305         NSDictionary *curApp = [apps objectAtIndex:i];
306         
307         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
308         {
309             number.highLongOfPSN = [[curApp objectForKey:
310                 @"NSApplicationProcessSerialNumberHigh"] intValue];
311             number.lowLongOfPSN = [[curApp objectForKey:
312                 @"NSApplicationProcessSerialNumberLow"] intValue];
313         }
314     }
315     return number;
316 }
317
318 - (void)applicationLaunched:(NSNotification *)note
319 {
320     NSDictionary *info = [note userInfo];
321
322     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
323         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
324         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
325
326         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
327     }
328 }
329
330 - (void)applicationTerminated:(NSNotification *)note
331 {
332     NSDictionary *info = [note userInfo];
333
334     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
335         iTunesPSN.highLongOfPSN = kNoProcess;
336         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
337     }
338 }
339
340 @end