1 #import "MenuController.h"
2 #import "MainController.h"
3 #import "NetworkController.h"
5 #import "PlaylistNode.h"
6 #import <ITFoundation/ITDebug.h>
7 #import <ITKit/ITHotKeyCenter.h>
8 #import <ITKit/ITHotKey.h>
9 #import <ITKit/ITKeyCombo.h>
10 #import <ITKit/ITCategory-NSMenu.h>
11 #import <ITKit/ITAboutWindowController.h>
13 @interface MenuController (SubmenuMethods)
14 - (NSMenu *)ratingMenu;
15 - (NSMenu *)upcomingSongsMenu;
16 - (NSMenu *)playlistsMenu;
18 - (NSMenu *)artistsMenu;
19 - (NSMenu *)albumsMenu;
20 - (void)playlistsMenuAux:(NSMenu *)menu node:(PlaylistNode *)node tagPrefix:(int)p;
21 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
22 onItem:(id <NSMenuItem>)item;
23 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
26 @implementation MenuController
30 if ( (self = [super init]) ) {
31 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
38 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
39 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
40 NSArray *menuArray = [defaults arrayForKey:@"menu"];
41 NSEnumerator *enumerator = [menuArray objectEnumerator];
43 id <NSMenuItem> tempItem;
44 NSEnumerator *itemEnum;
46 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
47 ITMTRemote *mtr = [[MainController sharedController] currentRemote];
48 int currentSongRating = 0;
52 _currentPlaylist = [mtr currentPlaylistIndex];
53 _playingRadio = ([mtr currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
54 currentSongRating = ( [mtr currentSongRating] != -1 );
56 [[MainController sharedController] networkError:localException];
59 ITDebugLog(@"Reset menu if required.");
61 //Kill the old submenu items
62 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
63 ITDebugLog(@"Removing \"Song Rating\" submenu.");
64 [tempItem setSubmenu:nil];
67 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
68 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
69 [tempItem setSubmenu:nil];
72 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
73 ITDebugLog(@"Removing \"Playlists\" submenu.");
74 [tempItem setSubmenu:nil];
77 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
78 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
79 [tempItem setSubmenu:nil];
82 if ( (tempItem = [_currentMenu itemWithTag:5]) ) {
83 ITDebugLog(@"Removing \"Artists\" submenu.");
84 [tempItem setSubmenu:nil];
87 if ( (tempItem = [_currentMenu itemWithTag:6]) ) {
88 ITDebugLog(@"Removing \"Albums\" submenu.");
89 [tempItem setSubmenu:nil];
92 ITDebugLog(@"Begin building menu.");
95 while ( (nextObject = [enumerator nextObject]) ) {
97 if ([nextObject isEqualToString:@"playPause"]) {
98 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
99 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
100 action:@selector(performMainMenuAction:)
102 [tempItem setTag:MTMenuPlayPauseItem];
103 [tempItem setTarget:self];
105 itemEnum = [hotKeys objectEnumerator];
106 while ( (hotKey = [itemEnum nextObject]) ) {
107 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
108 ITKeyCombo *combo = [hotKey keyCombo];
109 [self setKeyEquivalentForCode:[combo keyCode]
110 andModifiers:[combo modifiers]
115 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
117 switch ([mtr playerPlayingState]) {
118 case ITMTRemotePlayerPlaying:
119 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
121 case ITMTRemotePlayerRewinding:
122 case ITMTRemotePlayerForwarding:
123 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
129 [[MainController sharedController] networkError:localException];
131 } else if ([nextObject isEqualToString:@"nextTrack"]) {
132 ITDebugLog(@"Add \"Next Track\" menu item.");
133 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
134 action:@selector(performMainMenuAction:)
137 itemEnum = [hotKeys objectEnumerator];
138 while ( (hotKey = [itemEnum nextObject]) ) {
139 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
140 ITKeyCombo *combo = [hotKey keyCombo];
141 [self setKeyEquivalentForCode:[combo keyCode]
142 andModifiers:[combo modifiers]
147 if (_currentPlaylist) {
148 [tempItem setTag:MTMenuNextTrackItem];
149 [tempItem setTarget:self];
151 } else if ([nextObject isEqualToString:@"prevTrack"]) {
152 ITDebugLog(@"Add \"Previous Track\" menu item.");
153 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
154 action:@selector(performMainMenuAction:)
157 itemEnum = [hotKeys objectEnumerator];
158 while ( (hotKey = [itemEnum nextObject]) ) {
159 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
160 ITKeyCombo *combo = [hotKey keyCombo];
161 [self setKeyEquivalentForCode:[combo keyCode]
162 andModifiers:[combo modifiers]
167 if (_currentPlaylist) {
168 [tempItem setTag:MTMenuPreviousTrackItem];
169 [tempItem setTarget:self];
171 } else if ([nextObject isEqualToString:@"fastForward"]) {
172 ITDebugLog(@"Add \"Fast Forward\" menu item.");
173 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
174 action:@selector(performMainMenuAction:)
176 if (_currentPlaylist) {
177 [tempItem setTag:MTMenuFastForwardItem];
178 [tempItem setTarget:self];
180 } else if ([nextObject isEqualToString:@"rewind"]) {
181 ITDebugLog(@"Add \"Rewind\" menu item.");
182 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
183 action:@selector(performMainMenuAction:)
185 if (_currentPlaylist) {
186 [tempItem setTag:MTMenuRewindItem];
187 [tempItem setTarget:self];
189 } else if ([nextObject isEqualToString:@"showPlayer"]) {
190 ITDebugLog(@"Add \"Show Player\" menu item.");
192 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
193 NSLocalizedString(@"show", @"Show"),
194 [mtr playerSimpleName]]
195 action:@selector(performMainMenuAction:)
198 [[MainController sharedController] networkError:localException];
201 itemEnum = [hotKeys objectEnumerator];
202 while ( (hotKey = [itemEnum nextObject]) ) {
203 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
204 ITKeyCombo *combo = [hotKey keyCombo];
205 [self setKeyEquivalentForCode:[combo keyCode]
206 andModifiers:[combo modifiers]
211 [tempItem setTarget:self];
212 [tempItem setTag:MTMenuShowPlayerItem];
213 } else if ([nextObject isEqualToString:@"preferences"]) {
214 ITDebugLog(@"Add \"Preferences...\" menu item.");
215 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
216 action:@selector(performMainMenuAction:)
218 [tempItem setTag:MTMenuPreferencesItem];
219 [tempItem setTarget:self];
220 } else if ([nextObject isEqualToString:@"about"]) {
221 ITDebugLog(@"Add \"About MenuTunes...\" menu item.");
222 tempItem = [menu addItemWithTitle:NSLocalizedString(@"about", @"About MenuTunes...")
223 action:@selector(performMainMenuAction:)
225 [tempItem setTag:MTMenuAboutItem];
226 [tempItem setTarget:self];
227 } else if ([nextObject isEqualToString:@"quit"]) {
228 ITDebugLog(@"Add \"Quit\" menu item.");
229 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
230 action:@selector(performMainMenuAction:)
232 [tempItem setTag:MTMenuQuitItem];
233 [tempItem setTarget:self];
234 } else if ([nextObject isEqualToString:@"trackInfo"]) {
235 ITDebugLog(@"Check to see if a Track is playing...");
236 //Handle playing radio too
237 if (_currentTrack != -1 && _currentPlaylist > 0) {
238 NSString *title = nil;
240 title = [mtr currentSongTitle];
242 [[MainController sharedController] networkError:localException];
244 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
245 ITDebugLog(@"Add \"Now Playing\" menu item.");
246 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
248 if ([title length] > 0) {
249 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
251 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
254 if (!_playingRadio) {
255 if ([defaults boolForKey:@"showAlbum"]) {
256 NSString *curAlbum = nil;
258 curAlbum = [mtr currentSongAlbum];
260 [[MainController sharedController] networkError:localException];
262 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
265 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
269 if ([defaults boolForKey:@"showArtist"]) {
270 NSString *curArtist = nil;
272 curArtist = [mtr currentSongArtist];
274 [[MainController sharedController] networkError:localException];
276 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
279 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
283 if ([defaults boolForKey:@"showComposer"]) {
284 NSString *curComposer = nil;
286 curComposer = [mtr currentSongComposer];
288 [[MainController sharedController] networkError:localException];
290 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
293 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
297 if ([defaults boolForKey:@"showTrackNumber"]) {
300 track = [mtr currentSongTrack];
302 [[MainController sharedController] networkError:localException];
304 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
307 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
313 if ([defaults boolForKey:@"showTime"] && ( ([mtr currentSongElapsed] != nil) || ([mtr currentSongLength] != nil) )) {
314 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [mtr currentSongElapsed], [mtr currentSongLength]);
315 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [mtr currentSongElapsed], [mtr currentSongLength]] action:nil keyEquivalent:@""]];
318 [[MainController sharedController] networkError:localException];
321 if (!_playingRadio) {
323 if ([defaults boolForKey:@"showPlayCount"] && [mtr currentSource] == ITMTRemoteLibrarySource) {
324 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"Play Count: %i", [mtr currentSongPlayCount]] action:nil keyEquivalent:@""]];
326 if ([defaults boolForKey:@"showTrackRating"] && ( [mtr currentSongRating] != -1.0 )) {
327 NSString *string = nil;
328 switch ((int)([mtr currentSongRating] * 5)) {
330 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
333 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
336 string = [NSString stringWithUTF8String:"★★☆☆☆"];
339 string = [NSString stringWithUTF8String:"★★★☆☆"];
342 string = [NSString stringWithUTF8String:"★★★★☆"];
345 string = [NSString stringWithUTF8String:"★★★★★"];
348 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
349 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
352 [[MainController sharedController] networkError:localException];
355 /*if ([tempItem respondsToSelector:@selector(setAttributedTitle:)] && [defaults boolForKey:@"showAlbumArtwork"] && ![[NetworkController sharedController] isConnectedToServer]) {
356 NSImage *image = [mtr currentSongAlbumArt];
358 NSSize oldSize, newSize;
359 oldSize = [image size];
360 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
361 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
362 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
364 tempItem = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
365 NSTextAttachment *attachment = [[[NSTextAttachment alloc] init] autorelease];
366 [[attachment attachmentCell] setImage:image];
367 NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
368 [tempItem setAttributedTitle:attrString];
373 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
374 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
376 } else if ([nextObject isEqualToString:@"separator"]) {
377 ITDebugLog(@"Add a separator menu item.");
378 [menu addItem:[NSMenuItem separatorItem]];
380 } else if ([nextObject isEqualToString:@"playlists"]) {
381 ITDebugLog(@"Add \"Playlists\" submenu.");
382 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
385 [tempItem setSubmenu:_playlistsMenu];
387 } else if ([nextObject isEqualToString:@"eqPresets"]) {
388 ITDebugLog(@"Add \"EQ Presets\" submenu.");
389 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
392 [tempItem setSubmenu:_eqMenu];
395 itemEnum = [[_eqMenu itemArray] objectEnumerator];
396 while ( (tempItem = [itemEnum nextObject]) ) {
397 [tempItem setState:NSOffState];
400 [[_eqMenu itemAtIndex:0] setState:[mtr equalizerEnabled] ? NSOnState : NSOffState];
401 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] + 1)] setState:NSOnState];
403 [[MainController sharedController] networkError:localException];
405 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
406 ITDebugLog(@"Add \"Song Rating\" submenu.");
407 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
410 [tempItem setSubmenu:_ratingMenu];
412 if (_playingRadio || !_currentPlaylist) {
413 [tempItem setEnabled:NO];
416 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
417 while ( (tempItem = [itemEnum nextObject]) ) {
418 [tempItem setState:NSOffState];
422 [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
424 [[MainController sharedController] networkError:localException];
426 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
427 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
428 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
431 [tempItem setSubmenu:_upcomingSongsMenu];
433 if (_playingRadio || _currentPlaylist < 1) {
434 [tempItem setEnabled:NO];
436 } else if ([nextObject isEqualToString:@"artists"]) {
437 ITDebugLog(@"Add \"Artists\" submenu.");
438 tempItem = [menu addItemWithTitle:NSLocalizedString(@"artists", @"Artists")
441 [tempItem setSubmenu:_artistsMenu];
443 } else if ([nextObject isEqualToString:@"albums"]) {
444 ITDebugLog(@"Add \"Albums\" submenu.");
445 tempItem = [menu addItemWithTitle:NSLocalizedString(@"albums", @"Albums")
448 [tempItem setSubmenu:_albumsMenu];
452 ITDebugLog(@"Finished building menu.");
453 [_currentMenu release];
458 - (NSMenu *)menuForNoPlayer
460 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
461 id <NSMenuItem> tempItem = nil;
462 ITDebugLog(@"Creating menu for when player isn't running.");
464 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
465 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
467 [[MainController sharedController] networkError:localException];
469 [tempItem setTag:MTMenuShowPlayerItem];
470 [tempItem setTarget:self];
471 ITDebugLog(@"Add a separator menu item.");
472 [menu addItem:[NSMenuItem separatorItem]];
473 ITDebugLog(@"Add \"Preferences...\" menu item.");
474 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
475 [tempItem setTag:MTMenuPreferencesItem];
476 [tempItem setTarget:self];
477 ITDebugLog(@"Add \"Quit\" menu item.");
478 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
479 [tempItem setTag:MTMenuQuitItem];
480 [tempItem setTarget:self];
481 return [menu autorelease];
484 - (BOOL)rebuildSubmenus
486 NSArray *menu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
487 ITDebugLog(@"Rebuilding all of the submenus.");
489 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
490 if (_currentTrack > -1) {
491 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
493 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
495 [[MainController sharedController] networkError:localException];
497 ITDebugLog(@"Releasing old submenus.");
499 ITDebugLog(@" - Rating menu");
500 [_ratingMenu release];
502 ITDebugLog(@" - Upcoming songs menu");
503 [_upcomingSongsMenu release];
504 _upcomingSongsMenu = nil;
505 ITDebugLog(@" - Playlists menu");
506 [_playlistsMenu release];
507 _playlistsMenu = nil;
508 ITDebugLog(@" - EQ menu");
512 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
513 _ratingMenu = [self ratingMenu];
514 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
515 _upcomingSongsMenu = [self upcomingSongsMenu];
517 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
518 _playlistsMenu = [self playlistsMenu];
521 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
522 _eqMenu = [self eqMenu];
524 if (_continue && [menu containsObject:@"artists"]) {
525 ITDebugLog(@"Releasing artists menu");
526 [_artistsMenu release];
527 ITDebugLog(@"Beginning Rebuild of \"Artists\" submenu.");
528 _artistsMenu = [self artistsMenu];
531 if (_continue && [menu containsObject:@"albums"]) {
532 ITDebugLog(@"Releasing albums menu");
533 [_albumsMenu release];
534 ITDebugLog(@"Beginning Rebuild of \"Albums\" submenu.");
535 _albumsMenu = [self albumsMenu];
537 ITDebugLog(@"Done rebuilding all of the submenus.");
541 - (NSMenu *)ratingMenu
543 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
544 NSEnumerator *itemEnum;
547 SEL itemSelector = @selector(performRatingMenuAction:);
549 ITDebugLog(@"Building \"Song Rating\" menu.");
551 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
552 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
553 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
554 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
555 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
556 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
558 itemEnum = [[ratingMenu itemArray] objectEnumerator];
559 while ( (anItem = [itemEnum nextObject]) ) {
560 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
561 [anItem setAction:itemSelector];
562 [anItem setTarget:self];
563 [anItem setTag:itemTag];
566 ITDebugLog(@"Done Building \"Song Rating\" menu.");
570 - (NSMenu *)upcomingSongsMenu
572 NSMenu *upcomingSongsMenu;
573 int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
574 if (_currentTrack == -1) {
578 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
580 [[MainController sharedController] networkError:localException];
583 if (numSongs == -1) {
586 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
588 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
589 if (_currentPlaylist && !_playingRadio) {
592 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance && i <= numSongs; i++) {
595 //Check if the song at this index is enabled for playback. If it isn't, skip over it
597 enabled = [[[MainController sharedController] currentRemote] songEnabledAtIndex:i];
599 [[MainController sharedController] networkError:localException];
603 NSString *curSong = nil;
605 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
607 [[MainController sharedController] networkError:localException];
609 id <NSMenuItem> songItem;
610 ITDebugLog(@"Adding song: %@", curSong);
611 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
613 [songItem setTarget:self];
620 if ([upcomingSongsMenu numberOfItems] == 0) {
621 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
624 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
625 NS_VALUERETURN(upcomingSongsMenu, NSMenu *);
627 [upcomingSongsMenu release];
629 NS_VALUERETURN(nil, NSMenu *);
633 /*- (NSMenu *)playlistsMenu
635 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
637 id <NSMenuItem> tempItem;
638 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
641 playlists = [[[MainController sharedController] currentRemote] playlists];
643 [[MainController sharedController] networkError:localException];
646 ITDebugLog(@"Building \"Playlists\" menu.");
648 for (i = 0; i < [playlists count]; i++) {
649 NSString *curPlaylist = [playlists objectAtIndex:i];
650 ITDebugLog(@"Adding playlist: %@", curPlaylist);
651 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
652 [tempItem setTag:i + 1];
653 [tempItem setTarget:self];
656 if (source == ITMTRemoteRadioSource) {
657 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
658 } else if (source == ITMTRemoteGenericDeviceSource) {
659 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
660 } else if (source == ITMTRemoteiPodSource) {
661 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
662 } else if (source == ITMTRemoteCDSource) {
663 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
664 } else if (source == ITMTRemoteSharedLibrarySource) {
665 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
666 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
667 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
669 ITDebugLog(@"Done Building \"Playlists\" menu");
670 return playlistsMenu;
673 - (void)playlistsMenuAux:(NSMenu *)menu node:(PlaylistNode *)node tagPrefix:(int)p
675 id <NSMenuItem> tempItem;
678 for (i = 0; i < [[node children] count]; i++) {
679 PlaylistNode *nextNode = [[node children] objectAtIndex:i];
680 if ([nextNode type] == ITMTFolderNode) {
681 NSMenu *submenu = [[NSMenu alloc] init];
682 tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
683 [tempItem setTag:p + [nextNode index] + 1];
684 [tempItem setTarget:self];
685 [tempItem setSubmenu:submenu];
686 [self playlistsMenuAux:[submenu autorelease] node:nextNode tagPrefix:p];
688 tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
689 [tempItem setTag:p + [nextNode index] + 1];
690 [tempItem setTarget:self];
693 PlaylistNode *root = node;
694 while ([root type] == ITMTPlaylistNode || [root type] == ITMTFolderNode) {
695 root = [root parent];
698 if ([root index] == [[[MainController sharedController] currentRemote] currentSourceIndex] && [nextNode index] == _currentPlaylist) {
699 [tempItem setState:NSOnState];
704 - (NSMenu *)playlistsMenu
706 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
707 NSArray *playlists = nil;
708 id <NSMenuItem> tempItem;
709 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
711 NSMutableArray *indices = [[NSMutableArray alloc] init];
713 playlists = [[[MainController sharedController] currentRemote] playlists];
715 [[MainController sharedController] networkError:localException];
719 [playlistsMenu release];
723 ITDebugLog(@"Building \"Playlists\" menu.");
725 //First we add the main Library source, since it is guaranteed to be there.
726 PlaylistNode *library = [playlists objectAtIndex:0];
727 ITDebugLog(@"Adding main source: %@", [library name]);
728 [self playlistsMenuAux:playlistsMenu node:library tagPrefix:0];
729 ITDebugLog(@"Adding index to the index array.");
730 [indices addObject:[NSNumber numberWithInt:[library index]]];
733 //Next go through the other sources
734 if ([playlists count] > 1) {
735 //Add the radio source if it is playing
736 if ([[playlists objectAtIndex:1] sourceType] == ITMTRemoteRadioSource) {
737 [indices addObject:[NSNumber numberWithInt:[[playlists objectAtIndex:1] index]]];
738 if (source == ITMTRemoteRadioSource) {
739 [playlistsMenu addItem:[NSMenuItem separatorItem]];
740 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
741 } else if ([playlists count] > 2) {
742 [playlistsMenu addItem:[NSMenuItem separatorItem]];
746 //Add other sources as needed (shared music, iPods, CDs)
747 for (i = [playlists count] - 1; i > 1 ; i--) {
748 PlaylistNode *nextSource = [playlists objectAtIndex:i];
749 if ([nextSource type] != ITMTRemoteRadioSource) {
750 NSString *name = [nextSource name];
751 ITDebugLog(@"Adding source: %@", name);
753 if ( ([nextSource type] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
754 ITDebugLog(@"Invalid iPod source.");
755 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
757 NSMenu *menu = [[NSMenu alloc] init];
758 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[menu autorelease]];
759 [self playlistsMenuAux:menu node:nextSource tagPrefix:(i * 1000)];
761 ITDebugLog(@"Adding index to the index array.");
762 [indices addObject:[NSNumber numberWithInt:[nextSource index]]];
767 if (_currentPlaylist != -1) {
768 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
769 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
770 [tempItem setState:NSOnState];
776 tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
777 [tempItem setTarget:self];
778 [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
779 ITDebugLog(@"Done Building \"Playlists\" menu");
780 NS_VALUERETURN(playlistsMenu, NSMenu *);
782 [playlistsMenu release];
784 NS_VALUERETURN(nil, NSMenu *);
790 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
791 NSArray *eqPresets = nil;
792 id <NSMenuItem> tempItem;
796 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
798 [[MainController sharedController] networkError:localException];
801 ITDebugLog(@"Building \"EQ Presets\" menu.");
803 tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
804 [tempItem setTag:-1];
805 [tempItem setTarget:self];
806 [eqMenu addItem:[NSMenuItem separatorItem]];
808 for (i = 0; i < [eqPresets count]; i++) {
810 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
811 ITDebugLog(@"Adding EQ Preset: %@", name);
812 tempItem = [eqMenu addItemWithTitle:name
813 action:@selector(performEqualizerMenuAction:)
816 [tempItem setTarget:self];
819 ITDebugLog(@"Done Building \"EQ Presets\" menu");
823 - (NSMenu *)artistsMenu
825 NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@"Artists"];
826 NSEnumerator *artistsEnumerator;
827 NSString *nextArtist;
828 id <NSMenuItem> tempItem;
829 ITDebugLog(@"Building \"Artists\" menu.");
831 artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
832 while ( (nextArtist = [artistsEnumerator nextObject]) ) {
833 tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
834 [tempItem setTarget:self];
837 [[MainController sharedController] networkError:localException];
839 ITDebugLog(@"Done Building \"Artists\" menu");
843 - (NSMenu *)albumsMenu
845 NSMenu *albumsMenu = [[NSMenu alloc] initWithTitle:@"Albums"];
846 NSEnumerator *albumsEnumerator;
848 id <NSMenuItem> tempItem;
849 ITDebugLog(@"Building \"Albums\" menu.");
851 albumsEnumerator = [[[[MainController sharedController] currentRemote] albums] objectEnumerator];
852 while ( (nextAlbum = [albumsEnumerator nextObject]) ) {
853 tempItem = [albumsMenu addItemWithTitle:nextAlbum action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
854 [tempItem setTarget:self];
857 [[MainController sharedController] networkError:localException];
859 ITDebugLog(@"Done Building \"Albums\" menu");
863 - (void)performMainMenuAction:(id)sender
865 switch ( [sender tag] )
867 case MTMenuPlayPauseItem:
868 ITDebugLog(@"Performing Menu Action: Play/Pause");
869 [[MainController sharedController] playPause];
871 case MTMenuFastForwardItem:
872 ITDebugLog(@"Performing Menu Action: Fast Forward");
873 [[MainController sharedController] fastForward];
875 case MTMenuRewindItem:
876 ITDebugLog(@"Performing Menu Action: Rewind");
877 [[MainController sharedController] rewind];
879 case MTMenuPreviousTrackItem:
880 ITDebugLog(@"Performing Menu Action: Previous Track");
881 [[MainController sharedController] prevSong];
883 case MTMenuNextTrackItem:
884 ITDebugLog(@"Performing Menu Action: Next Track");
885 [[MainController sharedController] nextSong];
887 case MTMenuShowPlayerItem:
888 ITDebugLog(@"Performing Menu Action: Show Main Interface");
889 [[MainController sharedController] showPlayer];
891 case MTMenuPreferencesItem:
892 ITDebugLog(@"Performing Menu Action: Preferences...");
893 [[MainController sharedController] showPreferences];
895 case MTMenuAboutItem:
896 ITDebugLog(@"Performing Menu Action: About MenuTunes...");
897 [[ITAboutWindowController sharedController] showAboutWindow];
900 ITDebugLog(@"Performing Menu Action: Quit");
901 [[MainController sharedController] quitMenuTunes];
904 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
909 - (void)performRatingMenuAction:(id)sender
911 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
912 [[MainController sharedController] selectSongRating:[sender tag]];
915 - (void)performPlaylistMenuAction:(id)sender
917 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
918 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
921 - (void)performEqualizerMenuAction:(id)sender
923 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
924 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
927 - (void)performUpcomingSongsMenuAction:(id)sender
929 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
930 [[MainController sharedController] selectSongAtIndex:[sender tag]];
933 - (void)performBrowseMenuAction:(id)sender
935 ITDebugLog(@"Browse action selected on item named %@", [sender title]);
941 [[MainController sharedController] makePlaylistWithTerm:[sender title] ofType:(([[[sender menu] title] isEqualToString:@"Artists"]) ? 1 : 2)];
946 ITDebugLog(@"Update Menu");
947 [_currentMenu update];
950 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
955 //This is never used I know, keep it though
956 - (NSString *)systemUIColor
958 NSDictionary *tmpDict;
960 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
961 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
962 if ( ([tmpNumber intValue] == 1) ) {
975 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
976 onItem:(id <NSMenuItem>)item
978 unichar charcode = 'a';
980 long cocoaModifiers = 0;
981 static long carbonToCocoa[6][2] =
983 { cmdKey, NSCommandKeyMask },
984 { optionKey, NSAlternateKeyMask },
985 { controlKey, NSControlKeyMask },
986 { shiftKey, NSShiftKeyMask },
989 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
991 for (i = 0; i < 6; i++) {
992 if (modifiers & carbonToCocoa[i][0]) {
993 cocoaModifiers += carbonToCocoa[i][1];
996 [item setKeyEquivalentModifierMask:cocoaModifiers];
998 //Missing key combos for some keys. Must find them later.
1002 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
1007 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
1014 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
1015 // Haven't tested this, though it should work.
1016 // This doesn't work. :'(
1018 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
1019 //charcode = buffer;
1020 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1021 ITDebugLog(@"%@", menuRef);
1022 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1023 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1024 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1027 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
1033 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
1034 charcode = NSDeleteFunctionKey;
1038 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
1043 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
1048 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
1053 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
1054 charcode = NSF5FunctionKey;
1058 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
1059 charcode = NSF6FunctionKey;
1063 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
1064 charcode = NSF7FunctionKey;
1068 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
1069 charcode = NSF3FunctionKey;
1073 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
1074 charcode = NSF8FunctionKey;
1078 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
1079 charcode = NSF9FunctionKey;
1083 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1084 charcode = NSF11FunctionKey;
1088 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1089 charcode = NSF13FunctionKey;
1093 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1094 charcode = NSF14FunctionKey;
1098 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1099 charcode = NSF10FunctionKey;
1103 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1104 charcode = NSF12FunctionKey;
1108 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1109 charcode = NSF13FunctionKey;
1113 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1114 charcode = NSInsertFunctionKey;
1118 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1119 charcode = NSHomeFunctionKey;
1123 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1124 charcode = NSPageUpFunctionKey;
1128 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1129 charcode = NSDeleteFunctionKey;
1133 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1134 charcode = NSF4FunctionKey;
1138 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1139 charcode = NSEndFunctionKey;
1143 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1144 charcode = NSF2FunctionKey;
1148 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1149 charcode = NSPageDownFunctionKey;
1153 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1154 charcode = NSF1FunctionKey;
1158 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1159 charcode = NSLeftArrowFunctionKey;
1163 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1164 charcode = NSRightArrowFunctionKey;
1168 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1169 charcode = NSDownArrowFunctionKey;
1173 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1174 charcode = NSUpArrowFunctionKey;
1178 if (charcode == 'a') {
1179 unsigned long state;
1184 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1185 keyTrans = KeyTranslate(kchr, code, &state);
1186 charCode = keyTrans;
1187 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1188 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1189 } else if (charcode != 'b') {
1190 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1192 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1195 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1197 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1198 NSEnumerator *volEnum = [volumes objectEnumerator];
1199 NSString *nextVolume;
1200 ITDebugLog(@"Looking for an iPod named %@", name);
1201 while ( (nextVolume = [volEnum nextObject]) ) {
1202 ITDebugLog(@"- %@", nextVolume);
1203 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1204 NSFileHandle *handle;
1206 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1207 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1208 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1211 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1212 ITDebugLog(@"File handle: %@", handle);
1213 [handle seekToFileOffset:10];
1214 data = [handle readDataOfLength:1];
1215 ITDebugLog(@"Data: %@", data);
1216 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1217 ITDebugLog(@"iPod is manually updated. %@", path);
1219 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1220 ITDebugLog(@"iPod is automatically updated. %@", path);
1223 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);