Tons of changes. Fixed the iPod problem finally. Added composer to
[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     [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
611     if ( (source == ITMTRemoteRadioSource) || ([playlists count] - 2 > 0) ) {
612         [playlistsMenu addItem:[NSMenuItem separatorItem]];
613     }
614     
615     if (source == ITMTRemoteRadioSource) {
616         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
617     }
618     
619     for (i = 2; i < [playlists count]; i++) {
620         NSArray *curPlaylist = [playlists objectAtIndex:i];
621         NSString *name = [curPlaylist objectAtIndex:0];
622         NSMenu *submenu = [[NSMenu alloc] init];
623         ITDebugLog(@"Adding source: %@", name);
624         
625         if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
626             ITDebugLog(@"Invalid iPod source.");
627             [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
628         } else {
629             for (j = 3; j < [curPlaylist count]; j++) {
630                 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
631                 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
632                 [tempItem setTag:(i * 1000) + j - 1];
633                 [tempItem setTarget:self];
634             }
635             [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
636         }
637         ITDebugLog(@"Adding index to the index array.");
638         [indices addObject:[curPlaylist objectAtIndex:2]];
639     }
640     ITDebugLog(@"Checking the current source.");
641     if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
642         tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
643         [tempItem setState:NSOnState];
644         [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
645     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
646         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
647     }
648     [indices release];
649     ITDebugLog(@"Done Building \"Playlists\" menu");
650     return playlistsMenu;
651 }
652
653 - (NSMenu *)eqMenu
654 {
655     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
656     NSArray *eqPresets;
657     id <NSMenuItem> tempItem;
658     int i;
659     
660     NS_DURING
661         eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
662     NS_HANDLER
663         [[MainController sharedController] networkError:localException];
664     NS_ENDHANDLER
665     
666     ITDebugLog(@"Building \"EQ Presets\" menu.");
667     
668     for (i = 0; i < [eqPresets count]; i++) {
669         NSString *name;
670            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
671             ITDebugLog(@"Adding EQ Preset: %@", name);
672             tempItem = [eqMenu addItemWithTitle:name
673                     action:@selector(performEqualizerMenuAction:)
674                     keyEquivalent:@""];
675             [tempItem setTag:i];
676             [tempItem setTarget:self];
677            }
678     }
679     ITDebugLog(@"Done Building \"EQ Presets\" menu");
680     return eqMenu;
681 }
682
683 - (void)performMainMenuAction:(id)sender
684 {
685     switch ( [sender tag] )
686     {
687         case MTMenuPlayPauseItem:
688             ITDebugLog(@"Performing Menu Action: Play/Pause");
689             [[MainController sharedController] playPause];
690             break;
691         case MTMenuFastForwardItem:
692             ITDebugLog(@"Performing Menu Action: Fast Forward");
693             [[MainController sharedController] fastForward];
694             break;
695         case MTMenuRewindItem:
696             ITDebugLog(@"Performing Menu Action: Rewind");
697             [[MainController sharedController] rewind];
698             break;
699         case MTMenuPreviousTrackItem:
700             ITDebugLog(@"Performing Menu Action: Previous Track");
701             [[MainController sharedController] prevSong];
702             break;
703         case MTMenuNextTrackItem:
704             ITDebugLog(@"Performing Menu Action: Next Track");
705             [[MainController sharedController] nextSong];
706             break;
707         case MTMenuShowPlayerItem:
708             ITDebugLog(@"Performing Menu Action: Show Main Interface");
709             [[MainController sharedController] showPlayer];
710             break;
711         case MTMenuPreferencesItem:
712             ITDebugLog(@"Performing Menu Action: Preferences...");
713             [[MainController sharedController] showPreferences];
714             break;
715         case MTMenuQuitItem:
716             ITDebugLog(@"Performing Menu Action: Quit");
717             [[MainController sharedController] quitMenuTunes];
718             break;
719         case MTMenuRegisterItem:
720             ITDebugLog(@"Performing Menu Action: Register");
721             [[MainController sharedController] blingNow];
722             break;
723         default:
724             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
725             break;
726     }
727 }
728
729 - (void)performRatingMenuAction:(id)sender
730 {
731     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
732     [[MainController sharedController] selectSongRating:[sender tag]];
733 }
734
735 - (void)performPlaylistMenuAction:(id)sender
736 {
737     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
738     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
739 }
740
741 - (void)performEqualizerMenuAction:(id)sender
742 {
743     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
744     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
745 }
746
747 - (void)performUpcomingSongsMenuAction:(id)sender
748 {
749     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
750     [[MainController sharedController] selectSongAtIndex:[sender tag]];
751 }
752
753 - (void)updateMenu
754 {
755     ITDebugLog(@"Update Menu");
756     [_currentMenu update];
757 }
758
759 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
760 {
761     return YES;
762 }
763
764 //This is never used I know, keep it though
765 - (NSString *)systemUIColor
766 {
767     NSDictionary *tmpDict;
768     NSNumber *tmpNumber;
769     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
770         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
771             if ( ([tmpNumber intValue] == 1) ) {
772                 return @"Aqua";
773             } else {
774                 return @"Graphite";
775             }
776         } else {
777             return @"Aqua";
778         }
779     } else {
780         return @"Aqua";
781     }
782 }
783
784 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
785         onItem:(id <NSMenuItem>)item
786 {
787     unichar charcode = 'a';
788     int i;
789     long cocoaModifiers = 0;
790     static long carbonToCocoa[6][2] = 
791     {
792         { cmdKey, NSCommandKeyMask },
793         { optionKey, NSAlternateKeyMask },
794         { controlKey, NSControlKeyMask },
795         { shiftKey, NSShiftKeyMask },
796     };
797     
798     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
799     
800     for (i = 0; i < 6; i++) {
801         if (modifiers & carbonToCocoa[i][0]) {
802             cocoaModifiers += carbonToCocoa[i][1];
803         }
804     }
805     [item setKeyEquivalentModifierMask:cocoaModifiers];
806     
807     //Missing key combos for some keys. Must find them later.
808     switch (code)
809     {
810         case 36:
811             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
812             charcode = '\r';
813         break;
814         
815         case 48:
816             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
817             charcode = '\t';
818         break;
819         
820         //Space -- ARGH!
821         case 49:
822         {
823             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
824             // Haven't tested this, though it should work.
825             // This doesn't work. :'(
826             //unichar buffer;
827             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
828             //charcode = buffer;
829             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
830             ITDebugLog(@"%@", menuRef);
831             SetMenuItemCommandKey(menuRef, 0, NO, 49);
832             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
833             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
834             charcode = 'b';*/
835             unichar buffer;
836             [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
837             charcode = buffer;
838         }
839         break;
840         
841         case 51:
842             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
843             charcode = NSDeleteFunctionKey;
844         break;
845         
846         case 53:
847             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
848             charcode = '\e';
849         break;
850         
851         case 71:
852             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
853             charcode = '\e';
854         break;
855         
856         case 76:
857             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
858             charcode = '\r';
859         break;
860         
861         case 96:
862             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
863             charcode = NSF5FunctionKey;
864         break;
865         
866         case 97:
867             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
868             charcode = NSF6FunctionKey;
869         break;
870         
871         case 98:
872             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
873             charcode = NSF7FunctionKey;
874         break;
875         
876         case 99:
877             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
878             charcode = NSF3FunctionKey;
879         break;
880         
881         case 100:
882             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
883             charcode = NSF8FunctionKey;
884         break;
885         
886         case 101:
887             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
888             charcode = NSF9FunctionKey;
889         break;
890         
891         case 103:
892             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
893             charcode = NSF11FunctionKey;
894         break;
895         
896         case 105:
897             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
898             charcode = NSF13FunctionKey;
899         break;
900         
901         case 107:
902             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
903             charcode = NSF14FunctionKey;
904         break;
905         
906         case 109:
907             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
908             charcode = NSF10FunctionKey;
909         break;
910         
911         case 111:
912             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
913             charcode = NSF12FunctionKey;
914         break;
915         
916         case 113:
917             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
918             charcode = NSF13FunctionKey;
919         break;
920         
921         case 114:
922             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
923             charcode = NSInsertFunctionKey;
924         break;
925         
926         case 115:
927             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
928             charcode = NSHomeFunctionKey;
929         break;
930         
931         case 116:
932             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
933             charcode = NSPageUpFunctionKey;
934         break;
935         
936         case 117:
937             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
938             charcode = NSDeleteFunctionKey;
939         break;
940         
941         case 118:
942             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
943             charcode = NSF4FunctionKey;
944         break;
945         
946         case 119:
947             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
948             charcode = NSEndFunctionKey;
949         break;
950         
951         case 120:
952             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
953             charcode = NSF2FunctionKey;
954         break;
955         
956         case 121:
957             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
958             charcode = NSPageDownFunctionKey;
959         break;
960         
961         case 122:
962             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
963             charcode = NSF1FunctionKey;
964         break;
965         
966         case 123:
967             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
968             charcode = NSLeftArrowFunctionKey;
969         break;
970         
971         case 124:
972             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
973             charcode = NSRightArrowFunctionKey;
974         break;
975         
976         case 125:
977             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
978             charcode = NSDownArrowFunctionKey;
979         break;
980         
981         case 126:
982             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
983             charcode = NSUpArrowFunctionKey;
984         break;
985     }
986     
987     if (charcode == 'a') {
988         unsigned long state;
989         long keyTrans;
990         char charCode;
991         Ptr kchr;
992         state = 0;
993         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
994         keyTrans = KeyTranslate(kchr, code, &state);
995         charCode = keyTrans;
996         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
997         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
998     } else if (charcode != 'b') {
999         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1000     }
1001     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1002 }
1003
1004 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1005 {
1006     NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1007     NSEnumerator *volEnum = [volumes objectEnumerator];
1008     NSString *nextVolume;
1009     ITDebugLog(@"Looking for an iPod named %@", name);
1010     while ( (nextVolume = [volEnum nextObject]) ) {
1011         ITDebugLog(@"- %@", nextVolume);
1012         if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1013             NSFileHandle *handle;
1014             NSData *data;
1015             NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1016             if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1017                 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1018                 return NO;
1019             }
1020             handle = [NSFileHandle fileHandleForReadingAtPath:path];
1021             ITDebugLog(@"File handle: %@", handle);
1022             [handle seekToFileOffset:10];
1023             data = [handle readDataOfLength:1];
1024             ITDebugLog(@"Data: %@", data);
1025             if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1026                 ITDebugLog(@"iPod is manually updated. %@", path);
1027                 return NO;
1028             } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1029                 ITDebugLog(@"iPod is automatically updated. %@", path);
1030                 return YES;
1031             } else {
1032                 ITDebugLog(@"Error! Value: %h  Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);
1033                 return NO;
1034             }
1035         }
1036     }
1037     return YES;
1038 }
1039
1040 @end