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