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:@"showTrackNumber"]) {
276 track = [[[MainController sharedController] currentRemote] currentSongTrack];
278 [[MainController sharedController] networkError:localException];
280 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
283 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
289 if ([defaults boolForKey:@"showTime"] && ( ([[[MainController sharedController] currentRemote] currentSongElapsed] != nil) || ([[[MainController sharedController] currentRemote] currentSongLength] != nil) )) {
290 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]);
291 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]] action:nil keyEquivalent:@""]];
294 [[MainController sharedController] networkError:localException];
297 if (!_playingRadio) {
299 if ([defaults boolForKey:@"showTrackRating"] && ( [[[MainController sharedController] currentRemote] currentSongRating] != -1.0 )) {
300 NSString *string = nil;
301 switch ((int)([[[MainController sharedController] currentRemote] currentSongRating] * 5)) {
303 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
306 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
309 string = [NSString stringWithUTF8String:"★★☆☆☆"];
312 string = [NSString stringWithUTF8String:"★★★☆☆"];
315 string = [NSString stringWithUTF8String:"★★★★☆"];
318 string = [NSString stringWithUTF8String:"★★★★★"];
321 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
322 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
325 [[MainController sharedController] networkError:localException];
329 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
330 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
332 } else if ([nextObject isEqualToString:@"separator"]) {
333 ITDebugLog(@"Add a separator menu item.");
334 [menu addItem:[NSMenuItem separatorItem]];
336 } else if ([nextObject isEqualToString:@"playlists"]) {
337 ITDebugLog(@"Add \"Playlists\" submenu.");
338 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
341 [tempItem setSubmenu:_playlistsMenu];
343 } else if ([nextObject isEqualToString:@"eqPresets"]) {
344 ITDebugLog(@"Add \"EQ Presets\" submenu.");
345 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
348 [tempItem setSubmenu:_eqMenu];
351 itemEnum = [[_eqMenu itemArray] objectEnumerator];
352 while ( (tempItem = [itemEnum nextObject]) ) {
353 [tempItem setState:NSOffState];
356 [[_eqMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentEQPresetIndex] - 1)] setState:NSOnState];
358 [[MainController sharedController] networkError:localException];
360 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
361 ITDebugLog(@"Add \"Song Rating\" submenu.");
362 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
365 [tempItem setSubmenu:_ratingMenu];
367 if (_playingRadio || !_currentPlaylist) {
368 [tempItem setEnabled:NO];
371 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
372 while ( (tempItem = [itemEnum nextObject]) ) {
373 [tempItem setState:NSOffState];
377 [[_ratingMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentSongRating] * 5)] setState:NSOnState];
379 [[MainController sharedController] networkError:localException];
381 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
382 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
383 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
386 [tempItem setSubmenu:_upcomingSongsMenu];
388 if (_playingRadio || !_currentPlaylist) {
389 [tempItem setEnabled:NO];
393 ITDebugLog(@"Finished building menu.");
394 [_currentMenu release];
399 - (NSMenu *)menuForNoPlayer
401 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
402 id <NSMenuItem> tempItem;
403 ITDebugLog(@"Creating menu for when player isn't running.");
405 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
406 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
408 [[MainController sharedController] networkError:localException];
410 [tempItem setTag:MTMenuShowPlayerItem];
411 [tempItem setTarget:self];
412 ITDebugLog(@"Add a separator menu item.");
413 [menu addItem:[NSMenuItem separatorItem]];
414 ITDebugLog(@"Add \"Preferences...\" menu item.");
415 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
416 [tempItem setTag:MTMenuPreferencesItem];
417 [tempItem setTarget:self];
418 if ([[MainController sharedController] blingBling] == NO) {
419 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
420 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
421 [tempItem setTag:MTMenuRegisterItem];
422 [tempItem setTarget:self];
424 ITDebugLog(@"Add \"Quit\" menu item.");
425 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
426 [tempItem setTag:MTMenuQuitItem];
427 [tempItem setTarget:self];
428 return [menu autorelease];
431 - (void)rebuildSubmenus
433 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 ITDebugLog(@"Releasing old submenus.");
442 [_ratingMenu release];
443 [_upcomingSongsMenu release];
444 [_playlistsMenu 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.");
457 - (NSMenu *)ratingMenu
459 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
460 NSEnumerator *itemEnum;
463 SEL itemSelector = @selector(performRatingMenuAction:);
465 ITDebugLog(@"Building \"Song Rating\" menu.");
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:@""];
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];
482 ITDebugLog(@"Done Building \"Song Rating\" menu.");
486 - (NSMenu *)upcomingSongsMenu
488 NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
489 int numSongs, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
492 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
494 [[MainController sharedController] networkError:localException];
497 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
498 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 id <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 id <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 id <NSMenuItem> tempItem;
574 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
576 NSMutableArray *indices = [[NSMutableArray alloc] init];
578 playlists = [[[MainController sharedController] currentRemote] playlists];
580 [[MainController sharedController] networkError:localException];
582 ITDebugLog(@"Building \"Playlists\" menu.");
584 NSArray *curPlaylist = [playlists objectAtIndex:0];
585 NSString *name = [curPlaylist objectAtIndex:0];
586 ITDebugLog(@"Adding main source: %@", name);
587 for (i = 3; i < [curPlaylist count]; i++) {
588 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
589 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
590 [tempItem setTag:i - 1];
591 [tempItem setTarget:self];
593 ITDebugLog(@"Adding index to the index array.");
594 [indices addObject:[curPlaylist objectAtIndex:2]];
596 [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
597 if ( (source == ITMTRemoteRadioSource) || ([playlists count] - 2 > 0) ) {
598 [playlistsMenu addItem:[NSMenuItem separatorItem]];
601 if (source == ITMTRemoteRadioSource) {
602 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
605 for (i = 2; i < [playlists count]; i++) {
606 NSArray *curPlaylist = [playlists objectAtIndex:i];
607 NSString *name = [curPlaylist objectAtIndex:0];
608 NSMenu *submenu = [[NSMenu alloc] init];
609 ITDebugLog(@"Adding source: %@", name);
611 if ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) {
612 NSLog(@"We have an iPod!");
613 NSLog(@"This iPod is named %@!", name);
614 NSLog(@"Does it update automagically?");
615 NSLog(@"Result: %i", [self iPodWithNameAutomaticallyUpdates:name]);
618 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
619 ITDebugLog(@"Invalid iPod source.");
621 for (j = 3; j < [curPlaylist count]; j++) {
622 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
623 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
624 [tempItem setTag:(i * 1000) + j - 1];
625 [tempItem setTarget:self];
627 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
629 ITDebugLog(@"Adding index to the index array.");
630 [indices addObject:[curPlaylist objectAtIndex:2]];
632 ITDebugLog(@"Checking the current source.");
633 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
634 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
635 [tempItem setState:NSOnState];
636 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
637 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
638 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
641 ITDebugLog(@"Done Building \"Playlists\" menu");
642 return playlistsMenu;
647 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
649 id <NSMenuItem> tempItem;
653 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
655 [[MainController sharedController] networkError:localException];
658 ITDebugLog(@"Building \"EQ Presets\" menu.");
660 for (i = 0; i < [eqPresets count]; i++) {
662 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
663 ITDebugLog(@"Adding EQ Preset: %@", name);
664 tempItem = [eqMenu addItemWithTitle:name
665 action:@selector(performEqualizerMenuAction:)
668 [tempItem setTarget:self];
671 ITDebugLog(@"Done Building \"EQ Presets\" menu");
675 - (void)performMainMenuAction:(id)sender
677 switch ( [sender tag] )
679 case MTMenuPlayPauseItem:
680 ITDebugLog(@"Performing Menu Action: Play/Pause");
681 [[MainController sharedController] playPause];
683 case MTMenuFastForwardItem:
684 ITDebugLog(@"Performing Menu Action: Fast Forward");
685 [[MainController sharedController] fastForward];
687 case MTMenuRewindItem:
688 ITDebugLog(@"Performing Menu Action: Rewind");
689 [[MainController sharedController] rewind];
691 case MTMenuPreviousTrackItem:
692 ITDebugLog(@"Performing Menu Action: Previous Track");
693 [[MainController sharedController] prevSong];
695 case MTMenuNextTrackItem:
696 ITDebugLog(@"Performing Menu Action: Next Track");
697 [[MainController sharedController] nextSong];
699 case MTMenuShowPlayerItem:
700 ITDebugLog(@"Performing Menu Action: Show Main Interface");
701 [[MainController sharedController] showPlayer];
703 case MTMenuPreferencesItem:
704 ITDebugLog(@"Performing Menu Action: Preferences...");
705 [[MainController sharedController] showPreferences];
708 ITDebugLog(@"Performing Menu Action: Quit");
709 [[MainController sharedController] quitMenuTunes];
711 case MTMenuRegisterItem:
712 ITDebugLog(@"Performing Menu Action: Register");
713 [[MainController sharedController] blingNow];
716 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
721 - (void)performRatingMenuAction:(id)sender
723 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
724 [[MainController sharedController] selectSongRating:[sender tag]];
727 - (void)performPlaylistMenuAction:(id)sender
729 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
730 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
733 - (void)performEqualizerMenuAction:(id)sender
735 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
736 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
739 - (void)performUpcomingSongsMenuAction:(id)sender
741 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
742 [[MainController sharedController] selectSongAtIndex:[sender tag]];
747 ITDebugLog(@"Update Menu");
748 [_currentMenu update];
751 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
756 //This is never used I know, keep it though
757 - (NSString *)systemUIColor
759 NSDictionary *tmpDict;
761 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
762 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
763 if ( ([tmpNumber intValue] == 1) ) {
776 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
777 onItem:(id <NSMenuItem>)item
779 unichar charcode = 'a';
781 long cocoaModifiers = 0;
782 static long carbonToCocoa[6][2] =
784 { cmdKey, NSCommandKeyMask },
785 { optionKey, NSAlternateKeyMask },
786 { controlKey, NSControlKeyMask },
787 { shiftKey, NSShiftKeyMask },
790 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
792 for (i = 0; i < 6; i++) {
793 if (modifiers & carbonToCocoa[i][0]) {
794 cocoaModifiers += carbonToCocoa[i][1];
797 [item setKeyEquivalentModifierMask:cocoaModifiers];
799 //Missing key combos for some keys. Must find them later.
803 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
808 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
815 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
816 // Haven't tested this, though it should work.
817 // This doesn't work. :'(
819 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
821 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
822 ITDebugLog(@"%@", menuRef);
823 SetMenuItemCommandKey(menuRef, 0, NO, 49);
824 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
825 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
828 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
834 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
835 charcode = NSDeleteFunctionKey;
839 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
844 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
849 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
854 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
855 charcode = NSF5FunctionKey;
859 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
860 charcode = NSF6FunctionKey;
864 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
865 charcode = NSF7FunctionKey;
869 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
870 charcode = NSF3FunctionKey;
874 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
875 charcode = NSF8FunctionKey;
879 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
880 charcode = NSF9FunctionKey;
884 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
885 charcode = NSF11FunctionKey;
889 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
890 charcode = NSF13FunctionKey;
894 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
895 charcode = NSF14FunctionKey;
899 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
900 charcode = NSF10FunctionKey;
904 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
905 charcode = NSF12FunctionKey;
909 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
910 charcode = NSF13FunctionKey;
914 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
915 charcode = NSInsertFunctionKey;
919 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
920 charcode = NSHomeFunctionKey;
924 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
925 charcode = NSPageUpFunctionKey;
929 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
930 charcode = NSDeleteFunctionKey;
934 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
935 charcode = NSF4FunctionKey;
939 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
940 charcode = NSEndFunctionKey;
944 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
945 charcode = NSF2FunctionKey;
949 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
950 charcode = NSPageDownFunctionKey;
954 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
955 charcode = NSF1FunctionKey;
959 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
960 charcode = NSLeftArrowFunctionKey;
964 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
965 charcode = NSRightArrowFunctionKey;
969 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
970 charcode = NSDownArrowFunctionKey;
974 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
975 charcode = NSUpArrowFunctionKey;
979 if (charcode == 'a') {
985 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
986 keyTrans = KeyTranslate(kchr, code, &state);
988 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
989 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
990 } else if (charcode != 'b') {
991 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
993 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
996 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
998 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
999 NSEnumerator *volEnum = [volumes objectEnumerator];
1000 NSString *nextVolume;
1002 while ( (nextVolume = [volEnum nextObject]) ) {
1003 if ([nextVolume rangeOfString:name options:nil range:NSMakeRange(0, [name length] - 1)].location != NSNotFound) {
1004 NSFileHandle *handle;
1006 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1007 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1008 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1011 handle = [NSFileHandle fileHandleForReadingAtPath:name];
1012 [handle seekToFileOffset:10];
1013 data = [handle readDataOfLength:1];
1014 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1015 ITDebugLog(@"iPod is manually updated. %@", path);
1017 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1018 ITDebugLog(@"iPod is automatically updated. %@", path);
1021 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);