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:(id <NSMenuItem>)item;
25 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
28 @implementation MenuController
32 if ( (self = [super init]) ) {
33 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
40 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
41 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
42 NSArray *menuArray = [defaults arrayForKey:@"menu"];
43 NSEnumerator *enumerator = [menuArray objectEnumerator];
45 id <NSMenuItem> tempItem;
46 NSEnumerator *itemEnum;
48 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
49 int currentSongRating;
53 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
54 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
55 currentSongRating = ( [[[MainController sharedController] currentRemote] currentSongRating] != -1 );
57 [[MainController sharedController] networkError:localException];
60 ITDebugLog(@"Reset menu if required.");
62 //Kill the old submenu items
63 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
64 ITDebugLog(@"Removing \"Song Rating\" submenu.");
65 [tempItem setSubmenu:nil];
68 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
69 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
70 [tempItem setSubmenu:nil];
73 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
74 ITDebugLog(@"Removing \"Playlists\" submenu.");
75 [tempItem setSubmenu:nil];
78 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
79 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
80 [tempItem setSubmenu:nil];
83 ITDebugLog(@"Begin building menu.");
86 while ( (nextObject = [enumerator nextObject]) ) {
88 if ([nextObject isEqualToString:@"playPause"]) {
89 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
90 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
91 action:@selector(performMainMenuAction:)
93 [tempItem setTag:MTMenuPlayPauseItem];
94 [tempItem setTarget:self];
96 itemEnum = [hotKeys objectEnumerator];
97 while ( (hotKey = [itemEnum nextObject]) ) {
98 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
99 ITKeyCombo *combo = [hotKey keyCombo];
100 [self setKeyEquivalentForCode:[combo keyCode]
101 andModifiers:[combo modifiers]
106 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
108 switch ([[[MainController sharedController] currentRemote] playerPlayingState]) {
109 case ITMTRemotePlayerPlaying:
110 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
112 case ITMTRemotePlayerRewinding:
113 case ITMTRemotePlayerForwarding:
114 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
120 [[MainController sharedController] networkError:localException];
122 } else if ([nextObject isEqualToString:@"nextTrack"]) {
123 ITDebugLog(@"Add \"Next Track\" menu item.");
124 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
125 action:@selector(performMainMenuAction:)
128 itemEnum = [hotKeys objectEnumerator];
129 while ( (hotKey = [itemEnum nextObject]) ) {
130 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
131 ITKeyCombo *combo = [hotKey keyCombo];
132 [self setKeyEquivalentForCode:[combo keyCode]
133 andModifiers:[combo modifiers]
138 if (_currentPlaylist) {
139 [tempItem setTag:MTMenuNextTrackItem];
140 [tempItem setTarget:self];
142 } else if ([nextObject isEqualToString:@"prevTrack"]) {
143 ITDebugLog(@"Add \"Previous Track\" menu item.");
144 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
145 action:@selector(performMainMenuAction:)
148 itemEnum = [hotKeys objectEnumerator];
149 while ( (hotKey = [itemEnum nextObject]) ) {
150 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
151 ITKeyCombo *combo = [hotKey keyCombo];
152 [self setKeyEquivalentForCode:[combo keyCode]
153 andModifiers:[combo modifiers]
158 if (_currentPlaylist) {
159 [tempItem setTag:MTMenuPreviousTrackItem];
160 [tempItem setTarget:self];
162 } else if ([nextObject isEqualToString:@"fastForward"]) {
163 ITDebugLog(@"Add \"Fast Forward\" menu item.");
164 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
165 action:@selector(performMainMenuAction:)
167 if (_currentPlaylist) {
168 [tempItem setTag:MTMenuFastForwardItem];
169 [tempItem setTarget:self];
171 } else if ([nextObject isEqualToString:@"rewind"]) {
172 ITDebugLog(@"Add \"Rewind\" menu item.");
173 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
174 action:@selector(performMainMenuAction:)
176 if (_currentPlaylist) {
177 [tempItem setTag:MTMenuRewindItem];
178 [tempItem setTarget:self];
180 } else if ([nextObject isEqualToString:@"showPlayer"]) {
181 ITDebugLog(@"Add \"Show Player\" menu item.");
183 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
184 NSLocalizedString(@"show", @"Show"),
185 [[[MainController sharedController] currentRemote] playerSimpleName]]
186 action:@selector(performMainMenuAction:)
189 [[MainController sharedController] networkError:localException];
192 itemEnum = [hotKeys objectEnumerator];
193 while ( (hotKey = [itemEnum nextObject]) ) {
194 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
195 ITKeyCombo *combo = [hotKey keyCombo];
196 [self setKeyEquivalentForCode:[combo keyCode]
197 andModifiers:[combo modifiers]
202 [tempItem setTarget:self];
203 [tempItem setTag:MTMenuShowPlayerItem];
204 } else if ([nextObject isEqualToString:@"preferences"]) {
205 ITDebugLog(@"Add \"Preferences...\" menu item.");
206 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
207 action:@selector(performMainMenuAction:)
209 [tempItem setTag:MTMenuPreferencesItem];
210 [tempItem setTarget:self];
211 } else if ([nextObject isEqualToString:@"quit"]) {
212 if ([[MainController sharedController] blingBling] == NO) {
213 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
214 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
215 [tempItem setTag:MTMenuRegisterItem];
216 [tempItem setTarget:self];
218 ITDebugLog(@"Add \"Quit\" menu item.");
219 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
220 action:@selector(performMainMenuAction:)
222 [tempItem setTag:MTMenuQuitItem];
223 [tempItem setTarget:self];
224 } else if ([nextObject isEqualToString:@"trackInfo"]) {
225 ITDebugLog(@"Check to see if a Track is playing...");
226 //Handle playing radio too
227 if (_currentPlaylist) {
230 title = [[[MainController sharedController] currentRemote] currentSongTitle];
232 [[MainController sharedController] networkError:localException];
234 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
235 ITDebugLog(@"Add \"Now Playing\" menu item.");
236 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
238 if ([title length] > 0) {
239 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
241 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
244 if (!_playingRadio) {
245 if ([defaults boolForKey:@"showAlbum"]) {
248 curAlbum = [[[MainController sharedController] currentRemote] currentSongAlbum];
250 [[MainController sharedController] networkError:localException];
252 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
255 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
259 if ([defaults boolForKey:@"showArtist"]) {
262 curArtist = [[[MainController sharedController] currentRemote] currentSongArtist];
264 [[MainController sharedController] networkError:localException];
266 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
269 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
273 if ([defaults boolForKey:@"showComposer"]) {
274 NSString *curComposer;
276 curComposer = [[[MainController sharedController] currentRemote] currentSongComposer];
278 [[MainController sharedController] networkError:localException];
280 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
283 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
287 if ([defaults boolForKey:@"showTrackNumber"]) {
290 track = [[[MainController sharedController] currentRemote] currentSongTrack];
292 [[MainController sharedController] networkError:localException];
294 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
297 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
303 if ([defaults boolForKey:@"showTime"] && ( ([[[MainController sharedController] currentRemote] currentSongElapsed] != nil) || ([[[MainController sharedController] currentRemote] currentSongLength] != nil) )) {
304 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]);
305 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]] action:nil keyEquivalent:@""]];
308 [[MainController sharedController] networkError:localException];
311 if (!_playingRadio) {
313 if ([defaults boolForKey:@"showTrackRating"] && ( [[[MainController sharedController] currentRemote] currentSongRating] != -1.0 )) {
314 NSString *string = nil;
315 switch ((int)([[[MainController sharedController] currentRemote] currentSongRating] * 5)) {
317 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
320 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
323 string = [NSString stringWithUTF8String:"★★☆☆☆"];
326 string = [NSString stringWithUTF8String:"★★★☆☆"];
329 string = [NSString stringWithUTF8String:"★★★★☆"];
332 string = [NSString stringWithUTF8String:"★★★★★"];
335 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
336 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
339 [[MainController sharedController] networkError:localException];
343 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
344 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
346 } else if ([nextObject isEqualToString:@"separator"]) {
347 ITDebugLog(@"Add a separator menu item.");
348 [menu addItem:[NSMenuItem separatorItem]];
350 } else if ([nextObject isEqualToString:@"playlists"]) {
351 ITDebugLog(@"Add \"Playlists\" submenu.");
352 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
355 [tempItem setSubmenu:_playlistsMenu];
357 } else if ([nextObject isEqualToString:@"eqPresets"]) {
358 ITDebugLog(@"Add \"EQ Presets\" submenu.");
359 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
362 [tempItem setSubmenu:_eqMenu];
365 itemEnum = [[_eqMenu itemArray] objectEnumerator];
366 while ( (tempItem = [itemEnum nextObject]) ) {
367 [tempItem setState:NSOffState];
370 [[_eqMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentEQPresetIndex] - 1)] setState:NSOnState];
372 [[MainController sharedController] networkError:localException];
374 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
375 ITDebugLog(@"Add \"Song Rating\" submenu.");
376 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
379 [tempItem setSubmenu:_ratingMenu];
381 if (_playingRadio || !_currentPlaylist) {
382 [tempItem setEnabled:NO];
385 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
386 while ( (tempItem = [itemEnum nextObject]) ) {
387 [tempItem setState:NSOffState];
391 [[_ratingMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentSongRating] * 5)] setState:NSOnState];
393 [[MainController sharedController] networkError:localException];
395 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
396 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
397 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
400 [tempItem setSubmenu:_upcomingSongsMenu];
402 if (_playingRadio || !_currentPlaylist) {
403 [tempItem setEnabled:NO];
407 ITDebugLog(@"Finished building menu.");
408 [_currentMenu release];
413 - (NSMenu *)menuForNoPlayer
415 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
416 id <NSMenuItem> tempItem;
417 ITDebugLog(@"Creating menu for when player isn't running.");
419 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
420 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
422 [[MainController sharedController] networkError:localException];
424 [tempItem setTag:MTMenuShowPlayerItem];
425 [tempItem setTarget:self];
426 ITDebugLog(@"Add a separator menu item.");
427 [menu addItem:[NSMenuItem separatorItem]];
428 ITDebugLog(@"Add \"Preferences...\" menu item.");
429 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
430 [tempItem setTag:MTMenuPreferencesItem];
431 [tempItem setTarget:self];
432 if ([[MainController sharedController] blingBling] == NO) {
433 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
434 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
435 [tempItem setTag:MTMenuRegisterItem];
436 [tempItem setTarget:self];
438 ITDebugLog(@"Add \"Quit\" menu item.");
439 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
440 [tempItem setTag:MTMenuQuitItem];
441 [tempItem setTarget:self];
442 return [menu autorelease];
445 - (void)rebuildSubmenus
447 ITDebugLog(@"Rebuilding all of the submenus.");
449 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
450 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
451 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
453 [[MainController sharedController] networkError:localException];
455 ITDebugLog(@"Releasing old submenus.");
456 [_ratingMenu release];
457 [_upcomingSongsMenu release];
458 [_playlistsMenu release];
460 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
461 _ratingMenu = [self ratingMenu];
462 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
463 _upcomingSongsMenu = [self upcomingSongsMenu];
464 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
465 _playlistsMenu = [self playlistsMenu];
466 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
467 _eqMenu = [self eqMenu];
468 ITDebugLog(@"Done rebuilding all of the submenus.");
471 - (NSMenu *)ratingMenu
473 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
474 NSEnumerator *itemEnum;
477 SEL itemSelector = @selector(performRatingMenuAction:);
479 ITDebugLog(@"Building \"Song Rating\" menu.");
481 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
482 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
483 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
484 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
485 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
486 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
488 itemEnum = [[ratingMenu itemArray] objectEnumerator];
489 while ( (anItem = [itemEnum nextObject]) ) {
490 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
491 [anItem setAction:itemSelector];
492 [anItem setTarget:self];
493 [anItem setTag:itemTag];
496 ITDebugLog(@"Done Building \"Song Rating\" menu.");
500 - (NSMenu *)upcomingSongsMenu
502 NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
503 int numSongs, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
506 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
508 [[MainController sharedController] networkError:localException];
511 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
512 if (_currentPlaylist && !_playingRadio) {
515 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
519 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
521 [[MainController sharedController] networkError:localException];
523 id <NSMenuItem> songItem;
524 ITDebugLog(@"Adding song: %@", curSong);
525 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
527 [songItem setTarget:self];
534 if ([upcomingSongsMenu numberOfItems] == 0) {
535 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
538 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
539 return upcomingSongsMenu;
542 /*- (NSMenu *)playlistsMenu
544 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
546 id <NSMenuItem> tempItem;
547 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
550 playlists = [[[MainController sharedController] currentRemote] playlists];
552 [[MainController sharedController] networkError:localException];
555 ITDebugLog(@"Building \"Playlists\" menu.");
557 for (i = 0; i < [playlists count]; i++) {
558 NSString *curPlaylist = [playlists objectAtIndex:i];
559 ITDebugLog(@"Adding playlist: %@", curPlaylist);
560 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
561 [tempItem setTag:i + 1];
562 [tempItem setTarget:self];
565 if (source == ITMTRemoteRadioSource) {
566 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
567 } else if (source == ITMTRemoteGenericDeviceSource) {
568 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
569 } else if (source == ITMTRemoteiPodSource) {
570 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
571 } else if (source == ITMTRemoteCDSource) {
572 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
573 } else if (source == ITMTRemoteSharedLibrarySource) {
574 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
575 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
576 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
578 ITDebugLog(@"Done Building \"Playlists\" menu");
579 return playlistsMenu;
583 - (NSMenu *)playlistsMenu
585 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
587 id <NSMenuItem> tempItem;
588 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
590 NSMutableArray *indices = [[NSMutableArray alloc] init];
592 playlists = [[[MainController sharedController] currentRemote] playlists];
594 [[MainController sharedController] networkError:localException];
596 ITDebugLog(@"Building \"Playlists\" menu.");
598 NSArray *curPlaylist = [playlists objectAtIndex:0];
599 NSString *name = [curPlaylist objectAtIndex:0];
600 ITDebugLog(@"Adding main source: %@", name);
601 for (i = 3; i < [curPlaylist count]; i++) {
602 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
603 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
604 [tempItem setTag:i - 1];
605 [tempItem setTarget:self];
607 ITDebugLog(@"Adding index to the index array.");
608 [indices addObject:[curPlaylist objectAtIndex:2]];
610 if ([playlists count] > 1) {
611 if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
612 [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
613 if (source == ITMTRemoteRadioSource) {
614 [playlistsMenu addItem:[NSMenuItem separatorItem]];
615 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
618 [playlistsMenu addItem:[NSMenuItem separatorItem]];
622 if ([playlists count] > 1) {
623 for (i = 1; i < [playlists count]; i++) {
624 NSArray *curPlaylist = [playlists objectAtIndex:i];
625 if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
626 NSString *name = [curPlaylist objectAtIndex:0];
627 NSMenu *submenu = [[NSMenu alloc] init];
628 ITDebugLog(@"Adding source: %@", name);
630 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
631 ITDebugLog(@"Invalid iPod source.");
632 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
634 for (j = 3; j < [curPlaylist count]; j++) {
635 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
636 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
637 [tempItem setTag:(i * 1000) + j - 1];
638 [tempItem setTarget:self];
640 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
642 ITDebugLog(@"Adding index to the index array.");
643 [indices addObject:[curPlaylist objectAtIndex:2]];
647 ITDebugLog(@"Checking the current source.");
648 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
649 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
650 [tempItem setState:NSOnState];
651 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
652 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
653 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
656 ITDebugLog(@"Done Building \"Playlists\" menu");
657 return playlistsMenu;
662 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
664 id <NSMenuItem> tempItem;
668 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
670 [[MainController sharedController] networkError:localException];
673 ITDebugLog(@"Building \"EQ Presets\" menu.");
675 for (i = 0; i < [eqPresets count]; i++) {
677 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
678 ITDebugLog(@"Adding EQ Preset: %@", name);
679 tempItem = [eqMenu addItemWithTitle:name
680 action:@selector(performEqualizerMenuAction:)
683 [tempItem setTarget:self];
686 ITDebugLog(@"Done Building \"EQ Presets\" menu");
690 - (void)performMainMenuAction:(id)sender
692 switch ( [sender tag] )
694 case MTMenuPlayPauseItem:
695 ITDebugLog(@"Performing Menu Action: Play/Pause");
696 [[MainController sharedController] playPause];
698 case MTMenuFastForwardItem:
699 ITDebugLog(@"Performing Menu Action: Fast Forward");
700 [[MainController sharedController] fastForward];
702 case MTMenuRewindItem:
703 ITDebugLog(@"Performing Menu Action: Rewind");
704 [[MainController sharedController] rewind];
706 case MTMenuPreviousTrackItem:
707 ITDebugLog(@"Performing Menu Action: Previous Track");
708 [[MainController sharedController] prevSong];
710 case MTMenuNextTrackItem:
711 ITDebugLog(@"Performing Menu Action: Next Track");
712 [[MainController sharedController] nextSong];
714 case MTMenuShowPlayerItem:
715 ITDebugLog(@"Performing Menu Action: Show Main Interface");
716 [[MainController sharedController] showPlayer];
718 case MTMenuPreferencesItem:
719 ITDebugLog(@"Performing Menu Action: Preferences...");
720 [[MainController sharedController] showPreferences];
723 ITDebugLog(@"Performing Menu Action: Quit");
724 [[MainController sharedController] quitMenuTunes];
726 case MTMenuRegisterItem:
727 ITDebugLog(@"Performing Menu Action: Register");
728 [[MainController sharedController] blingNow];
731 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
736 - (void)performRatingMenuAction:(id)sender
738 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
739 [[MainController sharedController] selectSongRating:[sender tag]];
742 - (void)performPlaylistMenuAction:(id)sender
744 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
745 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
748 - (void)performEqualizerMenuAction:(id)sender
750 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
751 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
754 - (void)performUpcomingSongsMenuAction:(id)sender
756 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
757 [[MainController sharedController] selectSongAtIndex:[sender tag]];
762 ITDebugLog(@"Update Menu");
763 [_currentMenu update];
766 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
771 //This is never used I know, keep it though
772 - (NSString *)systemUIColor
774 NSDictionary *tmpDict;
776 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
777 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
778 if ( ([tmpNumber intValue] == 1) ) {
791 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
792 onItem:(id <NSMenuItem>)item
794 unichar charcode = 'a';
796 long cocoaModifiers = 0;
797 static long carbonToCocoa[6][2] =
799 { cmdKey, NSCommandKeyMask },
800 { optionKey, NSAlternateKeyMask },
801 { controlKey, NSControlKeyMask },
802 { shiftKey, NSShiftKeyMask },
805 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
807 for (i = 0; i < 6; i++) {
808 if (modifiers & carbonToCocoa[i][0]) {
809 cocoaModifiers += carbonToCocoa[i][1];
812 [item setKeyEquivalentModifierMask:cocoaModifiers];
814 //Missing key combos for some keys. Must find them later.
818 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
823 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
830 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
831 // Haven't tested this, though it should work.
832 // This doesn't work. :'(
834 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
836 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
837 ITDebugLog(@"%@", menuRef);
838 SetMenuItemCommandKey(menuRef, 0, NO, 49);
839 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
840 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
843 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
849 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
850 charcode = NSDeleteFunctionKey;
854 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
859 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
864 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
869 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
870 charcode = NSF5FunctionKey;
874 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
875 charcode = NSF6FunctionKey;
879 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
880 charcode = NSF7FunctionKey;
884 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
885 charcode = NSF3FunctionKey;
889 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
890 charcode = NSF8FunctionKey;
894 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
895 charcode = NSF9FunctionKey;
899 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
900 charcode = NSF11FunctionKey;
904 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
905 charcode = NSF13FunctionKey;
909 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
910 charcode = NSF14FunctionKey;
914 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
915 charcode = NSF10FunctionKey;
919 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
920 charcode = NSF12FunctionKey;
924 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
925 charcode = NSF13FunctionKey;
929 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
930 charcode = NSInsertFunctionKey;
934 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
935 charcode = NSHomeFunctionKey;
939 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
940 charcode = NSPageUpFunctionKey;
944 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
945 charcode = NSDeleteFunctionKey;
949 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
950 charcode = NSF4FunctionKey;
954 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
955 charcode = NSEndFunctionKey;
959 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
960 charcode = NSF2FunctionKey;
964 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
965 charcode = NSPageDownFunctionKey;
969 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
970 charcode = NSF1FunctionKey;
974 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
975 charcode = NSLeftArrowFunctionKey;
979 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
980 charcode = NSRightArrowFunctionKey;
984 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
985 charcode = NSDownArrowFunctionKey;
989 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
990 charcode = NSUpArrowFunctionKey;
994 if (charcode == 'a') {
1000 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1001 keyTrans = KeyTranslate(kchr, code, &state);
1002 charCode = keyTrans;
1003 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1004 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1005 } else if (charcode != 'b') {
1006 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1008 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1011 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1013 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1014 NSEnumerator *volEnum = [volumes objectEnumerator];
1015 NSString *nextVolume;
1016 ITDebugLog(@"Looking for an iPod named %@", name);
1017 while ( (nextVolume = [volEnum nextObject]) ) {
1018 ITDebugLog(@"- %@", nextVolume);
1019 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1020 NSFileHandle *handle;
1022 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1023 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1024 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1027 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1028 ITDebugLog(@"File handle: %@", handle);
1029 [handle seekToFileOffset:10];
1030 data = [handle readDataOfLength:1];
1031 ITDebugLog(@"Data: %@", data);
1032 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1033 ITDebugLog(@"iPod is manually updated. %@", path);
1035 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1036 ITDebugLog(@"iPod is automatically updated. %@", path);
1039 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);