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