Fixed stupid crash.
[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     
162     curTrackIndex = -1; //Force update of everything
163     [self timerUpdate]; //Updates dynamic info in the menu
164     
165     [self clearHotKeys];
166     [self setupHotKeys];
167 }
168
169 //Updates the menu with current player state, song, and upcoming songs
170 - (void)updateMenu
171 {
172     NSString *curSongName, *curAlbumName;
173     NSMenuItem *menuItem;
174     
175     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))
176     {
177         return;
178     }
179     
180     //Get the current track name and album.
181     curSongName = [self runScriptAndReturnResult:@"return name of current track"];
182     curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
183     
184     if (upcomingSongsItem)
185     {
186         [self rebuildUpcomingSongsMenu];
187     }
188     if (playlistItem)
189     {
190         [self rebuildPlaylistMenu];
191     }
192     if (eqItem)
193     {
194         [self rebuildEQPresetsMenu];
195     }
196     
197     if ([curSongName length] > 0)
198     {
199         int index = [menu indexOfItemWithTitle:@"Now Playing"];
200         
201         if (index > -1)
202         {
203             [menu removeItemAtIndex:index + 1];
204             if (didHaveAlbumName)
205             {
206                 [menu removeItemAtIndex:index + 1];
207             }
208         }
209         
210         if ([curAlbumName length] > 0)
211         {
212             menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName] action:NULL keyEquivalent:@""];
213             [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
214             [menuItem release];
215         }
216         
217         menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curSongName] action:NULL keyEquivalent:@""];
218         [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
219         [menuItem release];
220         
221         if (index == -1)
222         {
223             menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:NULL keyEquivalent:@""];
224             [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
225             [menu insertItem:menuItem atIndex:trackInfoIndex];
226             [menuItem release];
227         }
228     }
229     else if ([menu indexOfItemWithTitle:@"No Song"] == -1)
230     {
231         [menu removeItemAtIndex:trackInfoIndex];
232         [menu removeItemAtIndex:trackInfoIndex];
233         if (didHaveAlbumName)
234         {
235             [menu removeItemAtIndex:trackInfoIndex];
236         }
237         menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:NULL keyEquivalent:@""];
238         [menu insertItem:menuItem atIndex:trackInfoIndex];
239         [menuItem release];
240     }
241     
242     didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
243 }
244
245 //Rebuild the upcoming songs submenu. Can be improved a lot.
246 - (void)rebuildUpcomingSongsMenu
247 {
248     int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
249     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
250     
251     if (numSongs > 0)
252     {
253         int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
254         int i;
255         
256         [upcomingSongsMenu release];
257         upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
258         
259         for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++)
260         {
261             if (i <= numSongs)
262             {
263                 NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
264                 NSMenuItem *songItem;
265                 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
266                 [songItem setTarget:self];
267                 [songItem setEnabled:YES];
268                 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
269                 [upcomingSongsMenu addItem:songItem];
270                 [songItem release];
271             }
272             else
273             {
274                 [upcomingSongsMenu addItemWithTitle:@"End of playlist." action:NULL keyEquivalent:@""];
275                 break;
276             }
277         }
278         [upcomingSongsItem setSubmenu:upcomingSongsMenu];
279         [upcomingSongsItem setEnabled:YES];
280     }
281 }
282
283 - (void)rebuildPlaylistMenu
284 {
285     int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
286     int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
287     
288     if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
289         return;
290     
291     [playlistMenu release];
292     playlistMenu = [[NSMenu alloc] initWithTitle:@""];
293     
294     for (i = 1; i <= numPlaylists; i++)
295     {
296         NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
297         NSMenuItem *tempItem;
298         tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
299         [tempItem setTarget:self];
300         [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
301         [playlistMenu addItem:tempItem];
302         [tempItem release];
303     }
304     [playlistItem setSubmenu:playlistMenu];
305     
306     if (curPlaylist)
307     {
308         [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
309     }
310 }
311
312 //Build a menu with the list of all available EQ presets
313 - (void)rebuildEQPresetsMenu
314 {
315     int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
316     int i;
317     
318     if (eqMenu && (numSets == [eqMenu numberOfItems]))
319         return;
320     
321     [eqMenu release];
322     eqMenu = [[NSMenu alloc] initWithTitle:@""];
323     
324     for (i = 1; i <= numSets; i++)
325     {
326         NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
327         NSMenuItem *tempItem;
328         tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
329         [tempItem setTarget:self];
330         [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
331         [eqMenu addItem:tempItem];
332         [tempItem release];
333     }
334     [eqItem setSubmenu:eqMenu];
335     
336     [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
337 }
338
339 - (void)clearHotKeys
340 {
341     [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
342     [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
343     [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
344     [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
345     [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
346 }
347
348 - (void)setupHotKeys
349 {
350     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
351     
352     if ([defaults objectForKey:@"PlayPause"] != nil)
353     {
354         [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
355                 combo:[defaults keyComboForKey:@"PlayPause"]
356                 target:self action:@selector(playPause:)];
357     }
358     
359     if ([defaults objectForKey:@"NextTrack"] != nil)
360     {
361         [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
362                 combo:[defaults keyComboForKey:@"NextTrack"]
363                 target:self action:@selector(nextSong:)];
364     }
365     
366     if ([defaults objectForKey:@"PrevTrack"] != nil)
367     {
368         [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
369                 combo:[defaults keyComboForKey:@"PrevTrack"]
370                 target:self action:@selector(prevSong:)];
371     }
372     
373     if ([defaults objectForKey:@"TrackInfo"] != nil)
374     {
375         [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
376                 combo:[defaults keyComboForKey:@"TrackInfo"]
377                 target:self action:@selector(showCurrentTrackInfo)];
378     }
379     
380     if ([defaults objectForKey:@"UpcomingSongs"] != nil)
381     {
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(OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype), &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     {
414         result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
415     }
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     
426     if (GetProcessPID(&iTunesPSN, &pid) == noErr)
427     {
428         int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
429         
430         if (trackPlayingIndex != curTrackIndex)
431         {
432             [self updateMenu];
433             curTrackIndex = trackPlayingIndex;
434         }
435         
436         //Update Play/Pause menu item
437         if (playPauseMenuItem)
438         {
439             if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"])
440             {
441                 [playPauseMenuItem setTitle:@"Pause"];
442             }
443             else
444             {
445                 [playPauseMenuItem setTitle:@"Play"];
446             }
447         }
448     }
449     else
450     {
451         [menu release];
452         menu = [[NSMenu alloc] initWithTitle:@""];
453         [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
454         [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
455         [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
456         [statusItem setMenu:menu];
457         
458         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
459         [refreshTimer invalidate];
460         refreshTimer = nil;
461         [self clearHotKeys];
462     }
463 }
464
465 - (void)iTunesLaunched:(NSNotification *)note
466 {
467     NSDictionary *info = [note userInfo];
468     
469     iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
470     iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
471     
472     //Restart the timer
473     refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES]; 
474     
475     [self rebuildMenu]; //Rebuild the menu since no songs will be playing
476     [statusItem setMenu:menu]; //Set the menu back to the main one
477     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
478 }
479
480 //Return the PSN of iTunes, if it's running
481 - (ProcessSerialNumber)iTunesPSN
482 {
483     ProcessSerialNumber procNum;
484     procNum.highLongOfPSN = kNoProcess;
485     procNum.lowLongOfPSN = 0;
486     
487     while ( (GetNextProcess(&procNum) == noErr) ) 
488     {
489         CFStringRef procName;
490         if ( (CopyProcessName(&procNum, &procName) == noErr) )
491         {
492             if ([(NSString *)procName isEqualToString:@"iTunes"])
493             {
494                 return procNum;
495             }
496             [(NSString *)procName release];
497         }
498     }
499     return procNum;
500 }
501
502 //Send an AppleEvent with a given event ID
503 - (void)sendAEWithEventClass:(AEEventClass)eventClass 
504 andEventID:(AEEventID)eventID
505 {
506     OSType iTunesType = 'hook';
507     AppleEvent event, reply;
508     
509     AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, NULL, "");
510     
511     AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
512     AEDisposeDesc(&event);
513     AEDisposeDesc(&reply);
514 }
515
516 //
517 // Selectors - called from status item menu
518 //
519
520 - (void)playTrack:(id)sender
521 {
522     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
523     [self updateMenu];
524 }
525
526 - (void)selectPlaylist:(id)sender
527 {
528     int playlist = [[sender representedObject] intValue];
529     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
530     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
531     [self updateMenu];
532 }
533
534 - (void)selectEQPreset:(id)sender
535 {
536     int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
537     int item = [[sender representedObject] intValue];
538     [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
539     [self runScriptAndReturnResult:@"set EQ enabled to 1"];
540     [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
541     [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
542 }
543
544 - (void)playPause:(id)sender
545 {
546     NSString *state = [self runScriptAndReturnResult:@"return player state"];
547     if ([state isEqualToString:@"playing"])
548     {
549         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
550         [playPauseMenuItem setTitle:@"Play"];
551     }
552     else if ([state isEqualToString:@"fast forwarding"] || [state 
553 isEqualToString:@"rewinding"])
554     {
555         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
556         [self sendAEWithEventClass:'hook' andEventID:'Play'];
557     }
558     else
559     {
560         [self sendAEWithEventClass:'hook' andEventID:'Play'];
561         [playPauseMenuItem setTitle:@"Pause"];
562     }
563 }
564
565 - (void)nextSong:(id)sender
566 {
567     [self sendAEWithEventClass:'hook' andEventID:'Next'];
568 }
569
570 - (void)prevSong:(id)sender
571 {
572     [self sendAEWithEventClass:'hook' andEventID:'Prev'];
573 }
574
575 - (void)fastForward:(id)sender
576 {
577     [self sendAEWithEventClass:'hook' andEventID:'Fast'];
578 }
579
580 - (void)rewind:(id)sender
581 {
582     [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
583 }
584
585 - (void)quitMenuTunes:(id)sender
586 {
587     [NSApp terminate:self];
588 }
589
590 - (void)openiTunes:(id)sender
591 {
592     [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
593 }
594
595 - (void)showPreferences:(id)sender
596 {
597     if (!prefsController)
598     {
599         prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
600         [self clearHotKeys];
601     }
602 }
603
604
605 - (void)closePreferences
606 {
607     if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
608     {
609         [self setupHotKeys];
610     }
611     [prefsController release];
612     prefsController = nil;
613 }
614
615 //
616 //
617 // Show Current Track Info And Show Upcoming Songs Floaters
618 //
619 //
620
621 - (void)showCurrentTrackInfo
622 {
623     NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
624     if (!statusController && [trackName length])
625     {
626         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
627         NSString *stringToShow = @"";
628         int lines = 1;
629         
630         if ([defaults boolForKey:@"showName"])
631         {
632             if ([defaults boolForKey:@"showArtist"])
633             {
634                 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
635                 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
636             }
637             stringToShow = [stringToShow stringByAppendingString:trackName];
638             stringToShow = [stringToShow stringByAppendingString:@"\n"];
639             if ([trackName length] > 38)
640             {
641                 lines++;
642             }
643             lines++;
644         }
645         
646         if ([defaults boolForKey:@"showAlbum"])
647         {
648             NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
649             if ([trackAlbum length])
650             {
651                 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
652                 stringToShow = [stringToShow stringByAppendingString:@"\n"];
653                 lines++;
654             }
655         }
656         
657         if ([defaults boolForKey:@"showTime"])
658         {
659             NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
660             if ([trackTime length])
661             {
662                 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
663                 lines++;
664             }
665         }
666         
667         {
668             int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
669             int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
670             if (seconds < 10)
671             {
672                 stringToShow = [stringToShow stringByAppendingString:
673                             [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
674             }
675             else
676             {
677                 stringToShow = [stringToShow stringByAppendingString:
678                             [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
679             }
680         }
681         
682         statusController = [[StatusWindowController alloc] init];
683         [statusController setTrackInfo:stringToShow lines:lines];
684         [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeAndCloseStatusWindow) userInfo:nil repeats:NO];
685     }
686 }
687
688 - (void)showUpcomingSongs
689 {
690     if (!statusController)
691     {
692         int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
693         
694         if (numSongs > 0)
695         {
696             int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
697             int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
698             int i;
699             NSString *songs = @"";
700             
701             statusController = [[StatusWindowController alloc] init];
702             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++)
703             {
704                 if (i <= numSongs)
705                 {
706                     NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
707                     songs = [songs stringByAppendingString:curSong];
708                     songs = [songs stringByAppendingString:@"\n"];
709                 }
710             }
711             [statusController setUpcomingSongs:songs numSongs:numSongsInAdvance];
712             [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeAndCloseStatusWindow) userInfo:nil repeats:NO];
713         }
714     }
715 }
716
717 - (void)fadeAndCloseStatusWindow
718 {
719     [statusController fadeWindowOut];
720     [statusController release];
721     statusController = nil;
722 }
723
724 @end