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