Some Plugin API changes... Need to finish the API HeaderDocs sometime
[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 *)title
11 {
12     return @"iTunes";
13 }
14
15 - (NSString *)information;
16 {
17     return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
18 }
19
20 - (NSImage *)icon
21 {
22     return nil;
23 }
24
25 - (BOOL)begin
26 {
27     iTunesPSN = [self iTunesPSN];
28
29     //Register for application termination in NSWorkspace
30     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
31     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
32
33     return YES;
34 }
35
36 - (BOOL)halt
37 {
38     iTunesPSN.highLongOfPSN = kNoProcess;
39
40     //Unregister for application termination in NSWorkspace
41     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
42
43     return YES;
44 }
45
46 - (BOOL)isAppRunning
47 {
48     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
49     int i;
50     int count = [apps count];
51
52     for (i = 0; i < count; i++) {
53         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
54                 isEqualToString:@"iTunes"]) {
55             return YES;
56         }
57     }
58     return NO;
59 }
60
61 - (ITMTRemotePlayerState)playerState
62 {
63     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
64     
65     switch (result)
66     {
67         default:
68         case 'kPSS':
69             return stopped;
70         case 'kPSP':
71             return playing;
72         case 'kPSp':
73             return paused;
74         case 'kPSR':
75             return rewinding;
76         case 'kPSF':
77             return forwarding;
78     }
79     
80     return stopped;
81 }
82
83 - (NSArray *)playlists
84 {
85     long i = 0;
86     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
87     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
88
89
90            for (i = 1; i <= numPlaylists; i++) {
91                   const long j = i;
92                   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];
93                   NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
94                   NSLog(@"sent event cur %d max %d",i,numPlaylists);
95                   [playlists addObject:theObj];
96            }
97            return [playlists autorelease];
98 }
99
100 - (int)numberOfSongsInPlaylistAtIndex:(int)index
101 {
102     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];
103 }
104
105 - (NSString *)classOfPlaylistAtIndex:(int)index
106 {
107     int realResult = [[ITAppleEventCenter sharedCenter]
108                 sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
109
110     if (realResult == 'cRTP') return @"radio tuner playlist";
111     else return @"playlist";
112 }
113
114 - (int)currentPlaylistIndex
115 {
116     int result;
117     result = [[ITAppleEventCenter sharedCenter]
118                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
119     return result;
120 }
121
122 - (NSString *)songTitleAtIndex:(int)index
123 {
124     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];
125 }
126
127 - (int)currentSongIndex
128 {
129     int result;
130     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     return result;
132 }
133
134 - (NSString *)currentSongTitle
135 {
136     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
137 }
138
139 - (NSString *)currentSongArtist
140 {
141     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
142 }
143
144 - (NSString *)currentSongAlbum
145 {
146     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
147 }
148
149 - (NSString *)currentSongGenre
150 {
151     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
152 }
153
154 - (NSString *)currentSongLength
155 {
156     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
157 }
158
159 - (NSString *)currentSongRemaining
160 {
161     long duration = [[ITAppleEventCenter sharedCenter]
162                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
163     long current = [[ITAppleEventCenter sharedCenter]
164                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
165
166     return [[NSNumber numberWithLong:duration - current] stringValue];
167 }
168
169 - (float)currentSongRating
170 {
171     return 0.00;
172 }
173
174 - (BOOL)setCurrentSongRating:(float)rating
175 {
176     return NO;
177 }
178
179 - (float)volume
180 {
181     return 1.00;
182 }
183
184 - (BOOL)setVolume:(float)volume
185 {
186     return NO;
187 }
188
189 - (NSArray *)eqPresets;
190 {
191     int i;
192     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
193     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
194
195            for (i = 1; i <= numPresets; i++) {
196                   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];
197                   if (theObj) [presets addObject:theObj];
198            }
199            return [presets autorelease];
200 }
201
202 - (int)currentEQPresetIndex
203 {
204     int result;
205     result = [[ITAppleEventCenter sharedCenter]
206                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN];
207     return result;
208 }
209
210 - (BOOL)play
211 {
212     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
213     return YES;
214 }
215
216 - (BOOL)pause
217 {
218     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
219     return YES;
220 }
221
222 - (BOOL)goToNextSong
223 {
224     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
225     return YES;
226 }
227
228 - (BOOL)goToPreviousSong
229 {
230     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
231     return YES;
232 }
233
234 - (BOOL)fastForward
235 {
236     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
237     return YES;
238 }
239
240 - (BOOL)rewind
241 {
242     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
243     return YES;
244 }
245
246
247 - (BOOL)switchToPlaylistAtIndex:(int)index
248 {
249     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
250     return YES;
251 }
252
253 - (BOOL)switchToSongAtIndex:(int)index
254 {
255     [[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];
256     return YES;
257 }
258
259 - (BOOL)switchToEQAtIndex:(int)index
260 {
261     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'indx', 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];
262     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
263     return YES;
264 }
265
266 - (ProcessSerialNumber)iTunesPSN
267 {
268     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
269     ProcessSerialNumber number;
270     int i;
271     int count = [apps count];
272     
273     number.highLongOfPSN = kNoProcess;
274     
275     for (i = 0; i < count; i++)
276     {
277         NSDictionary *curApp = [apps objectAtIndex:i];
278         
279         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
280         {
281             number.highLongOfPSN = [[curApp objectForKey:
282                 @"NSApplicationProcessSerialNumberHigh"] intValue];
283             number.lowLongOfPSN = [[curApp objectForKey:
284                 @"NSApplicationProcessSerialNumberLow"] intValue];
285         }
286     }
287     return number;
288 }
289
290 - (void)applicationLaunched:(NSNotification *)note
291 {
292     NSDictionary *info = [note userInfo];
293
294     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
295         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
296         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
297
298         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
299     }
300 }
301
302 - (void)applicationTerminated:(NSNotification *)note
303 {
304     NSDictionary *info = [note userInfo];
305
306     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
307         iTunesPSN.highLongOfPSN = kNoProcess;
308         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
309     }
310 }
311
312 @end