424957f8ee4445334779ae31217cfa0582e89ba5
[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             NS_HANDLER
407                 [[MainController sharedController] networkError:localException];
408             NS_ENDHANDLER
409         } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
410             ITDebugLog(@"Add \"Song Rating\" submenu.");
411             tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
412                     action:nil
413                     keyEquivalent:@""];
414             [tempItem setSubmenu:_ratingMenu];
415             [tempItem setTag:1];
416             if (_playingRadio || !_currentPlaylist) {
417                 [tempItem setEnabled:NO];
418             }
419             
420             itemEnum = [[_ratingMenu itemArray] objectEnumerator];
421             while ( (tempItem = [itemEnum nextObject]) ) {
422                 [tempItem setState:NSOffState];
423             }
424             
425             NS_DURING
426                 [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
427             NS_HANDLER
428                 [[MainController sharedController] networkError:localException];
429             NS_ENDHANDLER
430         } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
431             ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
432             tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
433                     action:nil
434                     keyEquivalent:@""];
435             [tempItem setSubmenu:_upcomingSongsMenu];
436             [tempItem setTag:2];
437             if (_playingRadio || !_currentPlaylist) {
438                 [tempItem setEnabled:NO];
439             }
440         } else if ([nextObject isEqualToString:@"artists"]) {
441             ITDebugLog(@"Add \"Artists\" submenu.");
442             tempItem = [menu addItemWithTitle:NSLocalizedString(@"artists", @"Artists")
443                     action:nil
444                     keyEquivalent:@""];
445             [tempItem setSubmenu:_artistsMenu];
446             [tempItem setTag:5];
447         } else if ([nextObject isEqualToString:@"albums"]) {
448             ITDebugLog(@"Add \"Albums\" submenu.");
449             tempItem = [menu addItemWithTitle:NSLocalizedString(@"albums", @"Albums")
450                     action:nil
451                     keyEquivalent:@""];
452             [tempItem setSubmenu:_albumsMenu];
453             [tempItem setTag:6];
454         }
455     }
456     ITDebugLog(@"Finished building menu.");
457     [_currentMenu release];
458     _currentMenu = menu;
459     return _currentMenu;
460 }
461
462 - (NSMenu *)menuForNoPlayer
463 {
464     NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
465     id <NSMenuItem> tempItem = nil;
466     ITDebugLog(@"Creating menu for when player isn't running.");
467     NS_DURING
468         ITDebugLog(@"Add \"Open %@\" menu item.", [[[MainController sharedController] currentRemote] playerSimpleName]);
469         tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"open", @"Open"), [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""];
470     NS_HANDLER
471         [[MainController sharedController] networkError:localException];
472     NS_ENDHANDLER
473     [tempItem setTag:MTMenuShowPlayerItem];
474     [tempItem setTarget:self];
475     ITDebugLog(@"Add a separator menu item.");
476     [menu addItem:[NSMenuItem separatorItem]];
477     ITDebugLog(@"Add \"Preferences...\" menu item.");
478     tempItem = [menu addItemWithTitle:NSLocalizedString(@"preferences", @"Preferences...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
479     [tempItem setTag:MTMenuPreferencesItem];
480     [tempItem setTarget:self];
481     if ([[MainController sharedController] blingBling] == NO) {
482         ITDebugLog(@"Add \"Register MenuTunes...\" menu item.");
483         tempItem = [menu addItemWithTitle:NSLocalizedString(@"register", @"Register MenuTunes...") action:@selector(performMainMenuAction:) keyEquivalent:@""];
484         [tempItem setTag:MTMenuRegisterItem];
485         [tempItem setTarget:self];
486     }
487     ITDebugLog(@"Add \"Quit\" menu item.");
488     tempItem = [menu addItemWithTitle:NSLocalizedString(@"quit", @"Quit") action:@selector(performMainMenuAction:) keyEquivalent:@""];
489     [tempItem setTag:MTMenuQuitItem];
490     [tempItem setTarget:self];
491     return [menu autorelease];
492 }
493
494 - (void)rebuildSubmenus
495 {
496     NSArray *menu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
497     ITDebugLog(@"Rebuilding all of the submenus.");
498     NS_DURING
499         _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
500         _currentTrack = [[[MainController sharedController] currentRemote] currentSongIndex];
501         _playingRadio = ([[[MainController sharedController] currentRemote] currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist);
502     NS_HANDLER
503         [[MainController sharedController] networkError:localException];
504     NS_ENDHANDLER
505     ITDebugLog(@"Releasing old submenus.");
506     ITDebugLog(@" - Rating menu");
507     [_ratingMenu release];
508     ITDebugLog(@" - Upcoming songs menu");
509     [_upcomingSongsMenu release];
510     ITDebugLog(@" - Playlists menu");
511     [_playlistsMenu release];
512     ITDebugLog(@" - EQ menu");
513     [_eqMenu release];
514     
515     ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
516     _ratingMenu = [self ratingMenu];
517     ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
518     _upcomingSongsMenu = [self upcomingSongsMenu];
519     ITDebugLog(@"Beginning Rebuild of \"Playlists\" submenu.");
520     _playlistsMenu = [self playlistsMenu];
521     ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
522     _eqMenu = [self eqMenu];
523     
524     if ([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 ([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 }
539
540 - (NSMenu *)ratingMenu
541 {
542     NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""];
543     NSEnumerator *itemEnum;
544     id  anItem;
545     int itemTag = 0;
546     SEL itemSelector = @selector(performRatingMenuAction:);
547     
548     ITDebugLog(@"Building \"Song Rating\" menu.");
549     
550     [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""];
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     
557     itemEnum = [[ratingMenu itemArray] objectEnumerator];
558     while ( (anItem = [itemEnum nextObject]) ) {
559         ITDebugLog(@"Setting up \"%@\" menu item.", [anItem title]);
560         [anItem setAction:itemSelector];
561         [anItem setTarget:self];
562         [anItem setTag:itemTag];
563         itemTag += 20;
564     }
565     ITDebugLog(@"Done Building \"Song Rating\" menu.");
566     return ratingMenu;
567 }
568
569 - (NSMenu *)upcomingSongsMenu
570 {
571     NSMenu *upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
572     int numSongs = 0, numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
573     NS_DURING
574         numSongs = [[[MainController sharedController] currentRemote] numberOfSongsInPlaylistAtIndex:_currentPlaylist];
575     NS_HANDLER
576         [[MainController sharedController] networkError:localException];
577     NS_ENDHANDLER
578     
579     ITDebugLog(@"Building \"Upcoming Songs\" menu.");
580     if (_currentPlaylist && !_playingRadio) {
581         if (numSongs > 0) {
582             int i;
583             for (i = _currentTrack + 1; i <= _currentTrack + numSongsInAdvance; i++) {
584                 if (i <= numSongs) {
585                     NSString *curSong = nil;
586                     NS_DURING
587                         curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
588                     NS_HANDLER
589                         [[MainController sharedController] networkError:localException];
590                     NS_ENDHANDLER
591                     id <NSMenuItem> songItem;
592                     ITDebugLog(@"Adding song: %@", curSong);
593                     songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
594                     [songItem setTag:i];
595                     [songItem setTarget:self];
596                 } else {
597                     break;
598                 }
599             }
600         }
601         
602         if ([upcomingSongsMenu numberOfItems] == 0) {
603             [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
604         }
605     }
606     ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
607     return upcomingSongsMenu;
608 }
609
610 /*- (NSMenu *)playlistsMenu
611 {
612     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
613     NSArray *playlists;
614     id <NSMenuItem> tempItem;
615     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
616     int i;
617     NS_DURING
618         playlists = [[[MainController sharedController] currentRemote] playlists];
619     NS_HANDLER
620         [[MainController sharedController] networkError:localException];
621     NS_ENDHANDLER
622     
623     ITDebugLog(@"Building \"Playlists\" menu.");
624     
625     for (i = 0; i < [playlists count]; i++) {
626         NSString *curPlaylist = [playlists objectAtIndex:i];
627         ITDebugLog(@"Adding playlist: %@", curPlaylist);
628         tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
629         [tempItem setTag:i + 1];
630         [tempItem setTarget:self];
631     }
632     
633     if (source == ITMTRemoteRadioSource) {
634         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
635     } else if (source == ITMTRemoteGenericDeviceSource) {
636         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
637     } else if (source == ITMTRemoteiPodSource) {
638         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
639     } else if (source == ITMTRemoteCDSource) {
640         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
641     } else if (source == ITMTRemoteSharedLibrarySource) {
642         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
643     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
644         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
645     }
646     ITDebugLog(@"Done Building \"Playlists\" menu");
647     return playlistsMenu;
648 }*/
649
650
651 - (NSMenu *)playlistsMenu
652 {
653     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
654     NSArray *playlists = nil;
655     id <NSMenuItem> tempItem;
656     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
657     int i, j;
658     NSMutableArray *indices = [[NSMutableArray alloc] init];
659     NS_DURING
660         playlists = [[[MainController sharedController] currentRemote] playlists];
661     NS_HANDLER
662         [[MainController sharedController] networkError:localException];
663     NS_ENDHANDLER
664     ITDebugLog(@"Building \"Playlists\" menu.");
665     {
666         NSArray *curPlaylist = [playlists objectAtIndex:0];
667         NSString *name = [curPlaylist objectAtIndex:0];
668         ITDebugLog(@"Adding main source: %@", name);
669         for (i = 3; i < [curPlaylist count]; i++) {
670             ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:i]);
671             tempItem = [playlistsMenu addItemWithTitle:[curPlaylist objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
672             [tempItem setTag:i - 1];
673             [tempItem setTarget:self];
674         }
675         ITDebugLog(@"Adding index to the index array.");
676         [indices addObject:[curPlaylist objectAtIndex:2]];
677     }
678     if ([playlists count] > 1) {
679         if ([[[playlists objectAtIndex:1] objectAtIndex:1] intValue] == ITMTRemoteRadioSource) {
680             [indices addObject:[[playlists objectAtIndex:1] objectAtIndex:2]];
681             if (source == ITMTRemoteRadioSource) {
682                 [playlistsMenu addItem:[NSMenuItem separatorItem]];
683                 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
684             }
685         } else {
686             [playlistsMenu addItem:[NSMenuItem separatorItem]];
687         }
688     }
689     
690     if ([playlists count] > 1) {
691         for (i = 1; i < [playlists count]; i++) {
692             NSArray *curPlaylist = [playlists objectAtIndex:i];
693             if ([[curPlaylist objectAtIndex:1] intValue] != ITMTRemoteRadioSource) {
694                 NSString *name = [curPlaylist objectAtIndex:0];
695                 NSMenu *submenu = [[NSMenu alloc] init];
696                 ITDebugLog(@"Adding source: %@", name);
697                 
698                 if ( ([[curPlaylist objectAtIndex:1] intValue] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
699                     ITDebugLog(@"Invalid iPod source.");
700                     [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
701                 } else {
702                     for (j = 3; j < [curPlaylist count]; j++) {
703                         ITDebugLog(@"Adding playlist: %@", [curPlaylist objectAtIndex:j]);
704                         tempItem = [submenu addItemWithTitle:[curPlaylist objectAtIndex:j] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
705                         [tempItem setTag:(i * 1000) + j - 1];
706                         [tempItem setTarget:self];
707                     }
708                     [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[submenu autorelease]];
709                 }
710                 ITDebugLog(@"Adding index to the index array.");
711                 [indices addObject:[curPlaylist objectAtIndex:2]];
712             }
713         }
714     }
715     ITDebugLog(@"Checking the current source.");
716     if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
717         tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
718         [tempItem setState:NSOnState];
719         [[[tempItem submenu] itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
720     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
721         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
722     }
723     [indices release];
724     tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
725     [tempItem setTarget:self];
726     [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
727     ITDebugLog(@"Done Building \"Playlists\" menu");
728     return playlistsMenu;
729 }
730
731 - (NSMenu *)eqMenu
732 {
733     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
734     NSArray *eqPresets = nil;
735     id <NSMenuItem> tempItem;
736     int i;
737     
738     NS_DURING
739         eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
740     NS_HANDLER
741         [[MainController sharedController] networkError:localException];
742     NS_ENDHANDLER
743     
744     ITDebugLog(@"Building \"EQ Presets\" menu.");
745     
746     tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
747     [tempItem setTag:-1];
748     [tempItem setTarget:self];
749     [eqMenu addItem:[NSMenuItem separatorItem]];
750     
751     for (i = 0; i < [eqPresets count]; i++) {
752         NSString *name;
753            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
754             ITDebugLog(@"Adding EQ Preset: %@", name);
755             tempItem = [eqMenu addItemWithTitle:name
756                     action:@selector(performEqualizerMenuAction:)
757                     keyEquivalent:@""];
758             [tempItem setTag:i];
759             [tempItem setTarget:self];
760            }
761     }
762     ITDebugLog(@"Done Building \"EQ Presets\" menu");
763     return eqMenu;
764 }
765
766 - (NSMenu *)artistsMenu
767 {
768     NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@"Artists"];
769     NSEnumerator *artistsEnumerator;
770     NSString *nextArtist;
771     id <NSMenuItem> tempItem;
772     ITDebugLog(@"Building \"Artists\" menu.");
773     NS_DURING
774         artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
775         while ( (nextArtist = [artistsEnumerator nextObject]) ) {
776             tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
777             [tempItem setTarget:self];
778         }
779     NS_HANDLER
780         [[MainController sharedController] networkError:localException];
781     NS_ENDHANDLER
782     ITDebugLog(@"Done Building \"Artists\" menu");
783     return artistsMenu;
784 }
785
786 - (NSMenu *)albumsMenu
787 {
788     NSMenu *albumsMenu = [[NSMenu alloc] initWithTitle:@"Albums"];
789     NSEnumerator *albumsEnumerator;
790     NSString *nextAlbum;
791     id <NSMenuItem> tempItem;
792     ITDebugLog(@"Building \"Albums\" menu.");
793     NS_DURING
794         albumsEnumerator = [[[[MainController sharedController] currentRemote] albums] objectEnumerator];
795         while ( (nextAlbum = [albumsEnumerator nextObject]) ) {
796             tempItem = [albumsMenu addItemWithTitle:nextAlbum action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
797             [tempItem setTarget:self];
798         }
799     NS_HANDLER
800         [[MainController sharedController] networkError:localException];
801     NS_ENDHANDLER
802     ITDebugLog(@"Done Building \"Albums\" menu");
803     return albumsMenu;
804 }
805
806 - (void)performMainMenuAction:(id)sender
807 {
808     switch ( [sender tag] )
809     {
810         case MTMenuPlayPauseItem:
811             ITDebugLog(@"Performing Menu Action: Play/Pause");
812             [[MainController sharedController] playPause];
813             break;
814         case MTMenuFastForwardItem:
815             ITDebugLog(@"Performing Menu Action: Fast Forward");
816             [[MainController sharedController] fastForward];
817             break;
818         case MTMenuRewindItem:
819             ITDebugLog(@"Performing Menu Action: Rewind");
820             [[MainController sharedController] rewind];
821             break;
822         case MTMenuPreviousTrackItem:
823             ITDebugLog(@"Performing Menu Action: Previous Track");
824             [[MainController sharedController] prevSong];
825             break;
826         case MTMenuNextTrackItem:
827             ITDebugLog(@"Performing Menu Action: Next Track");
828             [[MainController sharedController] nextSong];
829             break;
830         case MTMenuShowPlayerItem:
831             ITDebugLog(@"Performing Menu Action: Show Main Interface");
832             [[MainController sharedController] showPlayer];
833             break;
834         case MTMenuPreferencesItem:
835             ITDebugLog(@"Performing Menu Action: Preferences...");
836             [[MainController sharedController] showPreferences];
837             break;
838         case MTMenuQuitItem:
839             ITDebugLog(@"Performing Menu Action: Quit");
840             [[MainController sharedController] quitMenuTunes];
841             break;
842         case MTMenuRegisterItem:
843             ITDebugLog(@"Performing Menu Action: Register");
844             [[MainController sharedController] blingNow];
845             break;
846         default:
847             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
848             break;
849     }
850 }
851
852 - (void)performRatingMenuAction:(id)sender
853 {
854     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
855     [[MainController sharedController] selectSongRating:[sender tag]];
856 }
857
858 - (void)performPlaylistMenuAction:(id)sender
859 {
860     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
861     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
862 }
863
864 - (void)performEqualizerMenuAction:(id)sender
865 {
866     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
867     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
868 }
869
870 - (void)performUpcomingSongsMenuAction:(id)sender
871 {
872     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
873     [[MainController sharedController] selectSongAtIndex:[sender tag]];
874 }
875
876 - (void)performBrowseMenuAction:(id)sender
877 {
878     ITDebugLog(@"Browse action selected on item named %@", [sender title]);
879     /*
880     ** 1 - Artist
881     ** 2 - Album
882     ** 3 - Genre?
883     */
884     [[MainController sharedController] makePlaylistWithTerm:[sender title] ofType:(([[[sender menu] title] isEqualToString:@"Artists"]) ? 1 : 2)];
885 }
886
887 - (void)updateMenu
888 {
889     ITDebugLog(@"Update Menu");
890     [_currentMenu update];
891 }
892
893 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
894 {
895     return YES;
896 }
897
898 //This is never used I know, keep it though
899 - (NSString *)systemUIColor
900 {
901     NSDictionary *tmpDict;
902     NSNumber *tmpNumber;
903     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
904         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
905             if ( ([tmpNumber intValue] == 1) ) {
906                 return @"Aqua";
907             } else {
908                 return @"Graphite";
909             }
910         } else {
911             return @"Aqua";
912         }
913     } else {
914         return @"Aqua";
915     }
916 }
917
918 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
919         onItem:(id <NSMenuItem>)item
920 {
921     unichar charcode = 'a';
922     int i;
923     long cocoaModifiers = 0;
924     static long carbonToCocoa[6][2] = 
925     {
926         { cmdKey, NSCommandKeyMask },
927         { optionKey, NSAlternateKeyMask },
928         { controlKey, NSControlKeyMask },
929         { shiftKey, NSShiftKeyMask },
930     };
931     
932     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
933     
934     for (i = 0; i < 6; i++) {
935         if (modifiers & carbonToCocoa[i][0]) {
936             cocoaModifiers += carbonToCocoa[i][1];
937         }
938     }
939     [item setKeyEquivalentModifierMask:cocoaModifiers];
940     
941     //Missing key combos for some keys. Must find them later.
942     switch (code)
943     {
944         case 36:
945             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
946             charcode = '\r';
947         break;
948         
949         case 48:
950             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
951             charcode = '\t';
952         break;
953         
954         //Space -- ARGH!
955         case 49:
956         {
957             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
958             // Haven't tested this, though it should work.
959             // This doesn't work. :'(
960             //unichar buffer;
961             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
962             //charcode = buffer;
963             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
964             ITDebugLog(@"%@", menuRef);
965             SetMenuItemCommandKey(menuRef, 0, NO, 49);
966             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
967             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
968             charcode = 'b';*/
969             unichar buffer;
970             [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
971             charcode = buffer;
972         }
973         break;
974         
975         case 51:
976             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
977             charcode = NSDeleteFunctionKey;
978         break;
979         
980         case 53:
981             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
982             charcode = '\e';
983         break;
984         
985         case 71:
986             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
987             charcode = '\e';
988         break;
989         
990         case 76:
991             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
992             charcode = '\r';
993         break;
994         
995         case 96:
996             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
997             charcode = NSF5FunctionKey;
998         break;
999         
1000         case 97:
1001             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
1002             charcode = NSF6FunctionKey;
1003         break;
1004         
1005         case 98:
1006             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
1007             charcode = NSF7FunctionKey;
1008         break;
1009         
1010         case 99:
1011             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
1012             charcode = NSF3FunctionKey;
1013         break;
1014         
1015         case 100:
1016             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
1017             charcode = NSF8FunctionKey;
1018         break;
1019         
1020         case 101:
1021             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
1022             charcode = NSF9FunctionKey;
1023         break;
1024         
1025         case 103:
1026             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1027             charcode = NSF11FunctionKey;
1028         break;
1029         
1030         case 105:
1031             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1032             charcode = NSF13FunctionKey;
1033         break;
1034         
1035         case 107:
1036             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1037             charcode = NSF14FunctionKey;
1038         break;
1039         
1040         case 109:
1041             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1042             charcode = NSF10FunctionKey;
1043         break;
1044         
1045         case 111:
1046             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1047             charcode = NSF12FunctionKey;
1048         break;
1049         
1050         case 113:
1051             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1052             charcode = NSF13FunctionKey;
1053         break;
1054         
1055         case 114:
1056             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1057             charcode = NSInsertFunctionKey;
1058         break;
1059         
1060         case 115:
1061             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1062             charcode = NSHomeFunctionKey;
1063         break;
1064         
1065         case 116:
1066             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1067             charcode = NSPageUpFunctionKey;
1068         break;
1069         
1070         case 117:
1071             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1072             charcode = NSDeleteFunctionKey;
1073         break;
1074         
1075         case 118:
1076             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1077             charcode = NSF4FunctionKey;
1078         break;
1079         
1080         case 119:
1081             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1082             charcode = NSEndFunctionKey;
1083         break;
1084         
1085         case 120:
1086             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1087             charcode = NSF2FunctionKey;
1088         break;
1089         
1090         case 121:
1091             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1092             charcode = NSPageDownFunctionKey;
1093         break;
1094         
1095         case 122:
1096             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1097             charcode = NSF1FunctionKey;
1098         break;
1099         
1100         case 123:
1101             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1102             charcode = NSLeftArrowFunctionKey;
1103         break;
1104         
1105         case 124:
1106             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1107             charcode = NSRightArrowFunctionKey;
1108         break;
1109         
1110         case 125:
1111             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1112             charcode = NSDownArrowFunctionKey;
1113         break;
1114         
1115         case 126:
1116             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1117             charcode = NSUpArrowFunctionKey;
1118         break;
1119     }
1120     
1121     if (charcode == 'a') {
1122         unsigned long state;
1123         long keyTrans;
1124         char charCode;
1125         Ptr kchr;
1126         state = 0;
1127         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1128         keyTrans = KeyTranslate(kchr, code, &state);
1129         charCode = keyTrans;
1130         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1131         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1132     } else if (charcode != 'b') {
1133         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1134     }
1135     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1136 }
1137
1138 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1139 {
1140     NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1141     NSEnumerator *volEnum = [volumes objectEnumerator];
1142     NSString *nextVolume;
1143     ITDebugLog(@"Looking for an iPod named %@", name);
1144     while ( (nextVolume = [volEnum nextObject]) ) {
1145         ITDebugLog(@"- %@", nextVolume);
1146         if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1147             NSFileHandle *handle;
1148             NSData *data;
1149             NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1150             if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1151                 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1152                 return NO;
1153             }
1154             handle = [NSFileHandle fileHandleForReadingAtPath:path];
1155             ITDebugLog(@"File handle: %@", handle);
1156             [handle seekToFileOffset:10];
1157             data = [handle readDataOfLength:1];
1158             ITDebugLog(@"Data: %@", data);
1159             if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1160                 ITDebugLog(@"iPod is manually updated. %@", path);
1161                 return NO;
1162             } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1163                 ITDebugLog(@"iPod is automatically updated. %@", path);
1164                 return YES;
1165             } else {
1166                 ITDebugLog(@"Error! Value: %h  Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);
1167                 return NO;
1168             }
1169         }
1170     }
1171     return YES;
1172 }
1173
1174 @end