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 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
25 onItem:(id <NSMenuItem>)item;
26 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
29 @implementation MenuController
33 if ( (self = [super init]) ) {
34 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
41 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
42 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
43 NSArray *menuArray = [defaults arrayForKey:@"menu"];
44 NSEnumerator *enumerator = [menuArray objectEnumerator];
46 id <NSMenuItem> tempItem;
47 NSEnumerator *itemEnum;
49 NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
50 ITMTRemote *mtr = [[MainController sharedController] currentRemote];
51 int currentSongRating = 0;
55 _currentPlaylist = [mtr currentPlaylistIndex];
56 _playingRadio = ([mtr currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
57 currentSongRating = ( [mtr currentSongRating] != -1 );
59 [[MainController sharedController] networkError:localException];
62 ITDebugLog(@"Reset menu if required.");
64 //Kill the old submenu items
65 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
66 ITDebugLog(@"Removing \"Song Rating\" submenu.");
67 [tempItem setSubmenu:nil];
70 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
71 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
72 [tempItem setSubmenu:nil];
75 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
76 ITDebugLog(@"Removing \"Playlists\" submenu.");
77 [tempItem setSubmenu:nil];
80 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
81 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
82 [tempItem setSubmenu:nil];
85 ITDebugLog(@"Begin building menu.");
88 while ( (nextObject = [enumerator nextObject]) ) {
90 if ([nextObject isEqualToString:@"playPause"]) {
91 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
92 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
93 action:@selector(performMainMenuAction:)
95 [tempItem setTag:MTMenuPlayPauseItem];
96 [tempItem setTarget:self];
98 itemEnum = [hotKeys objectEnumerator];
99 while ( (hotKey = [itemEnum nextObject]) ) {
100 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
101 ITKeyCombo *combo = [hotKey keyCombo];
102 [self setKeyEquivalentForCode:[combo keyCode]
103 andModifiers:[combo modifiers]
108 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
110 switch ([mtr playerPlayingState]) {
111 case ITMTRemotePlayerPlaying:
112 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
114 case ITMTRemotePlayerRewinding:
115 case ITMTRemotePlayerForwarding:
116 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
122 [[MainController sharedController] networkError:localException];
124 } else if ([nextObject isEqualToString:@"nextTrack"]) {
125 ITDebugLog(@"Add \"Next Track\" menu item.");
126 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
127 action:@selector(performMainMenuAction:)
130 itemEnum = [hotKeys objectEnumerator];
131 while ( (hotKey = [itemEnum nextObject]) ) {
132 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
133 ITKeyCombo *combo = [hotKey keyCombo];
134 [self setKeyEquivalentForCode:[combo keyCode]
135 andModifiers:[combo modifiers]
140 if (_currentPlaylist) {
141 [tempItem setTag:MTMenuNextTrackItem];
142 [tempItem setTarget:self];
144 } else if ([nextObject isEqualToString:@"prevTrack"]) {
145 ITDebugLog(@"Add \"Previous Track\" menu item.");
146 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
147 action:@selector(performMainMenuAction:)
150 itemEnum = [hotKeys objectEnumerator];
151 while ( (hotKey = [itemEnum nextObject]) ) {
152 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
153 ITKeyCombo *combo = [hotKey keyCombo];
154 [self setKeyEquivalentForCode:[combo keyCode]
155 andModifiers:[combo modifiers]
160 if (_currentPlaylist) {
161 [tempItem setTag:MTMenuPreviousTrackItem];
162 [tempItem setTarget:self];
164 } else if ([nextObject isEqualToString:@"fastForward"]) {
165 ITDebugLog(@"Add \"Fast Forward\" menu item.");
166 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
167 action:@selector(performMainMenuAction:)
169 if (_currentPlaylist) {
170 [tempItem setTag:MTMenuFastForwardItem];
171 [tempItem setTarget:self];
173 } else if ([nextObject isEqualToString:@"rewind"]) {
174 ITDebugLog(@"Add \"Rewind\" menu item.");
175 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
176 action:@selector(performMainMenuAction:)
178 if (_currentPlaylist) {
179 [tempItem setTag:MTMenuRewindItem];
180 [tempItem setTarget:self];
182 } else if ([nextObject isEqualToString:@"showPlayer"]) {
183 ITDebugLog(@"Add \"Show Player\" menu item.");
185 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
186 NSLocalizedString(@"show", @"Show"),
187 [mtr playerSimpleName]]
188 action:@selector(performMainMenuAction:)
191 [[MainController sharedController] networkError:localException];
194 itemEnum = [hotKeys objectEnumerator];
195 while ( (hotKey = [itemEnum nextObject]) ) {
196 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
197 ITKeyCombo *combo = [hotKey keyCombo];
198 [self setKeyEquivalentForCode:[combo keyCode]
199 andModifiers:[combo modifiers]
204 [tempItem setTarget:self];
205 [tempItem setTag:MTMenuShowPlayerItem];
206 } else if ([nextObject isEqualToString:@"preferences"]) {
207 ITDebugLog(@"Add \"Preferences...\" menu item.");
208 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
209 action:@selector(performMainMenuAction:)
211 [tempItem setTag:MTMenuPreferencesItem];
212 [tempItem setTarget:self];
213 } else if ([nextObject isEqualToString:@"quit"]) {
214 if ([[MainController sharedController] blingBling] == NO) {
215 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
216 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
217 [tempItem setTag:MTMenuRegisterItem];
218 [tempItem setTarget:self];
220 ITDebugLog(@"Add \"Quit\" menu item.");
221 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
222 action:@selector(performMainMenuAction:)
224 [tempItem setTag:MTMenuQuitItem];
225 [tempItem setTarget:self];
226 } else if ([nextObject isEqualToString:@"trackInfo"]) {
227 ITDebugLog(@"Check to see if a Track is playing...");
228 //Handle playing radio too
229 if (_currentPlaylist) {
230 NSString *title = nil;
232 title = [mtr currentSongTitle];
234 [[MainController sharedController] networkError:localException];
236 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
237 ITDebugLog(@"Add \"Now Playing\" menu item.");
238 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
240 if ([title length] > 0) {
241 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
243 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
246 if (!_playingRadio) {
247 if ([defaults boolForKey:@"showAlbum"]) {
248 NSString *curAlbum = nil;
250 curAlbum = [mtr currentSongAlbum];
252 [[MainController sharedController] networkError:localException];
254 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
257 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
261 if ([defaults boolForKey:@"showArtist"]) {
262 NSString *curArtist = nil;
264 curArtist = [mtr currentSongArtist];
266 [[MainController sharedController] networkError:localException];
268 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
271 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
275 if ([defaults boolForKey:@"showComposer"]) {
276 NSString *curComposer = nil;
278 curComposer = [mtr currentSongComposer];
280 [[MainController sharedController] networkError:localException];
282 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
285 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
289 if ([defaults boolForKey:@"showTrackNumber"]) {
292 track = [mtr currentSongTrack];
294 [[MainController sharedController] networkError:localException];
296 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
299 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
305 if ([defaults boolForKey:@"showTime"] && ( ([mtr currentSongElapsed] != nil) || ([mtr currentSongLength] != nil) )) {
306 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [mtr currentSongElapsed], [mtr currentSongLength]);
307 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [mtr currentSongElapsed], [mtr currentSongLength]] action:nil keyEquivalent:@""]];
310 [[MainController sharedController] networkError:localException];
313 if (!_playingRadio) {
315 if ([defaults boolForKey:@"showPlayCount"]) {
316 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"Play Count: %i", [mtr currentSongPlayCount]] action:nil keyEquivalent:@""]];
318 if ([defaults boolForKey:@"showTrackRating"] && ( [mtr currentSongRating] != -1.0 )) {
319 NSString *string = nil;
320 switch ((int)([mtr currentSongRating] * 5)) {
322 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
325 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
328 string = [NSString stringWithUTF8String:"★★☆☆☆"];
331 string = [NSString stringWithUTF8String:"★★★☆☆"];
334 string = [NSString stringWithUTF8String:"★★★★☆"];
337 string = [NSString stringWithUTF8String:"★★★★★"];
340 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
341 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
344 [[MainController sharedController] networkError:localException];
347 /*if ([tempItem respondsToSelector:@selector(setAttributedTitle:)] && [defaults boolForKey:@"showAlbumArtwork"] && ![[NetworkController sharedController] isConnectedToServer]) {
348 NSImage *image = [mtr currentSongAlbumArt];
350 NSSize oldSize, newSize;
351 oldSize = [image size];
352 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
353 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
354 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
356 tempItem = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
357 NSTextAttachment *attachment = [[[NSTextAttachment alloc] init] autorelease];
358 [[attachment attachmentCell] setImage:image];
359 NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
360 [tempItem setAttributedTitle:attrString];
365 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
366 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
368 } else if ([nextObject isEqualToString:@"separator"]) {
369 ITDebugLog(@"Add a separator menu item.");
370 [menu addItem:[NSMenuItem separatorItem]];
372 } else if ([nextObject isEqualToString:@"playlists"]) {
373 ITDebugLog(@"Add \"Playlists\" submenu.");
374 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
377 [tempItem setSubmenu:_playlistsMenu];
379 } else if ([nextObject isEqualToString:@"eqPresets"]) {
380 ITDebugLog(@"Add \"EQ Presets\" submenu.");
381 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
384 [tempItem setSubmenu:_eqMenu];
387 itemEnum = [[_eqMenu itemArray] objectEnumerator];
388 while ( (tempItem = [itemEnum nextObject]) ) {
389 [tempItem setState:NSOffState];
392 [[_eqMenu itemAtIndex:0] setState:[mtr equalizerEnabled] ? NSOnState : NSOffState];
393 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] + 1)] setState:NSOnState];
394 [[_eqMenu itemAtIndex:([mtr currentEQPresetIndex] - 1)] setState:NSOnState];
396 [[MainController sharedController] networkError:localException];
398 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
399 ITDebugLog(@"Add \"Song Rating\" submenu.");
400 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
403 [tempItem setSubmenu:_ratingMenu];
405 if (_playingRadio || !_currentPlaylist) {
406 [tempItem setEnabled:NO];
409 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
410 while ( (tempItem = [itemEnum nextObject]) ) {
411 [tempItem setState:NSOffState];
415 [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
417 [[MainController sharedController] networkError:localException];
419 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
420 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
421 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
424 [tempItem setSubmenu:_upcomingSongsMenu];
426 if (_playingRadio || !_currentPlaylist) {
427 [tempItem setEnabled:NO];
431 ITDebugLog(@"Finished building menu.");
432 [_currentMenu release];
437 - (NSMenu *)menuForNoPlayer
439 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
440 id <NSMenuItem> tempItem = nil;
441 ITDebugLog(@"Creating menu for when player isn't running.");
443 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
444 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
446 [[MainController sharedController] networkError:localException];
448 [tempItem setTag:MTMenuShowPlayerItem];
449 [tempItem setTarget:self];
450 ITDebugLog(@"Add a separator menu item.");
451 [menu addItem:[NSMenuItem separatorItem]];
452 ITDebugLog(@"Add \"Preferences...\" menu item.");
453 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
454 [tempItem setTag:MTMenuPreferencesItem];
455 [tempItem setTarget:self];
456 if ([[MainController sharedController] blingBling] == NO) {
457 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
458 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
459 [tempItem setTag:MTMenuRegisterItem];
460 [tempItem setTarget:self];
462 ITDebugLog(@"Add \"Quit\" menu item.");
463 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
464 [tempItem setTag:MTMenuQuitItem];
465 [tempItem setTarget:self];
466 return [menu autorelease];
469 - (void)rebuildSubmenus
471 ITDebugLog(@"Rebuilding all of the submenus.");
473 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
474 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
475 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
477 [[MainController sharedController] networkError:localException];
479 ITDebugLog(@"Releasing old submenus.");
480 ITDebugLog(@" - Rating menu");
481 [_ratingMenu release];
482 ITDebugLog(@" - Upcoming songs menu");
483 [_upcomingSongsMenu release];
484 ITDebugLog(@" - Playlists menu");
485 [_playlistsMenu release];
486 ITDebugLog(@" - EQ menu");
488 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
489 _ratingMenu = [self ratingMenu];
490 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
491 _upcomingSongsMenu = [self upcomingSongsMenu];
492 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
493 _playlistsMenu = [self playlistsMenu];
494 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
495 _eqMenu = [self eqMenu];
496 ITDebugLog(@"Done rebuilding all of the submenus.");
499 - (NSMenu *)ratingMenu
501 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
502 NSEnumerator *itemEnum;
505 SEL itemSelector = @selector(performRatingMenuAction:);
507 ITDebugLog(@"Building \"Song Rating\" menu.");
509 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
510 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
511 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
512 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
513 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
514 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
516 itemEnum = [[ratingMenu itemArray] objectEnumerator];
517 while ( (anItem = [itemEnum nextObject]) ) {
518 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
519 [anItem setAction:itemSelector];
520 [anItem setTarget:self];
521 [anItem setTag:itemTag];
524 ITDebugLog(@"Done Building \"Song Rating\" menu.");
528 - (NSMenu *)upcomingSongsMenu
530 NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
531 int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
534 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
536 [[MainController sharedController] networkError:localException];
539 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
540 if (_currentPlaylist && !_playingRadio) {
543 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
545 NSString *curSong = nil;
547 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
549 [[MainController sharedController] networkError:localException];
551 id <NSMenuItem> songItem;
552 ITDebugLog(@"Adding song: %@", curSong);
553 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
555 [songItem setTarget:self];
562 if ([upcomingSongsMenu numberOfItems] == 0) {
563 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
566 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
567 return upcomingSongsMenu;
570 /*- (NSMenu *)playlistsMenu
572 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
574 id <NSMenuItem> tempItem;
575 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
578 playlists = [[[MainController sharedController] currentRemote] playlists];
580 [[MainController sharedController] networkError:localException];
583 ITDebugLog(@"Building \"Playlists\" menu.");
585 for (i = 0; i < [playlists count]; i++) {
586 NSString *curPlaylist = [playlists objectAtIndex:i];
587 ITDebugLog(@"Adding playlist: %@", curPlaylist);
588 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
589 [tempItem setTag:i + 1];
590 [tempItem setTarget:self];
593 if (source == ITMTRemoteRadioSource) {
594 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
595 } else if (source == ITMTRemoteGenericDeviceSource) {
596 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
597 } else if (source == ITMTRemoteiPodSource) {
598 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
599 } else if (source == ITMTRemoteCDSource) {
600 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
601 } else if (source == ITMTRemoteSharedLibrarySource) {
602 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
603 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
604 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
606 ITDebugLog(@"Done Building \"Playlists\" menu");
607 return playlistsMenu;
611 - (NSMenu *)playlistsMenu
613 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
614 NSArray *playlists = nil;
615 id <NSMenuItem> tempItem;
616 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
618 NSMutableArray *indices = [[NSMutableArray alloc] init];
620 playlists = [[[MainController sharedController] currentRemote] playlists];
622 [[MainController sharedController] networkError:localException];
624 ITDebugLog(@"Building \"Playlists\" menu.");
626 NSArray *curPlaylist = [playlists objectAtIndex:0];
627 NSString *name = [curPlaylist objectAtIndex:0];
628 ITDebugLog(@"Adding main source: %@", name);
629 for (i = 3; i < [curPlaylist count]; i++) {
630 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
631 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
632 [tempItem setTag:i - 1];
633 [tempItem setTarget:self];
635 ITDebugLog(@"Adding index to the index array.");
636 [indices addObject:[curPlaylist objectAtIndex:2]];
638 if ([playlists count] > 1) {
639 if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
640 [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
641 if (source == ITMTRemoteRadioSource) {
642 [playlistsMenu addItem:[NSMenuItem separatorItem]];
643 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
646 [playlistsMenu addItem:[NSMenuItem separatorItem]];
650 if ([playlists count] > 1) {
651 for (i = 1; i < [playlists count]; i++) {
652 NSArray *curPlaylist = [playlists objectAtIndex:i];
653 if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
654 NSString *name = [curPlaylist objectAtIndex:0];
655 NSMenu *submenu = [[NSMenu alloc] init];
656 ITDebugLog(@"Adding source: %@", name);
658 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
659 ITDebugLog(@"Invalid iPod source.");
660 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
662 for (j = 3; j < [curPlaylist count]; j++) {
663 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
664 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
665 [tempItem setTag:(i * 1000) + j - 1];
666 [tempItem setTarget:self];
668 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
670 ITDebugLog(@"Adding index to the index array.");
671 [indices addObject:[curPlaylist objectAtIndex:2]];
675 ITDebugLog(@"Checking the current source.");
676 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
677 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
678 [tempItem setState:NSOnState];
679 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
680 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
681 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
684 [playlistsMenu addItem:[NSMenuItem separatorItem]];
685 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""] setTarget:self];
686 ITDebugLog(@"Done Building \"Playlists\" menu");
687 return playlistsMenu;
692 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
693 NSArray *eqPresets = nil;
694 id <NSMenuItem> tempItem;
698 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
700 [[MainController sharedController] networkError:localException];
703 ITDebugLog(@"Building \"EQ Presets\" menu.");
705 tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
706 [tempItem setTag:-1];
707 [tempItem setTarget:self];
709 [tempItem setState:[[[MainController sharedController] currentRemote] equalizerEnabled] ? NSOnState : NSOffState];
711 [[MainController sharedController] networkError:localException];
713 [eqMenu addItem:[NSMenuItem separatorItem]];
715 for (i = 0; i < [eqPresets count]; i++) {
717 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
718 ITDebugLog(@"Adding EQ Preset: %@", name);
719 tempItem = [eqMenu addItemWithTitle:name
720 action:@selector(performEqualizerMenuAction:)
723 [tempItem setTarget:self];
726 ITDebugLog(@"Done Building \"EQ Presets\" menu");
730 - (void)performMainMenuAction:(id)sender
732 switch ( [sender tag] )
734 case MTMenuPlayPauseItem:
735 ITDebugLog(@"Performing Menu Action: Play/Pause");
736 [[MainController sharedController] playPause];
738 case MTMenuFastForwardItem:
739 ITDebugLog(@"Performing Menu Action: Fast Forward");
740 [[MainController sharedController] fastForward];
742 case MTMenuRewindItem:
743 ITDebugLog(@"Performing Menu Action: Rewind");
744 [[MainController sharedController] rewind];
746 case MTMenuPreviousTrackItem:
747 ITDebugLog(@"Performing Menu Action: Previous Track");
748 [[MainController sharedController] prevSong];
750 case MTMenuNextTrackItem:
751 ITDebugLog(@"Performing Menu Action: Next Track");
752 [[MainController sharedController] nextSong];
754 case MTMenuShowPlayerItem:
755 ITDebugLog(@"Performing Menu Action: Show Main Interface");
756 [[MainController sharedController] showPlayer];
758 case MTMenuPreferencesItem:
759 ITDebugLog(@"Performing Menu Action: Preferences...");
760 [[MainController sharedController] showPreferences];
763 ITDebugLog(@"Performing Menu Action: Quit");
764 [[MainController sharedController] quitMenuTunes];
766 case MTMenuRegisterItem:
767 ITDebugLog(@"Performing Menu Action: Register");
768 [[MainController sharedController] blingNow];
771 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
776 - (void)performRatingMenuAction:(id)sender
778 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
779 [[MainController sharedController] selectSongRating:[sender tag]];
782 - (void)performPlaylistMenuAction:(id)sender
784 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
785 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
788 - (void)performEqualizerMenuAction:(id)sender
790 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
791 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
794 - (void)performUpcomingSongsMenuAction:(id)sender
796 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
797 [[MainController sharedController] selectSongAtIndex:[sender tag]];
802 ITDebugLog(@"Update Menu");
803 [_currentMenu update];
806 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
811 //This is never used I know, keep it though
812 - (NSString *)systemUIColor
814 NSDictionary *tmpDict;
816 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
817 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
818 if ( ([tmpNumber intValue] == 1) ) {
831 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
832 onItem:(id <NSMenuItem>)item
834 unichar charcode = 'a';
836 long cocoaModifiers = 0;
837 static long carbonToCocoa[6][2] =
839 { cmdKey, NSCommandKeyMask },
840 { optionKey, NSAlternateKeyMask },
841 { controlKey, NSControlKeyMask },
842 { shiftKey, NSShiftKeyMask },
845 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
847 for (i = 0; i < 6; i++) {
848 if (modifiers & carbonToCocoa[i][0]) {
849 cocoaModifiers += carbonToCocoa[i][1];
852 [item setKeyEquivalentModifierMask:cocoaModifiers];
854 //Missing key combos for some keys. Must find them later.
858 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
863 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
870 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
871 // Haven't tested this, though it should work.
872 // This doesn't work. :'(
874 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
876 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
877 ITDebugLog(@"%@", menuRef);
878 SetMenuItemCommandKey(menuRef, 0, NO, 49);
879 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
880 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
883 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
889 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
890 charcode = NSDeleteFunctionKey;
894 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
899 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
904 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
909 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
910 charcode = NSF5FunctionKey;
914 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
915 charcode = NSF6FunctionKey;
919 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
920 charcode = NSF7FunctionKey;
924 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
925 charcode = NSF3FunctionKey;
929 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
930 charcode = NSF8FunctionKey;
934 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
935 charcode = NSF9FunctionKey;
939 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
940 charcode = NSF11FunctionKey;
944 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
945 charcode = NSF13FunctionKey;
949 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
950 charcode = NSF14FunctionKey;
954 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
955 charcode = NSF10FunctionKey;
959 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
960 charcode = NSF12FunctionKey;
964 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
965 charcode = NSF13FunctionKey;
969 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
970 charcode = NSInsertFunctionKey;
974 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
975 charcode = NSHomeFunctionKey;
979 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
980 charcode = NSPageUpFunctionKey;
984 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
985 charcode = NSDeleteFunctionKey;
989 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
990 charcode = NSF4FunctionKey;
994 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
995 charcode = NSEndFunctionKey;
999 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1000 charcode = NSF2FunctionKey;
1004 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1005 charcode = NSPageDownFunctionKey;
1009 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1010 charcode = NSF1FunctionKey;
1014 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1015 charcode = NSLeftArrowFunctionKey;
1019 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1020 charcode = NSRightArrowFunctionKey;
1024 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1025 charcode = NSDownArrowFunctionKey;
1029 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1030 charcode = NSUpArrowFunctionKey;
1034 if (charcode == 'a') {
1035 unsigned long state;
1040 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1041 keyTrans = KeyTranslate(kchr, code, &state);
1042 charCode = keyTrans;
1043 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1044 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1045 } else if (charcode != 'b') {
1046 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1048 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1051 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1053 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1054 NSEnumerator *volEnum = [volumes objectEnumerator];
1055 NSString *nextVolume;
1056 ITDebugLog(@"Looking for an iPod named %@", name);
1057 while ( (nextVolume = [volEnum nextObject]) ) {
1058 ITDebugLog(@"- %@", nextVolume);
1059 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1060 NSFileHandle *handle;
1062 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1063 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1064 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1067 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1068 ITDebugLog(@"File handle: %@", handle);
1069 [handle seekToFileOffset:10];
1070 data = [handle readDataOfLength:1];
1071 ITDebugLog(@"Data: %@", data);
1072 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1073 ITDebugLog(@"iPod is manually updated. %@", path);
1075 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1076 ITDebugLog(@"iPod is automatically updated. %@", path);
1079 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);