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 "PlaylistNode.h"
14 #import <ITFoundation/ITDebug.h>
15 #import <ITKit/ITHotKeyCenter.h>
16 #import <ITKit/ITHotKey.h>
17 #import <ITKit/ITKeyCombo.h>
18 #import <ITKit/ITCategory-NSMenu.h>
19 #import <ITKit/ITAboutWindowController.h>
21 @interface MenuController (SubmenuMethods)
22 - (NSMenu *)ratingMenu;
23 - (NSMenu *)upcomingSongsMenu;
24 - (NSMenu *)playlistsMenu;
26 - (NSMenu *)artistsMenu;
27 - (NSMenu *)albumsMenu;
28 - (void)playlistsMenuAux:(NSMenu *)menu node:(PlaylistNode *)node tagPrefix:(int)p;
29 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
30 onItem:(id <NSMenuItem>)item;
31 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
34 @implementation MenuController
38 if ( (self = [super init]) ) {
39 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
46 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
47 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
48 NSArray *menuArray = [defaults arrayForKey:@"menu"];
49 NSEnumerator *enumerator = [menuArray objectEnumerator];
51 id <NSMenuItem> tempItem;
52 NSEnumerator *itemEnum;
54 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
55 ITMTRemote *mtr = [[MainController sharedController] currentRemote];
56 int currentSongRating = 0;
60 _currentPlaylist = [mtr currentPlaylistIndex];
61 _playingRadio = ([mtr currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
62 currentSongRating = ( [mtr currentSongRating] != -1 );
64 [[MainController sharedController] networkError:localException];
67 ITDebugLog(@"Reset menu if required.");
69 //Kill the old submenu items
70 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
71 ITDebugLog(@"Removing \"Song Rating\" submenu.");
72 [tempItem setSubmenu:nil];
75 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
76 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
77 [tempItem setSubmenu:nil];
80 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
81 ITDebugLog(@"Removing \"Playlists\" submenu.");
82 [tempItem setSubmenu:nil];
85 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
86 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
87 [tempItem setSubmenu:nil];
90 if ( (tempItem = [_currentMenu itemWithTag:5]) ) {
91 ITDebugLog(@"Removing \"Artists\" submenu.");
92 [tempItem setSubmenu:nil];
95 if ( (tempItem = [_currentMenu itemWithTag:6]) ) {
96 ITDebugLog(@"Removing \"Albums\" submenu.");
97 [tempItem setSubmenu:nil];
100 ITDebugLog(@"Begin building menu.");
103 while ( (nextObject = [enumerator nextObject]) ) {
105 if ([nextObject isEqualToString:@"playPause"]) {
106 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
107 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
108 action:@selector(performMainMenuAction:)
110 [tempItem setTag:MTMenuPlayPauseItem];
111 [tempItem setTarget:self];
113 itemEnum = [hotKeys objectEnumerator];
114 while ( (hotKey = [itemEnum nextObject]) ) {
115 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
116 ITKeyCombo *combo = [hotKey keyCombo];
117 [self setKeyEquivalentForCode:[combo keyCode]
118 andModifiers:[combo modifiers]
123 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
125 switch ([mtr playerPlayingState]) {
126 case ITMTRemotePlayerPlaying:
127 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
129 case ITMTRemotePlayerRewinding:
130 case ITMTRemotePlayerForwarding:
131 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
137 [[MainController sharedController] networkError:localException];
139 } else if ([nextObject isEqualToString:@"nextTrack"]) {
140 ITDebugLog(@"Add \"Next Track\" menu item.");
141 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
142 action:@selector(performMainMenuAction:)
145 itemEnum = [hotKeys objectEnumerator];
146 while ( (hotKey = [itemEnum nextObject]) ) {
147 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
148 ITKeyCombo *combo = [hotKey keyCombo];
149 [self setKeyEquivalentForCode:[combo keyCode]
150 andModifiers:[combo modifiers]
155 if (_currentPlaylist) {
156 [tempItem setTag:MTMenuNextTrackItem];
157 [tempItem setTarget:self];
159 } else if ([nextObject isEqualToString:@"prevTrack"]) {
160 ITDebugLog(@"Add \"Previous Track\" menu item.");
161 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
162 action:@selector(performMainMenuAction:)
165 itemEnum = [hotKeys objectEnumerator];
166 while ( (hotKey = [itemEnum nextObject]) ) {
167 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
168 ITKeyCombo *combo = [hotKey keyCombo];
169 [self setKeyEquivalentForCode:[combo keyCode]
170 andModifiers:[combo modifiers]
175 if (_currentPlaylist) {
176 [tempItem setTag:MTMenuPreviousTrackItem];
177 [tempItem setTarget:self];
179 } else if ([nextObject isEqualToString:@"fastForward"]) {
180 ITDebugLog(@"Add \"Fast Forward\" menu item.");
181 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
182 action:@selector(performMainMenuAction:)
184 if (_currentPlaylist) {
185 [tempItem setTag:MTMenuFastForwardItem];
186 [tempItem setTarget:self];
188 } else if ([nextObject isEqualToString:@"rewind"]) {
189 ITDebugLog(@"Add \"Rewind\" menu item.");
190 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
191 action:@selector(performMainMenuAction:)
193 if (_currentPlaylist) {
194 [tempItem setTag:MTMenuRewindItem];
195 [tempItem setTarget:self];
197 } else if ([nextObject isEqualToString:@"showPlayer"]) {
198 ITDebugLog(@"Add \"Show Player\" menu item.");
200 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
201 NSLocalizedString(@"show", @"Show"),
202 [mtr playerSimpleName]]
203 action:@selector(performMainMenuAction:)
206 [[MainController sharedController] networkError:localException];
209 itemEnum = [hotKeys objectEnumerator];
210 while ( (hotKey = [itemEnum nextObject]) ) {
211 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
212 ITKeyCombo *combo = [hotKey keyCombo];
213 [self setKeyEquivalentForCode:[combo keyCode]
214 andModifiers:[combo modifiers]
219 [tempItem setTarget:self];
220 [tempItem setTag:MTMenuShowPlayerItem];
221 } else if ([nextObject isEqualToString:@"preferences"]) {
222 ITDebugLog(@"Add \"Preferences...\" menu item.");
223 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
224 action:@selector(performMainMenuAction:)
226 [tempItem setTag:MTMenuPreferencesItem];
227 [tempItem setTarget:self];
228 } else if ([nextObject isEqualToString:@"about"]) {
229 ITDebugLog(@"Add \"About MenuTunes...\" menu item.");
230 tempItem = [menu addItemWithTitle:NSLocalizedString(@"about", @"About MenuTunes...")
231 action:@selector(performMainMenuAction:)
233 [tempItem setTag:MTMenuAboutItem];
234 [tempItem setTarget:self];
235 } else if ([nextObject isEqualToString:@"quit"]) {
236 if ([[MainController sharedController] blingBling] == NO) {
237 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
238 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
239 [tempItem setTag:MTMenuRegisterItem];
240 [tempItem setTarget:self];
242 ITDebugLog(@"Add \"Quit\" menu item.");
243 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
244 action:@selector(performMainMenuAction:)
246 [tempItem setTag:MTMenuQuitItem];
247 [tempItem setTarget:self];
248 } else if ([nextObject isEqualToString:@"trackInfo"]) {
249 ITDebugLog(@"Check to see if a Track is playing...");
250 //Handle playing radio too
251 if (_currentTrack != -1 && _currentPlaylist > 0) {
252 NSString *title = nil;
254 title = [mtr currentSongTitle];
256 [[MainController sharedController] networkError:localException];
258 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
259 ITDebugLog(@"Add \"Now Playing\" menu item.");
260 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
262 if ([title length] > 0) {
263 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
265 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
268 if (!_playingRadio) {
269 if ([defaults boolForKey:@"showAlbum"]) {
270 NSString *curAlbum = nil;
272 curAlbum = [mtr currentSongAlbum];
274 [[MainController sharedController] networkError:localException];
276 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
279 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
283 if ([defaults boolForKey:@"showArtist"]) {
284 NSString *curArtist = nil;
286 curArtist = [mtr currentSongArtist];
288 [[MainController sharedController] networkError:localException];
290 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
293 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
297 if ([defaults boolForKey:@"showComposer"]) {
298 NSString *curComposer = nil;
300 curComposer = [mtr currentSongComposer];
302 [[MainController sharedController] networkError:localException];
304 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
307 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
311 if ([defaults boolForKey:@"showTrackNumber"]) {
314 track = [mtr currentSongTrack];
316 [[MainController sharedController] networkError:localException];
318 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
321 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
327 if ([defaults boolForKey:@"showTime"] && ( ([mtr currentSongElapsed] != nil) || ([mtr currentSongLength] != nil) )) {
328 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [mtr currentSongElapsed], [mtr currentSongLength]);
329 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [mtr currentSongElapsed], [mtr currentSongLength]] action:nil keyEquivalent:@""]];
332 [[MainController sharedController] networkError:localException];
335 if (!_playingRadio) {
337 if ([defaults boolForKey:@"showPlayCount"] && [mtr currentSource] == ITMTRemoteLibrarySource) {
338 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"Play Count: %i", [mtr currentSongPlayCount]] action:nil keyEquivalent:@""]];
340 if ([defaults boolForKey:@"showTrackRating"] && ( [mtr currentSongRating] != -1.0 )) {
341 NSString *string = nil;
342 switch ((int)([mtr currentSongRating] * 5)) {
344 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
347 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
350 string = [NSString stringWithUTF8String:"★★☆☆☆"];
353 string = [NSString stringWithUTF8String:"★★★☆☆"];
356 string = [NSString stringWithUTF8String:"★★★★☆"];
359 string = [NSString stringWithUTF8String:"★★★★★"];
362 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
363 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
366 [[MainController sharedController] networkError:localException];
369 /*if ([tempItem respondsToSelector:@selector(setAttributedTitle:)] && [defaults boolForKey:@"showAlbumArtwork"] && ![[NetworkController sharedController] isConnectedToServer]) {
370 NSImage *image = [mtr currentSongAlbumArt];
372 NSSize oldSize, newSize;
373 oldSize = [image size];
374 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
375 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
376 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
378 tempItem = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
379 NSTextAttachment *attachment = [[[NSTextAttachment alloc] init] autorelease];
380 [[attachment attachmentCell] setImage:image];
381 NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
382 [tempItem setAttributedTitle:attrString];
387 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
388 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
390 } else if ([nextObject isEqualToString:@"separator"]) {
391 ITDebugLog(@"Add a separator menu item.");
392 [menu addItem:[NSMenuItem separatorItem]];
394 } else if ([nextObject isEqualToString:@"playlists"]) {
395 ITDebugLog(@"Add \"Playlists\" submenu.");
396 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
399 [tempItem setSubmenu:_playlistsMenu];
401 } else if ([nextObject isEqualToString:@"eqPresets"]) {
402 ITDebugLog(@"Add \"EQ Presets\" submenu.");
403 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
406 [tempItem setSubmenu:_eqMenu];
409 itemEnum = [[_eqMenu itemArray] objectEnumerator];
410 while ( (tempItem = [itemEnum nextObject]) ) {
411 [tempItem setState:NSOffState];
414 [[_eqMenu itemAtIndex:0] setState:[mtr equalizerEnabled] ? NSOnState : NSOffState];
415 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] + 1)] setState:NSOnState];
417 [[MainController sharedController] networkError:localException];
419 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
420 ITDebugLog(@"Add \"Song Rating\" submenu.");
421 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
424 [tempItem setSubmenu:_ratingMenu];
426 if (_playingRadio || !_currentPlaylist) {
427 [tempItem setEnabled:NO];
430 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
431 while ( (tempItem = [itemEnum nextObject]) ) {
432 [tempItem setState:NSOffState];
436 [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
438 [[MainController sharedController] networkError:localException];
440 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
441 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
442 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
445 [tempItem setSubmenu:_upcomingSongsMenu];
447 if (_playingRadio || _currentPlaylist < 1) {
448 [tempItem setEnabled:NO];
450 } else if ([nextObject isEqualToString:@"artists"]) {
451 ITDebugLog(@"Add \"Artists\" submenu.");
452 tempItem = [menu addItemWithTitle:NSLocalizedString(@"artists", @"Artists")
455 [tempItem setSubmenu:_artistsMenu];
457 } else if ([nextObject isEqualToString:@"albums"]) {
458 ITDebugLog(@"Add \"Albums\" submenu.");
459 tempItem = [menu addItemWithTitle:NSLocalizedString(@"albums", @"Albums")
462 [tempItem setSubmenu:_albumsMenu];
466 ITDebugLog(@"Finished building menu.");
467 [_currentMenu release];
472 - (NSMenu *)menuForNoPlayer
474 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
475 id <NSMenuItem> tempItem = nil;
476 ITDebugLog(@"Creating menu for when player isn't running.");
478 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
479 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
481 [[MainController sharedController] networkError:localException];
483 [tempItem setTag:MTMenuShowPlayerItem];
484 [tempItem setTarget:self];
485 ITDebugLog(@"Add a separator menu item.");
486 [menu addItem:[NSMenuItem separatorItem]];
487 ITDebugLog(@"Add \"Preferences...\" menu item.");
488 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
489 [tempItem setTag:MTMenuPreferencesItem];
490 [tempItem setTarget:self];
491 if ([[MainController sharedController] blingBling] == NO) {
492 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
493 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
494 [tempItem setTag:MTMenuRegisterItem];
495 [tempItem setTarget:self];
497 ITDebugLog(@"Add \"Quit\" menu item.");
498 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
499 [tempItem setTag:MTMenuQuitItem];
500 [tempItem setTarget:self];
501 return [menu autorelease];
504 - (BOOL)rebuildSubmenus
506 NSArray *menu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
507 ITDebugLog(@"Rebuilding all of the submenus.");
509 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
510 if (_currentTrack > -1) {
511 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
513 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
515 [[MainController sharedController] networkError:localException];
517 ITDebugLog(@"Releasing old submenus.");
519 ITDebugLog(@" - Rating menu");
520 [_ratingMenu release];
522 ITDebugLog(@" - Upcoming songs menu");
523 [_upcomingSongsMenu release];
524 _upcomingSongsMenu = nil;
525 ITDebugLog(@" - Playlists menu");
526 [_playlistsMenu release];
527 _playlistsMenu = nil;
528 ITDebugLog(@" - EQ menu");
532 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
533 _ratingMenu = [self ratingMenu];
534 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
535 _upcomingSongsMenu = [self upcomingSongsMenu];
537 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
538 _playlistsMenu = [self playlistsMenu];
541 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
542 _eqMenu = [self eqMenu];
544 if (_continue && [menu containsObject:@"artists"]) {
545 ITDebugLog(@"Releasing artists menu");
546 [_artistsMenu release];
547 ITDebugLog(@"Beginning Rebuild of \"Artists\" submenu.");
548 _artistsMenu = [self artistsMenu];
551 if (_continue && [menu containsObject:@"albums"]) {
552 ITDebugLog(@"Releasing albums menu");
553 [_albumsMenu release];
554 ITDebugLog(@"Beginning Rebuild of \"Albums\" submenu.");
555 _albumsMenu = [self albumsMenu];
557 ITDebugLog(@"Done rebuilding all of the submenus.");
561 - (NSMenu *)ratingMenu
563 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
564 NSEnumerator *itemEnum;
567 SEL itemSelector = @selector(performRatingMenuAction:);
569 ITDebugLog(@"Building \"Song Rating\" menu.");
571 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
572 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
573 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
574 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
575 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
576 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
578 itemEnum = [[ratingMenu itemArray] objectEnumerator];
579 while ( (anItem = [itemEnum nextObject]) ) {
580 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
581 [anItem setAction:itemSelector];
582 [anItem setTarget:self];
583 [anItem setTag:itemTag];
586 ITDebugLog(@"Done Building \"Song Rating\" menu.");
590 - (NSMenu *)upcomingSongsMenu
592 NSMenu *upcomingSongsMenu;
593 int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
594 if (_currentTrack == -1) {
598 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
600 [[MainController sharedController] networkError:localException];
603 if (numSongs == -1) {
606 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
608 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
609 if (_currentPlaylist && !_playingRadio) {
612 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance && i <= numSongs; i++) {
615 //Check if the song at this index is enabled for playback. If it isn't, skip over it
617 enabled = [[[MainController sharedController] currentRemote] songEnabledAtIndex:i];
619 [[MainController sharedController] networkError:localException];
623 NSString *curSong = nil;
625 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
627 [[MainController sharedController] networkError:localException];
629 id <NSMenuItem> songItem;
630 ITDebugLog(@"Adding song: %@", curSong);
631 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
633 [songItem setTarget:self];
640 if ([upcomingSongsMenu numberOfItems] == 0) {
641 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
644 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
645 NS_VALUERETURN(upcomingSongsMenu, NSMenu *);
647 [upcomingSongsMenu release];
649 NS_VALUERETURN(nil, NSMenu *);
653 /*- (NSMenu *)playlistsMenu
655 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
657 id <NSMenuItem> tempItem;
658 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
661 playlists = [[[MainController sharedController] currentRemote] playlists];
663 [[MainController sharedController] networkError:localException];
666 ITDebugLog(@"Building \"Playlists\" menu.");
668 for (i = 0; i < [playlists count]; i++) {
669 NSString *curPlaylist = [playlists objectAtIndex:i];
670 ITDebugLog(@"Adding playlist: %@", curPlaylist);
671 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
672 [tempItem setTag:i + 1];
673 [tempItem setTarget:self];
676 if (source == ITMTRemoteRadioSource) {
677 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
678 } else if (source == ITMTRemoteGenericDeviceSource) {
679 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
680 } else if (source == ITMTRemoteiPodSource) {
681 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
682 } else if (source == ITMTRemoteCDSource) {
683 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
684 } else if (source == ITMTRemoteSharedLibrarySource) {
685 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
686 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
687 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
689 ITDebugLog(@"Done Building \"Playlists\" menu");
690 return playlistsMenu;
693 - (void)playlistsMenuAux:(NSMenu *)menu node:(PlaylistNode *)node tagPrefix:(int)p
695 id <NSMenuItem> tempItem;
698 for (i = 0; i < [[node children] count]; i++) {
699 PlaylistNode *nextNode = [[node children] objectAtIndex:i];
700 if ([nextNode type] == ITMTFolderNode) {
701 NSMenu *submenu = [[NSMenu alloc] init];
702 tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
703 [tempItem setTag:p + [nextNode index] + 1];
704 [tempItem setTarget:self];
705 [tempItem setSubmenu:submenu];
706 [self playlistsMenuAux:[submenu autorelease] node:nextNode tagPrefix:p];
708 tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
709 [tempItem setTag:p + [nextNode index] + 1];
710 [tempItem setTarget:self];
713 PlaylistNode *root = node;
714 while ([root type] == ITMTPlaylistNode || [root type] == ITMTFolderNode) {
715 root = [root parent];
718 if ([root index] == [[[MainController sharedController] currentRemote] currentSourceIndex] && [nextNode index] == _currentPlaylist) {
719 [tempItem setState:NSOnState];
724 - (NSMenu *)playlistsMenu
726 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
727 NSArray *playlists = nil;
728 id <NSMenuItem> tempItem;
729 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
731 NSMutableArray *indices = [[NSMutableArray alloc] init];
733 playlists = [[[MainController sharedController] currentRemote] playlists];
735 [[MainController sharedController] networkError:localException];
739 [playlistsMenu release];
743 ITDebugLog(@"Building \"Playlists\" menu.");
745 //First we add the main Library source, since it is guaranteed to be there.
746 PlaylistNode *library = [playlists objectAtIndex:0];
747 ITDebugLog(@"Adding main source: %@", [library name]);
748 [self playlistsMenuAux:playlistsMenu node:library tagPrefix:0];
749 ITDebugLog(@"Adding index to the index array.");
750 [indices addObject:[NSNumber numberWithInt:[library index]]];
753 //Next go through the other sources
754 if ([playlists count] > 1) {
755 //Add the radio source if it is playing
756 if ([[playlists objectAtIndex:1] sourceType] == ITMTRemoteRadioSource) {
757 [indices addObject:[NSNumber numberWithInt:[[playlists objectAtIndex:1] index]]];
758 if (source == ITMTRemoteRadioSource) {
759 [playlistsMenu addItem:[NSMenuItem separatorItem]];
760 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
761 } else if ([playlists count] > 2) {
762 [playlistsMenu addItem:[NSMenuItem separatorItem]];
766 //Add other sources as needed (shared music, iPods, CDs)
767 for (i = [playlists count] - 1; i > 1 ; i--) {
768 PlaylistNode *nextSource = [playlists objectAtIndex:i];
769 if ([nextSource type] != ITMTRemoteRadioSource) {
770 NSString *name = [nextSource name];
771 ITDebugLog(@"Adding source: %@", name);
773 if ( ([nextSource type] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
774 ITDebugLog(@"Invalid iPod source.");
775 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
777 NSMenu *menu = [[NSMenu alloc] init];
778 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[menu autorelease]];
779 [self playlistsMenuAux:menu node:nextSource tagPrefix:(i * 1000)];
781 ITDebugLog(@"Adding index to the index array.");
782 [indices addObject:[NSNumber numberWithInt:[nextSource index]]];
787 if (_currentPlaylist != -1) {
788 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
789 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
790 [tempItem setState:NSOnState];
796 tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
797 [tempItem setTarget:self];
798 [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
799 ITDebugLog(@"Done Building \"Playlists\" menu");
800 NS_VALUERETURN(playlistsMenu, NSMenu *);
802 [playlistsMenu release];
804 NS_VALUERETURN(nil, NSMenu *);
810 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
811 NSArray *eqPresets = nil;
812 id <NSMenuItem> tempItem;
816 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
818 [[MainController sharedController] networkError:localException];
821 ITDebugLog(@"Building \"EQ Presets\" menu.");
823 tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
824 [tempItem setTag:-1];
825 [tempItem setTarget:self];
826 [eqMenu addItem:[NSMenuItem separatorItem]];
828 for (i = 0; i < [eqPresets count]; i++) {
830 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
831 ITDebugLog(@"Adding EQ Preset: %@", name);
832 tempItem = [eqMenu addItemWithTitle:name
833 action:@selector(performEqualizerMenuAction:)
836 [tempItem setTarget:self];
839 ITDebugLog(@"Done Building \"EQ Presets\" menu");
843 - (NSMenu *)artistsMenu
845 NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@"Artists"];
846 NSEnumerator *artistsEnumerator;
847 NSString *nextArtist;
848 id <NSMenuItem> tempItem;
849 ITDebugLog(@"Building \"Artists\" menu.");
851 artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
852 while ( (nextArtist = [artistsEnumerator nextObject]) ) {
853 tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
854 [tempItem setTarget:self];
857 [[MainController sharedController] networkError:localException];
859 ITDebugLog(@"Done Building \"Artists\" menu");
863 - (NSMenu *)albumsMenu
865 NSMenu *albumsMenu = [[NSMenu alloc] initWithTitle:@"Albums"];
866 NSEnumerator *albumsEnumerator;
868 id <NSMenuItem> tempItem;
869 ITDebugLog(@"Building \"Albums\" menu.");
871 albumsEnumerator = [[[[MainController sharedController] currentRemote] albums] objectEnumerator];
872 while ( (nextAlbum = [albumsEnumerator nextObject]) ) {
873 tempItem = [albumsMenu addItemWithTitle:nextAlbum action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
874 [tempItem setTarget:self];
877 [[MainController sharedController] networkError:localException];
879 ITDebugLog(@"Done Building \"Albums\" menu");
883 - (void)performMainMenuAction:(id)sender
885 switch ( [sender tag] )
887 case MTMenuPlayPauseItem:
888 ITDebugLog(@"Performing Menu Action: Play/Pause");
889 [[MainController sharedController] playPause];
891 case MTMenuFastForwardItem:
892 ITDebugLog(@"Performing Menu Action: Fast Forward");
893 [[MainController sharedController] fastForward];
895 case MTMenuRewindItem:
896 ITDebugLog(@"Performing Menu Action: Rewind");
897 [[MainController sharedController] rewind];
899 case MTMenuPreviousTrackItem:
900 ITDebugLog(@"Performing Menu Action: Previous Track");
901 [[MainController sharedController] prevSong];
903 case MTMenuNextTrackItem:
904 ITDebugLog(@"Performing Menu Action: Next Track");
905 [[MainController sharedController] nextSong];
907 case MTMenuShowPlayerItem:
908 ITDebugLog(@"Performing Menu Action: Show Main Interface");
909 [[MainController sharedController] showPlayer];
911 case MTMenuPreferencesItem:
912 ITDebugLog(@"Performing Menu Action: Preferences...");
913 [[MainController sharedController] showPreferences];
915 case MTMenuAboutItem:
916 ITDebugLog(@"Performing Menu Action: About MenuTunes...");
917 [[ITAboutWindowController sharedController] showAboutWindow];
920 ITDebugLog(@"Performing Menu Action: Quit");
921 [[MainController sharedController] quitMenuTunes];
923 case MTMenuRegisterItem:
924 ITDebugLog(@"Performing Menu Action: Register");
925 [[MainController sharedController] blingNow];
928 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
933 - (void)performRatingMenuAction:(id)sender
935 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
936 [[MainController sharedController] selectSongRating:[sender tag]];
939 - (void)performPlaylistMenuAction:(id)sender
941 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
942 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
945 - (void)performEqualizerMenuAction:(id)sender
947 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
948 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
951 - (void)performUpcomingSongsMenuAction:(id)sender
953 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
954 [[MainController sharedController] selectSongAtIndex:[sender tag]];
957 - (void)performBrowseMenuAction:(id)sender
959 ITDebugLog(@"Browse action selected on item named %@", [sender title]);
965 [[MainController sharedController] makePlaylistWithTerm:[sender title] ofType:(([[[sender menu] title] isEqualToString:@"Artists"]) ? 1 : 2)];
970 ITDebugLog(@"Update Menu");
971 [_currentMenu update];
974 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
979 //This is never used I know, keep it though
980 - (NSString *)systemUIColor
982 NSDictionary *tmpDict;
984 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
985 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
986 if ( ([tmpNumber intValue] == 1) ) {
999 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
1000 onItem:(id <NSMenuItem>)item
1002 unichar charcode = 'a';
1004 long cocoaModifiers = 0;
1005 static long carbonToCocoa[6][2] =
1007 { cmdKey, NSCommandKeyMask },
1008 { optionKey, NSAlternateKeyMask },
1009 { controlKey, NSControlKeyMask },
1010 { shiftKey, NSShiftKeyMask },
1013 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
1015 for (i = 0; i < 6; i++) {
1016 if (modifiers & carbonToCocoa[i][0]) {
1017 cocoaModifiers += carbonToCocoa[i][1];
1020 [item setKeyEquivalentModifierMask:cocoaModifiers];
1022 //Missing key combos for some keys. Must find them later.
1026 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
1031 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
1038 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
1039 // Haven't tested this, though it should work.
1040 // This doesn't work. :'(
1042 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
1043 //charcode = buffer;
1044 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1045 ITDebugLog(@"%@", menuRef);
1046 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1047 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1048 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1051 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
1057 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
1058 charcode = NSDeleteFunctionKey;
1062 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
1067 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
1072 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
1077 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
1078 charcode = NSF5FunctionKey;
1082 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
1083 charcode = NSF6FunctionKey;
1087 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
1088 charcode = NSF7FunctionKey;
1092 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
1093 charcode = NSF3FunctionKey;
1097 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
1098 charcode = NSF8FunctionKey;
1102 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
1103 charcode = NSF9FunctionKey;
1107 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1108 charcode = NSF11FunctionKey;
1112 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1113 charcode = NSF13FunctionKey;
1117 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1118 charcode = NSF14FunctionKey;
1122 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1123 charcode = NSF10FunctionKey;
1127 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1128 charcode = NSF12FunctionKey;
1132 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1133 charcode = NSF13FunctionKey;
1137 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1138 charcode = NSInsertFunctionKey;
1142 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1143 charcode = NSHomeFunctionKey;
1147 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1148 charcode = NSPageUpFunctionKey;
1152 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1153 charcode = NSDeleteFunctionKey;
1157 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1158 charcode = NSF4FunctionKey;
1162 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1163 charcode = NSEndFunctionKey;
1167 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1168 charcode = NSF2FunctionKey;
1172 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1173 charcode = NSPageDownFunctionKey;
1177 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1178 charcode = NSF1FunctionKey;
1182 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1183 charcode = NSLeftArrowFunctionKey;
1187 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1188 charcode = NSRightArrowFunctionKey;
1192 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1193 charcode = NSDownArrowFunctionKey;
1197 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1198 charcode = NSUpArrowFunctionKey;
1202 if (charcode == 'a') {
1203 unsigned long state;
1208 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1209 keyTrans = KeyTranslate(kchr, code, &state);
1210 charCode = keyTrans;
1211 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1212 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1213 } else if (charcode != 'b') {
1214 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1216 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1219 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1221 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1222 NSEnumerator *volEnum = [volumes objectEnumerator];
1223 NSString *nextVolume;
1224 ITDebugLog(@"Looking for an iPod named %@", name);
1225 while ( (nextVolume = [volEnum nextObject]) ) {
1226 ITDebugLog(@"- %@", nextVolume);
1227 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1228 NSFileHandle *handle;
1230 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1231 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1232 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1235 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1236 ITDebugLog(@"File handle: %@", handle);
1237 [handle seekToFileOffset:10];
1238 data = [handle readDataOfLength:1];
1239 ITDebugLog(@"Data: %@", data);
1240 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1241 ITDebugLog(@"iPod is manually updated. %@", path);
1243 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1244 ITDebugLog(@"iPod is automatically updated. %@", path);
1247 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);