Added tons of ITDebugLogs to iTunesRemote and MenuController.
[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 \"EQ Presets\" submenu.");
67         [tempItem setSubmenu:nil];
68     }
69     
70     if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
71         ITDebugLog(@"Removing \"Playlists\" 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             tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
198                     action:@selector(performMainMenuAction:)
199                     keyEquivalent:@""];
200             [tempItem setTag:MTMenuQuitItem];
201             [tempItem setTarget:self];
202         } else if ([nextObject isEqualToString:@"trackInfo"]) {
203             ITDebugLog(@"Check to see if a Track is playing...");
204             //Handle playing radio too
205             if (_currentPlaylist) {
206                 NSString *title = [currentRemote currentSongTitle];
207                 
208                 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
209                 ITDebugLog(@"Add \"Now Playing\" menu item.");
210                 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
211                 
212                 if ([title length] > 0) {
213                     ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
214                     [menu indentItem:
215                         [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
216                 }
217                 
218                 if ([defaults boolForKey:@"showAlbum"]) {
219                     NSString *curAlbum = [currentRemote currentSongAlbum];
220                     ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
221                     if ([curAlbum length]) {
222                         [menu indentItem:
223                             [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
224                     }
225                 }
226                 
227                 if ([defaults boolForKey:@"showArtist"]) {
228                     NSString *curArtist = [currentRemote currentSongArtist];
229                     ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
230                     if ([curArtist length]) {
231                         [menu indentItem:
232                             [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
233                     }
234                 }
235                 
236                 if ([defaults boolForKey:@"showTrackNumber"]) {
237                     int track = [currentRemote currentSongTrack];
238                     ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
239                     if (track) {
240                         [menu indentItem:
241                             [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
242                     }
243                 }
244                 
245                 if ([defaults boolForKey:@"showTime"]) {
246                     int left = [[currentRemote currentSongRemaining] intValue];
247                     NSString *remaining = [NSString stringWithFormat:@"%i:%02i", left / 60, left % 60];
248                     ITDebugLog(@"Add Track Remaining (\"%@/%@\") menu item.", remaining, [currentRemote currentSongLength]);
249                     [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", remaining, [currentRemote currentSongLength]] action:nil keyEquivalent:@""]];
250                 }
251                 
252                 if ([defaults boolForKey:@"showTrackRating"]) {
253                     NSString *string = nil;
254                     switch ((int)([currentRemote currentSongRating] * 5)) {
255                         case 0:
256                             string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
257                         break;
258                         case 1:
259                             string = [NSString stringWithUTF8String:"★☆☆☆☆"];
260                         break;
261                         case 2:
262                             string = [NSString stringWithUTF8String:"★★☆☆☆"];
263                         break;
264                         case 3:
265                             string = [NSString stringWithUTF8String:"★★★☆☆"];
266                         break;
267                         case 4:
268                             string = [NSString stringWithUTF8String:"★★★★☆"];
269                         break;
270                         case 5:
271                             string = [NSString stringWithUTF8String:"★★★★★"];
272                         break;
273                     }
274                     ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
275                     [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
276                 }
277             } else {
278                 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
279                 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
280             }
281         } else if ([nextObject isEqualToString:@"separator"]) {
282             ITDebugLog(@"Add a separator menu item.");
283             [menu addItem:[NSMenuItem separatorItem]];
284         //Submenu items
285         } else if ([nextObject isEqualToString:@"songRating"]) {
286             ITDebugLog(@"Add \"Song Rating\" submenu.");
287             tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
288                     action:nil
289                     keyEquivalent:@""];
290             [tempItem setSubmenu:_ratingMenu];
291             [tempItem setTag:1];
292             if (_playingRadio || !_currentPlaylist) {
293                 [tempItem setEnabled:NO];
294             }
295             
296             itemEnum = [[_ratingMenu itemArray] objectEnumerator];
297             while ( (tempItem = [itemEnum nextObject]) ) {
298                 [tempItem setState:NSOffState];
299             }
300             
301             [[_ratingMenu itemAtIndex:([currentRemote currentSongRating] * 5)] setState:NSOnState];
302         } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
303             ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
304             tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
305                     action:nil
306                     keyEquivalent:@""];
307             [tempItem setSubmenu:_upcomingSongsMenu];
308             [tempItem setTag:2];
309             if (_playingRadio || !_currentPlaylist) {
310                 [tempItem setEnabled:NO];
311             }
312         } else if ([nextObject isEqualToString:@"playlists"]) {
313             ITDebugLog(@"Add \"Playlists\" submenu.");
314             tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
315                     action:nil
316                     keyEquivalent:@""];
317             [tempItem setSubmenu:_playlistsMenu];
318             [tempItem setTag:3];
319         } else if ([nextObject isEqualToString:@"eqPresets"]) {
320             ITDebugLog(@"Add \"EQ Presets\" submenu.");
321             tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
322                     action:nil
323                     keyEquivalent:@""];
324             [tempItem setSubmenu:_eqMenu];
325             [tempItem setTag:4];
326             
327             itemEnum = [[_eqMenu itemArray] objectEnumerator];
328             while ( (tempItem = [itemEnum nextObject]) ) {
329                 [tempItem setState:NSOffState];
330             }
331             [[_eqMenu itemAtIndex:([currentRemote currentEQPresetIndex] - 1)] setState:NSOnState];
332         }
333     }
334     ITDebugLog(@"Finished building menu.");
335     [_currentMenu release];
336     _currentMenu = menu;
337     return _currentMenu;
338 }
339
340 - (NSMenu *)menuForNoPlayer
341 {
342     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
343     NSMenuItem *tempItem;
344     ITDebugLog(@"Creating menu for when player isn't running.");
345     ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
346     tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
347     [tempItem setTag:MTMenuShowPlayerItem];
348     [tempItem setTarget:self];
349     ITDebugLog(@"Add a separator menu item.");
350     [menu addItem:[NSMenuItem separatorItem]];
351     ITDebugLog(@"Add \"Preferences...\" menu item.");
352     tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
353     [tempItem setTag:MTMenuPreferencesItem];
354     [tempItem setTarget:self];
355     ITDebugLog(@"Add \"Quit\" menu item.");
356     tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
357     [tempItem setTag:MTMenuQuitItem];
358     [tempItem setTarget:self];
359     return [menu autorelease];
360 }
361
362 - (void)rebuildSubmenus
363 {
364     NSLog(@"Rebuilding all of the submenus.");
365     
366     currentRemote = [[MainController sharedController] currentRemote];
367     _currentPlaylist = [currentRemote currentPlaylistIndex];
368     _currentTrack = [currentRemote currentSongIndex];
369     _playingRadio = ([currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
370     
371     [_ratingMenu release];
372     [_upcomingSongsMenu release];
373     [_playlistsMenu release];
374     [_eqMenu release];
375     NSLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
376     _ratingMenu = [self ratingMenu];
377     NSLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
378     _upcomingSongsMenu = [self upcomingSongsMenu];
379     NSLog(@"Beginning Rebuild of \"Playlists\" submenu.");
380     _playlistsMenu = [self playlistsMenu];
381     NSLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
382     _eqMenu = [self eqMenu];
383     NSLog(@"Done rebuilding all of the submenus.");
384 }
385
386 - (NSMenu *)ratingMenu
387 {
388     NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
389     NSEnumerator *itemEnum;
390     id  anItem;
391     int itemTag = 0;
392     SEL itemSelector = @selector(performRatingMenuAction:);
393     
394     ITDebugLog(@"Building \"Song Rating\" menu.");
395     
396     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
397     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
398     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
399     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
400     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
401     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
402     
403     itemEnum = [[ratingMenu itemArray] objectEnumerator];
404     while ( (anItem = [itemEnum nextObject]) ) {
405         ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
406         [anItem setAction:itemSelector];
407         [anItem setTarget:self];
408         [anItem setTag:itemTag];
409         itemTag += 20;
410     }
411     ITDebugLog(@"Done Building \"Song Rating\" menu.");
412     return ratingMenu;
413 }
414
415 - (NSMenu *)upcomingSongsMenu
416 {
417     NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
418     int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:_currentPlaylist];
419     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
420     
421     ITDebugLog(@"Building \"Upcoming Songs\" menu.");
422     
423     if (_currentPlaylist && !_playingRadio) {
424         if (numSongs > 0) {
425             int i;
426
427             for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
428                 if (i <= numSongs) {
429                     NSString *curSong = [currentRemote songTitleAtIndex:i];
430                     NSMenuItem *songItem;
431                     ITDebugLog(@"Adding song: %@", curSong);
432                     songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
433                     [songItem setTag:i];
434                     [songItem setTarget:self];
435                 } else {
436                     break;
437                 }
438             }
439         }
440     }
441     ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
442     return upcomingSongsMenu;
443 }
444
445 - (NSMenu *)playlistsMenu
446 {
447     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
448     NSArray *playlists = [currentRemote playlists];
449     NSMenuItem *tempItem;
450     int i;
451     
452     ITDebugLog(@"Building \"Playlists\" menu.");
453     
454     for (i = 0; i < [playlists count]; i++) {
455         NSString *curPlaylist = [playlists objectAtIndex:i];
456         ITDebugLog(@"Adding playlist: %@", curPlaylist);
457         tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
458         [tempItem setTag:i + 1];
459         [tempItem setTarget:self];
460     }
461     
462     if (!_playingRadio && _currentPlaylist) {
463         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
464     }
465     ITDebugLog(@"Done Building \"Playlists\" menu");
466     return playlistsMenu;
467 }
468
469 - (NSMenu *)eqMenu
470 {
471     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
472     NSArray *eqPresets = [currentRemote eqPresets];
473     NSMenuItem *tempItem;
474     int i;
475     
476     ITDebugLog(@"Building \"EQ Presets\" menu.");
477     
478     for (i = 0; i < [eqPresets count]; i++) {
479         NSString *name;
480            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
481             ITDebugLog(@"Adding EQ Preset: %@", name);
482             tempItem = [eqMenu addItemWithTitle:name
483                     action:@selector(performEqualizerMenuAction:)
484                     keyEquivalent:@""];
485             [tempItem setTag:i];
486             [tempItem setTarget:self];
487            }
488     }
489     ITDebugLog(@"Done Building \"EQ Presets\" menu");
490     return eqMenu;
491 }
492
493 - (void)performMainMenuAction:(id)sender
494 {
495     switch ( [sender tag] )
496     {
497         case MTMenuPlayPauseItem:
498             ITDebugLog(@"Performing Menu Action: Play/Pause");
499             [[MainController sharedController] playPause];
500             break;
501         case MTMenuFastForwardItem:
502             ITDebugLog(@"Performing Menu Action: Fast Forward");
503             [[MainController sharedController] fastForward];
504             break;
505         case MTMenuRewindItem:
506             ITDebugLog(@"Performing Menu Action: Rewind");
507             [[MainController sharedController] rewind];
508             break;
509         case MTMenuPreviousTrackItem:
510             ITDebugLog(@"Performing Menu Action: Previous Track");
511             [[MainController sharedController] prevSong];
512             break;
513         case MTMenuNextTrackItem:
514             ITDebugLog(@"Performing Menu Action: Next Track");
515             [[MainController sharedController] nextSong];
516             break;
517         case MTMenuShowPlayerItem:
518             ITDebugLog(@"Performing Menu Action: Show Main Interface");
519             [[MainController sharedController] showPlayer];
520             break;
521         case MTMenuPreferencesItem:
522             ITDebugLog(@"Performing Menu Action: Preferences...");
523             [[MainController sharedController] showPreferences];
524             break;
525         case MTMenuQuitItem:
526             ITDebugLog(@"Performing Menu Action: Quit");
527             [[MainController sharedController] quitMenuTunes];
528             break;
529         default:
530             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
531             break;
532     }
533 }
534
535 - (void)performRatingMenuAction:(id)sender
536 {
537     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
538     [[MainController sharedController] selectSongRating:[sender tag]];
539 }
540
541 - (void)performPlaylistMenuAction:(id)sender
542 {
543     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
544     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
545 }
546
547 - (void)performEqualizerMenuAction:(id)sender
548 {
549     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
550     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
551 }
552
553 - (void)performUpcomingSongsMenuAction:(id)sender
554 {
555     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
556     [[MainController sharedController] selectSongAtIndex:[sender tag]];
557 }
558
559 - (void)updateMenu
560 {
561     ITDebugLog(@"Update Menu");
562     [_currentMenu update];
563 }
564
565 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
566 {
567     return YES;
568 }
569
570 //This is never used I know, keep it though
571 - (NSString *)systemUIColor
572 {
573     NSDictionary *tmpDict;
574     NSNumber *tmpNumber;
575     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
576         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
577             if ( ([tmpNumber intValue] == 1) ) {
578                 return @"Aqua";
579             } else {
580                 return @"Graphite";
581             }
582         } else {
583             return @"Aqua";
584         }
585     } else {
586         return @"Aqua";
587     }
588 }
589
590 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
591         onItem:(NSMenuItem *)item
592 {
593     unichar charcode = 'a';
594     int i;
595     long cocoaModifiers = 0;
596     static long carbonToCocoa[6][2] = 
597     {
598         { cmdKey, NSCommandKeyMask },
599         { optionKey, NSAlternateKeyMask },
600         { controlKey, NSControlKeyMask },
601         { shiftKey, NSShiftKeyMask },
602     };
603     
604     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
605     
606     for (i = 0; i < 6; i++) {
607         if (modifiers & carbonToCocoa[i][0]) {
608             cocoaModifiers += carbonToCocoa[i][1];
609         }
610     }
611     [item setKeyEquivalentModifierMask:cocoaModifiers];
612     
613     //Missing key combos for some keys. Must find them later.
614     switch (code)
615     {
616         case 36:
617             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
618             charcode = '\r';
619         break;
620         
621         case 48:
622             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
623             charcode = '\t';
624         break;
625         
626         //Space -- ARGH!
627         case 49:
628         {
629             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
630             // Haven't tested this, though it should work.
631             // This doesn't work. :'(
632             //unichar buffer;
633             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
634             //charcode = buffer;
635             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
636             ITDebugLog(@"%@", menuRef);
637             SetMenuItemCommandKey(menuRef, 0, NO, 49);
638             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
639             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
640             charcode = 'b';*/
641             unichar buffer;
642             [[NSString stringWithString:@"_"] getCharacters:&buffer]; // this will have to do for now :(
643             charcode = buffer;
644         }
645         break;
646         
647         case 51:
648             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
649             charcode = NSDeleteFunctionKey;
650         break;
651         
652         case 53:
653             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
654             charcode = '\e';
655         break;
656         
657         case 71:
658             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
659             charcode = '\e';
660         break;
661         
662         case 76:
663             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
664             charcode = '\r';
665         break;
666         
667         case 96:
668             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
669             charcode = NSF5FunctionKey;
670         break;
671         
672         case 97:
673             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
674             charcode = NSF6FunctionKey;
675         break;
676         
677         case 98:
678             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
679             charcode = NSF7FunctionKey;
680         break;
681         
682         case 99:
683             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
684             charcode = NSF3FunctionKey;
685         break;
686         
687         case 100:
688             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
689             charcode = NSF8FunctionKey;
690         break;
691         
692         case 101:
693             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
694             charcode = NSF9FunctionKey;
695         break;
696         
697         case 103:
698             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
699             charcode = NSF11FunctionKey;
700         break;
701         
702         case 105:
703             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F3)", [item title]);
704             charcode = NSF3FunctionKey;
705         break;
706         
707         case 107:
708             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
709             charcode = NSF14FunctionKey;
710         break;
711         
712         case 109:
713             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
714             charcode = NSF10FunctionKey;
715         break;
716         
717         case 111:
718             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
719             charcode = NSF12FunctionKey;
720         break;
721         
722         case 113:
723             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
724             charcode = NSF13FunctionKey;
725         break;
726         
727         case 114:
728             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
729             charcode = NSInsertFunctionKey;
730         break;
731         
732         case 115:
733             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
734             charcode = NSHomeFunctionKey;
735         break;
736         
737         case 116:
738             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
739             charcode = NSPageUpFunctionKey;
740         break;
741         
742         case 117:
743             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
744             charcode = NSDeleteFunctionKey;
745         break;
746         
747         case 118:
748             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
749             charcode = NSF4FunctionKey;
750         break;
751         
752         case 119:
753             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
754             charcode = NSEndFunctionKey;
755         break;
756         
757         case 120:
758             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
759             charcode = NSF2FunctionKey;
760         break;
761         
762         case 121:
763             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
764             charcode = NSPageDownFunctionKey;
765         break;
766         
767         case 122:
768             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
769             charcode = NSF1FunctionKey;
770         break;
771         
772         case 123:
773             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
774             charcode = NSLeftArrowFunctionKey;
775         break;
776         
777         case 124:
778             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
779             charcode = NSRightArrowFunctionKey;
780         break;
781         
782         case 125:
783             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
784             charcode = NSDownArrowFunctionKey;
785         break;
786         
787         case 126:
788             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
789             charcode = NSUpArrowFunctionKey;
790         break;
791     }
792     
793     if (charcode == 'a') {
794         unsigned long state;
795         long keyTrans;
796         char charCode;
797         Ptr kchr;
798         state = 0;
799         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
800         keyTrans = KeyTranslate(kchr, code, &state);
801         charCode = keyTrans;
802         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
803         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
804     } else if (charcode != 'b') {
805         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
806     }
807     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
808 }
809
810 @end