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