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