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 int currentSongRating = 0;
54 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
55 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
56 currentSongRating = ( [[[MainController sharedController] currentRemote] currentSongRating] != -1 );
58 [[MainController sharedController] networkError:localException];
61 ITDebugLog(@"Reset menu if required.");
63 //Kill the old submenu items
64 if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
65 ITDebugLog(@"Removing \"Song Rating\" submenu.");
66 [tempItem setSubmenu:nil];
69 if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
70 ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
71 [tempItem setSubmenu:nil];
74 if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
75 ITDebugLog(@"Removing \"Playlists\" submenu.");
76 [tempItem setSubmenu:nil];
79 if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
80 ITDebugLog(@"Removing \"EQ Presets\" submenu.");
81 [tempItem setSubmenu:nil];
84 ITDebugLog(@"Begin building menu.");
87 while ( (nextObject = [enumerator nextObject]) ) {
89 if ([nextObject isEqualToString:@"playPause"]) {
90 ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
91 tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
92 action:@selector(performMainMenuAction:)
94 [tempItem setTag:MTMenuPlayPauseItem];
95 [tempItem setTarget:self];
97 itemEnum = [hotKeys objectEnumerator];
98 while ( (hotKey = [itemEnum nextObject]) ) {
99 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
100 ITKeyCombo *combo = [hotKey keyCombo];
101 [self setKeyEquivalentForCode:[combo keyCode]
102 andModifiers:[combo modifiers]
107 ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
109 switch ([[[MainController sharedController] currentRemote] playerPlayingState]) {
110 case ITMTRemotePlayerPlaying:
111 [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
113 case ITMTRemotePlayerRewinding:
114 case ITMTRemotePlayerForwarding:
115 [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
121 [[MainController sharedController] networkError:localException];
123 } else if ([nextObject isEqualToString:@"nextTrack"]) {
124 ITDebugLog(@"Add \"Next Track\" menu item.");
125 tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
126 action:@selector(performMainMenuAction:)
129 itemEnum = [hotKeys objectEnumerator];
130 while ( (hotKey = [itemEnum nextObject]) ) {
131 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
132 ITKeyCombo *combo = [hotKey keyCombo];
133 [self setKeyEquivalentForCode:[combo keyCode]
134 andModifiers:[combo modifiers]
139 if (_currentPlaylist) {
140 [tempItem setTag:MTMenuNextTrackItem];
141 [tempItem setTarget:self];
143 } else if ([nextObject isEqualToString:@"prevTrack"]) {
144 ITDebugLog(@"Add \"Previous Track\" menu item.");
145 tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
146 action:@selector(performMainMenuAction:)
149 itemEnum = [hotKeys objectEnumerator];
150 while ( (hotKey = [itemEnum nextObject]) ) {
151 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
152 ITKeyCombo *combo = [hotKey keyCombo];
153 [self setKeyEquivalentForCode:[combo keyCode]
154 andModifiers:[combo modifiers]
159 if (_currentPlaylist) {
160 [tempItem setTag:MTMenuPreviousTrackItem];
161 [tempItem setTarget:self];
163 } else if ([nextObject isEqualToString:@"fastForward"]) {
164 ITDebugLog(@"Add \"Fast Forward\" menu item.");
165 tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
166 action:@selector(performMainMenuAction:)
168 if (_currentPlaylist) {
169 [tempItem setTag:MTMenuFastForwardItem];
170 [tempItem setTarget:self];
172 } else if ([nextObject isEqualToString:@"rewind"]) {
173 ITDebugLog(@"Add \"Rewind\" menu item.");
174 tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
175 action:@selector(performMainMenuAction:)
177 if (_currentPlaylist) {
178 [tempItem setTag:MTMenuRewindItem];
179 [tempItem setTarget:self];
181 } else if ([nextObject isEqualToString:@"showPlayer"]) {
182 ITDebugLog(@"Add \"Show Player\" menu item.");
184 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
185 NSLocalizedString(@"show", @"Show"),
186 [[[MainController sharedController] currentRemote] playerSimpleName]]
187 action:@selector(performMainMenuAction:)
190 [[MainController sharedController] networkError:localException];
193 itemEnum = [hotKeys objectEnumerator];
194 while ( (hotKey = [itemEnum nextObject]) ) {
195 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
196 ITKeyCombo *combo = [hotKey keyCombo];
197 [self setKeyEquivalentForCode:[combo keyCode]
198 andModifiers:[combo modifiers]
203 [tempItem setTarget:self];
204 [tempItem setTag:MTMenuShowPlayerItem];
205 } else if ([nextObject isEqualToString:@"preferences"]) {
206 ITDebugLog(@"Add \"Preferences...\" menu item.");
207 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
208 action:@selector(performMainMenuAction:)
210 [tempItem setTag:MTMenuPreferencesItem];
211 [tempItem setTarget:self];
212 } else if ([nextObject isEqualToString:@"quit"]) {
213 if ([[MainController sharedController] blingBling] == NO) {
214 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
215 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
216 [tempItem setTag:MTMenuRegisterItem];
217 [tempItem setTarget:self];
219 ITDebugLog(@"Add \"Quit\" menu item.");
220 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
221 action:@selector(performMainMenuAction:)
223 [tempItem setTag:MTMenuQuitItem];
224 [tempItem setTarget:self];
225 } else if ([nextObject isEqualToString:@"trackInfo"]) {
226 ITDebugLog(@"Check to see if a Track is playing...");
227 //Handle playing radio too
228 if (_currentPlaylist) {
229 NSString *title = nil;
231 title = [[[MainController sharedController] currentRemote] currentSongTitle];
233 [[MainController sharedController] networkError:localException];
235 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
236 ITDebugLog(@"Add \"Now Playing\" menu item.");
237 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
239 if ([title length] > 0) {
240 ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
242 [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
245 if (!_playingRadio) {
246 if ([defaults boolForKey:@"showAlbum"]) {
247 NSString *curAlbum = nil;
249 curAlbum = [[[MainController sharedController] currentRemote] currentSongAlbum];
251 [[MainController sharedController] networkError:localException];
253 ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
256 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
260 if ([defaults boolForKey:@"showArtist"]) {
261 NSString *curArtist = nil;
263 curArtist = [[[MainController sharedController] currentRemote] currentSongArtist];
265 [[MainController sharedController] networkError:localException];
267 ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
270 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
274 if ([defaults boolForKey:@"showComposer"]) {
275 NSString *curComposer = nil;
277 curComposer = [[[MainController sharedController] currentRemote] currentSongComposer];
279 [[MainController sharedController] networkError:localException];
281 ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
284 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
288 if ([defaults boolForKey:@"showTrackNumber"]) {
291 track = [[[MainController sharedController] currentRemote] currentSongTrack];
293 [[MainController sharedController] networkError:localException];
295 ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
298 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
304 if ([defaults boolForKey:@"showTime"] && ( ([[[MainController sharedController] currentRemote] currentSongElapsed] != nil) || ([[[MainController sharedController] currentRemote] currentSongLength] != nil) )) {
305 ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]);
306 [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]] action:nil keyEquivalent:@""]];
309 [[MainController sharedController] networkError:localException];
312 if (!_playingRadio) {
314 if ([defaults boolForKey:@"showTrackRating"] && ( [[[MainController sharedController] currentRemote] currentSongRating] != -1.0 )) {
315 NSString *string = nil;
316 switch ((int)([[[MainController sharedController] currentRemote] currentSongRating] * 5)) {
318 string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
321 string = [NSString stringWithUTF8String:"★☆☆☆☆"];
324 string = [NSString stringWithUTF8String:"★★☆☆☆"];
327 string = [NSString stringWithUTF8String:"★★★☆☆"];
330 string = [NSString stringWithUTF8String:"★★★★☆"];
333 string = [NSString stringWithUTF8String:"★★★★★"];
336 ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
337 [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
340 [[MainController sharedController] networkError:localException];
343 /*if ([tempItem respondsToSelector:@selector(setAttributedTitle:)] && [defaults boolForKey:@"showAlbumArtwork"] && ![[NetworkController sharedController] isConnectedToServer]) {
344 NSImage *image = [[[MainController sharedController] currentRemote] currentSongAlbumArt];
346 NSSize oldSize, newSize;
347 oldSize = [image size];
348 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
349 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
350 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
352 tempItem = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
353 NSTextAttachment *attachment = [[[NSTextAttachment alloc] init] autorelease];
354 [[attachment attachmentCell] setImage:image];
355 NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
356 [tempItem setAttributedTitle:attrString];
361 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
362 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
364 } else if ([nextObject isEqualToString:@"separator"]) {
365 ITDebugLog(@"Add a separator menu item.");
366 [menu addItem:[NSMenuItem separatorItem]];
368 } else if ([nextObject isEqualToString:@"playlists"]) {
369 ITDebugLog(@"Add \"Playlists\" submenu.");
370 tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
373 [tempItem setSubmenu:_playlistsMenu];
375 } else if ([nextObject isEqualToString:@"eqPresets"]) {
376 ITDebugLog(@"Add \"EQ Presets\" submenu.");
377 tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
380 [tempItem setSubmenu:_eqMenu];
383 itemEnum = [[_eqMenu itemArray] objectEnumerator];
384 while ( (tempItem = [itemEnum nextObject]) ) {
385 [tempItem setState:NSOffState];
388 [[_eqMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentEQPresetIndex] - 1)] setState:NSOnState];
390 [[MainController sharedController] networkError:localException];
392 } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
393 ITDebugLog(@"Add \"Song Rating\" submenu.");
394 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
397 [tempItem setSubmenu:_ratingMenu];
399 if (_playingRadio || !_currentPlaylist) {
400 [tempItem setEnabled:NO];
403 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
404 while ( (tempItem = [itemEnum nextObject]) ) {
405 [tempItem setState:NSOffState];
409 [[_ratingMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentSongRating] * 5)] setState:NSOnState];
411 [[MainController sharedController] networkError:localException];
413 } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
414 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
415 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
418 [tempItem setSubmenu:_upcomingSongsMenu];
420 if (_playingRadio || !_currentPlaylist) {
421 [tempItem setEnabled:NO];
425 ITDebugLog(@"Finished building menu.");
426 [_currentMenu release];
431 - (NSMenu *)menuForNoPlayer
433 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
434 id <NSMenuItem> tempItem = nil;
435 ITDebugLog(@"Creating menu for when player isn't running.");
437 ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
438 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
440 [[MainController sharedController] networkError:localException];
442 [tempItem setTag:MTMenuShowPlayerItem];
443 [tempItem setTarget:self];
444 ITDebugLog(@"Add a separator menu item.");
445 [menu addItem:[NSMenuItem separatorItem]];
446 ITDebugLog(@"Add \"Preferences...\" menu item.");
447 tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
448 [tempItem setTag:MTMenuPreferencesItem];
449 [tempItem setTarget:self];
450 if ([[MainController sharedController] blingBling] == NO) {
451 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
452 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
453 [tempItem setTag:MTMenuRegisterItem];
454 [tempItem setTarget:self];
456 ITDebugLog(@"Add \"Quit\" menu item.");
457 tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
458 [tempItem setTag:MTMenuQuitItem];
459 [tempItem setTarget:self];
460 return [menu autorelease];
463 - (void)rebuildSubmenus
465 ITDebugLog(@"Rebuilding all of the submenus.");
467 _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
468 _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
469 _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
471 [[MainController sharedController] networkError:localException];
473 ITDebugLog(@"Releasing old submenus.");
474 [_ratingMenu release];
475 [_upcomingSongsMenu release];
476 [_playlistsMenu release];
478 ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
479 _ratingMenu = [self ratingMenu];
480 ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
481 _upcomingSongsMenu = [self upcomingSongsMenu];
482 ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
483 _playlistsMenu = [self playlistsMenu];
484 ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
485 _eqMenu = [self eqMenu];
486 ITDebugLog(@"Done rebuilding all of the submenus.");
489 - (NSMenu *)ratingMenu
491 NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
492 NSEnumerator *itemEnum;
495 SEL itemSelector = @selector(performRatingMenuAction:);
497 ITDebugLog(@"Building \"Song Rating\" menu.");
499 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
500 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
501 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
502 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
503 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
504 [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
506 itemEnum = [[ratingMenu itemArray] objectEnumerator];
507 while ( (anItem = [itemEnum nextObject]) ) {
508 ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
509 [anItem setAction:itemSelector];
510 [anItem setTarget:self];
511 [anItem setTag:itemTag];
514 ITDebugLog(@"Done Building \"Song Rating\" menu.");
518 - (NSMenu *)upcomingSongsMenu
520 NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
521 int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
524 numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
526 [[MainController sharedController] networkError:localException];
529 ITDebugLog(@"Building \"Upcoming Songs\" menu.");
530 if (_currentPlaylist && !_playingRadio) {
533 for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
535 NSString *curSong = nil;
537 curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
539 [[MainController sharedController] networkError:localException];
541 id <NSMenuItem> songItem;
542 ITDebugLog(@"Adding song: %@", curSong);
543 songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
545 [songItem setTarget:self];
552 if ([upcomingSongsMenu numberOfItems] == 0) {
553 [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
556 ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
557 return upcomingSongsMenu;
560 /*- (NSMenu *)playlistsMenu
562 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
564 id <NSMenuItem> tempItem;
565 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
568 playlists = [[[MainController sharedController] currentRemote] playlists];
570 [[MainController sharedController] networkError:localException];
573 ITDebugLog(@"Building \"Playlists\" menu.");
575 for (i = 0; i < [playlists count]; i++) {
576 NSString *curPlaylist = [playlists objectAtIndex:i];
577 ITDebugLog(@"Adding playlist: %@", curPlaylist);
578 tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
579 [tempItem setTag:i + 1];
580 [tempItem setTarget:self];
583 if (source == ITMTRemoteRadioSource) {
584 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
585 } else if (source == ITMTRemoteGenericDeviceSource) {
586 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
587 } else if (source == ITMTRemoteiPodSource) {
588 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
589 } else if (source == ITMTRemoteCDSource) {
590 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
591 } else if (source == ITMTRemoteSharedLibrarySource) {
592 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
593 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
594 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
596 ITDebugLog(@"Done Building \"Playlists\" menu");
597 return playlistsMenu;
601 - (NSMenu *)playlistsMenu
603 NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
604 NSArray *playlists = nil;
605 id <NSMenuItem> tempItem;
606 ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
608 NSMutableArray *indices = [[NSMutableArray alloc] init];
610 playlists = [[[MainController sharedController] currentRemote] playlists];
612 [[MainController sharedController] networkError:localException];
614 ITDebugLog(@"Building \"Playlists\" menu.");
616 NSArray *curPlaylist = [playlists objectAtIndex:0];
617 NSString *name = [curPlaylist objectAtIndex:0];
618 ITDebugLog(@"Adding main source: %@", name);
619 for (i = 3; i < [curPlaylist count]; i++) {
620 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
621 tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
622 [tempItem setTag:i - 1];
623 [tempItem setTarget:self];
625 ITDebugLog(@"Adding index to the index array.");
626 [indices addObject:[curPlaylist objectAtIndex:2]];
628 if ([playlists count] > 1) {
629 if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
630 [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
631 if (source == ITMTRemoteRadioSource) {
632 [playlistsMenu addItem:[NSMenuItem separatorItem]];
633 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
636 [playlistsMenu addItem:[NSMenuItem separatorItem]];
640 if ([playlists count] > 1) {
641 for (i = 1; i < [playlists count]; i++) {
642 NSArray *curPlaylist = [playlists objectAtIndex:i];
643 if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
644 NSString *name = [curPlaylist objectAtIndex:0];
645 NSMenu *submenu = [[NSMenu alloc] init];
646 ITDebugLog(@"Adding source: %@", name);
648 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
649 ITDebugLog(@"Invalid iPod source.");
650 [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
652 for (j = 3; j < [curPlaylist count]; j++) {
653 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
654 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
655 [tempItem setTag:(i * 1000) + j - 1];
656 [tempItem setTarget:self];
658 [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
660 ITDebugLog(@"Adding index to the index array.");
661 [indices addObject:[curPlaylist objectAtIndex:2]];
665 ITDebugLog(@"Checking the current source.");
666 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
667 tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
668 [tempItem setState:NSOnState];
669 [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
670 } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
671 [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
674 ITDebugLog(@"Done Building \"Playlists\" menu");
675 return playlistsMenu;
680 NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
681 NSArray *eqPresets = nil;
682 id <NSMenuItem> tempItem;
686 eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
688 [[MainController sharedController] networkError:localException];
691 ITDebugLog(@"Building \"EQ Presets\" menu.");
693 for (i = 0; i < [eqPresets count]; i++) {
695 if ( ( name = [eqPresets objectAtIndex:i] ) ) {
696 ITDebugLog(@"Adding EQ Preset: %@", name);
697 tempItem = [eqMenu addItemWithTitle:name
698 action:@selector(performEqualizerMenuAction:)
701 [tempItem setTarget:self];
704 ITDebugLog(@"Done Building \"EQ Presets\" menu");
708 - (void)performMainMenuAction:(id)sender
710 switch ( [sender tag] )
712 case MTMenuPlayPauseItem:
713 ITDebugLog(@"Performing Menu Action: Play/Pause");
714 [[MainController sharedController] playPause];
716 case MTMenuFastForwardItem:
717 ITDebugLog(@"Performing Menu Action: Fast Forward");
718 [[MainController sharedController] fastForward];
720 case MTMenuRewindItem:
721 ITDebugLog(@"Performing Menu Action: Rewind");
722 [[MainController sharedController] rewind];
724 case MTMenuPreviousTrackItem:
725 ITDebugLog(@"Performing Menu Action: Previous Track");
726 [[MainController sharedController] prevSong];
728 case MTMenuNextTrackItem:
729 ITDebugLog(@"Performing Menu Action: Next Track");
730 [[MainController sharedController] nextSong];
732 case MTMenuShowPlayerItem:
733 ITDebugLog(@"Performing Menu Action: Show Main Interface");
734 [[MainController sharedController] showPlayer];
736 case MTMenuPreferencesItem:
737 ITDebugLog(@"Performing Menu Action: Preferences...");
738 [[MainController sharedController] showPreferences];
741 ITDebugLog(@"Performing Menu Action: Quit");
742 [[MainController sharedController] quitMenuTunes];
744 case MTMenuRegisterItem:
745 ITDebugLog(@"Performing Menu Action: Register");
746 [[MainController sharedController] blingNow];
749 ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
754 - (void)performRatingMenuAction:(id)sender
756 ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
757 [[MainController sharedController] selectSongRating:[sender tag]];
760 - (void)performPlaylistMenuAction:(id)sender
762 ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
763 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
766 - (void)performEqualizerMenuAction:(id)sender
768 ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
769 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
772 - (void)performUpcomingSongsMenuAction:(id)sender
774 ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
775 [[MainController sharedController] selectSongAtIndex:[sender tag]];
780 ITDebugLog(@"Update Menu");
781 [_currentMenu update];
784 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
789 //This is never used I know, keep it though
790 - (NSString *)systemUIColor
792 NSDictionary *tmpDict;
794 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
795 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
796 if ( ([tmpNumber intValue] == 1) ) {
809 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
810 onItem:(id <NSMenuItem>)item
812 unichar charcode = 'a';
814 long cocoaModifiers = 0;
815 static long carbonToCocoa[6][2] =
817 { cmdKey, NSCommandKeyMask },
818 { optionKey, NSAlternateKeyMask },
819 { controlKey, NSControlKeyMask },
820 { shiftKey, NSShiftKeyMask },
823 ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
825 for (i = 0; i < 6; i++) {
826 if (modifiers & carbonToCocoa[i][0]) {
827 cocoaModifiers += carbonToCocoa[i][1];
830 [item setKeyEquivalentModifierMask:cocoaModifiers];
832 //Missing key combos for some keys. Must find them later.
836 ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
841 ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
848 ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
849 // Haven't tested this, though it should work.
850 // This doesn't work. :'(
852 //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
854 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
855 ITDebugLog(@"%@", menuRef);
856 SetMenuItemCommandKey(menuRef, 0, NO, 49);
857 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
858 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
861 [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
867 ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
868 charcode = NSDeleteFunctionKey;
872 ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
877 ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
882 ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
887 ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
888 charcode = NSF5FunctionKey;
892 ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
893 charcode = NSF6FunctionKey;
897 ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
898 charcode = NSF7FunctionKey;
902 ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
903 charcode = NSF3FunctionKey;
907 ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
908 charcode = NSF8FunctionKey;
912 ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
913 charcode = NSF9FunctionKey;
917 ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
918 charcode = NSF11FunctionKey;
922 ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
923 charcode = NSF13FunctionKey;
927 ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
928 charcode = NSF14FunctionKey;
932 ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
933 charcode = NSF10FunctionKey;
937 ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
938 charcode = NSF12FunctionKey;
942 ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
943 charcode = NSF13FunctionKey;
947 ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
948 charcode = NSInsertFunctionKey;
952 ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
953 charcode = NSHomeFunctionKey;
957 ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
958 charcode = NSPageUpFunctionKey;
962 ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
963 charcode = NSDeleteFunctionKey;
967 ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
968 charcode = NSF4FunctionKey;
972 ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
973 charcode = NSEndFunctionKey;
977 ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
978 charcode = NSF2FunctionKey;
982 ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
983 charcode = NSPageDownFunctionKey;
987 ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
988 charcode = NSF1FunctionKey;
992 ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
993 charcode = NSLeftArrowFunctionKey;
997 ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
998 charcode = NSRightArrowFunctionKey;
1002 ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1003 charcode = NSDownArrowFunctionKey;
1007 ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1008 charcode = NSUpArrowFunctionKey;
1012 if (charcode == 'a') {
1013 unsigned long state;
1018 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1019 keyTrans = KeyTranslate(kchr, code, &state);
1020 charCode = keyTrans;
1021 ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1022 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1023 } else if (charcode != 'b') {
1024 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1026 ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1029 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1031 NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1032 NSEnumerator *volEnum = [volumes objectEnumerator];
1033 NSString *nextVolume;
1034 ITDebugLog(@"Looking for an iPod named %@", name);
1035 while ( (nextVolume = [volEnum nextObject]) ) {
1036 ITDebugLog(@"- %@", nextVolume);
1037 if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1038 NSFileHandle *handle;
1040 NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1041 if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1042 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1045 handle = [NSFileHandle fileHandleForReadingAtPath:path];
1046 ITDebugLog(@"File handle: %@", handle);
1047 [handle seekToFileOffset:10];
1048 data = [handle readDataOfLength:1];
1049 ITDebugLog(@"Data: %@", data);
1050 if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1051 ITDebugLog(@"iPod is manually updated. %@", path);
1053 } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1054 ITDebugLog(@"iPod is automatically updated. %@", path);
1057 ITDebugLog(@"Error! Value: %h Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);