HAY GUYS LETS SHIP
[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     ITDebugLog(@"iTunesRemote begun");
28     savedPSN = [self iTunesPSN];
29     return YES;
30 }
31
32 - (BOOL)halt
33 {
34     ITDebugLog(@"iTunesRemote halted");
35     return YES;
36 }
37
38 - (NSString *)playerFullName
39 {
40     return @"iTunes";
41 }
42
43 - (NSString *)playerSimpleName
44 {
45     return @"iTunes";
46 }
47
48 - (NSDictionary *)capabilities
49 {
50     return [NSDictionary dictionaryWithObjectsAndKeys:
51                 [NSNumber numberWithBool: YES], @"Remote",
52                 [NSNumber numberWithBool: YES], @"Basic Track Control",
53                 [NSNumber numberWithBool: YES], @"Track Information",
54                 [NSNumber numberWithBool: YES], @"Track Navigation",
55                 [NSNumber numberWithBool: YES], @"Upcoming Songs",
56                 [NSNumber numberWithBool: YES], @"Playlists",
57                 [NSNumber numberWithBool: YES], @"Volume",
58                 [NSNumber numberWithBool: YES], @"Shuffle",
59                 [NSNumber numberWithBool: YES], @"Repeat Modes",
60                 [NSNumber numberWithBool: YES], @"Equalizer",
61                 [NSNumber numberWithBool: YES], @"Track Rating",
62                 nil];
63 }
64
65 - (BOOL)showPrimaryInterface
66 {
67     ITDebugLog(@"Showing player primary interface.");
68     // Still have to convert these to AEs:
69     //  set minimized of browser window 1 to false
70     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(0), '----':obj { form:'prop', want:type('prop'), seld:type('pMin'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
71     //  set visible of browser window 1 to true
72     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pvis'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
73     // Make this into AppleEvents... shouldn't be too hard, I'm just too tired to do it right now.
74     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pisf'), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
75     ITDebugLog(@"Done showing player primary interface.");
76     return YES;
77 }
78
79 - (ITMTRemotePlayerRunningState)playerRunningState
80 {
81     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
82     int i;
83     int count = [apps count];
84     
85     for (i = 0; i < count; i++) {
86         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
87             ITDebugLog(@"Player running state: 1");
88             return ITMTRemotePlayerRunning;
89         }
90     }
91     ITDebugLog(@"Player running state: 0");
92     return ITMTRemotePlayerNotRunning;
93 }
94
95 - (ITMTRemotePlayerPlayingState)playerPlayingState
96 {
97     long result;
98     
99     ITDebugLog(@"Getting player playing state");
100     
101     result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
102     
103     switch (result)
104     {
105         case 'kPSP':
106             ITDebugLog(@"Getting player playing state done. Player state: Playing");
107             return ITMTRemotePlayerPlaying;
108         case 'kPSp':
109             ITDebugLog(@"Getting player playing state done. Player state: Paused");
110             return ITMTRemotePlayerPaused;
111         case 'kPSR':
112             ITDebugLog(@"Getting player playing state done. Player state: Rewinding");
113             return ITMTRemotePlayerRewinding;
114         case 'kPSF':
115             ITDebugLog(@"Getting player playing state done. Player state: Forwarding");
116             return ITMTRemotePlayerForwarding;
117         case 'kPSS':
118         default:
119             ITDebugLog(@"Getting player playing state done. Player state: Stopped");
120             return ITMTRemotePlayerStopped;
121     }
122     ITDebugLog(@"Getting player playing state done. Player state: Stopped");
123     return ITMTRemotePlayerStopped;
124 }
125
126 - (NSArray *)playlists
127 {
128     long i = 0;
129     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
130     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
131     
132     ITDebugLog(@"Getting playlists.");
133     
134     for (i = 1; i <= numPlaylists; i++) {
135         const long j = i;
136         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];
137         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
138         ITDebugLog(@"Adding playlist: %@", theObj);
139         [playlists addObject:theObj];
140     }
141     ITDebugLog(@"Finished getting playlists.");
142     return [playlists autorelease];
143 }
144
145 - (int)numberOfSongsInPlaylistAtIndex:(int)index
146 {
147     int temp1;
148     ITDebugLog(@"Getting number of songs in playlist at index %i", index);
149     temp1 = [[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];
150     ITDebugLog(@"Getting number of songs in playlist at index %i done", index);
151     return temp1;
152 }
153
154 - (ITMTRemotePlayerSource)currentSource
155 {
156     unsigned long fourcc;
157
158     ITDebugLog(@"Getting current source.");   
159     
160     fourcc = (unsigned long)[[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber :[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pKnd'), from:obj { form:'prop', want:type('prop'), seld:type('ctnr'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }"] eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
161     
162     switch (fourcc) {
163         case 'kTun':
164             ITDebugLog(@"Getting current source done. Source: Radio.");
165             return ITMTRemoteRadioSource;
166             break;
167         case 'kDev':
168             ITDebugLog(@"Getting current source done. Source: Generic Device.");
169             return ITMTRemoteGenericDeviceSource;
170         case 'kPod':
171             ITDebugLog(@"Getting current source done. Source: iPod.");
172             return ITMTRemoteiPodSource; //this is stupid
173             break;
174         case 'kMCD':
175         case 'kACD':
176             ITDebugLog(@"Getting current source done. Source: CD.");
177             return ITMTRemoteCDSource;
178             break;
179         case 'kUnk':
180         case 'kLib':
181         case 'kShd':
182         default:
183             ITDebugLog(@"Getting current source done. Source: Library.");
184             return ITMTRemoteLibrarySource;
185             break;
186     }
187 }
188
189 - (ITMTRemotePlayerPlaylistClass)currentPlaylistClass
190 {
191     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
192     
193     ITDebugLog(@"Getting current playlist class");
194     switch (realResult)
195            {
196            case 'cLiP':
197                ITDebugLog(@"Getting current playlist class done. Class: Library.");
198                return ITMTRemotePlayerLibraryPlaylist;
199                break;
200            case 'cRTP':
201                ITDebugLog(@"Getting current playlist class done. Class: Radio.");
202                return ITMTRemotePlayerRadioPlaylist;
203                break;
204            default:
205                ITDebugLog(@"Getting current playlist class done. Class: Standard playlist.");
206                return ITMTRemotePlayerPlaylist;
207            }
208 }
209
210 - (int)currentPlaylistIndex
211 {  
212     int temp1;
213     ITDebugLog(@"Getting current playlist index.");
214     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
215     ITDebugLog(@"Getting current playlist index done.");
216     return temp1;
217 }
218
219 - (NSString *)songTitleAtIndex:(int)index
220 {
221     NSString *temp1;
222     ITDebugLog(@"Getting song title at index %i.", index);
223     temp1 = [[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];
224     ITDebugLog(@"Getting song title at index %i done.", index);
225     return ( ([temp1 length]) ? temp1 : nil ) ;
226 }
227
228 - (int)currentAlbumTrackCount
229 {
230     int temp1;
231     ITDebugLog(@"Getting current album track count.");
232     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrC" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
233     if ( [self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist ) { temp1 = 0; }
234     ITDebugLog(@"Getting current album track count done.");
235     return temp1;
236 }
237
238 - (int)currentSongTrack
239 {
240     int temp1;
241     ITDebugLog(@"Getting current song track.");
242     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrN" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
243     if ( [self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist ) { temp1 = 0; }
244     ITDebugLog(@"Getting current song track done.");
245     return temp1;
246 }
247
248 - (NSString *)playerStateUniqueIdentifier
249 {
250     NSString *temp1;
251     ITDebugLog(@"Getting current unique identifier.");
252     temp1 = [NSString stringWithFormat:@"%i-%i", [self currentPlaylistIndex], [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pDID" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN]];
253     ITDebugLog(@"Getting current unique identifier done.");
254     return ( ([temp1 length]) ? temp1 : nil ) ;
255 }
256
257 - (int)currentSongIndex
258 {
259     int temp1;
260     ITDebugLog(@"Getting current song index.");
261     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
262     ITDebugLog(@"Getting current song index done.");
263     return temp1;
264 }
265
266 - (NSString *)currentSongTitle
267 {
268     NSString *temp1;
269     ITDebugLog(@"Getting current song title.");
270     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
271     ITDebugLog(@"Getting current song title done.");
272     return ( ([temp1 length]) ? temp1 : nil ) ;
273 }
274
275 - (NSString *)currentSongArtist
276 {
277     NSString *temp1;
278     ITDebugLog(@"Getting current song artist.");
279     if ( [self currentPlaylistClass] != ITMTRemotePlayerRadioPlaylist ) {
280         temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
281     } else {
282         temp1 = @"";
283     }
284     ITDebugLog(@"Getting current song artist done.");
285     return ( ([temp1 length]) ? temp1 : nil ) ;
286 }
287
288 - (NSString *)currentSongAlbum
289 {
290     NSString *temp1;
291     ITDebugLog(@"Getting current song album.");
292     if ( [self currentPlaylistClass] != ITMTRemotePlayerRadioPlaylist ) {
293         temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
294     } else {
295         temp1 = @"";
296     }
297     ITDebugLog(@"Getting current song album done.");
298     return ( ([temp1 length]) ? temp1 : nil ) ;
299 }
300
301 - (NSString *)currentSongGenre
302 {
303     NSString *temp1;
304     ITDebugLog(@"Getting current song genre.");
305     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
306     ITDebugLog(@"Getting current song genre done.");
307     return ( ([temp1 length]) ? temp1 : nil ) ;
308 }
309
310 - (NSString *)currentSongLength
311 {
312     int temp1;
313     NSString *temp2;
314     ITDebugLog(@"Getting current song length.");
315     temp1 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
316     temp2 = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
317     if ( ([self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist) || (temp1 == 'cURT') ) { temp2 = @"Continuous"; }
318     ITDebugLog(@"Getting current song length done.");
319     return temp2;
320 }
321
322 - (NSString *)currentSongRemaining
323 {
324     long duration;
325     long current;
326     long final;
327     NSString *finalString;
328     
329     ITDebugLog(@"Getting current song remaining time.");
330     
331     duration = [[ITAppleEventCenter sharedCenter]
332                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
333     current = [[ITAppleEventCenter sharedCenter]
334                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
335                         
336     final = duration - current;
337     finalString = [self formatTimeInSeconds:final];
338     
339     if ( [self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist ) { finalString = nil; }
340     
341     ITDebugLog(@"Getting current song remaining time done.");
342     
343     return finalString;
344 }
345
346 - (NSString *)currentSongElapsed
347 {
348     long final;
349     NSString *finalString;
350     
351     ITDebugLog(@"Getting current song elapsed time.");
352     
353     final = [[ITAppleEventCenter sharedCenter]
354                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
355                         
356     finalString = [self formatTimeInSeconds:final];
357     ITDebugLog(@"Getting current song elapsed time done.");
358     return finalString;
359 }
360
361 - (float)currentSongRating
362 {
363     float temp1;
364     ITDebugLog(@"Getting current song rating.");
365     temp1 = ((float)[[ITAppleEventCenter sharedCenter]
366                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN] / 100.0);
367     if ( [self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist ) { temp1 = -1.0; }
368     ITDebugLog(@"Getting current song rating done.");
369     return temp1;
370 }
371
372 - (BOOL)setCurrentSongRating:(float)rating
373 {
374     ITDebugLog(@"Setting current song rating to %f.", rating);
375     if ( [self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist ) { return NO; }
376     [[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];
377     ITDebugLog(@"Setting current song rating to %f done.", rating);
378     return YES;
379 }
380
381 - (BOOL)equalizerEnabled
382 {
383     ITDebugLog(@"Getting equalizer enabled status.");
384     int thingy = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:type('prop'), want:type('prop'), seld:type('pEQ '), from:() }" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
385     ITDebugLog(@"Done getting equalizer enabled status.");
386     return thingy;
387 }
388
389 - (BOOL)setEqualizerEnabled:(BOOL)enabled
390 {
391     ITDebugLog(@"Setting equalizer enabled to %i.", enabled);
392     [[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];
393     ITDebugLog(@"Done setting equalizer enabled to %i.", enabled);
394     return YES;
395 }
396
397 - (NSArray *)eqPresets
398 {
399     int i;
400     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
401     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
402     ITDebugLog(@"Getting EQ presets");
403     for (i = 1; i <= numPresets; i++) {
404         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];
405         if (theObj) {
406             ITDebugLog(@"Adding preset %@", theObj);
407             [presets addObject:theObj];
408         }
409     }
410     ITDebugLog(@"Done getting EQ presets");
411     return [presets autorelease];
412 }
413
414 - (int)currentEQPresetIndex
415 {
416     int result;
417     ITDebugLog(@"Getting current EQ preset index.");
418     result = [[ITAppleEventCenter sharedCenter]
419                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
420     ITDebugLog(@"Getting current EQ preset index done.");
421     return result;
422 }
423
424 - (float)volume
425 {
426     ITDebugLog(@"Getting volume.");
427     ITDebugLog(@"Getting volume done.");
428     return (float)[[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:savedPSN] / 100;
429 }
430
431 - (BOOL)setVolume:(float)volume
432 {
433     ITDebugLog(@"Setting volume to %f.", volume);
434     [[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];
435     ITDebugLog(@"Setting volume to %f done.", volume);
436     return YES;
437 }
438
439 - (BOOL)shuffleEnabled
440 {
441     ITDebugLog(@"Getting shuffle enabled status.");
442     BOOL final;
443     int result = [[ITAppleEventCenter sharedCenter]
444                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
445     if (result != 0) {
446         final = YES;
447     } else {
448         final = NO;
449     }
450     ITDebugLog(@"Getting shuffle enabled status done.");
451     return final;
452 }
453
454 - (BOOL)setShuffleEnabled:(BOOL)enabled
455 {
456     ITDebugLog(@"Set shuffle enabled to %i", enabled);
457     [[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];
458     ITDebugLog(@"Set shuffle enabled to %i done", enabled);
459     return YES;
460 }
461
462 - (ITMTRemotePlayerRepeatMode)repeatMode
463 {
464     FourCharCode m00f = 0;
465     int result = 0;
466     m00f = [[ITAppleEventCenter sharedCenter]
467                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
468     ITDebugLog(@"Getting repeat mode.");
469     switch (m00f)
470     {
471         //case 'kRp0':
472         case 1800564815:
473             ITDebugLog(@"Repeat off");
474             result = ITMTRemotePlayerRepeatOff;
475             break;
476         case 'kRp1':
477             ITDebugLog(@"Repeat one");
478             result = ITMTRemotePlayerRepeatOne;
479             break;
480         case 'kRpA':
481             ITDebugLog(@"Repeat all");
482             result = ITMTRemotePlayerRepeatAll;
483             break;
484     }
485     ITDebugLog(@"Getting repeat mode done.");
486     return result;
487 }
488
489 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
490 {
491     char *m00f;
492     ITDebugLog(@"Setting repeat mode to %i", repeatMode);
493     switch (repeatMode)
494     {
495         case ITMTRemotePlayerRepeatOne:
496             m00f = "kRp1";
497             break;
498         case ITMTRemotePlayerRepeatAll:
499             m00f = "kRpA";
500             break;
501         case ITMTRemotePlayerRepeatOff:
502         default:
503             m00f = "kRp0";
504             break;
505     }
506     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:'%s', '----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:() } }",m00f] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
507     ITDebugLog(@"Setting repeat mode to %c done", m00f);
508     return YES;
509 }
510
511 - (BOOL)play
512 {
513     ITDebugLog(@"Play");
514     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:savedPSN];
515     ITDebugLog(@"Play done");
516     return YES;
517 }
518
519 - (BOOL)pause
520 {
521     ITDebugLog(@"Pause");
522     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:savedPSN];
523     ITDebugLog(@"Pause done");
524     return YES;
525 }
526
527 - (BOOL)goToNextSong
528 {
529     ITDebugLog(@"Go to next track");
530     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:savedPSN];
531     ITDebugLog(@"Go to next track done");
532     return YES;
533 }
534
535 - (BOOL)goToPreviousSong
536 {
537     ITDebugLog(@"Go to previous track");
538     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:savedPSN];
539     ITDebugLog(@"Go to previous track done");
540     return YES;
541 }
542
543 - (BOOL)forward
544 {
545     ITDebugLog(@"Fast forward action");
546     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:savedPSN];
547     ITDebugLog(@"Fast forward action done");
548     return YES;
549 }
550
551 - (BOOL)rewind
552 {
553     ITDebugLog(@"Rewind action");
554     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:savedPSN];
555     ITDebugLog(@"Rewind action done");
556     return YES;
557 }
558
559 - (BOOL)switchToPlaylistAtIndex:(int)index
560 {
561     ITDebugLog(@"Switching to playlist at index %i", index);
562     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:savedPSN];
563     ITDebugLog(@"Done switching to playlist at index %i", index);
564     return YES;
565 }
566
567 - (BOOL)switchToSongAtIndex:(int)index
568 {
569     ITDebugLog(@"Switching to track at index %i", index);
570     [[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];
571     ITDebugLog(@"Done switching to track at index %i", index);
572     return YES;
573 }
574
575 - (BOOL)switchToEQAtIndex:(int)index
576 {
577     ITDebugLog(@"Switching to EQ preset at index %i", index);
578     // index should count from 0, but itunes counts from 1, so let's add 1.
579     [self setEqualizerEnabled:YES];
580     [[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];
581     ITDebugLog(@"Done switching to EQ preset at index %i", index);
582     return YES;
583 }
584
585 - (ProcessSerialNumber)iTunesPSN
586 {
587     /*NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
588     ProcessSerialNumber number;
589     int i;
590     int count = [apps count];
591     
592     number.highLongOfPSN = kNoProcess;
593     
594     for (i = 0; i < count; i++)
595     {
596         NSDictionary *curApp = [apps objectAtIndex:i];
597         
598         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
599         {
600             number.highLongOfPSN = [[curApp objectForKey:
601                 @"NSApplicationProcessSerialNumberHigh"] intValue];
602             number.lowLongOfPSN = [[curApp objectForKey:
603                 @"NSApplicationProcessSerialNumberLow"] intValue];
604         }
605     }
606     return number;*/
607     ProcessSerialNumber number;
608     number.highLongOfPSN = kNoProcess;
609     number.lowLongOfPSN = 0;
610     ITDebugLog(@"Getting iTunes' PSN.");
611     while ( (GetNextProcess(&number) == noErr) ) 
612     {
613         CFStringRef name;
614         if ( (CopyProcessName(&number, &name) == noErr) )
615         {
616             if ([(NSString *)name isEqualToString:@"iTunes"])
617             {
618                 ITDebugLog(@"iTunes' highLongOfPSN: %lu.", number.highLongOfPSN);
619                 ITDebugLog(@"iTunes' lowLongOfPSN: %lu.", number.lowLongOfPSN);
620                 ITDebugLog(@"Done getting iTunes' PSN.");
621                 return number;
622             }
623             [(NSString *)name release];
624         }
625     }
626     ITDebugLog(@"Failed getting iTunes' PSN.");
627     return number;
628 }
629
630 - (NSString*)formatTimeInSeconds:(long)seconds {
631     long final = seconds;
632     NSString *finalString;
633     if (final > 60) {
634         if (final > 3600) {
635             finalString = [NSString stringWithFormat:@"%i:%@:%@",(final / 3600),[self zeroSixty:(int)((final % 3600) / 60)],[self zeroSixty:(int)((final % 3600) % 60)]];
636         } else {
637             finalString = [NSString stringWithFormat:@"%i:%@",(final / 60),[self zeroSixty:(int)(final % 60)]];
638         }
639     } else {
640         finalString = [NSString stringWithFormat:@"0:%@",[self zeroSixty:(int)final]];
641     }
642     return finalString;
643 }
644 - (NSString*)zeroSixty:(int)seconds {
645     if ( (seconds < 10) && (seconds > 0) ) {
646         return [NSString stringWithFormat:@"0%i",seconds];
647     } else if ( (seconds == 0) ) {
648         return [NSString stringWithFormat:@"00"];
649     } else {
650         return [NSString stringWithFormat:@"%i",seconds];
651     }
652 }
653
654 @end