Fixed the hotkeys so they don't run even when they shouldn't (player no running,...
[MenuTunes.git] / MenuController.m
1 //
2 //  MenuController.m
3 //  MenuTunes
4 //
5 //  Created by Joseph Spiros on Wed Apr 30 2003.
6 //  Copyright (c) 2003 iThink Software. All rights reserved.
7 //
8
9 #import "MenuController.h"
10 #import "MainController.h"
11 #import <ITFoundation/ITDebug.h>
12 #import <ITKit/ITHotKeyCenter.h>
13 #import <ITKit/ITHotKey.h>
14 #import <ITKit/ITKeyCombo.h>
15 #import <ITKit/ITCategory-NSMenu.h>
16
17 @interface MenuController (SubmenuMethods)
18 - (NSMenu *)ratingMenu;
19 - (NSMenu *)upcomingSongsMenu;
20 - (NSMenu *)playlistsMenu;
21 - (NSMenu *)eqMenu;
22 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
23         onItem:(NSMenuItem *)item;
24 @end
25
26 @implementation MenuController
27
28 - (id)init
29 {
30     if ( (self = [super init]) ) {
31         _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
32     }
33     return self;
34 }
35
36 - (NSMenu *)menu
37 {
38     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
39     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
40     NSArray *menuArray = [defaults arrayForKey:@"menu"];
41     NSEnumerator *enumerator = [menuArray objectEnumerator];
42     NSString *nextObject;
43     NSMenuItem *tempItem;
44     NSEnumerator *itemEnum;
45     ITHotKey *hotKey;
46     NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
47     
48     //Get the information
49     _currentPlaylist = [currentRemote currentPlaylistIndex];
50     _playingRadio = ([currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
51     
52     ITDebugLog(@"Reset menu if required.");
53     
54     //Kill the old submenu items
55     if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
56         ITDebugLog(@"Removing \"Song Rating\" submenu.");
57         [tempItem setSubmenu:nil];
58     }
59     
60     if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
61         ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
62         [tempItem setSubmenu:nil];
63     }
64     
65     if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
66         ITDebugLog(@"Removing \"Playlists\" submenu.");
67         [tempItem setSubmenu:nil];
68     }
69     
70     if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
71         ITDebugLog(@"Removing \"EQ Presets\" submenu.");
72         [tempItem setSubmenu:nil];
73     }
74     
75     ITDebugLog(@"Begin building menu.");
76     
77     //create our menu
78     while ( (nextObject = [enumerator nextObject]) ) {
79         //Main menu items
80         if ([nextObject isEqualToString:@"playPause"]) {
81             ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
82             tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
83                     action:@selector(performMainMenuAction:)
84                     keyEquivalent:@""];
85             [tempItem setTag:MTMenuPlayPauseItem];
86             [tempItem setTarget:self];
87             
88             itemEnum = [hotKeys objectEnumerator];
89             while ( (hotKey = [itemEnum nextObject]) ) {
90                 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
91                     ITKeyCombo *combo = [hotKey keyCombo];
92                     [self setKeyEquivalentForCode:[combo keyCode]
93                           andModifiers:[combo modifiers]
94                           onItem:tempItem];
95                 }
96             }
97             
98             ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
99             switch ([currentRemote playerPlayingState]) {
100                 case ITMTRemotePlayerPlaying:
101                     [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
102                 break;
103                 case ITMTRemotePlayerRewinding:
104                 case ITMTRemotePlayerForwarding:
105                     [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
106                 break;
107                 default:
108                 break;
109             }
110         } else if ([nextObject isEqualToString:@"nextTrack"]) {
111             ITDebugLog(@"Add \"Next Track\" menu item.");
112             tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
113                     action:@selector(performMainMenuAction:)
114                     keyEquivalent:@""];
115             
116             itemEnum = [hotKeys objectEnumerator];
117             while ( (hotKey = [itemEnum nextObject]) ) {
118                 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
119                     ITKeyCombo *combo = [hotKey keyCombo];
120                     [self setKeyEquivalentForCode:[combo keyCode]
121                           andModifiers:[combo modifiers]
122                           onItem:tempItem];
123                 }
124             }
125             
126             if (_currentPlaylist) {
127                 [tempItem setTag:MTMenuNextTrackItem];
128                 [tempItem setTarget:self];
129             }
130         } else if ([nextObject isEqualToString:@"prevTrack"]) {
131             ITDebugLog(@"Add \"Previous Track\" menu item.");
132             tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
133                     action:@selector(performMainMenuAction:)
134                     keyEquivalent:@""];
135             
136             itemEnum = [hotKeys objectEnumerator];
137             while ( (hotKey = [itemEnum nextObject]) ) {
138                 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
139                     ITKeyCombo *combo = [hotKey keyCombo];
140                     [self setKeyEquivalentForCode:[combo keyCode]
141                           andModifiers:[combo modifiers]
142                           onItem:tempItem];
143                 }
144             }
145             
146             if (_currentPlaylist) {
147                 [tempItem setTag:MTMenuPreviousTrackItem];
148                 [tempItem setTarget:self];
149             }
150         } else if ([nextObject isEqualToString:@"fastForward"]) {
151             ITDebugLog(@"Add \"Fast Forward\" menu item.");
152             tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
153                     action:@selector(performMainMenuAction:)
154                     keyEquivalent:@""];
155             if (_currentPlaylist) {
156                 [tempItem setTag:MTMenuFastForwardItem];
157                 [tempItem setTarget:self];
158             }
159         } else if ([nextObject isEqualToString:@"rewind"]) {
160             ITDebugLog(@"Add \"Rewind\" menu item.");
161             tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
162                     action:@selector(performMainMenuAction:)
163                     keyEquivalent:@""];
164             if (_currentPlaylist) {
165                 [tempItem setTag:MTMenuRewindItem];
166                 [tempItem setTarget:self];
167             }
168         } else if ([nextObject isEqualToString:@"showPlayer"]) {
169             ITDebugLog(@"Add \"Show Player\" menu item.");
170             tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
171                             NSLocalizedString(@"show", @"Show"),
172                             [[[MainController sharedController] currentRemote] playerSimpleName]]
173                     action:@selector(performMainMenuAction:)
174                     keyEquivalent:@""];
175             
176             itemEnum = [hotKeys objectEnumerator];
177             while ( (hotKey = [itemEnum nextObject]) ) {
178                 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
179                     ITKeyCombo *combo = [hotKey keyCombo];
180                     [self setKeyEquivalentForCode:[combo keyCode]
181                           andModifiers:[combo modifiers]
182                           onItem:tempItem];
183                 }
184             }
185             
186             [tempItem setTarget:self];
187             [tempItem setTag:MTMenuShowPlayerItem];
188         } else if ([nextObject isEqualToString:@"preferences"]) {
189             ITDebugLog(@"Add \"Preferences...\" menu item.");
190             tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
191                     action:@selector(performMainMenuAction:)
192                     keyEquivalent:@""];
193             [tempItem setTag:MTMenuPreferencesItem];
194             [tempItem setTarget:self];
195         } else if ([nextObject isEqualToString:@"quit"]) {
196             ITDebugLog(@"Add \"Quit\" menu item.");
197             if ([[MainController sharedController] blingBling] == NO) {
198                 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
199                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
200                 [tempItem setTag:MTMenuRegisterItem];
201                 [tempItem setTarget:self];
202             }
203             tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
204                     action:@selector(performMainMenuAction:)
205                     keyEquivalent:@""];
206             [tempItem setTag:MTMenuQuitItem];
207             [tempItem setTarget:self];
208         } else if ([nextObject isEqualToString:@"trackInfo"]) {
209             ITDebugLog(@"Check to see if a Track is playing...");
210             //Handle playing radio too
211             if (_currentPlaylist) {
212                 NSString *title = [currentRemote currentSongTitle];
213                 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
214                 ITDebugLog(@"Add \"Now Playing\" menu item.");
215                 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
216                 
217                 if ([title length] > 0) {
218                     ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
219                     [menu indentItem:
220                         [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
221                 }
222                 
223                 if (!_playingRadio) {
224                     if ([defaults boolForKey:@"showAlbum"]) {
225                         NSString *curAlbum = [currentRemote currentSongAlbum];
226                         ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
227                         if ( curAlbum ) {
228                             [menu indentItem:
229                                 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
230                         }
231                     }
232                     
233                     if ([defaults boolForKey:@"showArtist"]) {
234                         NSString *curArtist = [currentRemote currentSongArtist];
235                         ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
236                         if ( curArtist ) {
237                             [menu indentItem:
238                                 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
239                         }
240                     }
241                     
242                     if ([defaults boolForKey:@"showTrackNumber"]) {
243                         int track = [currentRemote currentSongTrack];
244                         ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
245                         if ( track > 0 ) {
246                             [menu indentItem:
247                                 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
248                         }
249                     }
250                     
251                     if ([defaults boolForKey:@"showTime"] && ( ([currentRemote currentSongElapsed] != nil) || ([currentRemote currentSongLength] != nil) )) {
252                         ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [currentRemote currentSongElapsed], [currentRemote currentSongLength]);
253                         [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [currentRemote currentSongElapsed], [currentRemote currentSongLength]] action:nil keyEquivalent:@""]];
254                     }
255                     
256                     if ([defaults boolForKey:@"showTrackRating"] && ( [currentRemote currentSongRating] != -1.0 )) {
257                         NSString *string = nil;
258                         switch ((int)([currentRemote currentSongRating] * 5)) {
259                             case 0:
260                                 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
261                             break;
262                             case 1:
263                                 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
264                             break;
265                             case 2:
266                                 string = [NSString stringWithUTF8String:"★★☆☆☆"];
267                             break;
268                             case 3:
269                                 string = [NSString stringWithUTF8String:"★★★☆☆"];
270                             break;
271                             case 4:
272                                 string = [NSString stringWithUTF8String:"★★★★☆"];
273                             break;
274                             case 5:
275                                 string = [NSString stringWithUTF8String:"★★★★★"];
276                             break;
277                         }
278                         ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
279                         [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
280                     }
281                 }
282             } else {
283                 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
284                 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
285             }
286         } else if ([nextObject isEqualToString:@"separator"]) {
287             ITDebugLog(@"Add a separator menu item.");
288             [menu addItem:[NSMenuItem separatorItem]];
289         //Submenu items
290         } else if ([nextObject isEqualToString:@"playlists"]) {
291             ITDebugLog(@"Add \"Playlists\" submenu.");
292             tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
293                     action:nil
294                     keyEquivalent:@""];
295             [tempItem setSubmenu:_playlistsMenu];
296             [tempItem setTag:3];
297         } else if ([nextObject isEqualToString:@"eqPresets"]) {
298             ITDebugLog(@"Add \"EQ Presets\" submenu.");
299             tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
300                     action:nil
301                     keyEquivalent:@""];
302             [tempItem setSubmenu:_eqMenu];
303             [tempItem setTag:4];
304             
305             itemEnum = [[_eqMenu itemArray] objectEnumerator];
306             while ( (tempItem = [itemEnum nextObject]) ) {
307                 [tempItem setState:NSOffState];
308             }
309             [[_eqMenu itemAtIndex:([currentRemote currentEQPresetIndex] - 1)] setState:NSOnState];
310         } else if ([nextObject isEqualToString:@"songRating"] && ( [currentRemote currentSongRating] != -1 )) {
311                 ITDebugLog(@"Add \"Song Rating\" submenu.");
312                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
313                         action:nil
314                         keyEquivalent:@""];
315                 [tempItem setSubmenu:_ratingMenu];
316                 [tempItem setTag:1];
317                 if (_playingRadio || !_currentPlaylist) {
318                     [tempItem setEnabled:NO];
319                 }
320                 
321                 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
322                 while ( (tempItem = [itemEnum nextObject]) ) {
323                     [tempItem setState:NSOffState];
324                 }
325                 
326                 [[_ratingMenu itemAtIndex:([currentRemote currentSongRating] * 5)] setState:NSOnState];
327             } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
328                 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
329                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
330                         action:nil
331                         keyEquivalent:@""];
332                 [tempItem setSubmenu:_upcomingSongsMenu];
333                 [tempItem setTag:2];
334                 if (_playingRadio || !_currentPlaylist) {
335                     [tempItem setEnabled:NO];
336                 }
337             }
338         }
339     ITDebugLog(@"Finished building menu.");
340     [_currentMenu release];
341     _currentMenu = menu;
342     return _currentMenu;
343 }
344
345 - (NSMenu *)menuForNoPlayer
346 {
347     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
348     NSMenuItem *tempItem;
349     ITDebugLog(@"Creating menu for when player isn't running.");
350     ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
351     tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
352     [tempItem setTag:MTMenuShowPlayerItem];
353     [tempItem setTarget:self];
354     ITDebugLog(@"Add a separator menu item.");
355     [menu addItem:[NSMenuItem separatorItem]];
356     ITDebugLog(@"Add \"Preferences...\" menu item.");
357     tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
358     [tempItem setTag:MTMenuPreferencesItem];
359     [tempItem setTarget:self];
360     if ([[MainController sharedController] blingBling] == NO) {
361         ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
362         tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
363         [tempItem setTag:MTMenuRegisterItem];
364         [tempItem setTarget:self];
365     }
366     ITDebugLog(@"Add \"Quit\" menu item.");
367     tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
368     [tempItem setTag:MTMenuQuitItem];
369     [tempItem setTarget:self];
370     return [menu autorelease];
371 }
372
373 - (void)rebuildSubmenus
374 {
375     ITDebugLog(@"Rebuilding all of the submenus.");
376     
377     currentRemote = [[MainController sharedController] currentRemote];
378     _currentPlaylist = [currentRemote currentPlaylistIndex];
379     _currentTrack = [currentRemote currentSongIndex];
380     _playingRadio = ([currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
381     
382     [_ratingMenu release];
383     [_upcomingSongsMenu release];
384     [_playlistsMenu release];
385     [_eqMenu release];
386     ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
387     _ratingMenu = [self ratingMenu];
388     ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
389     _upcomingSongsMenu = [self upcomingSongsMenu];
390     ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
391     _playlistsMenu = [self playlistsMenu];
392     ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
393     _eqMenu = [self eqMenu];
394     ITDebugLog(@"Done rebuilding all of the submenus.");
395 }
396
397 - (NSMenu *)ratingMenu
398 {
399     NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
400     NSEnumerator *itemEnum;
401     id  anItem;
402     int itemTag = 0;
403     SEL itemSelector = @selector(performRatingMenuAction:);
404     
405     ITDebugLog(@"Building \"Song Rating\" menu.");
406     
407     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
408     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
409     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
410     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
411     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
412     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
413     
414     itemEnum = [[ratingMenu itemArray] objectEnumerator];
415     while ( (anItem = [itemEnum nextObject]) ) {
416         ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
417         [anItem setAction:itemSelector];
418         [anItem setTarget:self];
419         [anItem setTag:itemTag];
420         itemTag += 20;
421     }
422     ITDebugLog(@"Done Building \"Song Rating\" menu.");
423     return ratingMenu;
424 }
425
426 - (NSMenu *)upcomingSongsMenu
427 {
428     NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
429     int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:_currentPlaylist];
430     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
431     
432     ITDebugLog(@"Building \"Upcoming Songs\" menu.");
433     
434     if (_currentPlaylist && !_playingRadio) {
435         if (numSongs > 0) {
436             int i;
437
438             for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
439                 if (i <= numSongs) {
440                     NSString *curSong = [currentRemote songTitleAtIndex:i];
441                     NSMenuItem *songItem;
442                     ITDebugLog(@"Adding song: %@", curSong);
443                     songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
444                     [songItem setTag:i];
445                     [songItem setTarget:self];
446                 } else {
447                     break;
448                 }
449             }
450         }
451     }
452     ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
453     return upcomingSongsMenu;
454 }
455
456 - (NSMenu *)playlistsMenu
457 {
458     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
459     NSArray *playlists = [currentRemote playlists];
460     NSMenuItem *tempItem;
461     int i;
462     
463     ITDebugLog(@"Building \"Playlists\" menu.");
464     
465     for (i = 0; i < [playlists count]; i++) {
466         NSString *curPlaylist = [playlists objectAtIndex:i];
467         ITDebugLog(@"Adding playlist: %@", curPlaylist);
468         tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
469         [tempItem setTag:i + 1];
470         [tempItem setTarget:self];
471     }
472     
473     if (!_playingRadio && _currentPlaylist) {
474         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
475     }
476     ITDebugLog(@"Done Building \"Playlists\" menu");
477     return playlistsMenu;
478 }
479
480 - (NSMenu *)eqMenu
481 {
482     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
483     NSArray *eqPresets = [currentRemote eqPresets];
484     NSMenuItem *tempItem;
485     int i;
486     
487     ITDebugLog(@"Building \"EQ Presets\" menu.");
488     
489     for (i = 0; i < [eqPresets count]; i++) {
490         NSString *name;
491            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
492             ITDebugLog(@"Adding EQ Preset: %@", name);
493             tempItem = [eqMenu addItemWithTitle:name
494                     action:@selector(performEqualizerMenuAction:)
495                     keyEquivalent:@""];
496             [tempItem setTag:i];
497             [tempItem setTarget:self];
498            }
499     }
500     ITDebugLog(@"Done Building \"EQ Presets\" menu");
501     return eqMenu;
502 }
503
504 - (void)performMainMenuAction:(id)sender
505 {
506     switch ( [sender tag] )
507     {
508         case MTMenuPlayPauseItem:
509             ITDebugLog(@"Performing Menu Action: Play/Pause");
510             [[MainController sharedController] playPause];
511             break;
512         case MTMenuFastForwardItem:
513             ITDebugLog(@"Performing Menu Action: Fast Forward");
514             [[MainController sharedController] fastForward];
515             break;
516         case MTMenuRewindItem:
517             ITDebugLog(@"Performing Menu Action: Rewind");
518             [[MainController sharedController] rewind];
519             break;
520         case MTMenuPreviousTrackItem:
521             ITDebugLog(@"Performing Menu Action: Previous Track");
522             [[MainController sharedController] prevSong];
523             break;
524         case MTMenuNextTrackItem:
525             ITDebugLog(@"Performing Menu Action: Next Track");
526             [[MainController sharedController] nextSong];
527             break;
528         case MTMenuShowPlayerItem:
529             ITDebugLog(@"Performing Menu Action: Show Main Interface");
530             [[MainController sharedController] showPlayer];
531             break;
532         case MTMenuPreferencesItem:
533             ITDebugLog(@"Performing Menu Action: Preferences...");
534             [[MainController sharedController] showPreferences];
535             break;
536         case MTMenuQuitItem:
537             ITDebugLog(@"Performing Menu Action: Quit");
538             [[MainController sharedController] quitMenuTunes];
539             break;
540         case MTMenuRegisterItem:
541             ITDebugLog(@"Performing Menu Action: Register");
542             [[MainController sharedController] blingNow];
543             break;
544         default:
545             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
546             break;
547     }
548 }
549
550 - (void)performRatingMenuAction:(id)sender
551 {
552     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
553     [[MainController sharedController] selectSongRating:[sender tag]];
554 }
555
556 - (void)performPlaylistMenuAction:(id)sender
557 {
558     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
559     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
560 }
561
562 - (void)performEqualizerMenuAction:(id)sender
563 {
564     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
565     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
566 }
567
568 - (void)performUpcomingSongsMenuAction:(id)sender
569 {
570     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
571     [[MainController sharedController] selectSongAtIndex:[sender tag]];
572 }
573
574 - (void)updateMenu
575 {
576     ITDebugLog(@"Update Menu");
577     [_currentMenu update];
578 }
579
580 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
581 {
582     return YES;
583 }
584
585 //This is never used I know, keep it though
586 - (NSString *)systemUIColor
587 {
588     NSDictionary *tmpDict;
589     NSNumber *tmpNumber;
590     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
591         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
592             if ( ([tmpNumber intValue] == 1) ) {
593                 return @"Aqua";
594             } else {
595                 return @"Graphite";
596             }
597         } else {
598             return @"Aqua";
599         }
600     } else {
601         return @"Aqua";
602     }
603 }
604
605 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
606         onItem:(NSMenuItem *)item
607 {
608     unichar charcode = 'a';
609     int i;
610     long cocoaModifiers = 0;
611     static long carbonToCocoa[6][2] = 
612     {
613         { cmdKey, NSCommandKeyMask },
614         { optionKey, NSAlternateKeyMask },
615         { controlKey, NSControlKeyMask },
616         { shiftKey, NSShiftKeyMask },
617     };
618     
619     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
620     
621     for (i = 0; i < 6; i++) {
622         if (modifiers & carbonToCocoa[i][0]) {
623             cocoaModifiers += carbonToCocoa[i][1];
624         }
625     }
626     [item setKeyEquivalentModifierMask:cocoaModifiers];
627     
628     //Missing key combos for some keys. Must find them later.
629     switch (code)
630     {
631         case 36:
632             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
633             charcode = '\r';
634         break;
635         
636         case 48:
637             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
638             charcode = '\t';
639         break;
640         
641         //Space -- ARGH!
642         case 49:
643         {
644             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
645             // Haven't tested this, though it should work.
646             // This doesn't work. :'(
647             //unichar buffer;
648             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
649             //charcode = buffer;
650             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
651             ITDebugLog(@"%@", menuRef);
652             SetMenuItemCommandKey(menuRef, 0, NO, 49);
653             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
654             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
655             charcode = 'b';*/
656             unichar buffer;
657             [[NSString stringWithString:@"_"] getCharacters:&buffer]; // this will have to do for now :(
658             charcode = buffer;
659         }
660         break;
661         
662         case 51:
663             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
664             charcode = NSDeleteFunctionKey;
665         break;
666         
667         case 53:
668             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
669             charcode = '\e';
670         break;
671         
672         case 71:
673             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
674             charcode = '\e';
675         break;
676         
677         case 76:
678             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
679             charcode = '\r';
680         break;
681         
682         case 96:
683             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
684             charcode = NSF5FunctionKey;
685         break;
686         
687         case 97:
688             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
689             charcode = NSF6FunctionKey;
690         break;
691         
692         case 98:
693             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
694             charcode = NSF7FunctionKey;
695         break;
696         
697         case 99:
698             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
699             charcode = NSF3FunctionKey;
700         break;
701         
702         case 100:
703             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
704             charcode = NSF8FunctionKey;
705         break;
706         
707         case 101:
708             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
709             charcode = NSF9FunctionKey;
710         break;
711         
712         case 103:
713             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
714             charcode = NSF11FunctionKey;
715         break;
716         
717         case 105:
718             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F3)", [item title]);
719             charcode = NSF3FunctionKey;
720         break;
721         
722         case 107:
723             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
724             charcode = NSF14FunctionKey;
725         break;
726         
727         case 109:
728             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
729             charcode = NSF10FunctionKey;
730         break;
731         
732         case 111:
733             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
734             charcode = NSF12FunctionKey;
735         break;
736         
737         case 113:
738             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
739             charcode = NSF13FunctionKey;
740         break;
741         
742         case 114:
743             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
744             charcode = NSInsertFunctionKey;
745         break;
746         
747         case 115:
748             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
749             charcode = NSHomeFunctionKey;
750         break;
751         
752         case 116:
753             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
754             charcode = NSPageUpFunctionKey;
755         break;
756         
757         case 117:
758             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
759             charcode = NSDeleteFunctionKey;
760         break;
761         
762         case 118:
763             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
764             charcode = NSF4FunctionKey;
765         break;
766         
767         case 119:
768             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
769             charcode = NSEndFunctionKey;
770         break;
771         
772         case 120:
773             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
774             charcode = NSF2FunctionKey;
775         break;
776         
777         case 121:
778             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
779             charcode = NSPageDownFunctionKey;
780         break;
781         
782         case 122:
783             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
784             charcode = NSF1FunctionKey;
785         break;
786         
787         case 123:
788             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
789             charcode = NSLeftArrowFunctionKey;
790         break;
791         
792         case 124:
793             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
794             charcode = NSRightArrowFunctionKey;
795         break;
796         
797         case 125:
798             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
799             charcode = NSDownArrowFunctionKey;
800         break;
801         
802         case 126:
803             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
804             charcode = NSUpArrowFunctionKey;
805         break;
806     }
807     
808     if (charcode == 'a') {
809         unsigned long state;
810         long keyTrans;
811         char charCode;
812         Ptr kchr;
813         state = 0;
814         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
815         keyTrans = KeyTranslate(kchr, code, &state);
816         charCode = keyTrans;
817         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
818         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
819     } else if (charcode != 'b') {
820         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
821     }
822     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
823 }
824
825 @end