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