Readding multisource playlist thingy. Added a tooltip showing the current
[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)iPodAtPathAutomaticallyUpdates:(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:@"showTrackNumber"]) {
274                         int track;
275                         NS_DURING
276                             track = [[[MainController sharedController] currentRemote] currentSongTrack];
277                         NS_HANDLER
278                             [[MainController sharedController] networkError:localException];
279                         NS_ENDHANDLER
280                         ITDebugLog(@"Add Track Number (\"Track %i\") menu item.", track);
281                         if ( track > 0 ) {
282                             [menu indentItem:
283                                 [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %i", NSLocalizedString(@"track", @"Track"), track] action:nil keyEquivalent:@""]];
284                         }
285                     }
286                 }
287                 
288                 NS_DURING
289                     if ([defaults boolForKey:@"showTime"] && ( ([[[MainController sharedController] currentRemote] currentSongElapsed] != nil) || ([[[MainController sharedController] currentRemote] currentSongLength] != nil) )) {
290                         ITDebugLog(@"Add Track Elapsed (\"%@/%@\") menu item.", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]);
291                         [menu indentItem:[menu addItemWithTitle:[NSString stringWithFormat:@"%@/%@", [[[MainController sharedController] currentRemote] currentSongElapsed], [[[MainController sharedController] currentRemote] currentSongLength]] action:nil keyEquivalent:@""]];
292                     }
293                 NS_HANDLER
294                     [[MainController sharedController] networkError:localException];
295                 NS_ENDHANDLER
296                 
297                 if (!_playingRadio) {
298                     NS_DURING
299                         if ([defaults boolForKey:@"showTrackRating"] && ( [[[MainController sharedController] currentRemote] currentSongRating] != -1.0 )) {
300                             NSString *string = nil;
301                             switch ((int)([[[MainController sharedController] currentRemote] currentSongRating] * 5)) {
302                                 case 0:
303                                     string = [NSString stringWithUTF8String:"☆☆☆☆☆"];
304                                 break;
305                                 case 1:
306                                     string = [NSString stringWithUTF8String:"★☆☆☆☆"];
307                                 break;
308                                 case 2:
309                                     string = [NSString stringWithUTF8String:"★★☆☆☆"];
310                                 break;
311                                 case 3:
312                                     string = [NSString stringWithUTF8String:"★★★☆☆"];
313                                 break;
314                                 case 4:
315                                     string = [NSString stringWithUTF8String:"★★★★☆"];
316                                 break;
317                                 case 5:
318                                     string = [NSString stringWithUTF8String:"★★★★★"];
319                                 break;
320                             }
321                             ITDebugLog(@"Add Track Rating (\"%@\") menu item.", string);
322                             [menu indentItem:[menu addItemWithTitle:string action:nil keyEquivalent:@""]];
323                         }
324                     NS_HANDLER
325                         [[MainController sharedController] networkError:localException];
326                     NS_ENDHANDLER
327                 }
328             } else {
329                 ITDebugLog(@"No Track is Playing, Add \"No Song\" menu item.");
330                 [menu addItemWithTitle:NSLocalizedString(@"noSong", @"No Song") action:NULL keyEquivalent:@""];
331             }
332         } else if ([nextObject isEqualToString:@"separator"]) {
333             ITDebugLog(@"Add a separator menu item.");
334             [menu addItem:[NSMenuItem separatorItem]];
335         //Submenu items
336         } else if ([nextObject isEqualToString:@"playlists"]) {
337             ITDebugLog(@"Add \"Playlists\" submenu.");
338             tempItem = [menu addItemWithTitle:NSLocalizedString(@"playlists", @"Playlists")
339                     action:nil
340                     keyEquivalent:@""];
341             [tempItem setSubmenu:_playlistsMenu];
342             [tempItem setTag:3];
343         } else if ([nextObject isEqualToString:@"eqPresets"]) {
344             ITDebugLog(@"Add \"EQ Presets\" submenu.");
345             tempItem = [menu addItemWithTitle:NSLocalizedString(@"eqPresets", @"EQ Presets")
346                     action:nil
347                     keyEquivalent:@""];
348             [tempItem setSubmenu:_eqMenu];
349             [tempItem setTag:4];
350             
351             itemEnum = [[_eqMenu itemArray] objectEnumerator];
352             while ( (tempItem = [itemEnum nextObject]) ) {
353                 [tempItem setState:NSOffState];
354             }
355             NS_DURING
356                 [[_eqMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentEQPresetIndex] - 1)] setState:NSOnState];
357             NS_HANDLER
358                 [[MainController sharedController] networkError:localException];
359             NS_ENDHANDLER
360         } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
361                 ITDebugLog(@"Add \"Song Rating\" submenu.");
362                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
363                         action:nil
364                         keyEquivalent:@""];
365                 [tempItem setSubmenu:_ratingMenu];
366                 [tempItem setTag:1];
367                 if (_playingRadio || !_currentPlaylist) {
368                     [tempItem setEnabled:NO];
369                 }
370                 
371                 itemEnum = [[_ratingMenu itemArray] objectEnumerator];
372                 while ( (tempItem = [itemEnum nextObject]) ) {
373                     [tempItem setState:NSOffState];
374                 }
375                 
376                 NS_DURING
377                     [[_ratingMenu itemAtIndex:([[[MainController sharedController] currentRemote] currentSongRating] * 5)] setState:NSOnState];
378                 NS_HANDLER
379                     [[MainController sharedController] networkError:localException];
380                 NS_ENDHANDLER
381             } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
382                 ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
383                 tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
384                         action:nil
385                         keyEquivalent:@""];
386                 [tempItem setSubmenu:_upcomingSongsMenu];
387                 [tempItem setTag:2];
388                 if (_playingRadio || !_currentPlaylist) {
389                     [tempItem setEnabled:NO];
390                 }
391             }
392         }
393     ITDebugLog(@"Finished building menu.");
394     [_currentMenu release];
395     _currentMenu = menu;
396     return _currentMenu;
397 }
398
399 - (NSMenu *)menuForNoPlayer
400 {
401     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
402     id <NSMenuItem> tempItem;
403     ITDebugLog(@"Creating menu for when player isn't running.");
404     NS_DURING
405         ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
406         tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
407     NS_HANDLER
408         [[MainController sharedController] networkError:localException];
409     NS_ENDHANDLER
410     [tempItem setTag:MTMenuShowPlayerItem];
411     [tempItem setTarget:self];
412     ITDebugLog(@"Add a separator menu item.");
413     [menu addItem:[NSMenuItem separatorItem]];
414     ITDebugLog(@"Add \"Preferences...\" menu item.");
415     tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
416     [tempItem setTag:MTMenuPreferencesItem];
417     [tempItem setTarget:self];
418     if ([[MainController sharedController] blingBling] == NO) {
419         ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
420         tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
421         [tempItem setTag:MTMenuRegisterItem];
422         [tempItem setTarget:self];
423     }
424     ITDebugLog(@"Add \"Quit\" menu item.");
425     tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
426     [tempItem setTag:MTMenuQuitItem];
427     [tempItem setTarget:self];
428     return [menu autorelease];
429 }
430
431 - (void)rebuildSubmenus
432 {
433     ITDebugLog(@"Rebuilding all of the submenus.");
434     NS_DURING
435         _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
436         _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
437         _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
438     NS_HANDLER
439         [[MainController sharedController] networkError:localException];
440     NS_ENDHANDLER
441     [_ratingMenu release];
442     [_upcomingSongsMenu release];
443     [_playlistsMenu release];
444     [_eqMenu release];
445     ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
446     _ratingMenu = [self ratingMenu];
447     ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
448     _upcomingSongsMenu = [self upcomingSongsMenu];
449     ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
450     _playlistsMenu = [self playlistsMenu];
451     ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
452     _eqMenu = [self eqMenu];
453     ITDebugLog(@"Done rebuilding all of the submenus.");
454 }
455
456 - (NSMenu *)ratingMenu
457 {
458     NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
459     NSEnumerator *itemEnum;
460     id  anItem;
461     int itemTag = 0;
462     SEL itemSelector = @selector(performRatingMenuAction:);
463     
464     ITDebugLog(@"Building \"Song Rating\" menu.");
465     
466     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
467     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""];
468     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""];
469     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""];
470     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""];
471     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""];
472     
473     itemEnum = [[ratingMenu itemArray] objectEnumerator];
474     while ( (anItem = [itemEnum nextObject]) ) {
475         ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
476         [anItem setAction:itemSelector];
477         [anItem setTarget:self];
478         [anItem setTag:itemTag];
479         itemTag += 20;
480     }
481     ITDebugLog(@"Done Building \"Song Rating\" menu.");
482     return ratingMenu;
483 }
484
485 - (NSMenu *)upcomingSongsMenu
486 {
487     NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
488     int numSongs, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
489     
490     NS_DURING
491         numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
492     NS_HANDLER
493         [[MainController sharedController] networkError:localException];
494     NS_ENDHANDLER
495     
496     ITDebugLog(@"Building \"Upcoming Songs\" menu.");
497     if (_currentPlaylist && !_playingRadio) {
498         if (numSongs > 0) {
499             int i;
500
501             for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
502                 if (i <= numSongs) {
503                     NSString *curSong;
504                     NS_DURING
505                         curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
506                     NS_HANDLER
507                         [[MainController sharedController] networkError:localException];
508                     NS_ENDHANDLER
509                     id <NSMenuItem> songItem;
510                     ITDebugLog(@"Adding song: %@", curSong);
511                     songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
512                     [songItem setTag:i];
513                     [songItem setTarget:self];
514                 } else {
515                     break;
516                 }
517             }
518         }
519         
520         if ([upcomingSongsMenu numberOfItems] == 0) {
521             [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
522         }
523     }
524     ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
525     return upcomingSongsMenu;
526 }
527
528 /*- (NSMenu *)playlistsMenu
529 {
530     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
531     NSArray *playlists;
532     id <NSMenuItem> tempItem;
533     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
534     int i;
535     NS_DURING
536         playlists = [[[MainController sharedController] currentRemote] playlists];
537     NS_HANDLER
538         [[MainController sharedController] networkError:localException];
539     NS_ENDHANDLER
540     
541     ITDebugLog(@"Building \"Playlists\" menu.");
542     
543     for (i = 0; i < [playlists count]; i++) {
544         NSString *curPlaylist = [playlists objectAtIndex:i];
545         ITDebugLog(@"Adding playlist: %@", curPlaylist);
546         tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
547         [tempItem setTag:i + 1];
548         [tempItem setTarget:self];
549     }
550     
551     if (source == ITMTRemoteRadioSource) {
552         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
553     } else if (source == ITMTRemoteGenericDeviceSource) {
554         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
555     } else if (source == ITMTRemoteiPodSource) {
556         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
557     } else if (source == ITMTRemoteCDSource) {
558         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
559     } else if (source == ITMTRemoteSharedLibrarySource) {
560         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
561     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
562         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
563     }
564     ITDebugLog(@"Done Building \"Playlists\" menu");
565     return playlistsMenu;
566 }*/
567
568
569 - (NSMenu *)playlistsMenu
570 {
571     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
572     NSArray *playlists;
573     id <NSMenuItem> tempItem;
574     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
575     int i, j;
576     NS_DURING
577         playlists = [[[MainController sharedController] currentRemote] playlists];
578     NS_HANDLER
579         [[MainController sharedController] networkError:localException];
580     NS_ENDHANDLER
581     ITDebugLog(@"Building \"Playlists\" menu.");
582     {
583         NSArray *curPlaylist = [playlists objectAtIndex:0];
584         NSString *name = [curPlaylist objectAtIndex:0];
585         ITDebugLog(@"Adding main source: %@", name);
586         for (i = 2; i < [curPlaylist count]; i++) {
587             ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
588             tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
589             [tempItem setTag:i];
590             [tempItem setTarget:self];
591         }
592     }
593     if ( (source == ITMTRemoteRadioSource) || ([playlists count] - 2 > 0) ) {
594         [playlistsMenu addItem:[NSMenuItem separatorItem]];
595     }
596     
597     if (source == ITMTRemoteRadioSource) {
598         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
599     }
600     
601     for (i = 2; i < [playlists count]; i++) {
602         NSArray *curPlaylist = [playlists objectAtIndex:i];
603         NSString *name = [curPlaylist objectAtIndex:0];
604         NSMenu *submenu = [[NSMenu alloc] init];
605         ITDebugLog(@"Adding source: %@", name);
606         
607         if ( ([[curPlaylist objectAtIndex:i] intValue] == ITMTRemoteiPodSource) && (![self iPodAtPathAutomaticallyUpdates:[curPlaylist objectAtIndex:j]]) ) {
608             ITDebugLog(@"Invalid iPod source.");
609         } else {
610             for (j = 2; j < [curPlaylist count]; j++) {
611                 ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
612                 tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
613                 [tempItem setTag:(i * 1000) + j];
614                 [tempItem setTarget:self];
615             }
616             [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
617         }
618     }
619     
620     if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ){
621         tempItem = [playlistsMenu itemAtIndex:(int)[[[MainController sharedController] currentRemote] currentSourceIndex] + [playlistsMenu numberOfItems] - 5];
622         [tempItem setState:NSOnState];
623         [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
624     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
625         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
626     }
627     ITDebugLog(@"Done Building \"Playlists\" menu");
628     return playlistsMenu;
629 }
630
631 - (NSMenu *)eqMenu
632 {
633     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
634     NSArray *eqPresets;
635     id <NSMenuItem> tempItem;
636     int i;
637     
638     NS_DURING
639         eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
640     NS_HANDLER
641         [[MainController sharedController] networkError:localException];
642     NS_ENDHANDLER
643     
644     ITDebugLog(@"Building \"EQ Presets\" menu.");
645     
646     for (i = 0; i < [eqPresets count]; i++) {
647         NSString *name;
648            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
649             ITDebugLog(@"Adding EQ Preset: %@", name);
650             tempItem = [eqMenu addItemWithTitle:name
651                     action:@selector(performEqualizerMenuAction:)
652                     keyEquivalent:@""];
653             [tempItem setTag:i];
654             [tempItem setTarget:self];
655            }
656     }
657     ITDebugLog(@"Done Building \"EQ Presets\" menu");
658     return eqMenu;
659 }
660
661 - (void)performMainMenuAction:(id)sender
662 {
663     switch ( [sender tag] )
664     {
665         case MTMenuPlayPauseItem:
666             ITDebugLog(@"Performing Menu Action: Play/Pause");
667             [[MainController sharedController] playPause];
668             break;
669         case MTMenuFastForwardItem:
670             ITDebugLog(@"Performing Menu Action: Fast Forward");
671             [[MainController sharedController] fastForward];
672             break;
673         case MTMenuRewindItem:
674             ITDebugLog(@"Performing Menu Action: Rewind");
675             [[MainController sharedController] rewind];
676             break;
677         case MTMenuPreviousTrackItem:
678             ITDebugLog(@"Performing Menu Action: Previous Track");
679             [[MainController sharedController] prevSong];
680             break;
681         case MTMenuNextTrackItem:
682             ITDebugLog(@"Performing Menu Action: Next Track");
683             [[MainController sharedController] nextSong];
684             break;
685         case MTMenuShowPlayerItem:
686             ITDebugLog(@"Performing Menu Action: Show Main Interface");
687             [[MainController sharedController] showPlayer];
688             break;
689         case MTMenuPreferencesItem:
690             ITDebugLog(@"Performing Menu Action: Preferences...");
691             [[MainController sharedController] showPreferences];
692             break;
693         case MTMenuQuitItem:
694             ITDebugLog(@"Performing Menu Action: Quit");
695             [[MainController sharedController] quitMenuTunes];
696             break;
697         case MTMenuRegisterItem:
698             ITDebugLog(@"Performing Menu Action: Register");
699             [[MainController sharedController] blingNow];
700             break;
701         default:
702             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
703             break;
704     }
705 }
706
707 - (void)performRatingMenuAction:(id)sender
708 {
709     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
710     [[MainController sharedController] selectSongRating:[sender tag]];
711 }
712
713 - (void)performPlaylistMenuAction:(id)sender
714 {
715     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
716     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
717 }
718
719 - (void)performEqualizerMenuAction:(id)sender
720 {
721     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
722     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
723 }
724
725 - (void)performUpcomingSongsMenuAction:(id)sender
726 {
727     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
728     [[MainController sharedController] selectSongAtIndex:[sender tag]];
729 }
730
731 - (void)updateMenu
732 {
733     ITDebugLog(@"Update Menu");
734     [_currentMenu update];
735 }
736
737 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
738 {
739     return YES;
740 }
741
742 //This is never used I know, keep it though
743 - (NSString *)systemUIColor
744 {
745     NSDictionary *tmpDict;
746     NSNumber *tmpNumber;
747     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
748         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
749             if ( ([tmpNumber intValue] == 1) ) {
750                 return @"Aqua";
751             } else {
752                 return @"Graphite";
753             }
754         } else {
755             return @"Aqua";
756         }
757     } else {
758         return @"Aqua";
759     }
760 }
761
762 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
763         onItem:(id <NSMenuItem>)item
764 {
765     unichar charcode = 'a';
766     int i;
767     long cocoaModifiers = 0;
768     static long carbonToCocoa[6][2] = 
769     {
770         { cmdKey, NSCommandKeyMask },
771         { optionKey, NSAlternateKeyMask },
772         { controlKey, NSControlKeyMask },
773         { shiftKey, NSShiftKeyMask },
774     };
775     
776     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
777     
778     for (i = 0; i < 6; i++) {
779         if (modifiers & carbonToCocoa[i][0]) {
780             cocoaModifiers += carbonToCocoa[i][1];
781         }
782     }
783     [item setKeyEquivalentModifierMask:cocoaModifiers];
784     
785     //Missing key combos for some keys. Must find them later.
786     switch (code)
787     {
788         case 36:
789             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
790             charcode = '\r';
791         break;
792         
793         case 48:
794             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
795             charcode = '\t';
796         break;
797         
798         //Space -- ARGH!
799         case 49:
800         {
801             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
802             // Haven't tested this, though it should work.
803             // This doesn't work. :'(
804             //unichar buffer;
805             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
806             //charcode = buffer;
807             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
808             ITDebugLog(@"%@", menuRef);
809             SetMenuItemCommandKey(menuRef, 0, NO, 49);
810             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
811             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
812             charcode = 'b';*/
813             unichar buffer;
814             [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
815             charcode = buffer;
816         }
817         break;
818         
819         case 51:
820             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
821             charcode = NSDeleteFunctionKey;
822         break;
823         
824         case 53:
825             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
826             charcode = '\e';
827         break;
828         
829         case 71:
830             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
831             charcode = '\e';
832         break;
833         
834         case 76:
835             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
836             charcode = '\r';
837         break;
838         
839         case 96:
840             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
841             charcode = NSF5FunctionKey;
842         break;
843         
844         case 97:
845             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
846             charcode = NSF6FunctionKey;
847         break;
848         
849         case 98:
850             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
851             charcode = NSF7FunctionKey;
852         break;
853         
854         case 99:
855             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
856             charcode = NSF3FunctionKey;
857         break;
858         
859         case 100:
860             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
861             charcode = NSF8FunctionKey;
862         break;
863         
864         case 101:
865             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
866             charcode = NSF9FunctionKey;
867         break;
868         
869         case 103:
870             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
871             charcode = NSF11FunctionKey;
872         break;
873         
874         case 105:
875             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
876             charcode = NSF13FunctionKey;
877         break;
878         
879         case 107:
880             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
881             charcode = NSF14FunctionKey;
882         break;
883         
884         case 109:
885             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
886             charcode = NSF10FunctionKey;
887         break;
888         
889         case 111:
890             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
891             charcode = NSF12FunctionKey;
892         break;
893         
894         case 113:
895             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
896             charcode = NSF13FunctionKey;
897         break;
898         
899         case 114:
900             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
901             charcode = NSInsertFunctionKey;
902         break;
903         
904         case 115:
905             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
906             charcode = NSHomeFunctionKey;
907         break;
908         
909         case 116:
910             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
911             charcode = NSPageUpFunctionKey;
912         break;
913         
914         case 117:
915             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
916             charcode = NSDeleteFunctionKey;
917         break;
918         
919         case 118:
920             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
921             charcode = NSF4FunctionKey;
922         break;
923         
924         case 119:
925             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
926             charcode = NSEndFunctionKey;
927         break;
928         
929         case 120:
930             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
931             charcode = NSF2FunctionKey;
932         break;
933         
934         case 121:
935             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
936             charcode = NSPageDownFunctionKey;
937         break;
938         
939         case 122:
940             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
941             charcode = NSF1FunctionKey;
942         break;
943         
944         case 123:
945             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
946             charcode = NSLeftArrowFunctionKey;
947         break;
948         
949         case 124:
950             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
951             charcode = NSRightArrowFunctionKey;
952         break;
953         
954         case 125:
955             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
956             charcode = NSDownArrowFunctionKey;
957         break;
958         
959         case 126:
960             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
961             charcode = NSUpArrowFunctionKey;
962         break;
963     }
964     
965     if (charcode == 'a') {
966         unsigned long state;
967         long keyTrans;
968         char charCode;
969         Ptr kchr;
970         state = 0;
971         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
972         keyTrans = KeyTranslate(kchr, code, &state);
973         charCode = keyTrans;
974         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
975         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
976     } else if (charcode != 'b') {
977         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
978     }
979     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
980 }
981
982 - (BOOL)iPodAtPathAutomaticallyUpdates:(NSString *)name
983 {
984     NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
985     NSEnumerator *volEnum = [volumes objectEnumerator];
986     NSString *nextVolume;
987     
988     while ( (nextVolume = [volEnum nextObject]) ) {
989         if ([nextVolume rangeOfString:name options:nil range:NSMakeRange(0, [name length] - 1)].location != NSNotFound) {
990             NSFileHandle *handle;
991             NSData *data;
992             NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
993             if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
994                 ITDebugLog(@"Error, path isn't an iPod! %@", path);
995                 return NO;
996             }
997             handle = [NSFileHandle fileHandleForReadingAtPath:name];
998             [handle seekToFileOffset:10];
999             data = [handle readDataOfLength:1];
1000             if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1001                 ITDebugLog(@"iPod is manually updated. %@", path);
1002                 return NO;
1003             } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1004                 ITDebugLog(@"iPod is automatically updated. %@", path);
1005                 return YES;
1006             } else {
1007                 ITDebugLog(@"Error! Value: %h  Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);
1008                 return NO;
1009             }
1010         }
1011     }
1012     return NO;
1013 }
1014
1015 @end