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