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