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