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