Rewrote updateMenu/timerUpdate, hopefully it'll work better.
[MenuTunes.git] / OldMainController.m
1 #import "MainController.h"
2 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
4 #import "StatusWindow.h"
5
6 @interface MainController(Private)
7 - (ITMTRemote *)loadRemote;
8 - (void)rebuildUpcomingSongsMenu;
9 - (void)rebuildPlaylistMenu;
10 - (void)rebuildEQPresetsMenu;
11 - (void)setupHotKeys;
12 - (void)timerUpdate;
13 - (void)updateMenu;
14 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
15         onItem:(NSMenuItem *)item;
16
17 @end
18
19 @implementation MainController
20
21 /*************************************************************************/
22 #pragma mark -
23 #pragma mark INITIALIZATION/DEALLOCATION METHODS
24 /*************************************************************************/
25
26 - (id)init
27 {
28     if ( ( self = [super init] ) ) {
29         remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
30         statusWindow = [StatusWindow sharedWindow];
31     }
32     return self;
33 }
34
35 - (void)dealloc
36 {
37     if (refreshTimer) {
38         [refreshTimer invalidate];
39         [refreshTimer release];
40         refreshTimer = nil;
41     }
42     [currentRemote halt];
43     [statusItem release];
44     [menu release];
45     [super dealloc];
46 }
47
48 - (void)applicationDidFinishLaunching:(NSNotification *)note
49 {
50     currentRemote = [self loadRemote];
51     [currentRemote begin];
52     
53     //Setup for notification of the remote player launching or quitting
54     [[[NSWorkspace sharedWorkspace] notificationCenter]
55             addObserver:self
56             selector:@selector(applicationTerminated:)
57             name:NSWorkspaceDidTerminateApplicationNotification
58             object:nil];
59     
60     [[[NSWorkspace sharedWorkspace] notificationCenter]
61             addObserver:self
62             selector:@selector(applicationLaunched:)
63             name:NSWorkspaceDidLaunchApplicationNotification
64             object:nil];
65     
66     [self registerDefaults];
67     
68     statusItem = [[ITStatusItem alloc]
69             initWithStatusBar:[NSStatusBar systemStatusBar]
70             withLength:NSSquareStatusItemLength];
71     
72     menu = [[NSMenu alloc] initWithTitle:@""];
73     if ( ( [currentRemote playerRunningState] == ITMTRemotePlayerRunning ) ) {
74         [self applicationLaunched:nil];
75     } else {
76         [self applicationTerminated:nil];
77     }
78     
79     [statusItem setImage:[NSImage imageNamed:@"menu"]];
80     [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
81     // Below line of code is for creating builds for Beta Testers
82     // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
83 }
84
85 - (void)applicationWillTerminate:(NSNotification *)note
86 {
87     [self clearHotKeys];
88     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
89 }
90
91 - (ITMTRemote *)loadRemote
92 {
93     NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
94     
95     if (folderPath) {
96         NSArray      *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
97         NSEnumerator *enumerator     = [bundlePathList objectEnumerator];
98         NSString     *bundlePath;
99
100         while ( (bundlePath = [enumerator nextObject]) ) {
101             NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
102
103             if (remoteBundle) {
104                 Class remoteClass = [remoteBundle principalClass];
105
106                 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
107                     [remoteClass isKindOfClass:[NSObject class]]) {
108
109                     id remote = [remoteClass remote];
110                     [remoteArray addObject:remote];
111                 }
112             }
113         }
114
115 //      if ( [remoteArray count] > 0 ) {  // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
116 //          if ( [remoteArray count] > 1 ) {
117 //              [remoteArray sortUsingSelector:@selector(sortAlpha:)];
118 //          }
119 //          [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
120 //      }
121     }
122 //  NSLog(@"%@", [remoteArray objectAtIndex:0]);  //DEBUG
123     return [remoteArray objectAtIndex:0];
124 }
125
126 //
127 //
128
129 - (void)applicationLaunched:(NSNotification *)note
130 {
131     if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
132         [NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
133         
134         [self rebuildMenu];
135         [statusItem setMenu:menu];
136         [self setupHotKeys];
137         isAppRunning = ITMTRemotePlayerRunning;
138         return;
139     }
140     
141     isAppRunning = ITMTRemotePlayerRunning;
142 }
143
144 - (void)applicationTerminated:(NSNotification *)note
145 {
146     if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {        
147         NSMenu *notRunningMenu = [[NSMenu alloc] initWithTitle:@""];
148         [[notRunningMenu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self];
149         [notRunningMenu addItem:[NSMenuItem separatorItem]];
150         [[notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
151         [[notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
152         [statusItem setMenu:[notRunningMenu autorelease]];
153         
154         [refreshTimer invalidate];
155         [refreshTimer release];
156         refreshTimer = nil;
157         [self clearHotKeys];
158         isAppRunning = NO;
159         return;
160     }
161 }
162
163 /*************************************************************************/
164 #pragma mark -
165 #pragma mark INSTANCE METHODS
166 /*************************************************************************/
167
168 - (void)registerDefaults
169 {
170     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
171     if (![defaults objectForKey:@"menu"]) {
172         BOOL found = NO;
173         NSMutableDictionary *loginwindow;
174         NSMutableArray *loginarray;
175         int i;
176         
177         [defaults setObject:
178             [NSArray arrayWithObjects:
179                 @"Play/Pause",
180                 @"Next Track",
181                 @"Previous Track",
182                 @"Fast Forward",
183                 @"Rewind",
184                 @"<separator>",
185                 @"Upcoming Songs",
186                 @"Playlists",
187                 @"Song Rating",
188                 @"<separator>",
189                 @"PreferencesÉ",
190                 @"Quit",
191                 @"<separator>",
192                 @"Current Track Info",
193                 nil] forKey:@"menu"];
194         
195         [defaults synchronize];
196         loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
197         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
198         
199         for (i = 0; i < [loginarray count]; i++) {
200             NSDictionary *tempDict = [loginarray objectAtIndex:i];
201             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:
202                 [[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
203                 found = YES;
204             }
205         }
206         
207         //
208         //This is teh sux
209         //We must fix it so it is no longer suxy
210         if (!found) {
211             if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
212                 AEDesc scriptDesc, resultDesc;
213                 NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
214                 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
215                 
216                 AECreateDesc(typeChar, [script cString], [script cStringLength], 
217             &scriptDesc);
218                 
219                 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
220                 
221                 AEDisposeDesc(&scriptDesc);
222                 AEDisposeDesc(&resultDesc);
223                 
224                 CloseComponent(asComponent);
225             }
226         }
227     }
228     
229     if (![defaults integerForKey:@"SongsInAdvance"])
230     {
231         [defaults setInteger:5 forKey:@"SongsInAdvance"];
232     }
233     
234     if (![defaults objectForKey:@"showName"]) {
235         [defaults setBool:YES forKey:@"showName"];
236     }
237     
238     if (![defaults objectForKey:@"showArtist"]) {
239         [defaults setBool:YES forKey:@"showArtist"];
240     }
241     
242     if (![defaults objectForKey:@"showAlbum"]) {
243         [defaults setBool:NO forKey:@"showAlbum"];
244     }
245     
246     if (![defaults objectForKey:@"showTime"]) {
247         [defaults setBool:NO forKey:@"showTime"];
248     }
249 }
250
251 - (void)startTimerInNewThread
252 {
253     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
254     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
255     refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
256                              target:self
257                              selector:@selector(timerUpdate)
258                              userInfo:nil
259                              repeats:YES] retain];
260     [runLoop run];
261     [pool release];
262 }
263
264 //Recreate the status item menu
265 - (void)rebuildMenu
266 {
267     NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
268     int i;
269     
270     trackInfoIndex = -1;
271     lastPlaylistIndex = -1;
272     didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
273     didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
274     
275     [menu autorelease];
276     menu = [[NSMenu alloc] initWithTitle:@""];
277     
278     /*while ([menu numberOfItems] > 0) {
279         [menu removeItemAtIndex:0];
280     }*/
281     
282     playPauseItem = nil;
283     lastSongIdentifier = @"0-0";
284     
285     upcomingSongsItem = nil;
286     [upcomingSongsMenu release];
287     upcomingSongsMenu = nil;
288     
289     ratingItem = nil;
290     [ratingMenu release];
291     ratingMenu = nil;
292     
293     playlistItem = nil;
294     [playlistMenu release];
295     playlistMenu = nil;
296     
297     eqItem = nil;
298     [eqMenu release];
299     eqMenu = nil;
300     
301     for (i = 0; i < [myMenu count]; i++) {
302         NSString *item = [myMenu objectAtIndex:i];
303         if ([item isEqualToString:@"Play/Pause"]) {
304             KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
305             playPauseItem = [menu addItemWithTitle:@"Play"
306                                     action:@selector(playPause:)
307                                     keyEquivalent:@""];
308             
309             if (tempCombo) {
310                 [self setKeyEquivalentForCode:[tempCombo keyCode]
311                     andModifiers:[tempCombo modifiers] onItem:playPauseItem];
312                 [tempCombo release];
313             }
314         } else if ([item isEqualToString:@"Next Track"]) {
315             KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
316             NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
317                                         action:@selector(nextSong:)
318                                         keyEquivalent:@""];
319             
320             if (tempCombo) {
321                 [self setKeyEquivalentForCode:[tempCombo keyCode]
322                     andModifiers:[tempCombo modifiers] onItem:nextTrack];
323                 [tempCombo release];
324             }
325         } else if ([item isEqualToString:@"Previous Track"]) {
326             KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
327             NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
328                                         action:@selector(prevSong:)
329                                         keyEquivalent:@""];
330             
331             if (tempCombo) {
332                 [self setKeyEquivalentForCode:[tempCombo keyCode]
333                     andModifiers:[tempCombo modifiers] onItem:prevTrack];
334                 [tempCombo release];
335             }
336         } else if ([item isEqualToString:@"Fast Forward"]) {
337             [menu addItemWithTitle:@"Fast Forward"
338                     action:@selector(fastForward:)
339                     keyEquivalent:@""];
340         } else if ([item isEqualToString:@"Rewind"]) {
341             [menu addItemWithTitle:@"Rewind"
342                     action:@selector(rewind:)
343                     keyEquivalent:@""];
344         } else if ([item isEqualToString:@"Upcoming Songs"]) {
345             upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
346                     action:nil
347                     keyEquivalent:@""];
348         } else if ([item isEqualToString:@"Playlists"]) {
349             playlistItem = [menu addItemWithTitle:@"Playlists"
350                     action:nil
351                     keyEquivalent:@""];
352         } else if ([item isEqualToString:@"EQ Presets"]) {
353             eqItem = [menu addItemWithTitle:@"EQ Presets"
354                     action:nil
355                     keyEquivalent:@""];
356         } else if ([item isEqualToString:@"PreferencesÉ"]) {
357             [menu addItemWithTitle:@"PreferencesÉ"
358                     action:@selector(showPreferences:)
359                     keyEquivalent:@""];
360         } else if ([item isEqualToString:@"Quit"]) {
361             [menu addItemWithTitle:@"Quit"
362                     action:@selector(quitMenuTunes:)
363                     keyEquivalent:@""];
364         } else if ([item isEqualToString:@"Current Track Info"]) {
365             trackInfoIndex = [menu numberOfItems];
366             [menu addItemWithTitle:@"No Song"
367                     action:nil
368                     keyEquivalent:@""];
369         } else if ([item isEqualToString:@"Song Rating"]) {
370             unichar fullstar = 0x2605;
371             unichar emptystar = 0x2606;
372             NSString *fullStarChar = [NSString stringWithCharacters:&fullstar length:1];
373             NSString *emptyStarChar = [NSString stringWithCharacters:&emptystar length:1];
374             NSMenuItem *item;
375             
376             ratingItem = [menu addItemWithTitle:@"Song Rating"
377                     action:nil
378                     keyEquivalent:@""];
379             
380             ratingMenu = [[NSMenu alloc] initWithTitle:@""];
381             
382             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
383             [item setTag:0];
384             
385             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
386             [item setTag:20];
387             
388             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
389             [item setTag:40];
390             
391             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
392             [item setTag:60];
393             
394             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
395             [item setTag:80];
396             
397             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
398             [item setTag:100];
399         } else if ([item isEqualToString:@"<separator>"]) {
400             [menu addItem:[NSMenuItem separatorItem]];
401         }
402     }
403     
404     [statusItem setMenu:menu];
405     
406     [self updateMenu];
407     
408     [self clearHotKeys];
409     [self setupHotKeys];
410 }
411
412 //Rebuild the upcoming songs submenu. Can be improved a lot.
413 - (void)rebuildUpcomingSongsMenu
414 {
415     int curIndex = [currentRemote currentPlaylistIndex];
416     int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
417     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
418     
419     if (!isPlayingRadio) {
420         if (numSongs > 0) {
421             int curTrack = [currentRemote currentSongIndex];
422             int i;
423             
424             [upcomingSongsMenu release];
425             upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
426             [upcomingSongsItem setSubmenu:upcomingSongsMenu];
427             [upcomingSongsItem setEnabled:YES];
428             
429             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
430                 if (i <= numSongs) {
431                     NSString *curSong = [currentRemote songTitleAtIndex:i];
432                     NSMenuItem *songItem;
433                     songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(selectSong:) keyEquivalent:@""];
434                     [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
435                     [upcomingSongsMenu addItem:songItem];
436                     [songItem release];
437                 } else {
438                     break;
439                 }
440             }
441         }
442     } else {
443         [upcomingSongsItem setSubmenu:nil];
444         [upcomingSongsItem setEnabled:NO];
445     }
446 }
447
448 - (void)rebuildPlaylistMenu
449 {
450     NSArray *playlists = [currentRemote playlists];
451     int i, curPlaylist = [currentRemote currentPlaylistIndex];
452     
453     if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
454         return;
455     
456     [playlistMenu release];
457     playlistMenu = [[NSMenu alloc] initWithTitle:@""];
458     
459     for (i = 0; i < [playlists count]; i++) {
460         NSString *playlistName = [playlists objectAtIndex:i];
461         NSMenuItem *tempItem;
462         tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
463         [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
464         [playlistMenu addItem:tempItem];
465         [tempItem release];
466     }
467     [playlistItem setSubmenu:playlistMenu];
468     [playlistItem setEnabled:YES];
469     
470     if (!isPlayingRadio) {
471         [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
472     }
473 }
474
475 //Build a menu with the list of all available EQ presets
476 - (void)rebuildEQPresetsMenu
477 {
478     NSArray *eqPresets = [currentRemote eqPresets];
479     NSMenuItem *enabledItem;
480     int i;
481     
482     if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
483         return;
484     
485     [eqMenu release];
486     eqMenu = [[NSMenu alloc] initWithTitle:@""];
487     
488     enabledItem = [eqMenu addItemWithTitle:@"Enabled"
489                           action:@selector(selectEQPreset:)
490                           keyEquivalent:@""];
491     [enabledItem setTag:-1];
492     
493     if ([currentRemote equalizerEnabled]) {
494         [enabledItem setState:NSOnState];
495     }
496     
497     [eqMenu addItem:[NSMenuItem separatorItem]];
498     
499     for (i = 0; i < [eqPresets count]; i++) {
500         NSString *setName = [eqPresets objectAtIndex:i];
501         NSMenuItem *tempItem;
502         if (setName) {
503         tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
504         [tempItem setTag:i];
505         [eqMenu addItem:tempItem];
506         [tempItem release];
507         }
508     }
509     [eqItem setSubmenu:eqMenu];
510     
511     [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
512 }
513
514 - (void)timerUpdate
515 {
516     NSString *currentIdentifier = [currentRemote currentSongUniqueIdentifier];
517     if (![lastSongIdentifier isEqualToString:currentIdentifier]) {
518         [self updateMenu];
519     } else if (!isPlayingRadio && ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist)) {
520         [self rebuildMenu];
521     }
522     
523     //Update Play/Pause menu item
524     if (playPauseItem){
525         if ([currentRemote playerPlayingState] == ITMTRemotePlayerPlaying) {
526             [playPauseItem setTitle:@"Pause"];
527         } else {
528             [playPauseItem setTitle:@"Play"];
529         }
530     }
531 }
532
533 - (void)updateMenu
534 {
535     NSUserDefaults *defaults;
536     int playlist = [currentRemote currentPlaylistIndex];
537     BOOL wasPlayingRadio = isPlayingRadio;
538     
539     if ( (isAppRunning == ITMTRemotePlayerNotRunning) ) {
540         return;
541     }
542     
543     defaults = [NSUserDefaults standardUserDefaults];
544     
545     isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
546     
547     //
548     //
549     //rebuild submenus, make them properly enabled for once...
550     //
551     //
552     
553     if ( (trackInfoIndex > -1) && (playlist) ) {
554         NSString *title, *album, *artist;
555         int temp;
556         
557         lastSongIdentifier = [[currentRemote currentSongUniqueIdentifier] retain];
558         
559         if ( (temp = [menu indexOfItemWithTitle:@"No Song"]) && (temp > -1) ) {
560             [menu removeItemAtIndex:temp];
561             [menu insertItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@"" atIndex:temp];
562         } else {
563             if ([defaults boolForKey:@"showName"]) {
564                 [menu removeItemAtIndex:trackInfoIndex + 1];
565             }
566         }
567         
568         title = [currentRemote currentSongTitle];
569         
570         if (!wasPlayingRadio && (temp == -1)) {
571             if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
572                 [menu removeItemAtIndex:trackInfoIndex + 1];
573             }
574             if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
575                 [menu removeItemAtIndex:trackInfoIndex + 1];
576             }
577             if ([defaults boolForKey:@"showTime"]) {
578                 [menu removeItemAtIndex:trackInfoIndex + 1];
579             }
580         }
581         
582         if (!isPlayingRadio) {
583             ([defaults boolForKey:@"showAlbum"]) ? (album = [currentRemote currentSongAlbum]) :
584                                                    (album = @"");
585             ([defaults boolForKey:@"showArtist"]) ? (artist = [currentRemote currentSongArtist]) :
586                                                     (artist = @"");
587             if ([defaults boolForKey:@"showTime"]) {
588                 [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", [currentRemote currentSongLength]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
589             }
590             
591             if ([artist length] > 0) {
592                 [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", artist] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
593             }
594             
595             if ([album length] > 0) {
596                 [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", album] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
597             }
598             
599             if ([defaults boolForKey:@"showArtist"]) {
600                 didHaveArtistName = (([artist length] > 0) ? YES : NO);
601             }
602             
603             if ([defaults boolForKey:@"showAlbum"]) {
604                 didHaveAlbumName = (([album length] > 0) ? YES : NO);
605             }
606         }
607         
608         if ([title length] > 0) {
609             [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", title] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
610         }
611     }
612 }
613
614
615 //
616 //
617 // Menu Selectors
618 //
619 //
620
621 - (void)selectSong:(id)sender
622 {
623     [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
624 }
625
626 - (void)selectPlaylist:(id)sender
627 {
628     int playlist = [[sender representedObject] intValue];
629     if (!isPlayingRadio) {
630         int curPlaylist = [currentRemote currentPlaylistIndex];
631         if (curPlaylist > 0) {
632             [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
633         }
634     }
635     [currentRemote switchToPlaylistAtIndex:playlist];
636     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
637     [self updateMenu];
638 }
639
640 - (void)selectEQPreset:(id)sender
641 {
642     int curSet = [currentRemote currentEQPresetIndex];
643     int item = [sender tag];
644     
645     if (item == -1) {
646         [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
647     } else {
648         [currentRemote setEqualizerEnabled:YES];
649         [currentRemote switchToEQAtIndex:item];
650         [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState];
651         [[eqMenu itemAtIndex:item + 2] setState:NSOnState];
652     }
653 }
654
655 - (void)selectSongRating:(id)sender
656 {
657     [[ratingMenu itemAtIndex:([currentRemote currentSongRating] / 20)] setState:NSOffState];
658     [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
659     [sender setState:NSOnState];
660 }
661
662 - (void)playPause:(id)sender
663 {
664     ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
665     
666     if (state == ITMTRemotePlayerPlaying) {
667         [currentRemote pause];
668         [playPauseItem setTitle:@"Play"];
669     } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
670         [currentRemote pause];
671         [currentRemote play];
672     } else {
673         [currentRemote play];
674         [playPauseItem setTitle:@"Pause"];
675     }
676 }
677
678 - (void)nextSong:(id)sender
679 {
680     [currentRemote goToNextSong];
681 }
682
683 - (void)prevSong:(id)sender
684 {
685     [currentRemote goToPreviousSong];
686 }
687
688 - (void)fastForward:(id)sender
689 {
690     [currentRemote forward];
691     [playPauseItem setTitle:@"Play"];
692 }
693
694 - (void)rewind:(id)sender
695 {
696     [currentRemote rewind];
697     [playPauseItem setTitle:@"Play"];
698 }
699
700 //
701 //
702 - (void)quitMenuTunes:(id)sender
703 {
704     [NSApp terminate:self];
705 }
706
707 - (void)showPlayer:(id)sender
708 {
709     if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
710         [currentRemote showPrimaryInterface];
711     } else {
712         if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
713             NSLog(@"Error Launching Player");
714         }
715     }
716 }
717
718 - (void)showPreferences:(id)sender
719 {
720     if (!prefsController) {
721         prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
722         [self clearHotKeys];
723     }
724 }
725
726 - (void)closePreferences
727 {
728     if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
729         [self setupHotKeys];
730     }
731     [prefsController release];
732     prefsController = nil;
733 }
734
735
736 //
737 //
738 // Hot key setup
739 //
740 //
741
742 - (void)clearHotKeys
743 {
744     [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
745     [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
746     [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
747     [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
748     [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
749 }
750
751 - (void)setupHotKeys
752 {
753     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
754     
755     if ([defaults objectForKey:@"PlayPause"] != nil) {
756         [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
757                 combo:[defaults keyComboForKey:@"PlayPause"]
758                 target:self action:@selector(playPause:)];
759     }
760     
761     if ([defaults objectForKey:@"NextTrack"] != nil) {
762         [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
763                 combo:[defaults keyComboForKey:@"NextTrack"]
764                 target:self action:@selector(nextSong:)];
765     }
766     
767     if ([defaults objectForKey:@"PrevTrack"] != nil) {
768         [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
769                 combo:[defaults keyComboForKey:@"PrevTrack"]
770                 target:self action:@selector(prevSong:)];
771     }
772     
773     if ([defaults objectForKey:@"TrackInfo"] != nil) {
774         [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
775                 combo:[defaults keyComboForKey:@"TrackInfo"]
776                 target:self action:@selector(showCurrentTrackInfo)];
777     }
778     
779     if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
780         [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
781                combo:[defaults keyComboForKey:@"UpcomingSongs"]
782                target:self action:@selector(showUpcomingSongs)];
783     }
784 }
785
786 //
787 //
788 // Show Current Track Info And Show Upcoming Songs Floaters
789 //
790 //
791
792 - (void)showCurrentTrackInfo
793 {
794     NSString *trackName = [currentRemote currentSongTitle];
795     if (!statusWindow && [trackName length]) {
796         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
797         NSString *stringToShow = @"";
798         
799         if ([defaults boolForKey:@"showName"]) {
800             if ([defaults boolForKey:@"showArtist"]) {
801                 NSString *trackArtist = [currentRemote currentSongArtist];
802                 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
803             }
804             stringToShow = [stringToShow stringByAppendingString:trackName];
805             stringToShow = [stringToShow stringByAppendingString:@"\n"];
806         }
807         
808         if ([defaults boolForKey:@"showAlbum"]) {
809             NSString *trackAlbum = [currentRemote currentSongAlbum];
810             if ([trackAlbum length]) {
811                 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
812                 stringToShow = [stringToShow stringByAppendingString:@"\n"];
813             }
814         }
815         
816         if ([defaults boolForKey:@"showTime"]) {
817             NSString *trackTime = [currentRemote currentSongLength];
818             if ([trackTime length]) {
819                 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
820             }
821         }
822         
823         {
824             int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
825             int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
826             if (seconds < 10) {
827                 stringToShow = [stringToShow stringByAppendingString:
828                             [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
829             } else {
830                 stringToShow = [stringToShow stringByAppendingString:
831                             [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
832             }
833         }
834         
835         [statusWindow setText:stringToShow];
836         [NSTimer scheduledTimerWithTimeInterval:3.0
837                     target:self
838                     selector:@selector(fadeAndCloseStatusWindow)
839                     userInfo:nil
840                     repeats:NO];
841     }
842 }
843
844 - (void)showUpcomingSongs
845 {
846     int curPlaylist = [currentRemote currentPlaylistIndex];
847     if (!statusWindow) {
848         int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
849         
850         if (numSongs > 0) {
851             int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
852             int curTrack = [currentRemote currentSongIndex];
853             int i;
854             NSString *songs = @"";
855             
856             statusWindow = [ITTransientStatusWindow sharedWindow];
857             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
858                 if (i <= numSongs) {
859                     NSString *curSong = [currentRemote songTitleAtIndex:i];
860                     songs = [songs stringByAppendingString:curSong];
861                     songs = [songs stringByAppendingString:@"\n"];
862                 }
863             }
864             [statusWindow setText:songs];
865             [NSTimer scheduledTimerWithTimeInterval:3.0
866                         target:self
867                         selector:@selector(fadeAndCloseStatusWindow)
868                         userInfo:nil
869                         repeats:NO];
870         }
871     }
872 }
873
874 - (void)fadeAndCloseStatusWindow
875 {
876     [statusWindow orderOut:self];
877 }
878
879 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
880         onItem:(NSMenuItem *)item
881 {
882     unichar charcode = 'a';
883     int i;
884     long cocoaModifiers = 0;
885     static long carbonToCocoa[6][2] = 
886     {
887         { cmdKey, NSCommandKeyMask },
888         { optionKey, NSAlternateKeyMask },
889         { controlKey, NSControlKeyMask },
890         { shiftKey, NSShiftKeyMask },
891     };
892     
893     for (i = 0; i < 6; i++) {
894         if (modifiers & carbonToCocoa[i][0]) {
895             cocoaModifiers += carbonToCocoa[i][1];
896         }
897     }
898     [item setKeyEquivalentModifierMask:cocoaModifiers];
899     
900     //Missing key combos for some keys. Must find them later.
901     switch (code)
902     {
903         case 36:
904             charcode = '\r';
905         break;
906         
907         case 48:
908             charcode = '\t';
909         break;
910         
911         //Space -- ARGH!
912         case 49:
913         {
914             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
915             NSLog(@"%@", menuRef);
916             SetMenuItemCommandKey(menuRef, 0, NO, 49);
917             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
918             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
919             charcode = 'b';*/
920         }
921         break;
922         
923         case 51:
924             charcode = NSDeleteFunctionKey;
925         break;
926         
927         case 53:
928             charcode = '\e';
929         break;
930         
931         case 71:
932             charcode = '\e';
933         break;
934         
935         case 76:
936             charcode = '\r';
937         break;
938         
939         case 96:
940             charcode = NSF5FunctionKey;
941         break;
942         
943         case 97:
944             charcode = NSF6FunctionKey;
945         break;
946         
947         case 98:
948             charcode = NSF7FunctionKey;
949         break;
950         
951         case 99:
952             charcode = NSF3FunctionKey;
953         break;
954         
955         case 100:
956             charcode = NSF8FunctionKey;
957         break;
958         
959         case 101:
960             charcode = NSF9FunctionKey;
961         break;
962         
963         case 103:
964             charcode = NSF11FunctionKey;
965         break;
966         
967         case 105:
968             charcode = NSF3FunctionKey;
969         break;
970         
971         case 107:
972             charcode = NSF14FunctionKey;
973         break;
974         
975         case 109:
976             charcode = NSF10FunctionKey;
977         break;
978         
979         case 111:
980             charcode = NSF12FunctionKey;
981         break;
982         
983         case 113:
984             charcode = NSF13FunctionKey;
985         break;
986         
987         case 114:
988             charcode = NSInsertFunctionKey;
989         break;
990         
991         case 115:
992             charcode = NSHomeFunctionKey;
993         break;
994         
995         case 116:
996             charcode = NSPageUpFunctionKey;
997         break;
998         
999         case 117:
1000             charcode = NSDeleteFunctionKey;
1001         break;
1002         
1003         case 118:
1004             charcode = NSF4FunctionKey;
1005         break;
1006         
1007         case 119:
1008             charcode = NSEndFunctionKey;
1009         break;
1010         
1011         case 120:
1012             charcode = NSF2FunctionKey;
1013         break;
1014         
1015         case 121:
1016             charcode = NSPageDownFunctionKey;
1017         break;
1018         
1019         case 122:
1020             charcode = NSF1FunctionKey;
1021         break;
1022         
1023         case 123:
1024             charcode = NSLeftArrowFunctionKey;
1025         break;
1026         
1027         case 124:
1028             charcode = NSRightArrowFunctionKey;
1029         break;
1030         
1031         case 125:
1032             charcode = NSDownArrowFunctionKey;
1033         break;
1034         
1035         case 126:
1036             charcode = NSUpArrowFunctionKey;
1037         break;
1038     }
1039     
1040     if (charcode == 'a') {
1041         unsigned long state;
1042         long keyTrans;
1043         char charCode;
1044         Ptr kchr;
1045         state = 0;
1046         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1047         keyTrans = KeyTranslate(kchr, code, &state);
1048         charCode = keyTrans;
1049         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1050     } else if (charcode != 'b') {
1051         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1052     }
1053 }
1054
1055 @end