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