5 // Created by Joseph Spiros on Wed Apr 30 2003.
6 // Copyright (c) 2003 iThink Software. All rights reserved.
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>
18 @interface MenuController (SubmenuMethods)
19 - (NSMenu *)ratingMenu;
20 - (NSMenu *)upcomingSongsMenu;
21 - (NSMenu *)playlistsMenu;
23 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
24 onItem:(NSMenuItem *)item;
27 @implementation MenuController
31 if ( (self = [super init]) ) {
32 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
39 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
40 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
41 NSArray *menuArray = [defaults arrayForKey:@"menu"];
42 NSEnumerator *enumerator = [menuArray objectEnumerator];
45 NSEnumerator *itemEnum;
47 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
48 int currentSongRating;
52 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
53 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
54 currentSongRating = ( [[[MainController sharedController] currentRemote] currentSongRating] != -1 );
56 [[MainController sharedController] networkError:localException];
59 ITDebugLog(@"Reset menu if required.");
61 //Kill the old submenu items
62 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
63 ITDebugLog(@"Removing \"Song Rating\" submenu.");
64 [tempItem setSubmenu:nil];
67 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
68 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
69 [tempItem setSubmenu:nil];
72 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
73 ITDebugLog(@"Removing \"Playlists\" submenu.");
74 [tempItem setSubmenu:nil];
77 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
78 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
79 [tempItem setSubmenu:nil];
82 ITDebugLog(@"Begin building menu.");
85 while ( (nextObject = [enumerator nextObject]) ) {
87 if ([nextObject isEqualToString:@"playPause"]) {
88 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
89 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
90 action:@selector(performMainMenuAction:)
92 [tempItem setTag:MTMenuPlayPauseItem];
93 [tempItem setTarget:self];
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]
105 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
107 switch ([[[MainController sharedController] currentRemote] playerPlayingState]) {
108 case ITMTRemotePlayerPlaying:
109 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
111 case ITMTRemotePlayerRewinding:
112 case ITMTRemotePlayerForwarding:
113 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
119 [[MainController sharedController] networkError:localException];
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:)
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]
137 if (_currentPlaylist) {
138 [tempItem setTag:MTMenuNextTrackItem];
139 [tempItem setTarget:self];
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:)
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]
157 if (_currentPlaylist) {
158 [tempItem setTag:MTMenuPreviousTrackItem];
159 [tempItem setTarget:self];
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:)
166 if (_currentPlaylist) {
167 [tempItem setTag:MTMenuFastForwardItem];
168 [tempItem setTarget:self];
170 } else if ([nextObject isEqualToString:@"rewind"]) {
171 ITDebugLog(@"Add \"Rewind\" menu item.");
172 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
173 action:@selector(performMainMenuAction:)
175 if (_currentPlaylist) {
176 [tempItem setTag:MTMenuRewindItem];
177 [tempItem setTarget:self];
179 } else if ([nextObject isEqualToString:@"showPlayer"]) {
180 ITDebugLog(@"Add \"Show Player\" menu item.");
182 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
183 NSLocalizedString(@"show", @"Show"),
184 [[[MainController sharedController] currentRemote] playerSimpleName]]
185 action:@selector(performMainMenuAction:)
188 [[MainController sharedController] networkError:localException];
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]
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:)
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];
217 ITDebugLog(@"Add \"Quit\" menu item.");
218 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
219 action:@selector(performMainMenuAction:)
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) {
229 title = [[[MainController sharedController] currentRemote] currentSongTitle];
231 [[MainController sharedController] networkError:localException];
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:@""];
237 if ([title length] > 0) {
238 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
240 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
243 if (!_playingRadio) {
244 if ([defaults boolForKey:@"showAlbum"]) {
247 curAlbum = [[[MainController sharedController] currentRemote] currentSongAlbum];
249 [[MainController sharedController] networkError:localException];
251 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
254 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
258 if ([defaults boolForKey:@"showArtist"]) {
261 curArtist = [[[MainController sharedController] currentRemote] currentSongArtist];
263 [[MainController sharedController] networkError:localException];
265 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
268 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
272 if ([defaults boolForKey:@"showTrackNumber"]) {
275 track = [[[MainController sharedController] currentRemote] currentSongTrack];
277 [[MainController sharedController] networkError:localException];
279 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
282 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
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:@""]];
293 [[MainController sharedController] networkError:localException];
296 if (!_playingRadio) {
298 if ([defaults boolForKey:@"showTrackRating"] && ( [[[MainController sharedController] currentRemote] currentSongRating] != -1.0 )) {
299 NSString *string = nil;
300 switch ((int)([[[MainController sharedController] currentRemote] currentSongRating] * 5)) {
302 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
305 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
308 string = [NSString stringWithUTF8String:"★★☆☆☆"];
311 string = [NSString stringWithUTF8String:"★★★☆☆"];
314 string = [NSString stringWithUTF8String:"★★★★☆"];
317 string = [NSString stringWithUTF8String:"★★★★★"];
320 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
321 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
324 [[MainController sharedController] networkError:localException];
328 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
329 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
331 } else if ([nextObject isEqualToString:@"separator"]) {
332 ITDebugLog(@"Add a separator menu item.");
333 [menu addItem:[NSMenuItem separatorItem]];
335 } else if ([nextObject isEqualToString:@"playlists"]) {
336 ITDebugLog(@"Add \"Playlists\" submenu.");
337 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
340 [tempItem setSubmenu:_playlistsMenu];
342 } else if ([nextObject isEqualToString:@"eqPresets"]) {
343 ITDebugLog(@"Add \"EQ Presets\" submenu.");
344 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
347 [tempItem setSubmenu:_eqMenu];
350 itemEnum = [[_eqMenu itemArray] objectEnumerator];
351 while ( (tempItem = [itemEnum nextObject]) ) {
352 [tempItem setState:NSOffState];
355 [[_eqMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentEQPresetIndex] - 1)] setState:NSOnState];
357 [[MainController sharedController] networkError:localException];
359 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
360 ITDebugLog(@"Add \"Song Rating\" submenu.");
361 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
364 [tempItem setSubmenu:_ratingMenu];
366 if (_playingRadio || !_currentPlaylist) {
367 [tempItem setEnabled:NO];
370 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
371 while ( (tempItem = [itemEnum nextObject]) ) {
372 [tempItem setState:NSOffState];
376 [[_ratingMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentSongRating] * 5)] setState:NSOnState];
378 [[MainController sharedController] networkError:localException];
380 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
381 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
382 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
385 [tempItem setSubmenu:_upcomingSongsMenu];
387 if (_playingRadio || !_currentPlaylist) {
388 [tempItem setEnabled:NO];
392 ITDebugLog(@"Finished building menu.");
393 [_currentMenu release];
398 - (NSMenu *)menuForNoPlayer
400 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
401 NSMenuItem *tempItem;
402 ITDebugLog(@"Creating menu for when player isn't running.");
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:@""];
407 [[MainController sharedController] networkError:localException];
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];
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];
430 - (void)rebuildSubmenus
432 ITDebugLog(@"Rebuilding all of the submenus.");
435 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
436 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
437 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
439 [[MainController sharedController] networkError:localException];
441 [_ratingMenu release];
442 [_upcomingSongsMenu release];
443 [_playlistsMenu release];
445 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
446 _ratingMenu = [self ratingMenu];
447 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
448 _upcomingSongsMenu = [self upcomingSongsMenu];
449 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
450 _playlistsMenu = [self playlistsMenu];
451 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
452 _eqMenu = [self eqMenu];
453 ITDebugLog(@"Done rebuilding all of the submenus.");
456 - (NSMenu *)ratingMenu
458 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
459 NSEnumerator *itemEnum;
462 SEL itemSelector = @selector(performRatingMenuAction:);
464 ITDebugLog(@"Building \"Song Rating\" menu.");
466 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
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:@""];
473 itemEnum = [[ratingMenu itemArray] objectEnumerator];
474 while ( (anItem = [itemEnum nextObject]) ) {
475 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
476 [anItem setAction:itemSelector];
477 [anItem setTarget:self];
478 [anItem setTag:itemTag];
481 ITDebugLog(@"Done Building \"Song Rating\" menu.");
485 - (NSMenu *)upcomingSongsMenu
487 NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
488 int numSongs, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
491 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
493 [[MainController sharedController] networkError:localException];
496 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
497 if (_currentPlaylist && !_playingRadio) {
501 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
505 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
507 [[MainController sharedController] networkError:localException];
509 NSMenuItem *songItem;
510 ITDebugLog(@"Adding song: %@", curSong);
511 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
513 [songItem setTarget:self];
520 if ([upcomingSongsMenu numberOfItems] == 0) {
521 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
524 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
525 return upcomingSongsMenu;
528 /*- (NSMenu *)playlistsMenu
530 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
532 NSMenuItem *tempItem;
533 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
536 playlists = [[[MainController sharedController] currentRemote] playlists];
538 [[MainController sharedController] networkError:localException];
541 ITDebugLog(@"Building \"Playlists\" menu.");
543 for (i = 0; i < [playlists count]; i++) {
544 NSString *curPlaylist = [playlists objectAtIndex:i];
545 ITDebugLog(@"Adding playlist: %@", curPlaylist);
546 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
547 [tempItem setTag:i + 1];
548 [tempItem setTarget:self];
551 if (source == ITMTRemoteRadioSource) {
552 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
553 } else if (source == ITMTRemoteGenericDeviceSource) {
554 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
555 } else if (source == ITMTRemoteiPodSource) {
556 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
557 } else if (source == ITMTRemoteCDSource) {
558 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
559 } else if (source == ITMTRemoteSharedLibrarySource) {
560 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
561 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
562 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
564 ITDebugLog(@"Done Building \"Playlists\" menu");
565 return playlistsMenu;
569 - (NSMenu *)playlistsMenu
571 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
573 NSMenuItem *tempItem;
574 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
577 playlists = [[[MainController sharedController] currentRemote] playlists];
579 [[MainController sharedController] networkError:localException];
581 ITDebugLog(@"Building \"Playlists\" menu.");
583 NSArray *curPlaylist = [playlists objectAtIndex:0];
584 NSString *name = [curPlaylist objectAtIndex:0];
585 ITDebugLog(@"Adding main source: %@", name);
586 for (i = 2; i < [curPlaylist count]; i++) {
587 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
588 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
590 [tempItem setTarget:self];
594 if ( (source == ITMTRemoteRadioSource) || ([playlists count] - 2 > 0) ) {
595 [playlistsMenu addItem:[NSMenuItem separatorItem]];
598 if (source == ITMTRemoteRadioSource) {
599 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
602 for (i = 2; i < [playlists count]; i++) {
603 NSArray *curPlaylist = [playlists objectAtIndex:i];
604 NSString *name = [curPlaylist objectAtIndex:0];
605 NSMenu *submenu = [[NSMenu alloc] init];
606 ITDebugLog(@"Adding source: %@", name);
607 for (j = 2; j < [curPlaylist count]; j++) {
608 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
609 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
610 [tempItem setTag:(i * 1000) + j];
611 [tempItem setTarget:self];
612 if ([[curPlaylist objectAtIndex:i] intValue] == ITMTRemoteiPodSource) {
613 [tempItem setEnabled:NO];
616 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
619 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ){
620 tempItem = [playlistsMenu itemAtIndex:(int)[[[MainController sharedController] currentRemote] currentSourceIndex] + [playlistsMenu numberOfItems] - 5];
621 [tempItem setState:NSOnState];
622 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
623 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
624 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
626 ITDebugLog(@"Done Building \"Playlists\" menu");
627 return playlistsMenu;
632 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
634 NSMenuItem *tempItem;
638 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
640 [[MainController sharedController] networkError:localException];
643 ITDebugLog(@"Building \"EQ Presets\" menu.");
645 for (i = 0; i < [eqPresets count]; i++) {
647 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
648 ITDebugLog(@"Adding EQ Preset: %@", name);
649 tempItem = [eqMenu addItemWithTitle:name
650 action:@selector(performEqualizerMenuAction:)
653 [tempItem setTarget:self];
656 ITDebugLog(@"Done Building \"EQ Presets\" menu");
660 - (void)performMainMenuAction:(id)sender
662 switch ( [sender tag] )
664 case MTMenuPlayPauseItem:
665 ITDebugLog(@"Performing Menu Action: Play/Pause");
666 [[MainController sharedController] playPause];
668 case MTMenuFastForwardItem:
669 ITDebugLog(@"Performing Menu Action: Fast Forward");
670 [[MainController sharedController] fastForward];
672 case MTMenuRewindItem:
673 ITDebugLog(@"Performing Menu Action: Rewind");
674 [[MainController sharedController] rewind];
676 case MTMenuPreviousTrackItem:
677 ITDebugLog(@"Performing Menu Action: Previous Track");
678 [[MainController sharedController] prevSong];
680 case MTMenuNextTrackItem:
681 ITDebugLog(@"Performing Menu Action: Next Track");
682 [[MainController sharedController] nextSong];
684 case MTMenuShowPlayerItem:
685 ITDebugLog(@"Performing Menu Action: Show Main Interface");
686 [[MainController sharedController] showPlayer];
688 case MTMenuPreferencesItem:
689 ITDebugLog(@"Performing Menu Action: Preferences...");
690 [[MainController sharedController] showPreferences];
693 ITDebugLog(@"Performing Menu Action: Quit");
694 [[MainController sharedController] quitMenuTunes];
696 case MTMenuRegisterItem:
697 ITDebugLog(@"Performing Menu Action: Register");
698 [[MainController sharedController] blingNow];
701 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
706 - (void)performRatingMenuAction:(id)sender
708 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
709 [[MainController sharedController] selectSongRating:[sender tag]];
712 - (void)performPlaylistMenuAction:(id)sender
714 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
715 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
718 - (void)performEqualizerMenuAction:(id)sender
720 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
721 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
724 - (void)performUpcomingSongsMenuAction:(id)sender
726 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
727 [[MainController sharedController] selectSongAtIndex:[sender tag]];
732 ITDebugLog(@"Update Menu");
733 [_currentMenu update];
736 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
741 //This is never used I know, keep it though
742 - (NSString *)systemUIColor
744 NSDictionary *tmpDict;
746 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
747 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
748 if ( ([tmpNumber intValue] == 1) ) {
761 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
762 onItem:(NSMenuItem *)item
764 unichar charcode = 'a';
766 long cocoaModifiers = 0;
767 static long carbonToCocoa[6][2] =
769 { cmdKey, NSCommandKeyMask },
770 { optionKey, NSAlternateKeyMask },
771 { controlKey, NSControlKeyMask },
772 { shiftKey, NSShiftKeyMask },
775 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
777 for (i = 0; i < 6; i++) {
778 if (modifiers & carbonToCocoa[i][0]) {
779 cocoaModifiers += carbonToCocoa[i][1];
782 [item setKeyEquivalentModifierMask:cocoaModifiers];
784 //Missing key combos for some keys. Must find them later.
788 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
793 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
800 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
801 // Haven't tested this, though it should work.
802 // This doesn't work. :'(
804 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
806 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
807 ITDebugLog(@"%@", menuRef);
808 SetMenuItemCommandKey(menuRef, 0, NO, 49);
809 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
810 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
813 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
819 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
820 charcode = NSDeleteFunctionKey;
824 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
829 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
834 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
839 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
840 charcode = NSF5FunctionKey;
844 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
845 charcode = NSF6FunctionKey;
849 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
850 charcode = NSF7FunctionKey;
854 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
855 charcode = NSF3FunctionKey;
859 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
860 charcode = NSF8FunctionKey;
864 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
865 charcode = NSF9FunctionKey;
869 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
870 charcode = NSF11FunctionKey;
874 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
875 charcode = NSF13FunctionKey;
879 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
880 charcode = NSF14FunctionKey;
884 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
885 charcode = NSF10FunctionKey;
889 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
890 charcode = NSF12FunctionKey;
894 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
895 charcode = NSF13FunctionKey;
899 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
900 charcode = NSInsertFunctionKey;
904 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
905 charcode = NSHomeFunctionKey;
909 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
910 charcode = NSPageUpFunctionKey;
914 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
915 charcode = NSDeleteFunctionKey;
919 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
920 charcode = NSF4FunctionKey;
924 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
925 charcode = NSEndFunctionKey;
929 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
930 charcode = NSF2FunctionKey;
934 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
935 charcode = NSPageDownFunctionKey;
939 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
940 charcode = NSF1FunctionKey;
944 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
945 charcode = NSLeftArrowFunctionKey;
949 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
950 charcode = NSRightArrowFunctionKey;
954 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
955 charcode = NSDownArrowFunctionKey;
959 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
960 charcode = NSUpArrowFunctionKey;
964 if (charcode == 'a') {
970 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
971 keyTrans = KeyTranslate(kchr, code, &state);
973 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
974 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
975 } else if (charcode != 'b') {
976 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
978 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);