FINALLY got CVS working. Fixed another elusive menu bug that hated me.
[MenuTunes.git] / MenuTunes.m
1 /*
2 Things to do:
3 ¥ Make preferences window pretty
4 ¥ Optimize
5 ¥ Apple Events! Apple Events! Apple Events!
6 ¥ Manual and webpage
7 ¥ Finish up registration frontend
8 */
9
10 #import "MenuTunes.h"
11 #import "PreferencesController.h"
12 #import "HotKeyCenter.h"
13 #import "StatusWindowController.h"
14
15 @interface MenuTunes(Private)
16 - (void)updateMenu;
17 - (void)rebuildUpcomingSongsMenu;
18 - (void)rebuildPlaylistMenu;
19 - (void)rebuildEQPresetsMenu;
20 - (void)setupHotKeys;
21 - (NSString *)runScriptAndReturnResult:(NSString *)script;
22 - (void)timerUpdate;
23 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
24
25 @end
26
27 @implementation MenuTunes
28
29 /*************************************************************************/
30 #pragma mark -
31 #pragma mark INITIALIZATION METHODS
32 /*************************************************************************/
33
34 - (void)applicationDidFinishLaunching:(NSNotification *)note
35 {
36     asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
37
38     [self registerDefaultsIfNeeded];
39     
40     menu = [[NSMenu alloc] initWithTitle:@""];
41     iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
42     
43     if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
44     {
45         [self rebuildMenu];
46         refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
47                                                     target:self
48                                                     selector:@selector(timerUpdate)
49                                                     userInfo:nil
50                                                     repeats:YES];
51     }
52     else
53     {
54         menu = [[NSMenu alloc] initWithTitle:@""];
55         [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
56         [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
57         [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
58         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
59         refreshTimer = nil;
60     }
61
62     statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
63                                               withLength:NSSquareStatusItemLength];
64     
65     [statusItem setImage:[NSImage imageNamed:@"menu"]];
66     [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
67     [statusItem setMenu:menu];
68     // Below line of code is for creating builds for Beta Testers
69     // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
70 }
71
72
73 /*************************************************************************/
74 #pragma mark -
75 #pragma mark INSTANCE METHODS
76 /*************************************************************************/
77
78 - (void)registerDefaultsIfNeeded
79 {
80     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
81     if (![defaults objectForKey:@"menu"]) {
82         bool found = NO;
83         NSMutableDictionary *loginwindow;
84         NSMutableArray *loginarray;
85         int i;
86         
87         [defaults setObject:
88             [NSArray arrayWithObjects:
89                 @"Play/Pause",
90                 @"Next Track",
91                 @"Previous Track",
92                 @"Fast Forward",
93                 @"Rewind",
94                 @"<separator>",
95                 @"Upcoming Songs",
96                 @"Playlists",
97                 @"<separator>",
98                 @"PreferencesÉ",
99                 @"Quit",
100                 @"<separator>",
101                 @"Current Track Info",
102                 nil] forKey:@"menu"];
103         
104         [defaults synchronize];
105         loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
106         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
107         
108         for (i = 0; i < [loginarray count]; i++) {
109             NSDictionary *tempDict = [loginarray objectAtIndex:i];
110             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
111                 found = YES;
112             }
113         }
114         
115         if (!found) {
116             if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
117                 AEDesc scriptDesc, resultDesc;
118                 NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
119                 
120                 AECreateDesc(typeChar, [script cString], [script cStringLength], 
121             &scriptDesc);
122                 
123                 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
124                 
125                 AEDisposeDesc(&scriptDesc);
126                 AEDisposeDesc(&resultDesc);
127             }
128         }
129     }
130     
131     if (![defaults integerForKey:@"SongsInAdvance"])
132     {
133         [defaults setInteger:5 forKey:@"SongsInAdvance"];
134     }
135     
136     if (![defaults objectForKey:@"showName"]) {
137         [defaults setBool:YES forKey:@"showName"];
138     }
139     
140     if (![defaults objectForKey:@"showArtist"]) {
141         [defaults setBool:YES forKey:@"showArtist"];
142     }
143     
144     if (![defaults objectForKey:@"showAlbum"]) {
145         [defaults setBool:NO forKey:@"showAlbum"];
146     }
147     
148     if (![defaults objectForKey:@"showTime"]) {
149         [defaults setBool:NO forKey:@"showTime"];
150     }
151 }
152
153 //Recreate the status item menu
154 - (void)rebuildMenu
155 {
156     NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
157     int i;
158     
159     trackInfoIndex = -1;
160     didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
161     didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
162     
163     
164     while ([menu numberOfItems] > 0) {
165         [menu removeItemAtIndex:0];
166     }
167     
168     playPauseMenuItem = nil;
169     upcomingSongsItem = nil;
170     playlistItem = nil;
171     [playlistMenu release];
172     playlistMenu = nil;
173     eqItem = nil;
174     [eqMenu release];
175     eqMenu = nil;
176     
177     for (i = 0; i < [myMenu count]; i++) {
178         NSString *item = [myMenu objectAtIndex:i];
179         if ([item isEqualToString:@"Play/Pause"]) {
180             playPauseMenuItem = [menu addItemWithTitle:@"Play"
181                                                 action:@selector(playPause:)
182                                          keyEquivalent:@""];
183             [playPauseMenuItem setTarget:self];
184         } else if ([item isEqualToString:@"Next Track"]) {
185             [[menu addItemWithTitle:@"Next Track"
186                              action:@selector(nextSong:)
187                       keyEquivalent:@""] setTarget:self];
188         } else if ([item isEqualToString:@"Previous Track"]) {
189             [[menu addItemWithTitle:@"Previous Track"
190                              action:@selector(prevSong:)
191                       keyEquivalent:@""] setTarget:self];
192         } else if ([item isEqualToString:@"Fast Forward"]) {
193             [[menu addItemWithTitle:@"Fast Forward"
194                              action:@selector(fastForward:)
195                       keyEquivalent:@""] setTarget:self];
196         } else if ([item isEqualToString:@"Rewind"]) {
197             [[menu addItemWithTitle:@"Rewind"
198                              action:@selector(rewind:)
199                       keyEquivalent:@""] setTarget:self];
200         } else if ([item isEqualToString:@"Upcoming Songs"]) {
201             upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
202                                                 action:nil
203                                          keyEquivalent:@""];
204         } else if ([item isEqualToString:@"Playlists"]) {
205             playlistItem = [menu addItemWithTitle:@"Playlists"
206                                            action:nil
207                                     keyEquivalent:@""];
208         } else if ([item isEqualToString:@"EQ Presets"]) {
209             eqItem = [menu addItemWithTitle:@"EQ Presets"
210                                      action:nil
211                               keyEquivalent:@""];
212         } else if ([item isEqualToString:@"PreferencesÉ"]) {
213             [[menu addItemWithTitle:@"PreferencesÉ"
214                              action:@selector(showPreferences:)
215                       keyEquivalent:@""] setTarget:self];
216         } else if ([item isEqualToString:@"Quit"]) {
217             [[menu addItemWithTitle:@"Quit"
218                              action:@selector(quitMenuTunes:)
219                       keyEquivalent:@""] setTarget:self];
220         } else if ([item isEqualToString:@"Current Track Info"]) {
221             trackInfoIndex = [menu numberOfItems];
222             [menu addItemWithTitle:@"No Song"
223                             action:nil
224                      keyEquivalent:@""];
225         } else if ([item isEqualToString:@"<separator>"]) {
226             [menu addItem:[NSMenuItem separatorItem]];
227         }
228     }
229     
230     curTrackIndex = -1; //Force update of everything
231     [self timerUpdate]; //Updates dynamic info in the menu
232     
233     [self clearHotKeys];
234     [self setupHotKeys];
235 }
236
237 //Updates the menu with current player state, song, and upcoming songs
238 - (void)updateMenu
239 {
240     NSMenuItem *menuItem;
241     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
242     
243     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
244         return;
245     }
246     
247     if (upcomingSongsItem) {
248         [self rebuildUpcomingSongsMenu];
249     }
250     
251     if (playlistItem) {
252         [self rebuildPlaylistMenu];
253     }
254     
255     if (eqItem) {
256         [self rebuildEQPresetsMenu];
257     }
258     
259     if (trackInfoIndex > -1)
260     {
261         NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
262         curSongName = [self runScriptAndReturnResult:@"return name of current track"];
263         
264         if ([defaults boolForKey:@"showAlbum"]) {
265             curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
266         }
267         
268         if ([defaults boolForKey:@"showArtist"]) {
269             curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
270         }
271         
272         if ([curSongName length] > 0) {
273             int index = [menu indexOfItemWithTitle:@"Now Playing"];
274             if (index > -1) {
275                 if ([defaults boolForKey:@"showName"]) {
276                     [menu removeItemAtIndex:index + 1];
277                 }
278                 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
279                     [menu removeItemAtIndex:index + 1];
280                 }
281                 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
282                     [menu removeItemAtIndex:index + 1];
283                 }
284                 if ([defaults boolForKey:@"showTime"]) {
285                     [menu removeItemAtIndex:index + 1];
286                 }
287             }
288             
289             if (!isPlayingRadio) {
290                 if ([defaults boolForKey:@"showTime"]) {
291                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", [self runScriptAndReturnResult:@"return time of current track"]]
292                                                         action:nil
293                                                         keyEquivalent:@""];
294                     [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
295                     [menuItem release];
296                 }
297                 
298                 if ([curArtistName length] > 0) {
299                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curArtistName]
300                                                         action:nil
301                                                         keyEquivalent:@""];
302                     [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
303                     [menuItem release];
304                 }
305                 
306                 if ([curAlbumName length] > 0) {
307                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName]
308                                                         action:nil
309                                                         keyEquivalent:@""];
310                     [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
311                     [menuItem release];
312                 }
313             }
314             
315             if ([defaults boolForKey:@"showName"]) {
316                 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curSongName]
317                                                     action:nil
318                                                     keyEquivalent:@""];
319                 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
320                 [menuItem release];
321             }
322             
323             if (index == -1) {
324                 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
325                 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
326                 [menu insertItem:menuItem atIndex:trackInfoIndex];
327                 [menuItem release];
328             }
329         } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
330             [menu removeItemAtIndex:trackInfoIndex];
331             
332             if ([defaults boolForKey:@"showName"] == YES) {
333                 [menu removeItemAtIndex:trackInfoIndex];
334             }
335             
336             if ([defaults boolForKey:@"showTime"] == YES) {
337                 [menu removeItemAtIndex:trackInfoIndex];
338             }
339             
340             if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
341                 [menu removeItemAtIndex:trackInfoIndex];
342             }
343             
344             if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
345                 [menu removeItemAtIndex:trackInfoIndex];
346             }
347             
348             menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
349             [menu insertItem:menuItem atIndex:trackInfoIndex];
350             [menuItem release];
351         }
352         
353         if ([defaults boolForKey:@"showArtist"]) {
354             didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
355         }
356             
357         if ([defaults boolForKey:@"showAlbum"]) {
358             didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
359         }
360     }
361 }
362
363 //Rebuild the upcoming songs submenu. Can be improved a lot.
364 - (void)rebuildUpcomingSongsMenu
365 {
366     int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
367     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
368     if (!isPlayingRadio) {
369         if (numSongs > 0) {
370             int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
371             int i;
372             
373             [upcomingSongsMenu release];
374             upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
375             [upcomingSongsItem setSubmenu:upcomingSongsMenu];
376             [upcomingSongsItem setEnabled:YES];
377             
378             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
379                 if (i <= numSongs) {
380                     NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
381                     NSMenuItem *songItem;
382                     songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
383                     [songItem setTarget:self];
384                     [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
385                     [upcomingSongsMenu addItem:songItem];
386                     [songItem release];
387                 } else {
388                     break;
389                 }
390             }
391         }
392     } else {
393         [upcomingSongsItem setSubmenu:nil];
394         [upcomingSongsItem setEnabled:NO];
395     }
396 }
397
398 - (void)rebuildPlaylistMenu
399 {
400     int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
401     int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
402     
403     if (isPlayingRadio)
404     {
405         curPlaylist = 0;
406     }
407     
408     if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
409         return;
410     
411     [playlistMenu release];
412     playlistMenu = [[NSMenu alloc] initWithTitle:@""];
413     
414     for (i = 1; i <= numPlaylists; i++) {
415         NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
416         NSMenuItem *tempItem;
417         tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
418         [tempItem setTarget:self];
419         [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
420         [playlistMenu addItem:tempItem];
421         [tempItem release];
422     }
423     [playlistItem setSubmenu:playlistMenu];
424     
425     if (curPlaylist) {
426         [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
427     }
428 }
429
430 //Build a menu with the list of all available EQ presets
431 - (void)rebuildEQPresetsMenu
432 {
433     int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
434     int i;
435     
436     if (eqMenu && (numSets == [eqMenu numberOfItems]))
437         return;
438     
439     [eqMenu release];
440     eqMenu = [[NSMenu alloc] initWithTitle:@""];
441     
442     for (i = 1; i <= numSets; i++) {
443         NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
444         NSMenuItem *tempItem;
445         tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
446         [tempItem setTarget:self];
447         [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
448         [eqMenu addItem:tempItem];
449         [tempItem release];
450     }
451     [eqItem setSubmenu:eqMenu];
452     
453     [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
454 }
455
456 - (void)clearHotKeys
457 {
458     [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
459     [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
460     [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
461     [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
462     [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
463 }
464
465 - (void)setupHotKeys
466 {
467     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
468     
469     if ([defaults objectForKey:@"PlayPause"] != nil) {
470         [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
471                 combo:[defaults keyComboForKey:@"PlayPause"]
472                 target:self action:@selector(playPause:)];
473     }
474     
475     if ([defaults objectForKey:@"NextTrack"] != nil) {
476         [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
477                 combo:[defaults keyComboForKey:@"NextTrack"]
478                 target:self action:@selector(nextSong:)];
479     }
480     
481     if ([defaults objectForKey:@"PrevTrack"] != nil) {
482         [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
483                 combo:[defaults keyComboForKey:@"PrevTrack"]
484                 target:self action:@selector(prevSong:)];
485     }
486     
487     if ([defaults objectForKey:@"TrackInfo"] != nil) {
488         [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
489                 combo:[defaults keyComboForKey:@"TrackInfo"]
490                 target:self action:@selector(showCurrentTrackInfo)];
491     }
492     
493     if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
494         [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
495                combo:[defaults keyComboForKey:@"UpcomingSongs"]
496                target:self action:@selector(showUpcomingSongs)];
497     }
498 }
499
500 //Runs an AppleScript and returns the result as an NSString after stripping quotes, if needed. It takes in script and automatically adds the tell iTunes and end tell statements.
501 - (NSString *)runScriptAndReturnResult:(NSString *)script
502 {
503     AEDesc scriptDesc, resultDesc;
504     Size length;
505     NSString *result;
506     Ptr buffer;
507     
508     script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
509     
510     AECreateDesc(typeChar, [script cString], [script cStringLength], 
511 &scriptDesc);
512     
513     OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
514     
515     length = AEGetDescDataSize(&resultDesc);
516     buffer = malloc(length);
517     
518     AEGetDescData(&resultDesc, buffer, length);
519     AEDisposeDesc(&scriptDesc);
520     AEDisposeDesc(&resultDesc);
521     result = [NSString stringWithCString:buffer length:length];
522     if ( (! [result isEqualToString:@""])      &&
523          ([result characterAtIndex:0] == '\"') &&
524          ([result characterAtIndex:[result length] - 1] == '\"') ) {
525         result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
526     }
527     free(buffer);
528     buffer = nil;
529     return result;
530 }
531
532 //Called when the timer fires.
533 - (void)timerUpdate
534 {
535     int pid;
536     if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
537         int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
538         
539         if (trackPlayingIndex != curTrackIndex) {
540             bool wasPlayingRadio = isPlayingRadio;
541             isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
542             if (isPlayingRadio && !wasPlayingRadio) {
543                 int i;
544                 for (i = 0; i < [playlistMenu numberOfItems]; i++)
545                 {
546                     [[playlistMenu itemAtIndex:i] setState:NSOffState];
547                 }
548             }
549             if (wasPlayingRadio) {
550                 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
551                 [menu insertItem:temp atIndex:trackInfoIndex + 1];
552                 [temp release];
553             }
554             [self updateMenu];
555             curTrackIndex = trackPlayingIndex;
556         }
557         else
558         {
559             int playlist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
560             if (playlist != curPlaylistIndex) {
561                 bool wasPlayingRadio = isPlayingRadio;
562                 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
563                 if (isPlayingRadio && !wasPlayingRadio) {
564                     int i;
565                     for (i = 0; i < [playlistMenu numberOfItems]; i++)
566                     {
567                         [[playlistMenu itemAtIndex:i] setState:NSOffState];
568                     }
569                 }
570                 if (wasPlayingRadio) {
571                     NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
572                     [menu insertItem:temp atIndex:trackInfoIndex + 1];
573                     [temp release];
574                 }
575                 [self updateMenu];
576                 curTrackIndex = trackPlayingIndex;
577                 curPlaylistIndex = playlist;
578             }
579         }
580         //Update Play/Pause menu item
581         if (playPauseMenuItem){
582             if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
583                 [playPauseMenuItem setTitle:@"Pause"];
584             } else {
585                 [playPauseMenuItem setTitle:@"Play"];
586             }
587         }
588     } else {
589         [menu release];
590         menu = [[NSMenu alloc] initWithTitle:@""];
591         [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
592         [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
593         [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
594         [statusItem setMenu:menu];
595         
596         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
597         [refreshTimer invalidate];
598         refreshTimer = nil;
599         [self clearHotKeys];
600     }
601 }
602
603 - (void)iTunesLaunched:(NSNotification *)note
604 {
605     NSDictionary *info = [note userInfo];
606     
607     iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
608     iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
609     
610     //Restart the timer
611     refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES]; 
612     
613     [self rebuildMenu]; //Rebuild the menu since no songs will be playing
614     [statusItem setMenu:menu]; //Set the menu back to the main one
615     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
616 }
617
618 //Return the PSN of iTunes, if it's running
619 - (ProcessSerialNumber)iTunesPSN
620 {
621     ProcessSerialNumber procNum;
622     procNum.highLongOfPSN = kNoProcess;
623     procNum.lowLongOfPSN = 0;
624     
625     while ( (GetNextProcess(&procNum) == noErr) ) {
626         CFStringRef procName;
627         if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
628             if ([(NSString *)procName isEqualToString:@"iTunes"]) {
629                 return procNum;
630             }
631             CFRelease(procName);
632         }
633     }
634     return procNum;
635 }
636
637 //Send an AppleEvent with a given event ID
638 - (void)sendAEWithEventClass:(AEEventClass)eventClass 
639 andEventID:(AEEventID)eventID
640 {
641     OSType iTunesType = 'hook';
642     AppleEvent event, reply;
643     
644     AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
645     
646     AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
647     AEDisposeDesc(&event);
648     AEDisposeDesc(&reply);
649 }
650
651 //
652 // Selectors - called from status item menu
653 //
654
655 - (void)playTrack:(id)sender
656 {
657     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
658     [self updateMenu];
659 }
660
661 - (void)selectPlaylist:(id)sender
662 {
663     int playlist = [[sender representedObject] intValue];
664     if (!isPlayingRadio) {
665         int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
666         [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
667     }
668     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
669     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
670 }
671
672 - (void)selectEQPreset:(id)sender
673 {
674     int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
675     int item = [[sender representedObject] intValue];
676     [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
677     [self runScriptAndReturnResult:@"set EQ enabled to 1"];
678     [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
679     [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
680 }
681
682 - (void)playPause:(id)sender
683 {
684     NSString *state = [self runScriptAndReturnResult:@"return player state"];
685     if ([state isEqualToString:@"playing"]) {
686         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
687         [playPauseMenuItem setTitle:@"Play"];
688     } else if ([state isEqualToString:@"fast forwarding"] || [state 
689 isEqualToString:@"rewinding"]) {
690         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
691         [self sendAEWithEventClass:'hook' andEventID:'Play'];
692     } else {
693         [self sendAEWithEventClass:'hook' andEventID:'Play'];
694         [playPauseMenuItem setTitle:@"Pause"];
695     }
696 }
697
698 - (void)nextSong:(id)sender
699 {
700     [self sendAEWithEventClass:'hook' andEventID:'Next'];
701 }
702
703 - (void)prevSong:(id)sender
704 {
705     [self sendAEWithEventClass:'hook' andEventID:'Prev'];
706 }
707
708 - (void)fastForward:(id)sender
709 {
710     [self sendAEWithEventClass:'hook' andEventID:'Fast'];
711 }
712
713 - (void)rewind:(id)sender
714 {
715     [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
716 }
717
718 - (void)quitMenuTunes:(id)sender
719 {
720     [NSApp terminate:self];
721 }
722
723 - (void)openiTunes:(id)sender
724 {
725     [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
726 }
727
728 - (void)showPreferences:(id)sender
729 {
730     if (!prefsController) {
731         prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
732         [self clearHotKeys];
733     }
734 }
735
736
737 - (void)closePreferences
738 {
739     if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
740         [self setupHotKeys];
741     }
742     [prefsController release];
743     prefsController = nil;
744 }
745
746 //
747 //
748 // Show Current Track Info And Show Upcoming Songs Floaters
749 //
750 //
751
752 - (void)showCurrentTrackInfo
753 {
754     NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
755     if (!statusController && [trackName length]) {
756         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
757         NSString *stringToShow = @"";
758         
759         if ([defaults boolForKey:@"showName"]) {
760             if ([defaults boolForKey:@"showArtist"]) {
761                 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
762                 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
763             }
764             stringToShow = [stringToShow stringByAppendingString:trackName];
765             stringToShow = [stringToShow stringByAppendingString:@"\n"];
766         }
767         
768         if ([defaults boolForKey:@"showAlbum"]) {
769             NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
770             if ([trackAlbum length]) {
771                 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
772                 stringToShow = [stringToShow stringByAppendingString:@"\n"];
773             }
774         }
775         
776         if ([defaults boolForKey:@"showTime"]) {
777             NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
778             if ([trackTime length]) {
779                 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
780             }
781         }
782         
783         {
784             int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
785             int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
786             if (seconds < 10) {
787                 stringToShow = [stringToShow stringByAppendingString:
788                             [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
789             } else {
790                 stringToShow = [stringToShow stringByAppendingString:
791                             [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
792             }
793         }
794         
795         statusController = [[StatusWindowController alloc] init];
796         [statusController setTrackInfo:stringToShow];
797         [NSTimer scheduledTimerWithTimeInterval:3.0
798                                     target:self
799                                     selector:@selector(fadeAndCloseStatusWindow)
800                                     userInfo:nil
801                                     repeats:NO];
802     }
803 }
804
805 - (void)showUpcomingSongs
806 {
807     if (!statusController) {
808         int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
809         
810         if (numSongs > 0) {
811             int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
812             int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
813             int i;
814             NSString *songs = @"";
815             
816             statusController = [[StatusWindowController alloc] init];
817             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
818                 if (i <= numSongs) {
819                     NSString *curSong = [self runScriptAndReturnResult:
820                         [NSString stringWithFormat:@"return name of track %i of current playlist", i]];
821                     songs = [songs stringByAppendingString:curSong];
822                     songs = [songs stringByAppendingString:@"\n"];
823                 }
824             }
825             [statusController setUpcomingSongs:songs];
826             [NSTimer scheduledTimerWithTimeInterval:3.0
827                                              target:self
828                                            selector:@selector(fadeAndCloseStatusWindow)
829                                            userInfo:nil
830                                             repeats:NO];
831         }
832     }
833 }
834
835 - (void)fadeAndCloseStatusWindow
836 {
837     [statusController fadeWindowOut];
838     [statusController release];
839     statusController = nil;
840 }
841
842 /*************************************************************************/
843 #pragma mark -
844 #pragma mark NSApplication DELEGATE METHODS
845 /*************************************************************************/
846
847 - (void)applicationWillTerminate:(NSNotification *)note
848 {
849     [self clearHotKeys];
850     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
851 }
852
853
854 /*************************************************************************/
855 #pragma mark -
856 #pragma mark DEALLOCATION METHODS
857 /*************************************************************************/
858
859 - (void)dealloc
860 {
861     if (refreshTimer) {
862         [refreshTimer invalidate];
863         refreshTimer = nil;
864     }
865     CloseComponent(asComponent);
866     [statusItem release];
867     [menu release];
868 //  [view release];
869     [super dealloc];
870 }
871
872
873 @end