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) {
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++) {
614 NSString *curSong = nil;
616 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
618 [[MainController sharedController] networkError:localException];
620 id <NSMenuItem> songItem;
621 ITDebugLog(@"Adding song: %@", curSong);
622 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
624 [songItem setTarget:self];
631 if ([upcomingSongsMenu numberOfItems] == 0) {
632 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
635 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
636 NS_VALUERETURN(upcomingSongsMenu, NSMenu *);
638 [upcomingSongsMenu release];
640 NS_VALUERETURN(nil, NSMenu *);
644 /*- (NSMenu *)playlistsMenu
646 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
648 id <NSMenuItem> tempItem;
649 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
652 playlists = [[[MainController sharedController] currentRemote] playlists];
654 [[MainController sharedController] networkError:localException];
657 ITDebugLog(@"Building \"Playlists\" menu.");
659 for (i = 0; i < [playlists count]; i++) {
660 NSString *curPlaylist = [playlists objectAtIndex:i];
661 ITDebugLog(@"Adding playlist: %@", curPlaylist);
662 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
663 [tempItem setTag:i + 1];
664 [tempItem setTarget:self];
667 if (source == ITMTRemoteRadioSource) {
668 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
669 } else if (source == ITMTRemoteGenericDeviceSource) {
670 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
671 } else if (source == ITMTRemoteiPodSource) {
672 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
673 } else if (source == ITMTRemoteCDSource) {
674 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
675 } else if (source == ITMTRemoteSharedLibrarySource) {
676 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
677 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
678 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
680 ITDebugLog(@"Done Building \"Playlists\" menu");
681 return playlistsMenu;
684 - (void)playlistsMenuAux:(NSMenu *)menu node:(PlaylistNode *)node tagPrefix:(int)p
686 id <NSMenuItem> tempItem;
689 for (i = 0; i < [[node children] count]; i++) {
690 PlaylistNode *nextNode = [[node children] objectAtIndex:i];
691 if ([nextNode type] == ITMTFolderNode) {
692 NSMenu *submenu = [[NSMenu alloc] init];
693 tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
694 [tempItem setTag:p + [nextNode index] + 1];
695 [tempItem setTarget:self];
696 [tempItem setSubmenu:submenu];
697 [self playlistsMenuAux:[submenu autorelease] node:nextNode tagPrefix:p];
699 tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
700 [tempItem setTag:p + [nextNode index] + 1];
701 [tempItem setTarget:self];
704 PlaylistNode *root = node;
705 while ([root type] == ITMTPlaylistNode || [root type] == ITMTFolderNode) {
706 root = [root parent];
709 if ([root index] == [[[MainController sharedController] currentRemote] currentSourceIndex] && [nextNode index] == _currentPlaylist) {
710 [tempItem setState:NSOnState];
715 - (NSMenu *)playlistsMenu
717 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
718 NSArray *playlists = nil;
719 id <NSMenuItem> tempItem;
720 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
722 NSMutableArray *indices = [[NSMutableArray alloc] init];
724 playlists = [[[MainController sharedController] currentRemote] playlists];
726 [[MainController sharedController] networkError:localException];
730 [playlistsMenu release];
734 ITDebugLog(@"Building \"Playlists\" menu.");
736 //First we add the main Library source, since it is guaranteed to be there.
737 PlaylistNode *library = [playlists objectAtIndex:0];
738 ITDebugLog(@"Adding main source: %@", [library name]);
739 [self playlistsMenuAux:playlistsMenu node:library tagPrefix:0];
740 ITDebugLog(@"Adding index to the index array.");
741 [indices addObject:[NSNumber numberWithInt:[library index]]];
744 //Next go through the other sources
745 if ([playlists count] > 1) {
746 //Add the radio source if it is playing
747 if ([[playlists objectAtIndex:1] sourceType] == ITMTRemoteRadioSource) {
748 [indices addObject:[NSNumber numberWithInt:[[playlists objectAtIndex:1] index]]];
749 if (source == ITMTRemoteRadioSource) {
750 [playlistsMenu addItem:[NSMenuItem separatorItem]];
751 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
752 } else if ([playlists count] > 2) {
753 [playlistsMenu addItem:[NSMenuItem separatorItem]];
757 //Add other sources as needed (shared music, iPods, CDs)
758 for (i = [playlists count] - 1; i > 1 ; i--) {
759 PlaylistNode *nextSource = [playlists objectAtIndex:i];
760 if ([nextSource type] != ITMTRemoteRadioSource) {
761 NSString *name = [nextSource name];
762 ITDebugLog(@"Adding source: %@", name);
764 if ( ([nextSource type] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
765 ITDebugLog(@"Invalid iPod source.");
766 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
768 NSMenu *menu = [[NSMenu alloc] init];
769 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[menu autorelease]];
770 [self playlistsMenuAux:menu node:nextSource tagPrefix:(i * 1000)];
772 ITDebugLog(@"Adding index to the index array.");
773 [indices addObject:[NSNumber numberWithInt:[nextSource index]]];
778 if (_currentPlaylist != -1) {
779 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
780 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
781 [tempItem setState:NSOnState];
787 tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
788 [tempItem setTarget:self];
789 [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
790 ITDebugLog(@"Done Building \"Playlists\" menu");
791 NS_VALUERETURN(playlistsMenu, NSMenu *);
793 [playlistsMenu release];
795 NS_VALUERETURN(nil, NSMenu *);
801 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
802 NSArray *eqPresets = nil;
803 id <NSMenuItem> tempItem;
807 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
809 [[MainController sharedController] networkError:localException];
812 ITDebugLog(@"Building \"EQ Presets\" menu.");
814 tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
815 [tempItem setTag:-1];
816 [tempItem setTarget:self];
817 [eqMenu addItem:[NSMenuItem separatorItem]];
819 for (i = 0; i < [eqPresets count]; i++) {
821 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
822 ITDebugLog(@"Adding EQ Preset: %@", name);
823 tempItem = [eqMenu addItemWithTitle:name
824 action:@selector(performEqualizerMenuAction:)
827 [tempItem setTarget:self];
830 ITDebugLog(@"Done Building \"EQ Presets\" menu");
834 - (NSMenu *)artistsMenu
836 NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@"Artists"];
837 NSEnumerator *artistsEnumerator;
838 NSString *nextArtist;
839 id <NSMenuItem> tempItem;
840 ITDebugLog(@"Building \"Artists\" menu.");
842 artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
843 while ( (nextArtist = [artistsEnumerator nextObject]) ) {
844 tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
845 [tempItem setTarget:self];
848 [[MainController sharedController] networkError:localException];
850 ITDebugLog(@"Done Building \"Artists\" menu");
854 - (NSMenu *)albumsMenu
856 NSMenu *albumsMenu = [[NSMenu alloc] initWithTitle:@"Albums"];
857 NSEnumerator *albumsEnumerator;
859 id <NSMenuItem> tempItem;
860 ITDebugLog(@"Building \"Albums\" menu.");
862 albumsEnumerator = [[[[MainController sharedController] currentRemote] albums] objectEnumerator];
863 while ( (nextAlbum = [albumsEnumerator nextObject]) ) {
864 tempItem = [albumsMenu addItemWithTitle:nextAlbum action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
865 [tempItem setTarget:self];
868 [[MainController sharedController] networkError:localException];
870 ITDebugLog(@"Done Building \"Albums\" menu");
874 - (void)performMainMenuAction:(id)sender
876 switch ( [sender tag] )
878 case MTMenuPlayPauseItem:
879 ITDebugLog(@"Performing Menu Action: Play/Pause");
880 [[MainController sharedController] playPause];
882 case MTMenuFastForwardItem:
883 ITDebugLog(@"Performing Menu Action: Fast Forward");
884 [[MainController sharedController] fastForward];
886 case MTMenuRewindItem:
887 ITDebugLog(@"Performing Menu Action: Rewind");
888 [[MainController sharedController] rewind];
890 case MTMenuPreviousTrackItem:
891 ITDebugLog(@"Performing Menu Action: Previous Track");
892 [[MainController sharedController] prevSong];
894 case MTMenuNextTrackItem:
895 ITDebugLog(@"Performing Menu Action: Next Track");
896 [[MainController sharedController] nextSong];
898 case MTMenuShowPlayerItem:
899 ITDebugLog(@"Performing Menu Action: Show Main Interface");
900 [[MainController sharedController] showPlayer];
902 case MTMenuPreferencesItem:
903 ITDebugLog(@"Performing Menu Action: Preferences...");
904 [[MainController sharedController] showPreferences];
906 case MTMenuAboutItem:
907 ITDebugLog(@"Performing Menu Action: About MenuTunes...");
908 [[ITAboutWindowController sharedController] showAboutWindow];
911 ITDebugLog(@"Performing Menu Action: Quit");
912 [[MainController sharedController] quitMenuTunes];
914 case MTMenuRegisterItem:
915 ITDebugLog(@"Performing Menu Action: Register");
916 [[MainController sharedController] blingNow];
919 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
924 - (void)performRatingMenuAction:(id)sender
926 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
927 [[MainController sharedController] selectSongRating:[sender tag]];
930 - (void)performPlaylistMenuAction:(id)sender
932 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
933 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
936 - (void)performEqualizerMenuAction:(id)sender
938 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
939 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
942 - (void)performUpcomingSongsMenuAction:(id)sender
944 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
945 [[MainController sharedController] selectSongAtIndex:[sender tag]];
948 - (void)performBrowseMenuAction:(id)sender
950 ITDebugLog(@"Browse action selected on item named %@", [sender title]);
956 [[MainController sharedController] makePlaylistWithTerm:[sender title] ofType:(([[[sender menu] title] isEqualToString:@"Artists"]) ? 1 : 2)];
961 ITDebugLog(@"Update Menu");
962 [_currentMenu update];
965 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
970 //This is never used I know, keep it though
971 - (NSString *)systemUIColor
973 NSDictionary *tmpDict;
975 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
976 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
977 if ( ([tmpNumber intValue] == 1) ) {
990 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
991 onItem:(id <NSMenuItem>)item
993 unichar charcode = 'a';
995 long cocoaModifiers = 0;
996 static long carbonToCocoa[6][2] =
998 { cmdKey, NSCommandKeyMask },
999 { optionKey, NSAlternateKeyMask },
1000 { controlKey, NSControlKeyMask },
1001 { shiftKey, NSShiftKeyMask },
1004 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
1006 for (i = 0; i < 6; i++) {
1007 if (modifiers & carbonToCocoa[i][0]) {
1008 cocoaModifiers += carbonToCocoa[i][1];
1011 [item setKeyEquivalentModifierMask:cocoaModifiers];
1013 //Missing key combos for some keys. Must find them later.
1017 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
1022 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
1029 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
1030 // Haven't tested this, though it should work.
1031 // This doesn't work. :'(
1033 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
1034 //charcode = buffer;
1035 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1036 ITDebugLog(@"%@", menuRef);
1037 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1038 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1039 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1042 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
1048 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
1049 charcode = NSDeleteFunctionKey;
1053 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
1058 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
1063 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
1068 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
1069 charcode = NSF5FunctionKey;
1073 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
1074 charcode = NSF6FunctionKey;
1078 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
1079 charcode = NSF7FunctionKey;
1083 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
1084 charcode = NSF3FunctionKey;
1088 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
1089 charcode = NSF8FunctionKey;
1093 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
1094 charcode = NSF9FunctionKey;
1098 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1099 charcode = NSF11FunctionKey;
1103 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1104 charcode = NSF13FunctionKey;
1108 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1109 charcode = NSF14FunctionKey;
1113 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1114 charcode = NSF10FunctionKey;
1118 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1119 charcode = NSF12FunctionKey;
1123 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1124 charcode = NSF13FunctionKey;
1128 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1129 charcode = NSInsertFunctionKey;
1133 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1134 charcode = NSHomeFunctionKey;
1138 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1139 charcode = NSPageUpFunctionKey;
1143 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1144 charcode = NSDeleteFunctionKey;
1148 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1149 charcode = NSF4FunctionKey;
1153 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1154 charcode = NSEndFunctionKey;
1158 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1159 charcode = NSF2FunctionKey;
1163 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1164 charcode = NSPageDownFunctionKey;
1168 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1169 charcode = NSF1FunctionKey;
1173 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1174 charcode = NSLeftArrowFunctionKey;
1178 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1179 charcode = NSRightArrowFunctionKey;
1183 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1184 charcode = NSDownArrowFunctionKey;
1188 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1189 charcode = NSUpArrowFunctionKey;
1193 if (charcode == 'a') {
1194 unsigned long state;
1199 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1200 keyTrans = KeyTranslate(kchr, code, &state);
1201 charCode = keyTrans;
1202 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1203 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1204 } else if (charcode != 'b') {
1205 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1207 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1210 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1212 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1213 NSEnumerator *volEnum = [volumes objectEnumerator];
1214 NSString *nextVolume;
1215 ITDebugLog(@"Looking for an iPod named %@", name);
1216 while ( (nextVolume = [volEnum nextObject]) ) {
1217 ITDebugLog(@"- %@", nextVolume);
1218 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1219 NSFileHandle *handle;
1221 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1222 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1223 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1226 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1227 ITDebugLog(@"File handle: %@", handle);
1228 [handle seekToFileOffset:10];
1229 data = [handle readDataOfLength:1];
1230 ITDebugLog(@"Data: %@", data);
1231 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1232 ITDebugLog(@"iPod is manually updated. %@", path);
1234 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1235 ITDebugLog(@"iPod is automatically updated. %@", path);
1238 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);