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 "NetworkController.h"
12 #import "ITMTRemote.h"
13 #import <ITFoundation/ITDebug.h>
14 #import <ITKit/ITHotKeyCenter.h>
15 #import <ITKit/ITHotKey.h>
16 #import <ITKit/ITKeyCombo.h>
17 #import <ITKit/ITCategory-NSMenu.h>
19 @interface MenuController (SubmenuMethods)
20 - (NSMenu *)ratingMenu;
21 - (NSMenu *)upcomingSongsMenu;
22 - (NSMenu *)playlistsMenu;
24 - (NSMenu *)artistsMenu;
25 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
26 onItem:(id <NSMenuItem>)item;
27 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
30 @implementation MenuController
34 if ( (self = [super init]) ) {
35 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
42 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
43 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
44 NSArray *menuArray = [defaults arrayForKey:@"menu"];
45 NSEnumerator *enumerator = [menuArray objectEnumerator];
47 id <NSMenuItem> tempItem;
48 NSEnumerator *itemEnum;
50 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
51 ITMTRemote *mtr = [[MainController sharedController] currentRemote];
52 int currentSongRating = 0;
56 _currentPlaylist = [mtr currentPlaylistIndex];
57 _playingRadio = ([mtr currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
58 currentSongRating = ( [mtr currentSongRating] != -1 );
60 [[MainController sharedController] networkError:localException];
63 ITDebugLog(@"Reset menu if required.");
65 //Kill the old submenu items
66 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
67 ITDebugLog(@"Removing \"Song Rating\" submenu.");
68 [tempItem setSubmenu:nil];
71 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
72 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
73 [tempItem setSubmenu:nil];
76 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
77 ITDebugLog(@"Removing \"Playlists\" submenu.");
78 [tempItem setSubmenu:nil];
81 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
82 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
83 [tempItem setSubmenu:nil];
86 if ( (tempItem = [_currentMenu itemWithTag:5]) ) {
87 ITDebugLog(@"Removing \"Artists\" submenu.");
88 [tempItem setSubmenu:nil];
91 ITDebugLog(@"Begin building menu.");
94 while ( (nextObject = [enumerator nextObject]) ) {
96 if ([nextObject isEqualToString:@"playPause"]) {
97 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
98 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
99 action:@selector(performMainMenuAction:)
101 [tempItem setTag:MTMenuPlayPauseItem];
102 [tempItem setTarget:self];
104 itemEnum = [hotKeys objectEnumerator];
105 while ( (hotKey = [itemEnum nextObject]) ) {
106 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
107 ITKeyCombo *combo = [hotKey keyCombo];
108 [self setKeyEquivalentForCode:[combo keyCode]
109 andModifiers:[combo modifiers]
114 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
116 switch ([mtr playerPlayingState]) {
117 case ITMTRemotePlayerPlaying:
118 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
120 case ITMTRemotePlayerRewinding:
121 case ITMTRemotePlayerForwarding:
122 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
128 [[MainController sharedController] networkError:localException];
130 } else if ([nextObject isEqualToString:@"nextTrack"]) {
131 ITDebugLog(@"Add \"Next Track\" menu item.");
132 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
133 action:@selector(performMainMenuAction:)
136 itemEnum = [hotKeys objectEnumerator];
137 while ( (hotKey = [itemEnum nextObject]) ) {
138 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
139 ITKeyCombo *combo = [hotKey keyCombo];
140 [self setKeyEquivalentForCode:[combo keyCode]
141 andModifiers:[combo modifiers]
146 if (_currentPlaylist) {
147 [tempItem setTag:MTMenuNextTrackItem];
148 [tempItem setTarget:self];
150 } else if ([nextObject isEqualToString:@"prevTrack"]) {
151 ITDebugLog(@"Add \"Previous Track\" menu item.");
152 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
153 action:@selector(performMainMenuAction:)
156 itemEnum = [hotKeys objectEnumerator];
157 while ( (hotKey = [itemEnum nextObject]) ) {
158 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
159 ITKeyCombo *combo = [hotKey keyCombo];
160 [self setKeyEquivalentForCode:[combo keyCode]
161 andModifiers:[combo modifiers]
166 if (_currentPlaylist) {
167 [tempItem setTag:MTMenuPreviousTrackItem];
168 [tempItem setTarget:self];
170 } else if ([nextObject isEqualToString:@"fastForward"]) {
171 ITDebugLog(@"Add \"Fast Forward\" menu item.");
172 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
173 action:@selector(performMainMenuAction:)
175 if (_currentPlaylist) {
176 [tempItem setTag:MTMenuFastForwardItem];
177 [tempItem setTarget:self];
179 } else if ([nextObject isEqualToString:@"rewind"]) {
180 ITDebugLog(@"Add \"Rewind\" menu item.");
181 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
182 action:@selector(performMainMenuAction:)
184 if (_currentPlaylist) {
185 [tempItem setTag:MTMenuRewindItem];
186 [tempItem setTarget:self];
188 } else if ([nextObject isEqualToString:@"showPlayer"]) {
189 ITDebugLog(@"Add \"Show Player\" menu item.");
191 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
192 NSLocalizedString(@"show", @"Show"),
193 [mtr playerSimpleName]]
194 action:@selector(performMainMenuAction:)
197 [[MainController sharedController] networkError:localException];
200 itemEnum = [hotKeys objectEnumerator];
201 while ( (hotKey = [itemEnum nextObject]) ) {
202 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
203 ITKeyCombo *combo = [hotKey keyCombo];
204 [self setKeyEquivalentForCode:[combo keyCode]
205 andModifiers:[combo modifiers]
210 [tempItem setTarget:self];
211 [tempItem setTag:MTMenuShowPlayerItem];
212 } else if ([nextObject isEqualToString:@"preferences"]) {
213 ITDebugLog(@"Add \"Preferences...\" menu item.");
214 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
215 action:@selector(performMainMenuAction:)
217 [tempItem setTag:MTMenuPreferencesItem];
218 [tempItem setTarget:self];
219 } else if ([nextObject isEqualToString:@"quit"]) {
220 if ([[MainController sharedController] blingBling] == NO) {
221 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
222 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
223 [tempItem setTag:MTMenuRegisterItem];
224 [tempItem setTarget:self];
226 ITDebugLog(@"Add \"Quit\" menu item.");
227 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
228 action:@selector(performMainMenuAction:)
230 [tempItem setTag:MTMenuQuitItem];
231 [tempItem setTarget:self];
232 } else if ([nextObject isEqualToString:@"trackInfo"]) {
233 ITDebugLog(@"Check to see if a Track is playing...");
234 //Handle playing radio too
235 if (_currentPlaylist) {
236 NSString *title = nil;
238 title = [mtr currentSongTitle];
240 [[MainController sharedController] networkError:localException];
242 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
243 ITDebugLog(@"Add \"Now Playing\" menu item.");
244 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
246 if ([title length] > 0) {
247 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
249 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
252 if (!_playingRadio) {
253 if ([defaults boolForKey:@"showAlbum"]) {
254 NSString *curAlbum = nil;
256 curAlbum = [mtr currentSongAlbum];
258 [[MainController sharedController] networkError:localException];
260 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
263 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
267 if ([defaults boolForKey:@"showArtist"]) {
268 NSString *curArtist = nil;
270 curArtist = [mtr currentSongArtist];
272 [[MainController sharedController] networkError:localException];
274 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
277 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
281 if ([defaults boolForKey:@"showComposer"]) {
282 NSString *curComposer = nil;
284 curComposer = [mtr currentSongComposer];
286 [[MainController sharedController] networkError:localException];
288 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
291 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
295 if ([defaults boolForKey:@"showTrackNumber"]) {
298 track = [mtr currentSongTrack];
300 [[MainController sharedController] networkError:localException];
302 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
305 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
311 if ([defaults boolForKey:@"showTime"] && ( ([mtr currentSongElapsed] != nil) || ([mtr currentSongLength] != nil) )) {
312 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [mtr currentSongElapsed], [mtr currentSongLength]);
313 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [mtr currentSongElapsed], [mtr currentSongLength]] action:nil keyEquivalent:@""]];
316 [[MainController sharedController] networkError:localException];
319 if (!_playingRadio) {
321 if ([defaults boolForKey:@"showPlayCount"] && [mtr currentSource] == ITMTRemoteLibrarySource) {
322 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"Play Count: %i", [mtr currentSongPlayCount]] action:nil keyEquivalent:@""]];
324 if ([defaults boolForKey:@"showTrackRating"] && ( [mtr currentSongRating] != -1.0 )) {
325 NSString *string = nil;
326 switch ((int)([mtr currentSongRating] * 5)) {
328 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
331 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
334 string = [NSString stringWithUTF8String:"★★☆☆☆"];
337 string = [NSString stringWithUTF8String:"★★★☆☆"];
340 string = [NSString stringWithUTF8String:"★★★★☆"];
343 string = [NSString stringWithUTF8String:"★★★★★"];
346 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
347 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
350 [[MainController sharedController] networkError:localException];
353 /*if ([tempItem respondsToSelector:@selector(setAttributedTitle:)] && [defaults boolForKey:@"showAlbumArtwork"] && ![[NetworkController sharedController] isConnectedToServer]) {
354 NSImage *image = [mtr currentSongAlbumArt];
356 NSSize oldSize, newSize;
357 oldSize = [image size];
358 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
359 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
360 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
362 tempItem = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
363 NSTextAttachment *attachment = [[[NSTextAttachment alloc] init] autorelease];
364 [[attachment attachmentCell] setImage:image];
365 NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
366 [tempItem setAttributedTitle:attrString];
371 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
372 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
374 } else if ([nextObject isEqualToString:@"separator"]) {
375 ITDebugLog(@"Add a separator menu item.");
376 [menu addItem:[NSMenuItem separatorItem]];
378 } else if ([nextObject isEqualToString:@"playlists"]) {
379 ITDebugLog(@"Add \"Playlists\" submenu.");
380 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
383 [tempItem setSubmenu:_playlistsMenu];
385 } else if ([nextObject isEqualToString:@"eqPresets"]) {
386 ITDebugLog(@"Add \"EQ Presets\" submenu.");
387 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
390 [tempItem setSubmenu:_eqMenu];
393 itemEnum = [[_eqMenu itemArray] objectEnumerator];
394 while ( (tempItem = [itemEnum nextObject]) ) {
395 [tempItem setState:NSOffState];
398 [[_eqMenu itemAtIndex:0] setState:[mtr equalizerEnabled] ? NSOnState : NSOffState];
399 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] + 1)] setState:NSOnState];
400 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] - 1)] setState:NSOnState];
402 [[MainController sharedController] networkError:localException];
404 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
405 ITDebugLog(@"Add \"Song Rating\" submenu.");
406 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
409 [tempItem setSubmenu:_ratingMenu];
411 if (_playingRadio || !_currentPlaylist) {
412 [tempItem setEnabled:NO];
415 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
416 while ( (tempItem = [itemEnum nextObject]) ) {
417 [tempItem setState:NSOffState];
421 [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
423 [[MainController sharedController] networkError:localException];
425 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
426 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
427 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
430 [tempItem setSubmenu:_upcomingSongsMenu];
432 if (_playingRadio || !_currentPlaylist) {
433 [tempItem setEnabled:NO];
435 } else if ([nextObject isEqualToString:@"artists"]) {
436 ITDebugLog(@"Add \"Artists\" submenu.");
437 tempItem = [menu addItemWithTitle:NSLocalizedString(@"artists", @"Artists")
440 [tempItem setSubmenu:_artistsMenu];
443 itemEnum = [[_eqMenu itemArray] objectEnumerator];
444 while ( (tempItem = [itemEnum nextObject]) ) {
445 [tempItem setState:NSOffState];
449 ITDebugLog(@"Finished building menu.");
450 [_currentMenu release];
455 - (NSMenu *)menuForNoPlayer
457 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
458 id <NSMenuItem> tempItem = nil;
459 ITDebugLog(@"Creating menu for when player isn't running.");
461 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
462 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
464 [[MainController sharedController] networkError:localException];
466 [tempItem setTag:MTMenuShowPlayerItem];
467 [tempItem setTarget:self];
468 ITDebugLog(@"Add a separator menu item.");
469 [menu addItem:[NSMenuItem separatorItem]];
470 ITDebugLog(@"Add \"Preferences...\" menu item.");
471 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
472 [tempItem setTag:MTMenuPreferencesItem];
473 [tempItem setTarget:self];
474 if ([[MainController sharedController] blingBling] == NO) {
475 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
476 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
477 [tempItem setTag:MTMenuRegisterItem];
478 [tempItem setTarget:self];
480 ITDebugLog(@"Add \"Quit\" menu item.");
481 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
482 [tempItem setTag:MTMenuQuitItem];
483 [tempItem setTarget:self];
484 return [menu autorelease];
487 - (void)rebuildSubmenus
489 NSArray *menu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
490 ITDebugLog(@"Rebuilding all of the submenus.");
492 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
493 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
494 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
496 [[MainController sharedController] networkError:localException];
498 ITDebugLog(@"Releasing old submenus.");
499 ITDebugLog(@" - Rating menu");
500 [_ratingMenu release];
501 ITDebugLog(@" - Upcoming songs menu");
502 [_upcomingSongsMenu release];
503 ITDebugLog(@" - Playlists menu");
504 [_playlistsMenu release];
505 ITDebugLog(@" - EQ menu");
508 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
509 _ratingMenu = [self ratingMenu];
510 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
511 _upcomingSongsMenu = [self upcomingSongsMenu];
512 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
513 _playlistsMenu = [self playlistsMenu];
514 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
515 _eqMenu = [self eqMenu];
517 if ([menu containsObject:@"artists"]) {
518 ITDebugLog(@"Releasing artists menu");
519 [_artistsMenu release];
520 ITDebugLog(@"Beginning Rebuild of \"Artists\" submenu.");
521 _artistsMenu = [self artistsMenu];
523 ITDebugLog(@"Done rebuilding all of the submenus.");
526 - (NSMenu *)ratingMenu
528 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
529 NSEnumerator *itemEnum;
532 SEL itemSelector = @selector(performRatingMenuAction:);
534 ITDebugLog(@"Building \"Song Rating\" menu.");
536 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
537 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
538 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
539 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
540 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
541 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
543 itemEnum = [[ratingMenu itemArray] objectEnumerator];
544 while ( (anItem = [itemEnum nextObject]) ) {
545 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
546 [anItem setAction:itemSelector];
547 [anItem setTarget:self];
548 [anItem setTag:itemTag];
551 ITDebugLog(@"Done Building \"Song Rating\" menu.");
555 - (NSMenu *)upcomingSongsMenu
557 NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
558 int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
561 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
563 [[MainController sharedController] networkError:localException];
566 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
567 if (_currentPlaylist && !_playingRadio) {
570 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
572 NSString *curSong = nil;
574 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
576 [[MainController sharedController] networkError:localException];
578 id <NSMenuItem> songItem;
579 ITDebugLog(@"Adding song: %@", curSong);
580 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
582 [songItem setTarget:self];
589 if ([upcomingSongsMenu numberOfItems] == 0) {
590 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
593 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
594 return upcomingSongsMenu;
597 /*- (NSMenu *)playlistsMenu
599 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
601 id <NSMenuItem> tempItem;
602 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
605 playlists = [[[MainController sharedController] currentRemote] playlists];
607 [[MainController sharedController] networkError:localException];
610 ITDebugLog(@"Building \"Playlists\" menu.");
612 for (i = 0; i < [playlists count]; i++) {
613 NSString *curPlaylist = [playlists objectAtIndex:i];
614 ITDebugLog(@"Adding playlist: %@", curPlaylist);
615 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
616 [tempItem setTag:i + 1];
617 [tempItem setTarget:self];
620 if (source == ITMTRemoteRadioSource) {
621 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
622 } else if (source == ITMTRemoteGenericDeviceSource) {
623 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
624 } else if (source == ITMTRemoteiPodSource) {
625 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
626 } else if (source == ITMTRemoteCDSource) {
627 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
628 } else if (source == ITMTRemoteSharedLibrarySource) {
629 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
630 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
631 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
633 ITDebugLog(@"Done Building \"Playlists\" menu");
634 return playlistsMenu;
638 - (NSMenu *)playlistsMenu
640 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
641 NSArray *playlists = nil;
642 id <NSMenuItem> tempItem;
643 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
645 NSMutableArray *indices = [[NSMutableArray alloc] init];
647 playlists = [[[MainController sharedController] currentRemote] playlists];
649 [[MainController sharedController] networkError:localException];
651 ITDebugLog(@"Building \"Playlists\" menu.");
653 NSArray *curPlaylist = [playlists objectAtIndex:0];
654 NSString *name = [curPlaylist objectAtIndex:0];
655 ITDebugLog(@"Adding main source: %@", name);
656 for (i = 3; i < [curPlaylist count]; i++) {
657 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
658 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
659 [tempItem setTag:i - 1];
660 [tempItem setTarget:self];
662 ITDebugLog(@"Adding index to the index array.");
663 [indices addObject:[curPlaylist objectAtIndex:2]];
665 if ([playlists count] > 1) {
666 if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
667 [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
668 if (source == ITMTRemoteRadioSource) {
669 [playlistsMenu addItem:[NSMenuItem separatorItem]];
670 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
673 [playlistsMenu addItem:[NSMenuItem separatorItem]];
677 if ([playlists count] > 1) {
678 for (i = 1; i < [playlists count]; i++) {
679 NSArray *curPlaylist = [playlists objectAtIndex:i];
680 if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
681 NSString *name = [curPlaylist objectAtIndex:0];
682 NSMenu *submenu = [[NSMenu alloc] init];
683 ITDebugLog(@"Adding source: %@", name);
685 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
686 ITDebugLog(@"Invalid iPod source.");
687 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
689 for (j = 3; j < [curPlaylist count]; j++) {
690 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
691 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
692 [tempItem setTag:(i * 1000) + j - 1];
693 [tempItem setTarget:self];
695 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
697 ITDebugLog(@"Adding index to the index array.");
698 [indices addObject:[curPlaylist objectAtIndex:2]];
702 ITDebugLog(@"Checking the current source.");
703 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
704 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
705 [tempItem setState:NSOnState];
706 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
707 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
708 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
711 tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
712 [tempItem setTarget:self];
713 [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
714 ITDebugLog(@"Done Building \"Playlists\" menu");
715 return playlistsMenu;
720 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
721 NSArray *eqPresets = nil;
722 id <NSMenuItem> tempItem;
726 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
728 [[MainController sharedController] networkError:localException];
731 ITDebugLog(@"Building \"EQ Presets\" menu.");
733 tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
734 [tempItem setTag:-1];
735 [tempItem setTarget:self];
737 [tempItem setState:[[[MainController sharedController] currentRemote] equalizerEnabled] ? NSOnState : NSOffState];
739 [[MainController sharedController] networkError:localException];
741 [eqMenu addItem:[NSMenuItem separatorItem]];
743 for (i = 0; i < [eqPresets count]; i++) {
745 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
746 ITDebugLog(@"Adding EQ Preset: %@", name);
747 tempItem = [eqMenu addItemWithTitle:name
748 action:@selector(performEqualizerMenuAction:)
751 [tempItem setTarget:self];
754 ITDebugLog(@"Done Building \"EQ Presets\" menu");
758 - (NSMenu *)artistsMenu
760 NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@""];
761 NSEnumerator *artistsEnumerator;
762 NSString *nextArtist;
763 id <NSMenuItem> tempItem;
764 ITDebugLog(@"Building \"Artists\" menu.");
766 artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
767 while ( (nextArtist = [artistsEnumerator nextObject]) ) {
768 tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performArtistsMenuAction:) keyEquivalent:@""];
769 [tempItem setRepresentedObject:nextArtist];
770 [tempItem setTarget:self];
773 [[MainController sharedController] networkError:localException];
775 ITDebugLog(@"Done Building \"Artists\" menu");
779 - (void)performMainMenuAction:(id)sender
781 switch ( [sender tag] )
783 case MTMenuPlayPauseItem:
784 ITDebugLog(@"Performing Menu Action: Play/Pause");
785 [[MainController sharedController] playPause];
787 case MTMenuFastForwardItem:
788 ITDebugLog(@"Performing Menu Action: Fast Forward");
789 [[MainController sharedController] fastForward];
791 case MTMenuRewindItem:
792 ITDebugLog(@"Performing Menu Action: Rewind");
793 [[MainController sharedController] rewind];
795 case MTMenuPreviousTrackItem:
796 ITDebugLog(@"Performing Menu Action: Previous Track");
797 [[MainController sharedController] prevSong];
799 case MTMenuNextTrackItem:
800 ITDebugLog(@"Performing Menu Action: Next Track");
801 [[MainController sharedController] nextSong];
803 case MTMenuShowPlayerItem:
804 ITDebugLog(@"Performing Menu Action: Show Main Interface");
805 [[MainController sharedController] showPlayer];
807 case MTMenuPreferencesItem:
808 ITDebugLog(@"Performing Menu Action: Preferences...");
809 [[MainController sharedController] showPreferences];
812 ITDebugLog(@"Performing Menu Action: Quit");
813 [[MainController sharedController] quitMenuTunes];
815 case MTMenuRegisterItem:
816 ITDebugLog(@"Performing Menu Action: Register");
817 [[MainController sharedController] blingNow];
820 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
825 - (void)performRatingMenuAction:(id)sender
827 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
828 [[MainController sharedController] selectSongRating:[sender tag]];
831 - (void)performPlaylistMenuAction:(id)sender
833 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
834 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
837 - (void)performEqualizerMenuAction:(id)sender
839 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
840 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
843 - (void)performUpcomingSongsMenuAction:(id)sender
845 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
846 [[MainController sharedController] selectSongAtIndex:[sender tag]];
849 - (void)performArtistsMenuAction:(id)sender
851 ITDebugLog(@"Artist action selected on item with object %i", [sender representedObject]);
857 //[[MainController sharedController] createAndPlayPlaylistWithTerm:[sender representedObject] ofType:1];
862 ITDebugLog(@"Update Menu");
863 [_currentMenu update];
866 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
871 //This is never used I know, keep it though
872 - (NSString *)systemUIColor
874 NSDictionary *tmpDict;
876 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
877 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
878 if ( ([tmpNumber intValue] == 1) ) {
891 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
892 onItem:(id <NSMenuItem>)item
894 unichar charcode = 'a';
896 long cocoaModifiers = 0;
897 static long carbonToCocoa[6][2] =
899 { cmdKey, NSCommandKeyMask },
900 { optionKey, NSAlternateKeyMask },
901 { controlKey, NSControlKeyMask },
902 { shiftKey, NSShiftKeyMask },
905 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
907 for (i = 0; i < 6; i++) {
908 if (modifiers & carbonToCocoa[i][0]) {
909 cocoaModifiers += carbonToCocoa[i][1];
912 [item setKeyEquivalentModifierMask:cocoaModifiers];
914 //Missing key combos for some keys. Must find them later.
918 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
923 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
930 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
931 // Haven't tested this, though it should work.
932 // This doesn't work. :'(
934 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
936 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
937 ITDebugLog(@"%@", menuRef);
938 SetMenuItemCommandKey(menuRef, 0, NO, 49);
939 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
940 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
943 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
949 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
950 charcode = NSDeleteFunctionKey;
954 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
959 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
964 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
969 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
970 charcode = NSF5FunctionKey;
974 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
975 charcode = NSF6FunctionKey;
979 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
980 charcode = NSF7FunctionKey;
984 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
985 charcode = NSF3FunctionKey;
989 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
990 charcode = NSF8FunctionKey;
994 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
995 charcode = NSF9FunctionKey;
999 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1000 charcode = NSF11FunctionKey;
1004 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1005 charcode = NSF13FunctionKey;
1009 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1010 charcode = NSF14FunctionKey;
1014 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1015 charcode = NSF10FunctionKey;
1019 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1020 charcode = NSF12FunctionKey;
1024 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1025 charcode = NSF13FunctionKey;
1029 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1030 charcode = NSInsertFunctionKey;
1034 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1035 charcode = NSHomeFunctionKey;
1039 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1040 charcode = NSPageUpFunctionKey;
1044 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1045 charcode = NSDeleteFunctionKey;
1049 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1050 charcode = NSF4FunctionKey;
1054 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1055 charcode = NSEndFunctionKey;
1059 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1060 charcode = NSF2FunctionKey;
1064 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1065 charcode = NSPageDownFunctionKey;
1069 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1070 charcode = NSF1FunctionKey;
1074 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1075 charcode = NSLeftArrowFunctionKey;
1079 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1080 charcode = NSRightArrowFunctionKey;
1084 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1085 charcode = NSDownArrowFunctionKey;
1089 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1090 charcode = NSUpArrowFunctionKey;
1094 if (charcode == 'a') {
1095 unsigned long state;
1100 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1101 keyTrans = KeyTranslate(kchr, code, &state);
1102 charCode = keyTrans;
1103 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1104 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1105 } else if (charcode != 'b') {
1106 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1108 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1111 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1113 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1114 NSEnumerator *volEnum = [volumes objectEnumerator];
1115 NSString *nextVolume;
1116 ITDebugLog(@"Looking for an iPod named %@", name);
1117 while ( (nextVolume = [volEnum nextObject]) ) {
1118 ITDebugLog(@"- %@", nextVolume);
1119 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1120 NSFileHandle *handle;
1122 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1123 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1124 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1127 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1128 ITDebugLog(@"File handle: %@", handle);
1129 [handle seekToFileOffset:10];
1130 data = [handle readDataOfLength:1];
1131 ITDebugLog(@"Data: %@", data);
1132 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1133 ITDebugLog(@"iPod is manually updated. %@", path);
1135 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1136 ITDebugLog(@"iPod is automatically updated. %@", path);
1139 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);