Added the network library source to the remote and to show up in the
[MenuTunes.git] / Deprecated / 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     savedPSN = [self iTunesPSN];
28     return YES;
29 }
30
31 - (BOOL)halt
32 {
33     return YES;
34 }
35
36 - (NSString *)playerFullName
37 {
38     return @"iTunes";
39 }
40
41 - (NSString *)playerSimpleName
42 {
43     return @"iTunes";
44 }
45
46 - (NSDictionary *)capabilities
47 {
48     return [NSDictionary dictionaryWithObjectsAndKeys:
49                 [NSNumber numberWithBool: YES], @"Remote",
50                 [NSNumber numberWithBool: YES], @"Basic Track Control",
51                 [NSNumber numberWithBool: YES], @"Track Information",
52                 [NSNumber numberWithBool: YES], @"Track Navigation",
53                 [NSNumber numberWithBool: YES], @"Upcoming Songs",
54                 [NSNumber numberWithBool: YES], @"Playlists",
55                 [NSNumber numberWithBool: YES], @"Volume",
56                 [NSNumber numberWithBool: YES], @"Shuffle",
57                 [NSNumber numberWithBool: YES], @"Repeat Modes",
58                 [NSNumber numberWithBool: YES], @"Equalizer",
59                 [NSNumber numberWithBool: YES], @"Track Rating",
60                 nil];
61 }
62
63 - (BOOL)showPrimaryInterface
64 {
65     // Make this into AppleEvents... shouldn't be too hard, I'm just too tired to do it right now.
66     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pisf'), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
67     // Still have to convert these to AEs:
68     //  set visible of browser window 1 to true
69     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), ----:obj { form:'prop', want:'prop', seld:'pvis', from:obj { form:'indx', want:'cBrW', seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
70     //  set minimized of browser window 1 to false
71     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(0), ----:obj { form:'prop', want:'prop', seld:'pMin', from:obj { form:'indx', want:'cBrW', seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
72
73     return NO;
74 }
75
76 - (ITMTRemotePlayerRunningState)playerRunningState
77 {
78     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
79     int i;
80     int count = [apps count];
81     
82     for (i = 0; i < count; i++) {
83         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
84             return ITMTRemotePlayerRunning;
85         }
86     }
87     return ITMTRemotePlayerNotRunning;
88 }
89
90 - (ITMTRemotePlayerPlayingState)playerPlayingState
91 {
92     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
93     
94     switch (result)
95     {
96         default:
97         case 'kPSS':
98             return ITMTRemotePlayerStopped;
99         case 'kPSP':
100             return ITMTRemotePlayerPlaying;
101         case 'kPSp':
102             return ITMTRemotePlayerPaused;
103         case 'kPSR':
104             return ITMTRemotePlayerRewinding;
105         case 'kPSF':
106             return ITMTRemotePlayerForwarding;
107     }
108     
109     return ITMTRemotePlayerStopped;
110 }
111
112 - (NSArray *)playlists
113 {
114     long i = 0;
115     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
116     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
117     
118     for (i = 1; i <= numPlaylists; i++) {
119         const long j = i;
120         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];
121         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
122         [playlists addObject:theObj];
123     }
124     return [playlists autorelease];
125 }
126
127 - (int)numberOfSongsInPlaylistAtIndex:(int)index
128 {
129     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:savedPSN];
130 }
131
132 - (ITMTRemotePlayerPlaylistClass)currentPlaylistClass
133 {
134     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
135     
136
137     switch (realResult)
138            {
139            case 'cLiP':
140                   return ITMTRemotePlayerLibraryPlaylist;
141                   break;
142            case 'cRTP':
143                   return ITMTRemotePlayerRadioPlaylist;
144                   break;
145            default:
146                   return ITMTRemotePlayerPlaylist;
147            }
148 }
149
150 - (int)currentPlaylistIndex
151 {
152     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
153 }
154
155 - (NSString *)songTitleAtIndex:(int)index
156 {
157     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:savedPSN];
158 }
159
160 - (int)currentAlbumTrackCount
161 {
162     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrC" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
163 }
164
165 - (int)currentSongTrack
166 {
167     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrN" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
168 }
169
170 - (NSString *)playerStateUniqueIdentifier
171 {
172     return [NSString stringWithFormat:@"%i-%i", [self currentPlaylistIndex], [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pDID" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN]];
173 }
174
175 - (int)currentSongIndex
176 {
177     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
178 }
179
180 - (NSString *)currentSongTitle
181 {
182     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
183 }
184
185 - (NSString *)currentSongArtist
186 {
187     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
188 }
189
190 - (NSString *)currentSongAlbum
191 {
192     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
193 }
194
195 - (NSString *)currentSongGenre
196 {
197     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
198 }
199
200 - (NSString *)currentSongLength
201 {
202     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
203 }
204
205 - (NSString *)currentSongRemaining
206 {
207     long duration = [[ITAppleEventCenter sharedCenter]
208                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
209     long current = [[ITAppleEventCenter sharedCenter]
210                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
211
212     return [[NSNumber numberWithLong:duration - current] stringValue];
213 }
214
215 - (float)currentSongRating
216 {
217     return (float)[[ITAppleEventCenter sharedCenter]
218                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN] / 100.0;
219 }
220
221 - (BOOL)setCurrentSongRating:(float)rating
222 {
223     [[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:savedPSN];
224     return YES;
225 }
226
227 /* - (BOOL)equalizerEnabled
228 {
229     int thingy = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:type('prop'), want:type('prop'), seld:type('pEQ '), from:() }" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
230     NSLog(@"Debug equalizerEnabled: %i", thingy);
231     return thingy;
232 }
233
234 - (BOOL)setEqualizerEnabled:(BOOL)enabled
235 {
236     [[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:savedPSN];
237     return YES;
238 } */
239
240 - (NSArray *)eqPresets
241 {
242     int i;
243     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
244     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
245     
246     for (i = 1; i <= numPresets; i++) {
247         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:savedPSN];
248         if (theObj) {
249             [presets addObject:theObj];
250         }
251     }
252     return [presets autorelease];
253 }
254
255 - (int)currentEQPresetIndex
256 {
257     int result;
258     result = [[ITAppleEventCenter sharedCenter]
259                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
260     return result;
261 }
262
263 - (float)volume
264 {
265     return (float)[[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:savedPSN] / 100;
266 }
267
268 - (BOOL)setVolume:(float)volume
269 {
270     [[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:savedPSN];
271     return NO;
272 }
273
274 - (BOOL)shuffleEnabled
275 {
276     int result = [[ITAppleEventCenter sharedCenter]
277                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
278     return result;
279 }
280
281 - (BOOL)setShuffleEnabled:(BOOL)enabled
282 {
283     [[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'() } }",(unsigned long)enabled] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
284     return YES;
285 }
286
287 - (ITMTRemotePlayerRepeatMode)repeatMode
288 {
289     FourCharCode m00f = 0;
290     int result = 0;
291     m00f = [[ITAppleEventCenter sharedCenter]
292                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
293
294     switch (m00f)
295            {
296            case 'kRp0':
297                   result = ITMTRemotePlayerRepeatOff;
298                   break;
299            case 'kRp1':
300                   result = ITMTRemotePlayerRepeatOne;
301                   break;
302            case 'kRpA':
303                   result = ITMTRemotePlayerRepeatAll;
304                   break;
305            }
306     
307     return result;
308 }
309
310 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
311 {
312     char *m00f = NULL;
313     switch (repeatMode)
314            {
315            case ITMTRemotePlayerRepeatOff:
316                   m00f = "kRp0";
317                   break;
318            case ITMTRemotePlayerRepeatOne:
319                   m00f = "kRp1";
320                   break;
321            case ITMTRemotePlayerRepeatAll:
322                   m00f = "kRpA";
323                   break;
324            }
325
326     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:type('%s') ----: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:savedPSN];
327     return YES;
328 }
329
330 - (BOOL)play
331 {
332     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:savedPSN];
333     return YES;
334 }
335
336 - (BOOL)pause
337 {
338     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:savedPSN];
339     return YES;
340 }
341
342 - (BOOL)goToNextSong
343 {
344     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:savedPSN];
345     return YES;
346 }
347
348 - (BOOL)goToPreviousSong
349 {
350     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:savedPSN];
351     return YES;
352 }
353
354 - (BOOL)forward
355 {
356     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:savedPSN];
357     return YES;
358 }
359
360 - (BOOL)rewind
361 {
362     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:savedPSN];
363     return YES;
364 }
365
366 - (BOOL)switchToPlaylistAtIndex:(int)index
367 {
368     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:savedPSN];
369     return YES;
370 }
371
372 - (BOOL)switchToSongAtIndex:(int)index
373 {
374     [[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:savedPSN];
375     return YES;
376 }
377
378 - (BOOL)switchToEQAtIndex:(int)index
379 {
380     // index should count from 0, but itunes counts from 1, so let's add 1.
381     [[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:savedPSN];
382     return YES;
383 }
384
385 - (ProcessSerialNumber)iTunesPSN
386 {
387     /*NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
388     ProcessSerialNumber number;
389     int i;
390     int count = [apps count];
391     
392     number.highLongOfPSN = kNoProcess;
393     
394     for (i = 0; i < count; i++)
395     {
396         NSDictionary *curApp = [apps objectAtIndex:i];
397         
398         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
399         {
400             number.highLongOfPSN = [[curApp objectForKey:
401                 @"NSApplicationProcessSerialNumberHigh"] intValue];
402             number.lowLongOfPSN = [[curApp objectForKey:
403                 @"NSApplicationProcessSerialNumberLow"] intValue];
404         }
405     }
406     return number;*/
407     ProcessSerialNumber number;
408     number.highLongOfPSN = kNoProcess;
409     number.lowLongOfPSN = 0;
410     
411     while ( (GetNextProcess(&number) == noErr) ) 
412     {
413         CFStringRef name;
414         if ( (CopyProcessName(&number, &name) == noErr) )
415         {
416             if ([(NSString *)name isEqualToString:@"iTunes"])
417             {
418                 return number;
419             }
420             [(NSString *)name release];
421         }
422     }
423     return number;
424 }
425
426 @end