Updated to version 1.6.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 < 1) {
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 <= numSongs; i++) {
613                                 BOOL enabled = YES;
614                                 
615                                 //Check if the song at this index is enabled for playback. If it isn't, skip over it
616                                 NS_DURING
617                                         enabled = [[[MainController sharedController] currentRemote] songEnabledAtIndex:i];
618                                 NS_HANDLER
619                                         [[MainController sharedController] networkError:localException];
620                                 NS_ENDHANDLER
621                                 
622                 if (enabled) {
623                     NSString *curSong = nil;
624                     NS_DURING
625                         curSong = [[[MainController sharedController] currentRemote] songTitleAtIndex:i];
626                     NS_HANDLER
627                         [[MainController sharedController] networkError:localException];
628                     NS_ENDHANDLER
629                     id <NSMenuItem> songItem;
630                     ITDebugLog(@"Adding song: %@", curSong);
631                     songItem = [upcomingSongsMenu addItemWithTitle:curSong action:@selector(performUpcomingSongsMenuAction:) keyEquivalent:@""];
632                     [songItem setTag:i];
633                     [songItem setTarget:self];
634                 } else {
635                                         numSongsInAdvance++;
636                                 }
637             }
638         }
639         
640         if ([upcomingSongsMenu numberOfItems] == 0) {
641             [upcomingSongsMenu addItemWithTitle:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.") action:NULL keyEquivalent:@""];
642         }
643     }
644     ITDebugLog(@"Done Building \"Upcoming Songs\" menu.");
645         NS_VALUERETURN(upcomingSongsMenu, NSMenu *);
646         NS_HANDLER
647                 [upcomingSongsMenu release];
648                 _continue = NO;
649                 NS_VALUERETURN(nil, NSMenu *);
650         NS_ENDHANDLER
651 }
652
653 /*- (NSMenu *)playlistsMenu
654 {
655     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
656     NSArray *playlists;
657     id <NSMenuItem> tempItem;
658     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
659     int i;
660     NS_DURING
661         playlists = [[[MainController sharedController] currentRemote] playlists];
662     NS_HANDLER
663         [[MainController sharedController] networkError:localException];
664     NS_ENDHANDLER
665     
666     ITDebugLog(@"Building \"Playlists\" menu.");
667     
668     for (i = 0; i < [playlists count]; i++) {
669         NSString *curPlaylist = [playlists objectAtIndex:i];
670         ITDebugLog(@"Adding playlist: %@", curPlaylist);
671         tempItem = [playlistsMenu addItemWithTitle:curPlaylist action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
672         [tempItem setTag:i + 1];
673         [tempItem setTarget:self];
674     }
675     
676     if (source == ITMTRemoteRadioSource) {
677         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:NULL keyEquivalent:@""] setState:NSOnState];
678     } else if (source == ITMTRemoteGenericDeviceSource) {
679         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"genericDevice", @"Generic Device") action:NULL keyEquivalent:@""] setState:NSOnState];
680     } else if (source == ITMTRemoteiPodSource) {
681         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"iPod", @"iPod") action:NULL keyEquivalent:@""] setState:NSOnState];
682     } else if (source == ITMTRemoteCDSource) {
683         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"cd", @"CD") action:NULL keyEquivalent:@""] setState:NSOnState];
684     } else if (source == ITMTRemoteSharedLibrarySource) {
685         [[playlistsMenu addItemWithTitle:NSLocalizedString(@"sharedLibrary", @"Shared Library") action:NULL keyEquivalent:@""] setState:NSOnState];
686     } else if (source == ITMTRemoteLibrarySource && _currentPlaylist) {
687         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
688     }
689     ITDebugLog(@"Done Building \"Playlists\" menu");
690     return playlistsMenu;
691 }*/
692
693 - (void)playlistsMenuAux:(NSMenu *)menu node:(PlaylistNode *)node tagPrefix:(int)p
694 {
695         id <NSMenuItem> tempItem;
696         int i;
697         
698         for (i = 0; i < [[node children] count]; i++) {
699                 PlaylistNode *nextNode = [[node children] objectAtIndex:i];
700                 if ([nextNode type] == ITMTFolderNode) {
701                         NSMenu *submenu = [[NSMenu alloc] init];
702                         tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
703                         [tempItem setTag:p + [nextNode index] + 1];
704                         [tempItem setTarget:self];
705                         [tempItem setSubmenu:submenu];
706                         [self playlistsMenuAux:[submenu autorelease] node:nextNode tagPrefix:p];
707                 } else {
708                         tempItem = [menu addItemWithTitle:[nextNode name] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""];
709                         [tempItem setTag:p + [nextNode index] + 1];
710                         [tempItem setTarget:self];
711                 }
712                 
713                 PlaylistNode *root = node;
714                 while ([root type] == ITMTPlaylistNode || [root type] == ITMTFolderNode) {
715                         root = [root parent];
716                 }
717                 
718                 if ([root index] == [[[MainController sharedController] currentRemote] currentSourceIndex] && [nextNode index] == _currentPlaylist) {
719                         [tempItem setState:NSOnState];
720                 }
721         }
722 }
723
724 - (NSMenu *)playlistsMenu
725 {
726     NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""];
727     NSArray *playlists = nil;
728     id <NSMenuItem> tempItem;
729     ITMTRemotePlayerSource source = [[[MainController sharedController] currentRemote] currentSource];
730         int i;
731     NSMutableArray *indices = [[NSMutableArray alloc] init];
732     NS_DURING
733         playlists = [[[MainController sharedController] currentRemote] playlists];
734     NS_HANDLER
735         [[MainController sharedController] networkError:localException];
736     NS_ENDHANDLER
737         
738         if (!playlists) {
739                 [playlistsMenu release];
740                 return nil;
741         }
742         NS_DURING
743     ITDebugLog(@"Building \"Playlists\" menu.");
744     {
745                 //First we add the main Library source, since it is guaranteed to be there.
746         PlaylistNode *library = [playlists objectAtIndex:0];
747         ITDebugLog(@"Adding main source: %@", [library name]);
748                 [self playlistsMenuAux:playlistsMenu node:library tagPrefix:0];
749         ITDebugLog(@"Adding index to the index array.");
750         [indices addObject:[NSNumber numberWithInt:[library index]]];
751     }
752         
753         //Next go through the other sources
754     if ([playlists count] > 1) {
755                 //Add the radio source if it is playing
756         if ([[playlists objectAtIndex:1] sourceType] == ITMTRemoteRadioSource) {
757             [indices addObject:[NSNumber numberWithInt:[[playlists objectAtIndex:1] index]]];
758             if (source == ITMTRemoteRadioSource) {
759                 [playlistsMenu addItem:[NSMenuItem separatorItem]];
760                 [[playlistsMenu addItemWithTitle:NSLocalizedString(@"radio", @"Radio") action:@selector(performPlaylistMenuAction:) keyEquivalent:@""] setState:NSOnState];
761             } else if ([playlists count] > 2) {
762                                 [playlistsMenu addItem:[NSMenuItem separatorItem]];
763                         }
764         }
765                 
766                 //Add other sources as needed (shared music, iPods, CDs)
767         for (i = [playlists count] - 1; i > 1 ; i--) {
768             PlaylistNode *nextSource = [playlists objectAtIndex:i];
769             if ([nextSource type] != ITMTRemoteRadioSource) {
770                 NSString *name = [nextSource name];
771                 ITDebugLog(@"Adding source: %@", name);
772                 
773                 if ( ([nextSource type] == ITMTRemoteiPodSource) && [self iPodWithNameAutomaticallyUpdates:name] ) {
774                     ITDebugLog(@"Invalid iPod source.");
775                     [playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""];
776                 } else {
777                                         NSMenu *menu = [[NSMenu alloc] init];
778                                         [[playlistsMenu addItemWithTitle:name action:NULL keyEquivalent:@""] setSubmenu:[menu autorelease]];
779                                         [self playlistsMenuAux:menu node:nextSource tagPrefix:(i * 1000)];
780                 }
781                 ITDebugLog(@"Adding index to the index array.");
782                 [indices addObject:[NSNumber numberWithInt:[nextSource index]]];
783             }
784         }
785     }
786         NS_DURING
787         if (_currentPlaylist != -1) {
788                 if ( (source == ITMTRemoteSharedLibrarySource) || (source == ITMTRemoteiPodSource) || (source == ITMTRemoteGenericDeviceSource) || (source == ITMTRemoteCDSource) ) {
789                         tempItem = [playlistsMenu itemAtIndex:[playlistsMenu numberOfItems] + [indices indexOfObject:[NSNumber numberWithInt:[[[MainController sharedController] currentRemote] currentSourceIndex]]] - [indices count]];
790                         [tempItem setState:NSOnState];
791                 }
792         }
793         NS_HANDLER
794         NS_ENDHANDLER
795     [indices release];
796     tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
797     [tempItem setTarget:self];
798     [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
799     ITDebugLog(@"Done Building \"Playlists\" menu");
800     NS_VALUERETURN(playlistsMenu, NSMenu *);
801         NS_HANDLER
802                 [playlistsMenu release];
803                 _continue = NO;
804                 NS_VALUERETURN(nil, NSMenu *);
805         NS_ENDHANDLER
806 }
807
808 - (NSMenu *)eqMenu
809 {
810     NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""];
811     NSArray *eqPresets = nil;
812     id <NSMenuItem> tempItem;
813     int i;
814     
815     NS_DURING
816         eqPresets = [[[MainController sharedController] currentRemote] eqPresets];
817     NS_HANDLER
818         [[MainController sharedController] networkError:localException];
819     NS_ENDHANDLER
820     
821     ITDebugLog(@"Building \"EQ Presets\" menu.");
822     
823     tempItem = [eqMenu addItemWithTitle:@"Enabled" action:@selector(performEqualizerMenuAction:) keyEquivalent:@""];
824     [tempItem setTag:-1];
825     [tempItem setTarget:self];
826     [eqMenu addItem:[NSMenuItem separatorItem]];
827     
828     for (i = 0; i < [eqPresets count]; i++) {
829         NSString *name;
830            if ( ( name = [eqPresets objectAtIndex:i] ) ) {
831             ITDebugLog(@"Adding EQ Preset: %@", name);
832             tempItem = [eqMenu addItemWithTitle:name
833                     action:@selector(performEqualizerMenuAction:)
834                     keyEquivalent:@""];
835             [tempItem setTag:i];
836             [tempItem setTarget:self];
837            }
838     }
839     ITDebugLog(@"Done Building \"EQ Presets\" menu");
840     return eqMenu;
841 }
842
843 - (NSMenu *)artistsMenu
844 {
845     NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@"Artists"];
846     NSEnumerator *artistsEnumerator;
847     NSString *nextArtist;
848     id <NSMenuItem> tempItem;
849     ITDebugLog(@"Building \"Artists\" menu.");
850     NS_DURING
851         artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
852         while ( (nextArtist = [artistsEnumerator nextObject]) ) {
853             tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
854             [tempItem setTarget:self];
855         }
856     NS_HANDLER
857         [[MainController sharedController] networkError:localException];
858     NS_ENDHANDLER
859     ITDebugLog(@"Done Building \"Artists\" menu");
860     return artistsMenu;
861 }
862
863 - (NSMenu *)albumsMenu
864 {
865     NSMenu *albumsMenu = [[NSMenu alloc] initWithTitle:@"Albums"];
866     NSEnumerator *albumsEnumerator;
867     NSString *nextAlbum;
868     id <NSMenuItem> tempItem;
869     ITDebugLog(@"Building \"Albums\" menu.");
870     NS_DURING
871         albumsEnumerator = [[[[MainController sharedController] currentRemote] albums] objectEnumerator];
872         while ( (nextAlbum = [albumsEnumerator nextObject]) ) {
873             tempItem = [albumsMenu addItemWithTitle:nextAlbum action:@selector(performBrowseMenuAction:) keyEquivalent:@""];
874             [tempItem setTarget:self];
875         }
876     NS_HANDLER
877         [[MainController sharedController] networkError:localException];
878     NS_ENDHANDLER
879     ITDebugLog(@"Done Building \"Albums\" menu");
880     return albumsMenu;
881 }
882
883 - (void)performMainMenuAction:(id)sender
884 {
885     switch ( [sender tag] )
886     {
887         case MTMenuPlayPauseItem:
888             ITDebugLog(@"Performing Menu Action: Play/Pause");
889             [[MainController sharedController] playPause];
890             break;
891         case MTMenuFastForwardItem:
892             ITDebugLog(@"Performing Menu Action: Fast Forward");
893             [[MainController sharedController] fastForward];
894             break;
895         case MTMenuRewindItem:
896             ITDebugLog(@"Performing Menu Action: Rewind");
897             [[MainController sharedController] rewind];
898             break;
899         case MTMenuPreviousTrackItem:
900             ITDebugLog(@"Performing Menu Action: Previous Track");
901             [[MainController sharedController] prevSong];
902             break;
903         case MTMenuNextTrackItem:
904             ITDebugLog(@"Performing Menu Action: Next Track");
905             [[MainController sharedController] nextSong];
906             break;
907         case MTMenuShowPlayerItem:
908             ITDebugLog(@"Performing Menu Action: Show Main Interface");
909             [[MainController sharedController] showPlayer];
910             break;
911         case MTMenuPreferencesItem:
912             ITDebugLog(@"Performing Menu Action: Preferences...");
913             [[MainController sharedController] showPreferences];
914             break;
915                 case MTMenuAboutItem:
916                         ITDebugLog(@"Performing Menu Action: About MenuTunes...");
917                         [[ITAboutWindowController sharedController] showAboutWindow];
918                         break;
919         case MTMenuQuitItem:
920             ITDebugLog(@"Performing Menu Action: Quit");
921             [[MainController sharedController] quitMenuTunes];
922             break;
923         case MTMenuRegisterItem:
924             ITDebugLog(@"Performing Menu Action: Register");
925             [[MainController sharedController] blingNow];
926             break;
927         default:
928             ITDebugLog(@"Performing Menu Action: Unimplemented Menu Item OR Child-bearing Menu Item");
929             break;
930     }
931 }
932
933 - (void)performRatingMenuAction:(id)sender
934 {
935     ITDebugLog(@"Rating action selected on item with tag %i", [sender tag]);
936     [[MainController sharedController] selectSongRating:[sender tag]];
937 }
938
939 - (void)performPlaylistMenuAction:(id)sender
940 {
941     ITDebugLog(@"Playlist action selected on item with tag %i", [sender tag]);
942     [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
943 }
944
945 - (void)performEqualizerMenuAction:(id)sender
946 {
947     ITDebugLog(@"EQ action selected on item with tag %i", [sender tag]);
948     [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
949 }
950
951 - (void)performUpcomingSongsMenuAction:(id)sender
952 {
953     ITDebugLog(@"Song action selected on item with tag %i", [sender tag]);
954     [[MainController sharedController] selectSongAtIndex:[sender tag]];
955 }
956
957 - (void)performBrowseMenuAction:(id)sender
958 {
959     ITDebugLog(@"Browse action selected on item named %@", [sender title]);
960     /*
961     ** 1 - Artist
962     ** 2 - Album
963     ** 3 - Genre?
964     */
965     [[MainController sharedController] makePlaylistWithTerm:[sender title] ofType:(([[[sender menu] title] isEqualToString:@"Artists"]) ? 1 : 2)];
966 }
967
968 - (void)updateMenu
969 {
970     ITDebugLog(@"Update Menu");
971     [_currentMenu update];
972 }
973
974 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
975 {
976     return YES;
977 }
978
979 //This is never used I know, keep it though
980 - (NSString *)systemUIColor
981 {
982     NSDictionary *tmpDict;
983     NSNumber *tmpNumber;
984     if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
985         if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
986             if ( ([tmpNumber intValue] == 1) ) {
987                 return @"Aqua";
988             } else {
989                 return @"Graphite";
990             }
991         } else {
992             return @"Aqua";
993         }
994     } else {
995         return @"Aqua";
996     }
997 }
998
999 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
1000         onItem:(id <NSMenuItem>)item
1001 {
1002     unichar charcode = 'a';
1003     int i;
1004     long cocoaModifiers = 0;
1005     static long carbonToCocoa[6][2] = 
1006     {
1007         { cmdKey, NSCommandKeyMask },
1008         { optionKey, NSAlternateKeyMask },
1009         { controlKey, NSControlKeyMask },
1010         { shiftKey, NSShiftKeyMask },
1011     };
1012     
1013     ITDebugLog(@"Setting Key Equivelent on menu item \"%@\".", [item title]);
1014     
1015     for (i = 0; i < 6; i++) {
1016         if (modifiers & carbonToCocoa[i][0]) {
1017             cocoaModifiers += carbonToCocoa[i][1];
1018         }
1019     }
1020     [item setKeyEquivalentModifierMask:cocoaModifiers];
1021     
1022     //Missing key combos for some keys. Must find them later.
1023     switch (code)
1024     {
1025         case 36:
1026             ITDebugLog(@"Keycode for menu item \"%@\": 36 (Return)", [item title]);
1027             charcode = '\r';
1028         break;
1029         
1030         case 48:
1031             ITDebugLog(@"Keycode for menu item \"%@\": 48 (Tab)", [item title]);
1032             charcode = '\t';
1033         break;
1034         
1035         //Space -- ARGH!
1036         case 49:
1037         {
1038             ITDebugLog(@"Keycode for menu item \"%@\": 49 (Space)", [item title]);
1039             // Haven't tested this, though it should work.
1040             // This doesn't work. :'(
1041             //unichar buffer;
1042             //[[NSString stringWithString:@"Space"] getCharacters:&buffer];
1043             //charcode = buffer;
1044             /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1045             ITDebugLog(@"%@", menuRef);
1046             SetMenuItemCommandKey(menuRef, 0, NO, 49);
1047             SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1048             SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1049             charcode = 'b';*/
1050             unichar buffer;
1051             [[NSString stringWithString:@" "] getCharacters:&buffer]; // this will have to do for now :(
1052             charcode = buffer;
1053         }
1054         break;
1055         
1056         case 51:
1057             ITDebugLog(@"Keycode for menu item \"%@\": 51 (Delete)", [item title]);
1058             charcode = NSDeleteFunctionKey;
1059         break;
1060         
1061         case 53:
1062             ITDebugLog(@"Keycode for menu item \"%@\": 53 (Escape)", [item title]);
1063             charcode = '\e';
1064         break;
1065         
1066         case 71:
1067             ITDebugLog(@"Keycode for menu item \"%@\": 71 (Escape)", [item title]);
1068             charcode = '\e';
1069         break;
1070         
1071         case 76:
1072             ITDebugLog(@"Keycode for menu item \"%@\": 76 (Return)", [item title]);
1073             charcode = '\r';
1074         break;
1075         
1076         case 96:
1077             ITDebugLog(@"Keycode for menu item \"%@\": 96 (F5)", [item title]);
1078             charcode = NSF5FunctionKey;
1079         break;
1080         
1081         case 97:
1082             ITDebugLog(@"Keycode for menu item \"%@\": 97 (F6)", [item title]);
1083             charcode = NSF6FunctionKey;
1084         break;
1085         
1086         case 98:
1087             ITDebugLog(@"Keycode for menu item \"%@\": 98 (F7)", [item title]);
1088             charcode = NSF7FunctionKey;
1089         break;
1090         
1091         case 99:
1092             ITDebugLog(@"Keycode for menu item \"%@\": 99 (F3)", [item title]);
1093             charcode = NSF3FunctionKey;
1094         break;
1095         
1096         case 100:
1097             ITDebugLog(@"Keycode for menu item \"%@\": 100 (F8)", [item title]);
1098             charcode = NSF8FunctionKey;
1099         break;
1100         
1101         case 101:
1102             ITDebugLog(@"Keycode for menu item \"%@\": 101 (F9)", [item title]);
1103             charcode = NSF9FunctionKey;
1104         break;
1105         
1106         case 103:
1107             ITDebugLog(@"Keycode for menu item \"%@\": 103 (F11)", [item title]);
1108             charcode = NSF11FunctionKey;
1109         break;
1110         
1111         case 105:
1112             ITDebugLog(@"Keycode for menu item \"%@\": 105 (F13)", [item title]);
1113             charcode = NSF13FunctionKey;
1114         break;
1115         
1116         case 107:
1117             ITDebugLog(@"Keycode for menu item \"%@\": 107 (F14)", [item title]);
1118             charcode = NSF14FunctionKey;
1119         break;
1120         
1121         case 109:
1122             ITDebugLog(@"Keycode for menu item \"%@\": 109 (F10)", [item title]);
1123             charcode = NSF10FunctionKey;
1124         break;
1125         
1126         case 111:
1127             ITDebugLog(@"Keycode for menu item \"%@\": 111 (F12)", [item title]);
1128             charcode = NSF12FunctionKey;
1129         break;
1130         
1131         case 113:
1132             ITDebugLog(@"Keycode for menu item \"%@\": 113 (F13)", [item title]);
1133             charcode = NSF13FunctionKey;
1134         break;
1135         
1136         case 114:
1137             ITDebugLog(@"Keycode for menu item \"%@\": 114 (Insert)", [item title]);
1138             charcode = NSInsertFunctionKey;
1139         break;
1140         
1141         case 115:
1142             ITDebugLog(@"Keycode for menu item \"%@\": 115 (Home)", [item title]);
1143             charcode = NSHomeFunctionKey;
1144         break;
1145         
1146         case 116:
1147             ITDebugLog(@"Keycode for menu item \"%@\": 116 (PgUp)", [item title]);
1148             charcode = NSPageUpFunctionKey;
1149         break;
1150         
1151         case 117:
1152             ITDebugLog(@"Keycode for menu item \"%@\": 117 (Delete)", [item title]);
1153             charcode = NSDeleteFunctionKey;
1154         break;
1155         
1156         case 118:
1157             ITDebugLog(@"Keycode for menu item \"%@\": 118 (F4)", [item title]);
1158             charcode = NSF4FunctionKey;
1159         break;
1160         
1161         case 119:
1162             ITDebugLog(@"Keycode for menu item \"%@\": 119 (End)", [item title]);
1163             charcode = NSEndFunctionKey;
1164         break;
1165         
1166         case 120:
1167             ITDebugLog(@"Keycode for menu item \"%@\": 120 (F2)", [item title]);
1168             charcode = NSF2FunctionKey;
1169         break;
1170         
1171         case 121:
1172             ITDebugLog(@"Keycode for menu item \"%@\": 121 (PgDown)", [item title]);
1173             charcode = NSPageDownFunctionKey;
1174         break;
1175         
1176         case 122:
1177             ITDebugLog(@"Keycode for menu item \"%@\": 122 (F1)", [item title]);
1178             charcode = NSF1FunctionKey;
1179         break;
1180         
1181         case 123:
1182             ITDebugLog(@"Keycode for menu item \"%@\": 123 (Left Arrow)", [item title]);
1183             charcode = NSLeftArrowFunctionKey;
1184         break;
1185         
1186         case 124:
1187             ITDebugLog(@"Keycode for menu item \"%@\": 124 (Right Arrow)", [item title]);
1188             charcode = NSRightArrowFunctionKey;
1189         break;
1190         
1191         case 125:
1192             ITDebugLog(@"Keycode for menu item \"%@\": 125 (Down Arrow)", [item title]);
1193             charcode = NSDownArrowFunctionKey;
1194         break;
1195         
1196         case 126:
1197             ITDebugLog(@"Keycode for menu item \"%@\": 126 (Up Arrow)", [item title]);
1198             charcode = NSUpArrowFunctionKey;
1199         break;
1200     }
1201     
1202     if (charcode == 'a') {
1203         unsigned long state;
1204         long keyTrans;
1205         char charCode;
1206         Ptr kchr;
1207         state = 0;
1208         kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1209         keyTrans = KeyTranslate(kchr, code, &state);
1210         charCode = keyTrans;
1211         ITDebugLog(@"Keycode for menu item \"%@\": %i (%c)", [item title], code, charCode);
1212         [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1213     } else if (charcode != 'b') {
1214         [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1215     }
1216     ITDebugLog(@"Done setting key equivalent on menu item: %@", [item title]);
1217 }
1218
1219 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name
1220 {
1221     NSArray *volumes = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
1222     NSEnumerator *volEnum = [volumes objectEnumerator];
1223     NSString *nextVolume;
1224     ITDebugLog(@"Looking for an iPod named %@", name);
1225     while ( (nextVolume = [volEnum nextObject]) ) {
1226         ITDebugLog(@"- %@", nextVolume);
1227         if ([nextVolume rangeOfString:name options:nil /*range:NSMakeRange(0, [name length] - 1)*/].location != NSNotFound) {
1228             NSFileHandle *handle;
1229             NSData *data;
1230             NSString *path = [nextVolume stringByAppendingPathComponent:@"/iPod_Control/iTunes/iTunesPrefs"];
1231             if ( ![[NSFileManager defaultManager] fileExistsAtPath:path] ) {
1232                 ITDebugLog(@"Error, path isn't an iPod! %@", path);
1233                 return NO;
1234             }
1235             handle = [NSFileHandle fileHandleForReadingAtPath:path];
1236             ITDebugLog(@"File handle: %@", handle);
1237             [handle seekToFileOffset:10];
1238             data = [handle readDataOfLength:1];
1239             ITDebugLog(@"Data: %@", data);
1240             if ( (*((unsigned char*)[data bytes]) == 0x00) ) {
1241                 ITDebugLog(@"iPod is manually updated. %@", path);
1242                 return NO;
1243             } else if ( ( *((unsigned char*)[data bytes]) == 0x01 ) ) {
1244                 ITDebugLog(@"iPod is automatically updated. %@", path);
1245                 return YES;
1246             } else {
1247                 ITDebugLog(@"Error! Value: %h  Desc: %@ Path: %@", *((unsigned char*)[data bytes]), [data description], path);
1248                 return NO;
1249             }
1250         }
1251     }
1252     return YES;
1253 }
1254
1255 @end