5 // Created by Joseph Spiros on Wed Apr 30 2003.
6 // Copyright (c) 2003 iThink Software. All rights reserved.
9 #import "MenuController.h"
10 #import "MainController.h"
11 #import "NetworkController.h"
12 #import "ITMTRemote.h"
13 #import <ITFoundation/ITDebug.h>
14 #import <ITKit/ITHotKeyCenter.h>
15 #import <ITKit/ITHotKey.h>
16 #import <ITKit/ITKeyCombo.h>
17 #import <ITKit/ITCategory-NSMenu.h>
19 @interface MenuController (SubmenuMethods)
20 - (NSMenu *)ratingMenu;
21 - (NSMenu *)upcomingSongsMenu;
22 - (NSMenu *)playlistsMenu;
24 - (NSMenu *)artistsMenu;
25 - (NSMenu *)albumsMenu;
26 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
27 onItem:(id <NSMenuItem>)item;
28 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
31 @implementation MenuController
35 if ( (self = [super init]) ) {
36 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
43 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
44 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
45 NSArray *menuArray = [defaults arrayForKey:@"menu"];
46 NSEnumerator *enumerator = [menuArray objectEnumerator];
48 id <NSMenuItem> tempItem;
49 NSEnumerator *itemEnum;
51 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
52 ITMTRemote *mtr = [[MainController sharedController] currentRemote];
53 int currentSongRating = 0;
57 _currentPlaylist = [mtr currentPlaylistIndex];
58 _playingRadio = ([mtr currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
59 currentSongRating = ( [mtr currentSongRating] != -1 );
61 [[MainController sharedController] networkError:localException];
64 ITDebugLog(@"Reset menu if required.");
66 //Kill the old submenu items
67 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
68 ITDebugLog(@"Removing \"Song Rating\" submenu.");
69 [tempItem setSubmenu:nil];
72 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
73 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
74 [tempItem setSubmenu:nil];
77 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
78 ITDebugLog(@"Removing \"Playlists\" submenu.");
79 [tempItem setSubmenu:nil];
82 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
83 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
84 [tempItem setSubmenu:nil];
87 if ( (tempItem = [_currentMenu itemWithTag:5]) ) {
88 ITDebugLog(@"Removing \"Artists\" submenu.");
89 [tempItem setSubmenu:nil];
92 if ( (tempItem = [_currentMenu itemWithTag:6]) ) {
93 ITDebugLog(@"Removing \"Albums\" submenu.");
94 [tempItem setSubmenu:nil];
97 ITDebugLog(@"Begin building menu.");
100 while ( (nextObject = [enumerator nextObject]) ) {
102 if ([nextObject isEqualToString:@"playPause"]) {
103 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
104 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
105 action:@selector(performMainMenuAction:)
107 [tempItem setTag:MTMenuPlayPauseItem];
108 [tempItem setTarget:self];
110 itemEnum = [hotKeys objectEnumerator];
111 while ( (hotKey = [itemEnum nextObject]) ) {
112 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
113 ITKeyCombo *combo = [hotKey keyCombo];
114 [self setKeyEquivalentForCode:[combo keyCode]
115 andModifiers:[combo modifiers]
120 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
122 switch ([mtr playerPlayingState]) {
123 case ITMTRemotePlayerPlaying:
124 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
126 case ITMTRemotePlayerRewinding:
127 case ITMTRemotePlayerForwarding:
128 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
134 [[MainController sharedController] networkError:localException];
136 } else if ([nextObject isEqualToString:@"nextTrack"]) {
137 ITDebugLog(@"Add \"Next Track\" menu item.");
138 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
139 action:@selector(performMainMenuAction:)
142 itemEnum = [hotKeys objectEnumerator];
143 while ( (hotKey = [itemEnum nextObject]) ) {
144 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
145 ITKeyCombo *combo = [hotKey keyCombo];
146 [self setKeyEquivalentForCode:[combo keyCode]
147 andModifiers:[combo modifiers]
152 if (_currentPlaylist) {
153 [tempItem setTag:MTMenuNextTrackItem];
154 [tempItem setTarget:self];
156 } else if ([nextObject isEqualToString:@"prevTrack"]) {
157 ITDebugLog(@"Add \"Previous Track\" menu item.");
158 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
159 action:@selector(performMainMenuAction:)
162 itemEnum = [hotKeys objectEnumerator];
163 while ( (hotKey = [itemEnum nextObject]) ) {
164 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
165 ITKeyCombo *combo = [hotKey keyCombo];
166 [self setKeyEquivalentForCode:[combo keyCode]
167 andModifiers:[combo modifiers]
172 if (_currentPlaylist) {
173 [tempItem setTag:MTMenuPreviousTrackItem];
174 [tempItem setTarget:self];
176 } else if ([nextObject isEqualToString:@"fastForward"]) {
177 ITDebugLog(@"Add \"Fast Forward\" menu item.");
178 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
179 action:@selector(performMainMenuAction:)
181 if (_currentPlaylist) {
182 [tempItem setTag:MTMenuFastForwardItem];
183 [tempItem setTarget:self];
185 } else if ([nextObject isEqualToString:@"rewind"]) {
186 ITDebugLog(@"Add \"Rewind\" menu item.");
187 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
188 action:@selector(performMainMenuAction:)
190 if (_currentPlaylist) {
191 [tempItem setTag:MTMenuRewindItem];
192 [tempItem setTarget:self];
194 } else if ([nextObject isEqualToString:@"showPlayer"]) {
195 ITDebugLog(@"Add \"Show Player\" menu item.");
197 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
198 NSLocalizedString(@"show", @"Show"),
199 [mtr playerSimpleName]]
200 action:@selector(performMainMenuAction:)
203 [[MainController sharedController] networkError:localException];
206 itemEnum = [hotKeys objectEnumerator];
207 while ( (hotKey = [itemEnum nextObject]) ) {
208 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
209 ITKeyCombo *combo = [hotKey keyCombo];
210 [self setKeyEquivalentForCode:[combo keyCode]
211 andModifiers:[combo modifiers]
216 [tempItem setTarget:self];
217 [tempItem setTag:MTMenuShowPlayerItem];
218 } else if ([nextObject isEqualToString:@"preferences"]) {
219 ITDebugLog(@"Add \"Preferences...\" menu item.");
220 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
221 action:@selector(performMainMenuAction:)
223 [tempItem setTag:MTMenuPreferencesItem];
224 [tempItem setTarget:self];
225 } else if ([nextObject isEqualToString:@"quit"]) {
226 if ([[MainController sharedController] blingBling] == NO) {
227 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
228 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
229 [tempItem setTag:MTMenuRegisterItem];
230 [tempItem setTarget:self];
232 ITDebugLog(@"Add \"Quit\" menu item.");
233 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
234 action:@selector(performMainMenuAction:)
236 [tempItem setTag:MTMenuQuitItem];
237 [tempItem setTarget:self];
238 } else if ([nextObject isEqualToString:@"trackInfo"]) {
239 ITDebugLog(@"Check to see if a Track is playing...");
240 //Handle playing radio too
241 if (_currentTrack != -1 && _currentPlaylist > 1) {
242 NSString *title = nil;
244 title = [mtr currentSongTitle];
246 [[MainController sharedController] networkError:localException];
248 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
249 ITDebugLog(@"Add \"Now Playing\" menu item.");
250 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
252 if ([title length] > 0) {
253 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
255 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
258 if (!_playingRadio) {
259 if ([defaults boolForKey:@"showAlbum"]) {
260 NSString *curAlbum = nil;
262 curAlbum = [mtr currentSongAlbum];
264 [[MainController sharedController] networkError:localException];
266 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
269 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
273 if ([defaults boolForKey:@"showArtist"]) {
274 NSString *curArtist = nil;
276 curArtist = [mtr currentSongArtist];
278 [[MainController sharedController] networkError:localException];
280 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
283 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
287 if ([defaults boolForKey:@"showComposer"]) {
288 NSString *curComposer = nil;
290 curComposer = [mtr currentSongComposer];
292 [[MainController sharedController] networkError:localException];
294 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
297 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
301 if ([defaults boolForKey:@"showTrackNumber"]) {
304 track = [mtr currentSongTrack];
306 [[MainController sharedController] networkError:localException];
308 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
311 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
317 if ([defaults boolForKey:@"showTime"] && ( ([mtr currentSongElapsed] != nil) || ([mtr currentSongLength] != nil) )) {
318 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [mtr currentSongElapsed], [mtr currentSongLength]);
319 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [mtr currentSongElapsed], [mtr currentSongLength]] action:nil keyEquivalent:@""]];
322 [[MainController sharedController] networkError:localException];
325 if (!_playingRadio) {
327 if ([defaults boolForKey:@"showPlayCount"] && [mtr currentSource] == ITMTRemoteLibrarySource) {
328 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"Play Count: %i", [mtr currentSongPlayCount]] action:nil keyEquivalent:@""]];
330 if ([defaults boolForKey:@"showTrackRating"] && ( [mtr currentSongRating] != -1.0 )) {
331 NSString *string = nil;
332 switch ((int)([mtr currentSongRating] * 5)) {
334 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
337 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
340 string = [NSString stringWithUTF8String:"★★☆☆☆"];
343 string = [NSString stringWithUTF8String:"★★★☆☆"];
346 string = [NSString stringWithUTF8String:"★★★★☆"];
349 string = [NSString stringWithUTF8String:"★★★★★"];
352 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
353 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
356 [[MainController sharedController] networkError:localException];
359 /*if ([tempItem respondsToSelector:@selector(setAttributedTitle:)] && [defaults boolForKey:@"showAlbumArtwork"] && ![[NetworkController sharedController] isConnectedToServer]) {
360 NSImage *image = [mtr currentSongAlbumArt];
362 NSSize oldSize, newSize;
363 oldSize = [image size];
364 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
365 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
366 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
368 tempItem = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
369 NSTextAttachment *attachment = [[[NSTextAttachment alloc] init] autorelease];
370 [[attachment attachmentCell] setImage:image];
371 NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
372 [tempItem setAttributedTitle:attrString];
377 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
378 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
380 } else if ([nextObject isEqualToString:@"separator"]) {
381 ITDebugLog(@"Add a separator menu item.");
382 [menu addItem:[NSMenuItem separatorItem]];
384 } else if ([nextObject isEqualToString:@"playlists"]) {
385 ITDebugLog(@"Add \"Playlists\" submenu.");
386 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
389 [tempItem setSubmenu:_playlistsMenu];
391 } else if ([nextObject isEqualToString:@"eqPresets"]) {
392 ITDebugLog(@"Add \"EQ Presets\" submenu.");
393 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
396 [tempItem setSubmenu:_eqMenu];
399 itemEnum = [[_eqMenu itemArray] objectEnumerator];
400 while ( (tempItem = [itemEnum nextObject]) ) {
401 [tempItem setState:NSOffState];
404 [[_eqMenu itemAtIndex:0] setState:[mtr equalizerEnabled] ? NSOnState : NSOffState];
405 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] + 1)] setState:NSOnState];
407 [[MainController sharedController] networkError:localException];
409 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
410 ITDebugLog(@"Add \"Song Rating\" submenu.");
411 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
414 [tempItem setSubmenu:_ratingMenu];
416 if (_playingRadio || !_currentPlaylist) {
417 [tempItem setEnabled:NO];
420 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
421 while ( (tempItem = [itemEnum nextObject]) ) {
422 [tempItem setState:NSOffState];
426 [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
428 [[MainController sharedController] networkError:localException];
430 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
431 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
432 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
435 [tempItem setSubmenu:_upcomingSongsMenu];
437 if (_playingRadio || !_currentPlaylist) {
438 [tempItem setEnabled:NO];
440 } else if ([nextObject isEqualToString:@"artists"]) {
441 ITDebugLog(@"Add \"Artists\" submenu.");
442 tempItem = [menu addItemWithTitle:NSLocalizedString(@"artists", @"Artists")
445 [tempItem setSubmenu:_artistsMenu];
447 } else if ([nextObject isEqualToString:@"albums"]) {
448 ITDebugLog(@"Add \"Albums\" submenu.");
449 tempItem = [menu addItemWithTitle:NSLocalizedString(@"albums", @"Albums")
452 [tempItem setSubmenu:_albumsMenu];
456 ITDebugLog(@"Finished building menu.");
457 [_currentMenu release];
462 - (NSMenu *)menuForNoPlayer
464 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
465 id <NSMenuItem> tempItem = nil;
466 ITDebugLog(@"Creating menu for when player isn't running.");
468 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
469 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
471 [[MainController sharedController] networkError:localException];
473 [tempItem setTag:MTMenuShowPlayerItem];
474 [tempItem setTarget:self];
475 ITDebugLog(@"Add a separator menu item.");
476 [menu addItem:[NSMenuItem separatorItem]];
477 ITDebugLog(@"Add \"Preferences...\" menu item.");
478 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
479 [tempItem setTag:MTMenuPreferencesItem];
480 [tempItem setTarget:self];
481 if ([[MainController sharedController] blingBling] == NO) {
482 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
483 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
484 [tempItem setTag:MTMenuRegisterItem];
485 [tempItem setTarget:self];
487 ITDebugLog(@"Add \"Quit\" menu item.");
488 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
489 [tempItem setTag:MTMenuQuitItem];
490 [tempItem setTarget:self];
491 return [menu autorelease];
494 - (BOOL)rebuildSubmenus
496 NSArray *menu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
497 ITDebugLog(@"Rebuilding all of the submenus.");
499 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
500 if (_currentTrack > -1) {
501 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
503 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
505 [[MainController sharedController] networkError:localException];
507 ITDebugLog(@"Releasing old submenus.");
509 ITDebugLog(@" - Rating menu");
510 [_ratingMenu release];
512 ITDebugLog(@" - Upcoming songs menu");
513 [_upcomingSongsMenu release];
514 _upcomingSongsMenu = nil;
515 ITDebugLog(@" - Playlists menu");
516 [_playlistsMenu release];
517 _playlistsMenu = nil;
518 ITDebugLog(@" - EQ menu");
522 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
523 _ratingMenu = [self ratingMenu];
524 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
525 _upcomingSongsMenu = [self upcomingSongsMenu];
527 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
528 _playlistsMenu = [self playlistsMenu];
531 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
532 _eqMenu = [self eqMenu];
534 if (_continue && [menu containsObject:@"artists"]) {
535 ITDebugLog(@"Releasing artists menu");
536 [_artistsMenu release];
537 ITDebugLog(@"Beginning Rebuild of \"Artists\" submenu.");
538 _artistsMenu = [self artistsMenu];
541 if (_continue && [menu containsObject:@"albums"]) {
542 ITDebugLog(@"Releasing albums menu");
543 [_albumsMenu release];
544 ITDebugLog(@"Beginning Rebuild of \"Albums\" submenu.");
545 _albumsMenu = [self albumsMenu];
547 ITDebugLog(@"Done rebuilding all of the submenus.");
551 - (NSMenu *)ratingMenu
553 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
554 NSEnumerator *itemEnum;
557 SEL itemSelector = @selector(performRatingMenuAction:);
559 ITDebugLog(@"Building \"Song Rating\" menu.");
561 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
562 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
563 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
564 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
565 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
566 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
568 itemEnum = [[ratingMenu itemArray] objectEnumerator];
569 while ( (anItem = [itemEnum nextObject]) ) {
570 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
571 [anItem setAction:itemSelector];
572 [anItem setTarget:self];
573 [anItem setTag:itemTag];
576 ITDebugLog(@"Done Building \"Song Rating\" menu.");
580 - (NSMenu *)upcomingSongsMenu
582 NSMenu *upcomingSongsMenu;
583 int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
584 if (_currentTrack == -1) {
588 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
590 [[MainController sharedController] networkError:localException];
593 if (numSongs == -1) {
596 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
598 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
599 if (_currentPlaylist && !_playingRadio) {
602 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
604 NSString *curSong = nil;
606 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
608 [[MainController sharedController] networkError:localException];
610 id <NSMenuItem> songItem;
611 ITDebugLog(@"Adding song: %@", curSong);
612 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
614 [songItem setTarget:self];
621 if ([upcomingSongsMenu numberOfItems] == 0) {
622 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
625 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
626 NS_VALUERETURN(upcomingSongsMenu, NSMenu *);
628 [upcomingSongsMenu release];
630 NS_VALUERETURN(nil, NSMenu *);
634 /*- (NSMenu *)playlistsMenu
636 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
638 id <NSMenuItem> tempItem;
639 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
642 playlists = [[[MainController sharedController] currentRemote] playlists];
644 [[MainController sharedController] networkError:localException];
647 ITDebugLog(@"Building \"Playlists\" menu.");
649 for (i = 0; i < [playlists count]; i++) {
650 NSString *curPlaylist = [playlists objectAtIndex:i];
651 ITDebugLog(@"Adding playlist: %@", curPlaylist);
652 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
653 [tempItem setTag:i + 1];
654 [tempItem setTarget:self];
657 if (source == ITMTRemoteRadioSource) {
658 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
659 } else if (source == ITMTRemoteGenericDeviceSource) {
660 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
661 } else if (source == ITMTRemoteiPodSource) {
662 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
663 } else if (source == ITMTRemoteCDSource) {
664 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
665 } else if (source == ITMTRemoteSharedLibrarySource) {
666 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
667 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
668 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
670 ITDebugLog(@"Done Building \"Playlists\" menu");
671 return playlistsMenu;
675 - (NSMenu *)playlistsMenu
677 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
678 NSArray *playlists = nil;
679 id <NSMenuItem> tempItem;
680 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
682 NSMutableArray *indices = [[NSMutableArray alloc] init];
684 playlists = [[[MainController sharedController] currentRemote] playlists];
686 [[MainController sharedController] networkError:localException];
690 [playlistsMenu release];
694 ITDebugLog(@"Building \"Playlists\" menu.");
696 NSArray *curPlaylist = [playlists objectAtIndex:0];
697 NSString *name = [curPlaylist objectAtIndex:0];
698 ITDebugLog(@"Adding main source: %@", name);
699 for (i = 3; i < [curPlaylist count]; i++) {
700 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
701 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
702 [tempItem setTag:i - 1];
703 [tempItem setTarget:self];
705 ITDebugLog(@"Adding index to the index array.");
706 [indices addObject:[curPlaylist objectAtIndex:2]];
709 if ([playlists count] > 1) {
710 if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
711 [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
712 if (source == ITMTRemoteRadioSource) {
713 [playlistsMenu addItem:[NSMenuItem separatorItem]];
714 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
717 [playlistsMenu addItem:[NSMenuItem separatorItem]];
721 if ([playlists count] > 1) {
722 for (i = 1; i < [playlists count]; i++) {
723 NSArray *curPlaylist = [playlists objectAtIndex:i];
724 if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
725 NSString *name = [curPlaylist objectAtIndex:0];
726 NSMenu *submenu = [[NSMenu alloc] init];
727 ITDebugLog(@"Adding source: %@", name);
729 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
730 ITDebugLog(@"Invalid iPod source.");
731 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
733 for (j = 3; j < [curPlaylist count]; j++) {
734 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
735 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
736 [tempItem setTag:(i * 1000) + j - 1];
737 [tempItem setTarget:self];
739 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
741 ITDebugLog(@"Adding index to the index array.");
742 [indices addObject:[curPlaylist objectAtIndex:2]];
746 ITDebugLog(@"Checking the current source.");
748 if (_currentPlaylist != -1) {
749 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
750 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
751 [tempItem setState:NSOnState];
752 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
753 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
754 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
760 tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
761 [tempItem setTarget:self];
762 [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
763 ITDebugLog(@"Done Building \"Playlists\" menu");
764 NS_VALUERETURN(playlistsMenu, NSMenu *);
766 [playlistsMenu release];
768 NS_VALUERETURN(nil, NSMenu *);
774 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
775 NSArray *eqPresets = nil;
776 id <NSMenuItem> tempItem;
780 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
782 [[MainController sharedController] networkError:localException];
785 ITDebugLog(@"Building \"EQ Presets\" menu.");
787 tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
788 [tempItem setTag:-1];
789 [tempItem setTarget:self];
790 [eqMenu addItem:[NSMenuItem separatorItem]];
792 for (i = 0; i < [eqPresets count]; i++) {
794 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
795 ITDebugLog(@"Adding EQ Preset: %@", name);
796 tempItem = [eqMenu addItemWithTitle:name
797 action:@selector(performEqualizerMenuAction:)
800 [tempItem setTarget:self];
803 ITDebugLog(@"Done Building \"EQ Presets\" menu");
807 - (NSMenu *)artistsMenu
809 NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@"Artists"];
810 NSEnumerator *artistsEnumerator;
811 NSString *nextArtist;
812 id <NSMenuItem> tempItem;
813 ITDebugLog(@"Building \"Artists\" menu.");
815 artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
816 while ( (nextArtist = [artistsEnumerator nextObject]) ) {
817 tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
818 [tempItem setTarget:self];
821 [[MainController sharedController] networkError:localException];
823 ITDebugLog(@"Done Building \"Artists\" menu");
827 - (NSMenu *)albumsMenu
829 NSMenu *albumsMenu = [[NSMenu alloc] initWithTitle:@"Albums"];
830 NSEnumerator *albumsEnumerator;
832 id <NSMenuItem> tempItem;
833 ITDebugLog(@"Building \"Albums\" menu.");
835 albumsEnumerator = [[[[MainController sharedController] currentRemote] albums] objectEnumerator];
836 while ( (nextAlbum = [albumsEnumerator nextObject]) ) {
837 tempItem = [albumsMenu addItemWithTitle:nextAlbum action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
838 [tempItem setTarget:self];
841 [[MainController sharedController] networkError:localException];
843 ITDebugLog(@"Done Building \"Albums\" menu");
847 - (void)performMainMenuAction:(id)sender
849 switch ( [sender tag] )
851 case MTMenuPlayPauseItem:
852 ITDebugLog(@"Performing Menu Action: Play/Pause");
853 [[MainController sharedController] playPause];
855 case MTMenuFastForwardItem:
856 ITDebugLog(@"Performing Menu Action: Fast Forward");
857 [[MainController sharedController] fastForward];
859 case MTMenuRewindItem:
860 ITDebugLog(@"Performing Menu Action: Rewind");
861 [[MainController sharedController] rewind];
863 case MTMenuPreviousTrackItem:
864 ITDebugLog(@"Performing Menu Action: Previous Track");
865 [[MainController sharedController] prevSong];
867 case MTMenuNextTrackItem:
868 ITDebugLog(@"Performing Menu Action: Next Track");
869 [[MainController sharedController] nextSong];
871 case MTMenuShowPlayerItem:
872 ITDebugLog(@"Performing Menu Action: Show Main Interface");
873 [[MainController sharedController] showPlayer];
875 case MTMenuPreferencesItem:
876 ITDebugLog(@"Performing Menu Action: Preferences...");
877 [[MainController sharedController] showPreferences];
880 ITDebugLog(@"Performing Menu Action: Quit");
881 [[MainController sharedController] quitMenuTunes];
883 case MTMenuRegisterItem:
884 ITDebugLog(@"Performing Menu Action: Register");
885 [[MainController sharedController] blingNow];
888 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
893 - (void)performRatingMenuAction:(id)sender
895 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
896 [[MainController sharedController] selectSongRating:[sender tag]];
899 - (void)performPlaylistMenuAction:(id)sender
901 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
902 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
905 - (void)performEqualizerMenuAction:(id)sender
907 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
908 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
911 - (void)performUpcomingSongsMenuAction:(id)sender
913 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
914 [[MainController sharedController] selectSongAtIndex:[sender tag]];
917 - (void)performBrowseMenuAction:(id)sender
919 ITDebugLog(@"Browse action selected on item named %@", [sender title]);
925 [[MainController sharedController] makePlaylistWithTerm:[sender title] ofType:(([[[sender menu] title] isEqualToString:@"Artists"]) ? 1 : 2)];
930 ITDebugLog(@"Update Menu");
931 [_currentMenu update];
934 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
939 //This is never used I know, keep it though
940 - (NSString *)systemUIColor
942 NSDictionary *tmpDict;
944 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
945 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
946 if ( ([tmpNumber intValue] == 1) ) {
959 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
960 onItem:(id <NSMenuItem>)item
962 unichar charcode = 'a';
964 long cocoaModifiers = 0;
965 static long carbonToCocoa[6][2] =
967 { cmdKey, NSCommandKeyMask },
968 { optionKey, NSAlternateKeyMask },
969 { controlKey, NSControlKeyMask },
970 { shiftKey, NSShiftKeyMask },
973 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
975 for (i = 0; i < 6; i++) {
976 if (modifiers & carbonToCocoa[i][0]) {
977 cocoaModifiers += carbonToCocoa[i][1];
980 [item setKeyEquivalentModifierMask:cocoaModifiers];
982 //Missing key combos for some keys. Must find them later.
986 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
991 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
998 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
999 // Haven't tested this, though it should work.
1000 // This doesn't work. :'(
1002 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
1003 //charcode = buffer;
1004 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1005 ITDebugLog(@"%@", menuRef);
1006 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1007 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1008 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1011 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
1017 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
1018 charcode = NSDeleteFunctionKey;
1022 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
1027 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
1032 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
1037 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
1038 charcode = NSF5FunctionKey;
1042 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
1043 charcode = NSF6FunctionKey;
1047 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
1048 charcode = NSF7FunctionKey;
1052 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
1053 charcode = NSF3FunctionKey;
1057 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
1058 charcode = NSF8FunctionKey;
1062 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
1063 charcode = NSF9FunctionKey;
1067 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1068 charcode = NSF11FunctionKey;
1072 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1073 charcode = NSF13FunctionKey;
1077 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1078 charcode = NSF14FunctionKey;
1082 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1083 charcode = NSF10FunctionKey;
1087 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1088 charcode = NSF12FunctionKey;
1092 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1093 charcode = NSF13FunctionKey;
1097 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1098 charcode = NSInsertFunctionKey;
1102 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1103 charcode = NSHomeFunctionKey;
1107 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1108 charcode = NSPageUpFunctionKey;
1112 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1113 charcode = NSDeleteFunctionKey;
1117 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1118 charcode = NSF4FunctionKey;
1122 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1123 charcode = NSEndFunctionKey;
1127 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1128 charcode = NSF2FunctionKey;
1132 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1133 charcode = NSPageDownFunctionKey;
1137 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1138 charcode = NSF1FunctionKey;
1142 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1143 charcode = NSLeftArrowFunctionKey;
1147 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1148 charcode = NSRightArrowFunctionKey;
1152 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1153 charcode = NSDownArrowFunctionKey;
1157 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1158 charcode = NSUpArrowFunctionKey;
1162 if (charcode == 'a') {
1163 unsigned long state;
1168 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1169 keyTrans = KeyTranslate(kchr, code, &state);
1170 charCode = keyTrans;
1171 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1172 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1173 } else if (charcode != 'b') {
1174 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1176 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1179 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1181 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1182 NSEnumerator *volEnum = [volumes objectEnumerator];
1183 NSString *nextVolume;
1184 ITDebugLog(@"Looking for an iPod named %@", name);
1185 while ( (nextVolume = [volEnum nextObject]) ) {
1186 ITDebugLog(@"- %@", nextVolume);
1187 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1188 NSFileHandle *handle;
1190 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1191 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1192 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1195 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1196 ITDebugLog(@"File handle: %@", handle);
1197 [handle seekToFileOffset:10];
1198 data = [handle readDataOfLength:1];
1199 ITDebugLog(@"Data: %@", data);
1200 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1201 ITDebugLog(@"iPod is manually updated. %@", path);
1203 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1204 ITDebugLog(@"iPod is automatically updated. %@", path);
1207 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);