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