Fixed MT for iTunes 4.5 and the radio source possibly not existing.
[MenuTunes.git] / MenuController.m
1 //
2 //  MenuController.m
3 //  MenuTunes
4 //
5 //  Created by Joseph Spiros on Wed Apr 30 2003.
6 //  Copyright (c) 2003 iThink Software. All rights reserved.
7 //
8
9 #import "MenuController.h"
10 #import "MainController.h"
11 #import "ITMTRemote.h"
12 #import <ITFoundation/ITDebug.h>
13 #import <ITKit/ITHotKeyCenter.h>
14 #import <ITKit/ITHotKey.h>
15 #import <ITKit/ITKeyCombo.h>
16 #import <ITKit/ITCategory-NSMenu.h>
17
18 @interface MenuController (SubmenuMethods)
19 - (NSMenu *)ratingMenu;
20 - (NSMenu *)upcomingSongsMenu;
21 - (NSMenu *)playlistsMenu;
22 - (NSMenu *)eqMenu;
23 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
24         onItem:(id <NSMenuItem>)item;
25 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
26 @end
27
28 @implementation MenuController
29
30 - (id)init
31 {
32     if ( (self = [super init]) ) {
33         _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
34     }
35     return self;
36 }
37
38 - (NSMenu *)menu
39 {
40     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
41     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
42     NSArray *menuArray = [defaults arrayForKey:@"menu"];
43     NSEnumerator *enumerator = [menuArray objectEnumerator];
44     NSString *nextObject;
45     id <NSMenuItem> tempItem;
46     NSEnumerator *itemEnum;
47     ITHotKey *hotKey;
48     NSArray *hotKeys = [[ITHotKeyCenter sharedCenter] allHotKeys];
49     int currentSongRating;
50     
51     //Get the information
52     NS_DURING
53         _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
54         _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
55         currentSongRating = ( [[[MainController sharedController] currentRemote] currentSongRating] != -1 );
56     NS_HANDLER
57         [[MainController sharedController] networkError:localException];
58     NS_ENDHANDLER
59     
60     ITDebugLog(@"Reset menu if required.");
61     
62     //Kill the old submenu items
63     if ( (tempItem = [_currentMenu itemWithTag:1]) ) {
64         ITDebugLog(@"Removing \"Song Rating\" submenu.");
65         [tempItem setSubmenu:nil];
66     }
67     
68     if ( (tempItem = [_currentMenu itemWithTag:2]) ) {
69         ITDebugLog(@"Removing \"Upcoming Songs\" submenu.");
70         [tempItem setSubmenu:nil];
71     }
72     
73     if ( (tempItem = [_currentMenu itemWithTag:3]) ) {
74         ITDebugLog(@"Removing \"Playlists\" submenu.");
75         [tempItem setSubmenu:nil];
76     }
77     
78     if ( (tempItem = [_currentMenu itemWithTag:4]) ) {
79         ITDebugLog(@"Removing \"EQ Presets\" submenu.");
80         [tempItem setSubmenu:nil];
81     }
82     
83     ITDebugLog(@"Begin building menu.");
84     
85     //create our menu
86     while ( (nextObject = [enumerator nextObject]) ) {
87         //Main menu items
88         if ([nextObject isEqualToString:@"playPause"]) {
89             ITDebugLog(@"Add \"Play\"/\"Pause\" menu item.");
90             tempItem = [menu addItemWithTitle:NSLocalizedString(@"play", @"Play")
91                     action:@selector(performMainMenuAction:)
92                     keyEquivalent:@""];
93             [tempItem setTag:MTMenuPlayPauseItem];
94             [tempItem setTarget:self];
95             
96             itemEnum = [hotKeys objectEnumerator];
97             while ( (hotKey = [itemEnum nextObject]) ) {
98                 if ([[hotKey name] isEqualToString:@"PlayPause"]) {
99                     ITKeyCombo *combo = [hotKey keyCombo];
100                     [self setKeyEquivalentForCode:[combo keyCode]
101                           andModifiers:[combo modifiers]
102                           onItem:tempItem];
103                 }
104             }
105             
106             ITDebugLog(@"Set \"Play\"/\"Pause\" menu item's title to correct state.");
107             NS_DURING
108                 switch ([[[MainController sharedController] currentRemote] playerPlayingState]) {
109                     case ITMTRemotePlayerPlaying:
110                         [tempItem setTitle:NSLocalizedString(@"pause", @"Pause")];
111                     break;
112                     case ITMTRemotePlayerRewinding:
113                     case ITMTRemotePlayerForwarding:
114                         [tempItem setTitle:NSLocalizedString(@"resume", @"Resume")];
115                     break;
116                     default:
117                     break;
118                 }
119             NS_HANDLER
120                 [[MainController sharedController] networkError:localException];
121             NS_ENDHANDLER
122         } else if ([nextObject isEqualToString:@"nextTrack"]) {
123             ITDebugLog(@"Add \"Next Track\" menu item.");
124             tempItem = [menu addItemWithTitle:NSLocalizedString(@"nextTrack", @"Next Track")
125                     action:@selector(performMainMenuAction:)
126                     keyEquivalent:@""];
127             
128             itemEnum = [hotKeys objectEnumerator];
129             while ( (hotKey = [itemEnum nextObject]) ) {
130                 if ([[hotKey name] isEqualToString:@"NextTrack"]) {
131                     ITKeyCombo *combo = [hotKey keyCombo];
132                     [self setKeyEquivalentForCode:[combo keyCode]
133                           andModifiers:[combo modifiers]
134                           onItem:tempItem];
135                 }
136             }
137             
138             if (_currentPlaylist) {
139                 [tempItem setTag:MTMenuNextTrackItem];
140                 [tempItem setTarget:self];
141             }
142         } else if ([nextObject isEqualToString:@"prevTrack"]) {
143             ITDebugLog(@"Add \"Previous Track\" menu item.");
144             tempItem = [menu addItemWithTitle:NSLocalizedString(@"prevTrack", @"Previous Track")
145                     action:@selector(performMainMenuAction:)
146                     keyEquivalent:@""];
147             
148             itemEnum = [hotKeys objectEnumerator];
149             while ( (hotKey = [itemEnum nextObject]) ) {
150                 if ([[hotKey name] isEqualToString:@"PrevTrack"]) {
151                     ITKeyCombo *combo = [hotKey keyCombo];
152                     [self setKeyEquivalentForCode:[combo keyCode]
153                           andModifiers:[combo modifiers]
154                           onItem:tempItem];
155                 }
156             }
157             
158             if (_currentPlaylist) {
159                 [tempItem setTag:MTMenuPreviousTrackItem];
160                 [tempItem setTarget:self];
161             }
162         } else if ([nextObject isEqualToString:@"fastForward"]) {
163             ITDebugLog(@"Add \"Fast Forward\" menu item.");
164             tempItem = [menu addItemWithTitle:NSLocalizedString(@"fastForward", @"Fast Forward")
165                     action:@selector(performMainMenuAction:)
166                     keyEquivalent:@""];
167             if (_currentPlaylist) {
168                 [tempItem setTag:MTMenuFastForwardItem];
169                 [tempItem setTarget:self];
170             }
171         } else if ([nextObject isEqualToString:@"rewind"]) {
172             ITDebugLog(@"Add \"Rewind\" menu item.");
173             tempItem = [menu addItemWithTitle:NSLocalizedString(@"rewind", @"Rewind")
174                     action:@selector(performMainMenuAction:)
175                     keyEquivalent:@""];
176             if (_currentPlaylist) {
177                 [tempItem setTag:MTMenuRewindItem];
178                 [tempItem setTarget:self];
179             }
180         } else if ([nextObject isEqualToString:@"showPlayer"]) {
181             ITDebugLog(@"Add \"Show Player\" menu item.");
182             NS_DURING
183                 tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@",
184                                 NSLocalizedString(@"show", @"Show"),
185                                     [[[MainController sharedController] currentRemote] playerSimpleName]]
186                                 action:@selector(performMainMenuAction:)
187                                 keyEquivalent:@""];
188             NS_HANDLER
189                 [[MainController sharedController] networkError:localException];
190             NS_ENDHANDLER
191             
192             itemEnum = [hotKeys objectEnumerator];
193             while ( (hotKey = [itemEnum nextObject]) ) {
194                 if ([[hotKey name] isEqualToString:@"ShowPlayer"]) {
195                     ITKeyCombo *combo = [hotKey keyCombo];
196                     [self setKeyEquivalentForCode:[combo keyCode]
197                           andModifiers:[combo modifiers]
198                           onItem:tempItem];
199                 }
200             }
201             
202             [tempItem setTarget:self];
203             [tempItem setTag:MTMenuShowPlayerItem];
204         } else if ([nextObject isEqualToString:@"preferences"]) {
205             ITDebugLog(@"Add \"Preferences...\" menu item.");
206             tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...")
207                     action:@selector(performMainMenuAction:)
208                     keyEquivalent:@""];
209             [tempItem setTag:MTMenuPreferencesItem];
210             [tempItem setTarget:self];
211         } else if ([nextObject isEqualToString:@"quit"]) {
212             if ([[MainController sharedController] blingBling] == NO) {
213                 ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
214                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
215                 [tempItem setTag:MTMenuRegisterItem];
216                 [tempItem setTarget:self];
217             }
218             ITDebugLog(@"Add \"Quit\" menu item.");
219             tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit")
220                     action:@selector(performMainMenuAction:)
221                     keyEquivalent:@""];
222             [tempItem setTag:MTMenuQuitItem];
223             [tempItem setTarget:self];
224         } else if ([nextObject isEqualToString:@"trackInfo"]) {
225             ITDebugLog(@"Check to see if a Track is playing...");
226             //Handle playing radio too
227             if (_currentPlaylist) {
228                 NSString *title;
229                 NS_DURING
230                     title = [[[MainController sharedController] currentRemote] currentSongTitle];
231                 NS_HANDLER
232                     [[MainController sharedController] networkError:localException];
233                 NS_ENDHANDLER
234                 ITDebugLog(@"A Track is Playing, Add \"Track Info\" menu items.");
235                 ITDebugLog(@"Add \"Now Playing\" menu item.");
236                 [menu addItemWithTitle:NSLocalizedString(@"nowPlaying", @"Now Playing") action:NULL keyEquivalent:@""];
237                 
238                 if ([title length] > 0) {
239                     ITDebugLog(@"Add Track Title (\"%@\") menu item.", title);
240                     [menu indentItem:
241                         [menu addItemWithTitle:title action:nil keyEquivalent:@""]];
242                 }
243                 
244                 if (!_playingRadio) {
245                     if ([defaults boolForKey:@"showAlbum"]) {
246                         NSString *curAlbum;
247                         NS_DURING
248                             curAlbum = [[[MainController sharedController] currentRemote] currentSongAlbum];
249                         NS_HANDLER
250                             [[MainController sharedController] networkError:localException];
251                         NS_ENDHANDLER
252                         ITDebugLog(@"Add Track Album (\"%@\") menu item.", curAlbum);
253                         if ( curAlbum ) {
254                             [menu indentItem:
255                                 [menu addItemWithTitle:curAlbum action:nil keyEquivalent:@""]];
256                         }
257                     }
258                     
259                     if ([defaults boolForKey:@"showArtist"]) {
260                         NSString *curArtist;
261                         NS_DURING
262                             curArtist = [[[MainController sharedController] currentRemote] currentSongArtist];
263                         NS_HANDLER
264                             [[MainController sharedController] networkError:localException];
265                         NS_ENDHANDLER
266                         ITDebugLog(@"Add Track Artist (\"%@\") menu item.", curArtist);
267                         if ( curArtist ) {
268                             [menu indentItem:
269                                 [menu addItemWithTitle:curArtist action:nil keyEquivalent:@""]];
270                         }
271                     }
272                     
273                     if ([defaults boolForKey:@"showComposer"]) {
274                         NSString *curComposer;
275                         NS_DURING
276                             curComposer = [[[MainController sharedController] currentRemote] currentSongComposer];
277                         NS_HANDLER
278                             [[MainController sharedController] networkError:localException];
279                         NS_ENDHANDLER
280                         ITDebugLog(@"Add Track Composer (\"%@\") menu item.", curComposer);
281                         if ( curComposer ) {
282                             [menu indentItem:
283                                 [menu addItemWithTitle:curComposer action:nil keyEquivalent:@""]];
284                         }
285                     }
286                     
287                     if ([defaults boolForKey:@"showTrackNumber"]) {
288                         int track;
289                         NS_DURING
290                             track = [[[MainController sharedController] currentRemote] currentSongTrack];
291                         NS_HANDLER
292                             [[MainController sharedController] networkError:localException];
293                         NS_ENDHANDLER
294                         ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
295                         if ( track > 0 ) {
296                             [menu indentItem:
297                                 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
298                         }
299                     }
300                 }
301                 
302                 NS_DURING
303                     if ([defaults boolForKey:@"showTime"] && ( ([[[MainController sharedController] currentRemote] currentSongElapsed] != nil) || ([[[MainController sharedController] currentRemote] currentSongLength] != nil) )) {
304                         ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]);
305                         [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]] action:nil keyEquivalent:@""]];
306                     }
307                 NS_HANDLER
308                     [[MainController sharedController] networkError:localException];
309                 NS_ENDHANDLER
310                 
311                 if (!_playingRadio) {
312                     NS_DURING
313                         if ([defaults boolForKey:@"showTrackRating"] && ( [[[MainController sharedController] currentRemote] currentSongRating] != -1.0 )) {
314                             NSString *string = nil;
315                             switch ((int)([[[MainController sharedController] currentRemote] currentSongRating] * 5)) {
316                                 case 0:
317                                     string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
318                                 break;
319                                 case 1:
320                                     string = [NSString stringWithUTF8String:"★☆☆☆☆"];
321                                 break;
322                                 case 2:
323                                     string = [NSString stringWithUTF8String:"★★☆☆☆"];
324                                 break;
325                                 case 3:
326                                     string = [NSString stringWithUTF8String:"★★★☆☆"];
327                                 break;
328                                 case 4:
329                                     string = [NSString stringWithUTF8String:"★★★★☆"];
330                                 break;
331                                 case 5:
332                                     string = [NSString stringWithUTF8String:"★★★★★"];
333                                 break;
334                             }
335                             ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
336                             [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
337                         }
338                     NS_HANDLER
339                         [[MainController sharedController] networkError:localException];
340                     NS_ENDHANDLER
341                 }
342             } else {
343                 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
344                 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
345             }
346         } else if ([nextObject isEqualToString:@"separator"]) {
347             ITDebugLog(@"Add a separator menu item.");
348             [menu addItem:[NSMenuItem separatorItem]];
349         //Submenu items
350         } else if ([nextObject isEqualToString:@"playlists"]) {
351             ITDebugLog(@"Add \"Playlists\" submenu.");
352             tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
353                     action:nil
354                     keyEquivalent:@""];
355             [tempItem setSubmenu:_playlistsMenu];
356             [tempItem setTag:3];
357         } else if ([nextObject isEqualToString:@"eqPresets"]) {
358             ITDebugLog(@"Add \"EQ Presets\" submenu.");
359             tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
360                     action:nil
361                     keyEquivalent:@""];
362             [tempItem setSubmenu:_eqMenu];
363             [tempItem setTag:4];
364             
365             itemEnum = [[_eqMenu itemArray] objectEnumerator];
366             while ( (tempItem = [itemEnum nextObject]) ) {
367                 [tempItem setState:NSOffState];
368             }
369             NS_DURING
370                 [[_eqMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentEQPresetIndex] - 1)] setState:NSOnState];
371             NS_HANDLER
372                 [[MainController sharedController] networkError:localException];
373             NS_ENDHANDLER
374         } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
375                 ITDebugLog(@"Add \"Song Rating\" submenu.");
376                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
377                         action:nil
378                         keyEquivalent:@""];
379                 [tempItem setSubmenu:_ratingMenu];
380                 [tempItem setTag:1];
381                 if (_playingRadio || !_currentPlaylist) {
382                     [tempItem setEnabled:NO];
383                 }
384                 
385                 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
386                 while ( (tempItem = [itemEnum nextObject]) ) {
387                     [tempItem setState:NSOffState];
388                 }
389                 
390                 NS_DURING
391                     [[_ratingMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentSongRating] * 5)] setState:NSOnState];
392                 NS_HANDLER
393                     [[MainController sharedController] networkError:localException];
394                 NS_ENDHANDLER
395             } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
396                 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
397                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
398                         action:nil
399                         keyEquivalent:@""];
400                 [tempItem setSubmenu:_upcomingSongsMenu];
401                 [tempItem setTag:2];
402                 if (_playingRadio || !_currentPlaylist) {
403                     [tempItem setEnabled:NO];
404                 }
405             }
406         }
407     ITDebugLog(@"Finished building menu.");
408     [_currentMenu release];
409     _currentMenu = menu;
410     return _currentMenu;
411 }
412
413 - (NSMenu *)menuForNoPlayer
414 {
415     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
416     id <NSMenuItem> tempItem;
417     ITDebugLog(@"Creating menu for when player isn't running.");
418     NS_DURING
419         ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
420         tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
421     NS_HANDLER
422         [[MainController sharedController] networkError:localException];
423     NS_ENDHANDLER
424     [tempItem setTag:MTMenuShowPlayerItem];
425     [tempItem setTarget:self];
426     ITDebugLog(@"Add a separator menu item.");
427     [menu addItem:[NSMenuItem separatorItem]];
428     ITDebugLog(@"Add \"Preferences...\" menu item.");
429     tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
430     [tempItem setTag:MTMenuPreferencesItem];
431     [tempItem setTarget:self];
432     if ([[MainController sharedController] blingBling] == NO) {
433         ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
434         tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
435         [tempItem setTag:MTMenuRegisterItem];
436         [tempItem setTarget:self];
437     }
438     ITDebugLog(@"Add \"Quit\" menu item.");
439     tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
440     [tempItem setTag:MTMenuQuitItem];
441     [tempItem setTarget:self];
442     return [menu autorelease];
443 }
444
445 - (void)rebuildSubmenus
446 {
447     ITDebugLog(@"Rebuilding all of the submenus.");
448     NS_DURING
449         _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
450         _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
451         _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
452     NS_HANDLER
453         [[MainController sharedController] networkError:localException];
454     NS_ENDHANDLER
455     ITDebugLog(@"Releasing old submenus.");
456     [_ratingMenu release];
457     [_upcomingSongsMenu release];
458     [_playlistsMenu release];
459     [_eqMenu release];
460     ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
461     _ratingMenu = [self ratingMenu];
462     ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
463     _upcomingSongsMenu = [self upcomingSongsMenu];
464     ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
465     _playlistsMenu = [self playlistsMenu];
466     ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
467     _eqMenu = [self eqMenu];
468     ITDebugLog(@"Done rebuilding all of the submenus.");
469 }
470
471 - (NSMenu *)ratingMenu
472 {
473     NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
474     NSEnumerator *itemEnum;
475     id  anItem;
476     int itemTag = 0;
477     SEL itemSelector = @selector(performRatingMenuAction:);
478     
479     ITDebugLog(@"Building \"Song Rating\" menu.");
480     
481     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
482     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
483     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
484     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
485     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
486     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
487     
488     itemEnum = [[ratingMenu itemArray] objectEnumerator];
489     while ( (anItem = [itemEnum nextObject]) ) {
490         ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
491         [anItem setAction:itemSelector];
492         [anItem setTarget:self];
493         [anItem setTag:itemTag];
494         itemTag += 20;
495     }
496     ITDebugLog(@"Done Building \"Song Rating\" menu.");
497     return ratingMenu;
498 }
499
500 - (NSMenu *)upcomingSongsMenu
501 {
502     NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
503     int numSongs, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
504     
505     NS_DURING
506         numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
507     NS_HANDLER
508         [[MainController sharedController] networkError:localException];
509     NS_ENDHANDLER
510     
511     ITDebugLog(@"Building \"Upcoming Songs\" menu.");
512     if (_currentPlaylist && !_playingRadio) {
513         if (numSongs > 0) {
514             int i;
515             for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
516                 if (i <= numSongs) {
517                     NSString *curSong;
518                     NS_DURING
519                         curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
520                     NS_HANDLER
521                         [[MainController sharedController] networkError:localException];
522                     NS_ENDHANDLER
523                     id <NSMenuItem> songItem;
524                     ITDebugLog(@"Adding song: %@", curSong);
525                     songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
526                     [songItem setTag:i];
527                     [songItem setTarget:self];
528                 } else {
529                     break;
530                 }
531             }
532         }
533         
534         if ([upcomingSongsMenu numberOfItems] == 0) {
535             [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
536         }
537     }
538     ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
539     return upcomingSongsMenu;
540 }
541
542 /*- (NSMenu *)playlistsMenu
543 {
544     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
545     NSArray *playlists;
546     id <NSMenuItem> tempItem;
547     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
548     int i;
549     NS_DURING
550         playlists = [[[MainController sharedController] currentRemote] playlists];
551     NS_HANDLER
552         [[MainController sharedController] networkError:localException];
553     NS_ENDHANDLER
554     
555     ITDebugLog(@"Building \"Playlists\" menu.");
556     
557     for (i = 0; i < [playlists count]; i++) {
558         NSString *curPlaylist = [playlists objectAtIndex:i];
559         ITDebugLog(@"Adding playlist: %@", curPlaylist);
560         tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
561         [tempItem setTag:i + 1];
562         [tempItem setTarget:self];
563     }
564     
565     if (source == ITMTRemoteRadioSource) {
566         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
567     } else if (source == ITMTRemoteGenericDeviceSource) {
568         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
569     } else if (source == ITMTRemoteiPodSource) {
570         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
571     } else if (source == ITMTRemoteCDSource) {
572         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
573     } else if (source == ITMTRemoteSharedLibrarySource) {
574         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
575     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
576         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
577     }
578     ITDebugLog(@"Done Building \"Playlists\" menu");
579     return playlistsMenu;
580 }*/
581
582
583 - (NSMenu *)playlistsMenu
584 {
585     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
586     NSArray *playlists;
587     id <NSMenuItem> tempItem;
588     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
589     int i, j;
590     NSMutableArray *indices = [[NSMutableArray alloc] init];
591     NS_DURING
592         playlists = [[[MainController sharedController] currentRemote] playlists];
593     NS_HANDLER
594         [[MainController sharedController] networkError:localException];
595     NS_ENDHANDLER
596     ITDebugLog(@"Building \"Playlists\" menu.");
597     {
598         NSArray *curPlaylist = [playlists objectAtIndex:0];
599         NSString *name = [curPlaylist objectAtIndex:0];
600         ITDebugLog(@"Adding main source: %@", name);
601         for (i = 3; i < [curPlaylist count]; i++) {
602             ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
603             tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
604             [tempItem setTag:i - 1];
605             [tempItem setTarget:self];
606         }
607         ITDebugLog(@"Adding index to the index array.");
608         [indices addObject:[curPlaylist objectAtIndex:2]];
609     }
610     if ([playlists count] > 1) {
611         if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
612             [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
613             if (source == ITMTRemoteRadioSource) {
614                 [playlistsMenu addItem:[NSMenuItem separatorItem]];
615                 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
616             }
617         } else {
618             [playlistsMenu addItem:[NSMenuItem separatorItem]];
619         }
620     }
621     
622     if ([playlists count] > 1) {
623         for (i = 1; i < [playlists count]; i++) {
624             NSArray *curPlaylist = [playlists objectAtIndex:i];
625             if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
626                 NSString *name = [curPlaylist objectAtIndex:0];
627                 NSMenu *submenu = [[NSMenu alloc] init];
628                 ITDebugLog(@"Adding source: %@", name);
629                 
630                 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
631                     ITDebugLog(@"Invalid iPod source.");
632                     [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
633                 } else {
634                     for (j = 3; j < [curPlaylist count]; j++) {
635                         ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
636                         tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
637                         [tempItem setTag:(i * 1000) + j - 1];
638                         [tempItem setTarget:self];
639                     }
640                     [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
641                 }
642                 ITDebugLog(@"Adding index to the index array.");
643                 [indices addObject:[curPlaylist objectAtIndex:2]];
644             }
645         }
646     }
647     ITDebugLog(@"Checking the current source.");
648     if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
649         tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
650         [tempItem setState:NSOnState];
651         [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
652     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
653         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
654     }
655     [indices release];
656     ITDebugLog(@"Done Building \"Playlists\" menu");
657     return playlistsMenu;
658 }
659
660 - (NSMenu *)eqMenu
661 {
662     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
663     NSArray *eqPresets;
664     id <NSMenuItem> tempItem;
665     int i;
666     
667     NS_DURING
668         eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
669     NS_HANDLER
670         [[MainController sharedController] networkError:localException];
671     NS_ENDHANDLER
672     
673     ITDebugLog(@"Building \"EQ Presets\" menu.");
674     
675     for (i = 0; i < [eqPresets count]; i++) {
676         NSString *name;
677            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
678             ITDebugLog(@"Adding EQ Preset: %@", name);
679             tempItem = [eqMenu addItemWithTitle:name
680                     action:@selector(performEqualizerMenuAction:)
681                     keyEquivalent:@""];
682             [tempItem setTag:i];
683             [tempItem setTarget:self];
684            }
685     }
686     ITDebugLog(@"Done Building \"EQ Presets\" menu");
687     return eqMenu;
688 }
689
690 - (void)performMainMenuAction:(id)sender
691 {
692     switch ( [sender tag] )
693     {
694         case MTMenuPlayPauseItem:
695             ITDebugLog(@"Performing Menu Action: Play/Pause");
696             [[MainController sharedController] playPause];
697             break;
698         case MTMenuFastForwardItem:
699             ITDebugLog(@"Performing Menu Action: Fast Forward");
700             [[MainController sharedController] fastForward];
701             break;
702         case MTMenuRewindItem:
703             ITDebugLog(@"Performing Menu Action: Rewind");
704             [[MainController sharedController] rewind];
705             break;
706         case MTMenuPreviousTrackItem:
707             ITDebugLog(@"Performing Menu Action: Previous Track");
708             [[MainController sharedController] prevSong];
709             break;
710         case MTMenuNextTrackItem:
711             ITDebugLog(@"Performing Menu Action: Next Track");
712             [[MainController sharedController] nextSong];
713             break;
714         case MTMenuShowPlayerItem:
715             ITDebugLog(@"Performing Menu Action: Show Main Interface");
716             [[MainController sharedController] showPlayer];
717             break;
718         case MTMenuPreferencesItem:
719             ITDebugLog(@"Performing Menu Action: Preferences...");
720             [[MainController sharedController] showPreferences];
721             break;
722         case MTMenuQuitItem:
723             ITDebugLog(@"Performing Menu Action: Quit");
724             [[MainController sharedController] quitMenuTunes];
725             break;
726         case MTMenuRegisterItem:
727             ITDebugLog(@"Performing Menu Action: Register");
728             [[MainController sharedController] blingNow];
729             break;
730         default:
731             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
732             break;
733     }
734 }
735
736 - (void)performRatingMenuAction:(id)sender
737 {
738     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
739     [[MainController sharedController] selectSongRating:[sender tag]];
740 }
741
742 - (void)performPlaylistMenuAction:(id)sender
743 {
744     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
745     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
746 }
747
748 - (void)performEqualizerMenuAction:(id)sender
749 {
750     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
751     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
752 }
753
754 - (void)performUpcomingSongsMenuAction:(id)sender
755 {
756     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
757     [[MainController sharedController] selectSongAtIndex:[sender tag]];
758 }
759
760 - (void)updateMenu
761 {
762     ITDebugLog(@"Update Menu");
763     [_currentMenu update];
764 }
765
766 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
767 {
768     return YES;
769 }
770
771 //This is never used I know, keep it though
772 - (NSString *)systemUIColor
773 {
774     NSDictionary *tmpDict;
775     NSNumber *tmpNumber;
776     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
777         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
778             if ( ([tmpNumber intValue] == 1) ) {
779                 return @"Aqua";
780             } else {
781                 return @"Graphite";
782             }
783         } else {
784             return @"Aqua";
785         }
786     } else {
787         return @"Aqua";
788     }
789 }
790
791 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
792         onItem:(id <NSMenuItem>)item
793 {
794     unichar charcode = 'a';
795     int i;
796     long cocoaModifiers = 0;
797     static long carbonToCocoa[6][2] = 
798     {
799         { cmdKey, NSCommandKeyMask },
800         { optionKey, NSAlternateKeyMask },
801         { controlKey, NSControlKeyMask },
802         { shiftKey, NSShiftKeyMask },
803     };
804     
805     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
806     
807     for (i = 0; i < 6; i++) {
808         if (modifiers & carbonToCocoa[i][0]) {
809             cocoaModifiers += carbonToCocoa[i][1];
810         }
811     }
812     [item setKeyEquivalentModifierMask:cocoaModifiers];
813     
814     //Missing key combos for some keys. Must find them later.
815     switch (code)
816     {
817         case 36:
818             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
819             charcode = '\r';
820         break;
821         
822         case 48:
823             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
824             charcode = '\t';
825         break;
826         
827         //Space -- ARGH!
828         case 49:
829         {
830             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
831             // Haven't tested this, though it should work.
832             // This doesn't work. :'(
833             //unichar buffer;
834             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
835             //charcode = buffer;
836             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
837             ITDebugLog(@"%@", menuRef);
838             SetMenuItemCommandKey(menuRef, 0, NO, 49);
839             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
840             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
841             charcode = 'b';*/
842             unichar buffer;
843             [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
844             charcode = buffer;
845         }
846         break;
847         
848         case 51:
849             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
850             charcode = NSDeleteFunctionKey;
851         break;
852         
853         case 53:
854             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
855             charcode = '\e';
856         break;
857         
858         case 71:
859             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
860             charcode = '\e';
861         break;
862         
863         case 76:
864             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
865             charcode = '\r';
866         break;
867         
868         case 96:
869             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
870             charcode = NSF5FunctionKey;
871         break;
872         
873         case 97:
874             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
875             charcode = NSF6FunctionKey;
876         break;
877         
878         case 98:
879             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
880             charcode = NSF7FunctionKey;
881         break;
882         
883         case 99:
884             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
885             charcode = NSF3FunctionKey;
886         break;
887         
888         case 100:
889             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
890             charcode = NSF8FunctionKey;
891         break;
892         
893         case 101:
894             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
895             charcode = NSF9FunctionKey;
896         break;
897         
898         case 103:
899             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
900             charcode = NSF11FunctionKey;
901         break;
902         
903         case 105:
904             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
905             charcode = NSF13FunctionKey;
906         break;
907         
908         case 107:
909             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
910             charcode = NSF14FunctionKey;
911         break;
912         
913         case 109:
914             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
915             charcode = NSF10FunctionKey;
916         break;
917         
918         case 111:
919             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
920             charcode = NSF12FunctionKey;
921         break;
922         
923         case 113:
924             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
925             charcode = NSF13FunctionKey;
926         break;
927         
928         case 114:
929             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
930             charcode = NSInsertFunctionKey;
931         break;
932         
933         case 115:
934             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
935             charcode = NSHomeFunctionKey;
936         break;
937         
938         case 116:
939             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
940             charcode = NSPageUpFunctionKey;
941         break;
942         
943         case 117:
944             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
945             charcode = NSDeleteFunctionKey;
946         break;
947         
948         case 118:
949             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
950             charcode = NSF4FunctionKey;
951         break;
952         
953         case 119:
954             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
955             charcode = NSEndFunctionKey;
956         break;
957         
958         case 120:
959             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
960             charcode = NSF2FunctionKey;
961         break;
962         
963         case 121:
964             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
965             charcode = NSPageDownFunctionKey;
966         break;
967         
968         case 122:
969             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
970             charcode = NSF1FunctionKey;
971         break;
972         
973         case 123:
974             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
975             charcode = NSLeftArrowFunctionKey;
976         break;
977         
978         case 124:
979             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
980             charcode = NSRightArrowFunctionKey;
981         break;
982         
983         case 125:
984             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
985             charcode = NSDownArrowFunctionKey;
986         break;
987         
988         case 126:
989             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
990             charcode = NSUpArrowFunctionKey;
991         break;
992     }
993     
994     if (charcode == 'a') {
995         unsigned long state;
996         long keyTrans;
997         char charCode;
998         Ptr kchr;
999         state = 0;
1000         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1001         keyTrans = KeyTranslate(kchr, code, &state);
1002         charCode = keyTrans;
1003         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1004         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1005     } else if (charcode != 'b') {
1006         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1007     }
1008     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1009 }
1010
1011 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1012 {
1013     NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1014     NSEnumerator *volEnum = [volumes objectEnumerator];
1015     NSString *nextVolume;
1016     ITDebugLog(@"Looking for an iPod named %@", name);
1017     while ( (nextVolume = [volEnum nextObject]) ) {
1018         ITDebugLog(@"- %@", nextVolume);
1019         if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1020             NSFileHandle *handle;
1021             NSData *data;
1022             NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1023             if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1024                 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1025                 return NO;
1026             }
1027             handle = [NSFileHandle fileHandleForReadingAtPath:path];
1028             ITDebugLog(@"File handle: %@", handle);
1029             [handle seekToFileOffset:10];
1030             data = [handle readDataOfLength:1];
1031             ITDebugLog(@"Data: %@", data);
1032             if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1033                 ITDebugLog(@"iPod is manually updated. %@", path);
1034                 return NO;
1035             } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1036                 ITDebugLog(@"iPod is automatically updated. %@", path);
1037                 return YES;
1038             } else {
1039                 ITDebugLog(@"Error! Value: %h  Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);
1040                 return NO;
1041             }
1042         }
1043     }
1044     return YES;
1045 }
1046
1047 @end