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