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