Added checking to prevent cascading threads and server checks.
[MenuTunes.git] / MainController.m
1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import "NetworkController.h"
5 #import "NetworkObject.h"
6 #import <ITKit/ITHotKeyCenter.h>
7 #import <ITKit/ITHotKey.h>
8 #import <ITKit/ITKeyCombo.h>
9 #import "StatusWindow.h"
10 #import "StatusWindowController.h"
11 #import "StatusItemHack.h"
12
13 @implementation NSImage (SmoothAdditions)
14
15 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
16 {
17     NSImage *newImage;
18     NSImageRep *rep = [self bestRepresentationForDevice:nil];
19     
20     newImage = [[NSImage alloc] initWithSize:scaledSize];
21     [newImage lockFocus];
22     {
23         [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
24         [[NSGraphicsContext currentContext] setShouldAntialias:YES];
25         [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
26     }
27     [newImage unlockFocus];
28     return [newImage autorelease];
29 }
30
31 @end
32
33 @interface MainController(Private)
34 - (ITMTRemote *)loadRemote;
35 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
36 - (void)applicationLaunched:(NSNotification *)note;
37 - (void)applicationTerminated:(NSNotification *)note;
38 @end
39
40 static MainController *sharedController;
41
42 @implementation MainController
43
44 + (MainController *)sharedController
45 {
46     return sharedController;
47 }
48
49 /*************************************************************************/
50 #pragma mark -
51 #pragma mark INITIALIZATION/DEALLOCATION METHODS
52 /*************************************************************************/
53
54 - (id)init
55 {
56     if ( ( self = [super init] ) ) {
57         sharedController = self;
58         
59         remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
60         [[PreferencesController sharedPrefs] setController:self];
61         statusWindowController = [StatusWindowController sharedController];
62         menuController = [[MenuController alloc] init];
63         df = [[NSUserDefaults standardUserDefaults] retain];
64         timerUpdating = NO;
65         blinged = NO;
66     }
67     return self;
68 }
69
70 - (void)applicationDidFinishLaunching:(NSNotification *)note
71 {
72     //Turn on debug mode if needed
73     if ([df boolForKey:@"ITDebugMode"]) {
74         SetITDebugMode(YES);
75     }
76     
77     if (([df integerForKey:@"appVersion"] < 1200) && ([df integerForKey:@"SongsInAdvance"] > 0)) {
78         [df removePersistentDomainForName:@"com.ithinksw.menutunes"];
79         [df synchronize];
80         [[PreferencesController sharedPrefs] registerDefaults];
81         [[StatusWindowController sharedController] showPreferencesUpdateWindow];
82     }
83     
84     currentRemote = [self loadRemote];
85     [[self currentRemote] begin];
86     
87     //Turn on network stuff if needed
88     networkController = [[NetworkController alloc] init];
89     if ([df boolForKey:@"enableSharing"]) {
90         [self setServerStatus:YES];
91     } else if ([df boolForKey:@"useSharedPlayer"]) {
92         [self checkForRemoteServerAndConnectImmediately:YES];
93     }
94     
95     //Setup for notification of the remote player launching or quitting
96     [[[NSWorkspace sharedWorkspace] notificationCenter]
97             addObserver:self
98             selector:@selector(applicationTerminated:)
99             name:NSWorkspaceDidTerminateApplicationNotification
100             object:nil];
101     
102     [[[NSWorkspace sharedWorkspace] notificationCenter]
103             addObserver:self
104             selector:@selector(applicationLaunched:)
105             name:NSWorkspaceDidLaunchApplicationNotification
106             object:nil];
107     
108     if (![df objectForKey:@"menu"]) {  // If this is nil, defaults have never been registered.
109         [[PreferencesController sharedPrefs] registerDefaults];
110     }
111     
112     if ([df boolForKey:@"ITMTNoStatusItem"]) {
113         statusItem = nil;
114     } else {
115         [StatusItemHack install];
116         statusItem = [[ITStatusItem alloc]
117                 initWithStatusBar:[NSStatusBar systemStatusBar]
118                 withLength:NSSquareStatusItemLength];
119     }
120     
121     bling = [[MTBlingController alloc] init];
122     [self blingTime];
123     registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
124                              target:self
125                              selector:@selector(blingTime)
126                              userInfo:nil
127                              repeats:YES] retain];
128     
129     NS_DURING
130         if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
131             [self applicationLaunched:nil];
132         } else {
133             if ([df boolForKey:@"LaunchPlayerWithMT"])
134                 [self showPlayer];
135             else
136                 [self applicationTerminated:nil];
137         }
138     NS_HANDLER
139         [self networkError:localException];
140     NS_ENDHANDLER
141     
142     [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
143     [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
144
145     [networkController startRemoteServerSearch];
146     [NSApp deactivate];
147 }
148
149 - (ITMTRemote *)loadRemote
150 {
151     NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
152     ITDebugLog(@"Gathering remotes.");
153     if (folderPath) {
154         NSArray      *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
155         NSEnumerator *enumerator     = [bundlePathList objectEnumerator];
156         NSString     *bundlePath;
157
158         while ( (bundlePath = [enumerator nextObject]) ) {
159             NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
160
161             if (remoteBundle) {
162                 Class remoteClass = [remoteBundle principalClass];
163
164                 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
165                     [(NSObject *)remoteClass isKindOfClass:[NSObject class]]) {
166                     id remote = [remoteClass remote];
167                     ITDebugLog(@"Adding remote at path %@", bundlePath);
168                     [remoteArray addObject:remote];
169                 }
170             }
171         }
172
173 //      if ( [remoteArray count] > 0 ) {  // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
174 //          if ( [remoteArray count] > 1 ) {
175 //              [remoteArray sortUsingSelector:@selector(sortAlpha:)];
176 //          }
177 //          [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
178 //      }
179     }
180 //  NSLog(@"%@", [remoteArray objectAtIndex:0]);  //DEBUG
181     return [remoteArray objectAtIndex:0];
182 }
183
184 /*************************************************************************/
185 #pragma mark -
186 #pragma mark INSTANCE METHODS
187 /*************************************************************************/
188
189 /*- (void)startTimerInNewThread
190 {
191     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
192     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
193     refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
194                              target:self
195                              selector:@selector(timerUpdate)
196                              userInfo:nil
197                              repeats:YES] retain];
198     [runLoop run];
199     ITDebugLog(@"Timer started.");
200     [pool release];
201 }*/
202
203 - (void)setBlingTime:(NSDate*)date
204 {
205     NSMutableDictionary *globalPrefs;
206     [df synchronize];
207     globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
208     if (date) {
209         [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
210         [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
211     } else {
212         [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
213         [globalPrefs removeObjectForKey:@"ITMTTrialVers"];
214     }
215     [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
216     [df synchronize];
217     [globalPrefs release];
218 }
219
220 - (NSDate*)getBlingTime
221 {
222     [df synchronize];
223     return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
224 }
225
226 - (void)blingTime
227 {
228     NSDate *now = [NSDate date];
229     if (![self blingBling]) {
230         if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
231             [self setBlingTime:now];
232         } else if ([[[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialVers"] intValue] < MT_CURRENT_VERSION) {
233             if ([now timeIntervalSinceDate:[self getBlingTime]] >= 345600) {
234                 [self setBlingTime:[now addTimeInterval:-259200]];
235             } else {
236                 NSMutableDictionary *globalPrefs;
237                 [df synchronize];
238                 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
239                 [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
240                 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
241                 [df synchronize];
242                 [globalPrefs release];
243             }
244         }
245         
246         if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
247             blinged = YES;
248             [statusItem setEnabled:NO];
249             [self clearHotKeys];
250             if ([refreshTimer isValid]) {
251                 [refreshTimer invalidate];
252             }
253             [statusWindowController showRegistrationQueryWindow];
254         }
255     } else {
256         if (blinged) {
257             [statusItem setEnabled:YES];
258             [self setupHotKeys];
259             if (![refreshTimer isValid]) {
260                 [refreshTimer release];
261                 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
262                              target:self
263                              selector:@selector(timerUpdate)
264                              userInfo:nil
265                              repeats:YES] retain];
266             }
267             blinged = NO;
268         }
269         [self setBlingTime:nil];
270     }
271 }
272
273 - (void)blingNow
274 {
275     [bling showPanel];
276 }
277
278 - (BOOL)blingBling
279 {
280     if ( ! ([bling checkDone] == 2475) ) {
281         return NO;
282     } else {
283         return YES;
284     }
285 }
286
287 - (BOOL)songIsPlaying
288 {
289     NSString *identifier = nil;
290     NS_DURING
291         identifier = [[self currentRemote] playerStateUniqueIdentifier];
292     NS_HANDLER
293         [self networkError:localException];
294     NS_ENDHANDLER
295     return ( ! ([identifier isEqualToString:@"0-0"]) );
296 }
297
298 - (BOOL)radioIsPlaying
299 {
300     ITMTRemotePlayerPlaylistClass class = nil;
301     NS_DURING
302         class = [[self currentRemote] currentPlaylistClass];
303     NS_HANDLER
304         [self networkError:localException];
305     NS_ENDHANDLER
306     return (class  == ITMTRemotePlayerRadioPlaylist );
307 }
308
309 - (BOOL)songChanged
310 {
311     NSString *identifier = nil;
312     NS_DURING
313         identifier = [[self currentRemote] playerStateUniqueIdentifier];
314     NS_HANDLER
315         [self networkError:localException];
316     NS_ENDHANDLER
317     return ( ! [identifier isEqualToString:_latestSongIdentifier] );
318 }
319
320 - (NSString *)latestSongIdentifier
321 {
322     return _latestSongIdentifier;
323 }
324
325 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
326 {
327     ITDebugLog(@"Setting latest song identifier:");
328     ITDebugLog(@"   - Identifier: %@", newIdentifier);
329     [_latestSongIdentifier autorelease];
330     _latestSongIdentifier = [newIdentifier retain];
331 }
332
333 - (void)timerUpdate
334 {
335     if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
336         ITDebugLog(@"The song changed.");
337         
338         if ([df boolForKey:@"runScripts"]) {
339             NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
340             NSEnumerator *scriptsEnum = [scripts objectEnumerator];
341             NSString *nextScript;
342             ITDebugLog(@"Running AppleScripts for song change.");
343             while ( (nextScript = [scriptsEnum nextObject]) ) {
344                 NSDictionary *error;
345                 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
346                 ITDebugLog(@"Running script: %@", nextScript);
347                 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
348                     ITDebugLog(@"Error running script %@.", nextScript);
349                 }
350                 [currentScript release];
351             }
352         }
353         
354         timerUpdating = YES;
355         [statusItem setEnabled:NO];
356         
357         NS_DURING
358             latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
359             [menuController rebuildSubmenus];
360     
361             if ( [df boolForKey:@"showSongInfoOnChange"] ) {
362                 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
363             }
364             
365             [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
366             
367             //Create the tooltip for the status item
368             if ( [df boolForKey:@"showToolTip"] ) {
369                 NSString *artist = [[self currentRemote] currentSongArtist];
370                 NSString *title = [[self currentRemote] currentSongTitle];
371                 NSString *toolTip;
372                 ITDebugLog(@"Creating status item tooltip.");
373                 if (artist) {
374                     toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
375                 } else if (title) {
376                     toolTip = title;
377                 } else {
378                     toolTip = @"No Song Playing";
379                 }
380                 [statusItem setToolTip:toolTip];
381             } else {
382                 [statusItem setToolTip:nil];
383             }
384         NS_HANDLER
385             [self networkError:localException];
386         NS_ENDHANDLER
387         
388         timerUpdating = NO;
389         [statusItem setEnabled:YES];
390     }
391     
392     if ([networkController isConnectedToServer]) {
393         [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
394     }
395 }
396
397 - (void)menuClicked
398 {
399     ITDebugLog(@"Menu clicked.");
400     if ([networkController isConnectedToServer]) {
401         //Used the cached version
402         return;
403     }
404     
405     NS_DURING
406         if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
407             [statusItem setMenu:[menuController menu]];
408         } else {
409             [statusItem setMenu:[menuController menuForNoPlayer]];
410         }
411     NS_HANDLER
412         [self networkError:localException];
413     NS_ENDHANDLER
414 }
415
416 //
417 //
418 // Menu Selectors
419 //
420 //
421
422 - (void)playPause
423 {
424     NS_DURING
425         ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
426         ITDebugLog(@"Play/Pause toggled");
427         if (state == ITMTRemotePlayerPlaying) {
428             [[self currentRemote] pause];
429         } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
430             [[self currentRemote] pause];
431             [[self currentRemote] play];
432         } else {
433             [[self currentRemote] play];
434         }
435     NS_HANDLER
436         [self networkError:localException];
437     NS_ENDHANDLER
438     
439     [self timerUpdate];
440 }
441
442 - (void)nextSong
443 {
444     ITDebugLog(@"Going to next song.");
445     NS_DURING
446         [[self currentRemote] goToNextSong];
447     NS_HANDLER
448         [self networkError:localException];
449     NS_ENDHANDLER
450     [self timerUpdate];
451 }
452
453 - (void)prevSong
454 {
455     ITDebugLog(@"Going to previous song.");
456     NS_DURING
457         [[self currentRemote] goToPreviousSong];
458     NS_HANDLER
459         [self networkError:localException];
460     NS_ENDHANDLER
461     [self timerUpdate];
462 }
463
464 - (void)fastForward
465 {
466     ITDebugLog(@"Fast forwarding.");
467     NS_DURING
468         [[self currentRemote] forward];
469     NS_HANDLER
470         [self networkError:localException];
471     NS_ENDHANDLER
472     [self timerUpdate];
473 }
474
475 - (void)rewind
476 {
477     ITDebugLog(@"Rewinding.");
478     NS_DURING
479         [[self currentRemote] rewind];
480     NS_HANDLER
481         [self networkError:localException];
482     NS_ENDHANDLER
483     [self timerUpdate];
484 }
485
486 - (void)selectPlaylistAtIndex:(int)index
487 {
488     ITDebugLog(@"Selecting playlist %i", index);
489     NS_DURING
490         [[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
491         //[[self currentRemote] switchToPlaylistAtIndex:index];
492     NS_HANDLER
493         [self networkError:localException];
494     NS_ENDHANDLER
495     [self timerUpdate];
496 }
497
498 - (void)selectSongAtIndex:(int)index
499 {
500     ITDebugLog(@"Selecting song %i", index);
501     NS_DURING
502         [[self currentRemote] switchToSongAtIndex:index];
503     NS_HANDLER
504         [self networkError:localException];
505     NS_ENDHANDLER
506     [self timerUpdate];
507 }
508
509 - (void)selectSongRating:(int)rating
510 {
511     ITDebugLog(@"Selecting song rating %i", rating);
512     NS_DURING
513         [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
514     NS_HANDLER
515         [self networkError:localException];
516     NS_ENDHANDLER
517     [self timerUpdate];
518 }
519
520 - (void)selectEQPresetAtIndex:(int)index
521 {
522     ITDebugLog(@"Selecting EQ preset %i", index);
523     NS_DURING
524         [[self currentRemote] switchToEQAtIndex:index];
525     NS_HANDLER
526         [self networkError:localException];
527     NS_ENDHANDLER
528     [self timerUpdate];
529 }
530
531 - (void)showPlayer
532 {
533     ITDebugLog(@"Beginning show player.");
534     //if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
535         ITDebugLog(@"Showing player interface.");
536         NS_DURING
537             [[self currentRemote] showPrimaryInterface];
538         NS_HANDLER
539             [self networkError:localException];
540         NS_ENDHANDLER
541     /*} else {
542         ITDebugLog(@"Launching player.");
543         NS_DURING
544             NSString *path;
545             if ( (path = [df stringForKey:@"CustomPlayerPath"]) ) {
546             } else {
547                 pathITDebugLog(@"Showing player interface."); = [[self currentRemote] playerFullName];
548             }
549             if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
550                 ITDebugLog(@"Error Launching Player");
551             }
552         NS_HANDLER
553             [self networkError:localException];
554         NS_ENDHANDLER
555     }*/
556     ITDebugLog(@"Finished show player.");
557 }
558
559 - (void)showPreferences
560 {
561     ITDebugLog(@"Show preferences.");
562     [[PreferencesController sharedPrefs] showPrefsWindow:self];
563 }
564
565 - (void)showPreferencesAndClose
566 {
567     ITDebugLog(@"Show preferences.");
568     [[PreferencesController sharedPrefs] showPrefsWindow:self];
569     [[StatusWindow sharedWindow] setLocked:NO];
570     [[StatusWindow sharedWindow] vanish:self];
571     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
572 }
573
574 - (void)showTestWindow
575 {
576     [self showCurrentTrackInfo];
577 }
578
579 - (void)quitMenuTunes
580 {
581     ITDebugLog(@"Quitting MenuTunes.");
582     [NSApp terminate:self];
583 }
584
585 //
586 //
587
588 - (MenuController *)menuController
589 {
590     return menuController;
591 }
592
593 - (void)closePreferences
594 {
595     ITDebugLog(@"Preferences closed.");
596     if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
597         [self setupHotKeys];
598     }
599 }
600
601 - (ITMTRemote *)currentRemote
602 {
603     if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
604         [self networkError:nil];
605         return nil;
606     }
607     return currentRemote;
608 }
609
610 //
611 //
612 // Hot key setup
613 //
614 //
615
616 - (void)clearHotKeys
617 {
618     NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
619     ITHotKey *nextHotKey;
620     ITDebugLog(@"Clearing hot keys.");
621     while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
622         [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
623     }
624     ITDebugLog(@"Done clearing hot keys.");
625 }
626
627 - (void)setupHotKeys
628 {
629     ITHotKey *hotKey;
630     ITDebugLog(@"Setting up hot keys.");
631     
632     if (playerRunningState == ITMTRemotePlayerNotRunning && ![[NetworkController sharedController] isConnectedToServer]) {
633         return;
634     }
635     
636     if ([df objectForKey:@"PlayPause"] != nil) {
637         ITDebugLog(@"Setting up play pause hot key.");
638         hotKey = [[ITHotKey alloc] init];
639         [hotKey setName:@"PlayPause"];
640         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
641         [hotKey setTarget:self];
642         [hotKey setAction:@selector(playPause)];
643         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
644     }
645     
646     if ([df objectForKey:@"NextTrack"] != nil) {
647         ITDebugLog(@"Setting up next track hot key.");
648         hotKey = [[ITHotKey alloc] init];
649         [hotKey setName:@"NextTrack"];
650         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
651         [hotKey setTarget:self];
652         [hotKey setAction:@selector(nextSong)];
653         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
654     }
655     
656     if ([df objectForKey:@"PrevTrack"] != nil) {
657         ITDebugLog(@"Setting up previous track hot key.");
658         hotKey = [[ITHotKey alloc] init];
659         [hotKey setName:@"PrevTrack"];
660         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
661         [hotKey setTarget:self];
662         [hotKey setAction:@selector(prevSong)];
663         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
664     }
665     
666     if ([df objectForKey:@"ShowPlayer"] != nil) {
667         ITDebugLog(@"Setting up show player hot key.");
668         hotKey = [[ITHotKey alloc] init];
669         [hotKey setName:@"ShowPlayer"];
670         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
671         [hotKey setTarget:self];
672         [hotKey setAction:@selector(showPlayer)];
673         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
674     }
675     
676     if ([df objectForKey:@"TrackInfo"] != nil) {
677         ITDebugLog(@"Setting up track info hot key.");
678         hotKey = [[ITHotKey alloc] init];
679         [hotKey setName:@"TrackInfo"];
680         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
681         [hotKey setTarget:self];
682         [hotKey setAction:@selector(showCurrentTrackInfo)];
683         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
684     }
685     
686     if ([df objectForKey:@"UpcomingSongs"] != nil) {
687         ITDebugLog(@"Setting up upcoming songs hot key.");
688         hotKey = [[ITHotKey alloc] init];
689         [hotKey setName:@"UpcomingSongs"];
690         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
691         [hotKey setTarget:self];
692         [hotKey setAction:@selector(showUpcomingSongs)];
693         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
694     }
695     
696     if ([df objectForKey:@"ToggleLoop"] != nil) {
697         ITDebugLog(@"Setting up toggle loop hot key.");
698         hotKey = [[ITHotKey alloc] init];
699         [hotKey setName:@"ToggleLoop"];
700         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
701         [hotKey setTarget:self];
702         [hotKey setAction:@selector(toggleLoop)];
703         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
704     }
705     
706     if ([df objectForKey:@"ToggleShuffle"] != nil) {
707         ITDebugLog(@"Setting up toggle shuffle hot key.");
708         hotKey = [[ITHotKey alloc] init];
709         [hotKey setName:@"ToggleShuffle"];
710         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
711         [hotKey setTarget:self];
712         [hotKey setAction:@selector(toggleShuffle)];
713         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
714     }
715     
716     if ([df objectForKey:@"IncrementVolume"] != nil) {
717         ITDebugLog(@"Setting up increment volume hot key.");
718         hotKey = [[ITHotKey alloc] init];
719         [hotKey setName:@"IncrementVolume"];
720         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
721         [hotKey setTarget:self];
722         [hotKey setAction:@selector(incrementVolume)];
723         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
724     }
725     
726     if ([df objectForKey:@"DecrementVolume"] != nil) {
727         ITDebugLog(@"Setting up decrement volume hot key.");
728         hotKey = [[ITHotKey alloc] init];
729         [hotKey setName:@"DecrementVolume"];
730         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
731         [hotKey setTarget:self];
732         [hotKey setAction:@selector(decrementVolume)];
733         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
734     }
735     
736     if ([df objectForKey:@"IncrementRating"] != nil) {
737         ITDebugLog(@"Setting up increment rating hot key.");
738         hotKey = [[ITHotKey alloc] init];
739         [hotKey setName:@"IncrementRating"];
740         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
741         [hotKey setTarget:self];
742         [hotKey setAction:@selector(incrementRating)];
743         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
744     }
745     
746     if ([df objectForKey:@"DecrementRating"] != nil) {
747         ITDebugLog(@"Setting up decrement rating hot key.");
748         hotKey = [[ITHotKey alloc] init];
749         [hotKey setName:@"DecrementRating"];
750         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
751         [hotKey setTarget:self];
752         [hotKey setAction:@selector(decrementRating)];
753         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
754     }
755     ITDebugLog(@"Finished setting up hot keys.");
756 }
757
758 - (void)showCurrentTrackInfo
759 {
760     ITMTRemotePlayerSource  source      = 0;
761     NSString               *title       = nil;
762     NSString               *album       = nil;
763     NSString               *artist      = nil;
764     NSString               *composer    = nil;
765     NSString               *time        = nil;
766     NSString               *track       = nil;
767     NSImage                *art         = nil;
768     int                     rating      = -1;
769     
770     ITDebugLog(@"Showing track info status window.");
771     
772     NS_DURING
773         source      = [[self currentRemote] currentSource];
774         title       = [[self currentRemote] currentSongTitle];
775     NS_HANDLER
776         [self networkError:localException];
777     NS_ENDHANDLER
778     
779     if ( title ) {
780         if ( [df boolForKey:@"showAlbumArtwork"] ) {
781             NSSize oldSize, newSize;
782              NS_DURING
783                  art = [[self currentRemote] currentSongAlbumArt];
784                  oldSize = [art size];
785                  if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
786                  else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
787                 art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
788             NS_HANDLER
789                 [self networkError:localException];
790             NS_ENDHANDLER
791         }
792         
793         if ( [df boolForKey:@"showAlbum"] ) {
794             NS_DURING
795                 album = [[self currentRemote] currentSongAlbum];
796             NS_HANDLER
797                 [self networkError:localException];
798             NS_ENDHANDLER
799         }
800
801         if ( [df boolForKey:@"showArtist"] ) {
802             NS_DURING
803                 artist = [[self currentRemote] currentSongArtist];
804             NS_HANDLER
805                 [self networkError:localException];
806             NS_ENDHANDLER
807         }
808
809         if ( [df boolForKey:@"showComposer"] ) {
810             NS_DURING
811                 composer = [[self currentRemote] currentSongComposer];
812             NS_HANDLER
813                 [self networkError:localException];
814             NS_ENDHANDLER
815         }
816
817         if ( [df boolForKey:@"showTime"] ) {
818             NS_DURING
819                 time = [NSString stringWithFormat:@"%@: %@ / %@",
820                 @"Time",
821                 [[self currentRemote] currentSongElapsed],
822                 [[self currentRemote] currentSongLength]];
823             NS_HANDLER
824                 [self networkError:localException];
825             NS_ENDHANDLER
826         }
827
828         if ( [df boolForKey:@"showTrackNumber"] ) {
829             int trackNo    = 0;
830             int trackCount = 0;
831             
832             NS_DURING
833                 trackNo    = [[self currentRemote] currentSongTrack];
834                 trackCount = [[self currentRemote] currentAlbumTrackCount];
835             NS_HANDLER
836                 [self networkError:localException];
837             NS_ENDHANDLER
838             
839             if ( (trackNo > 0) || (trackCount > 0) ) {
840                 track = [NSString stringWithFormat:@"%@: %i %@ %i",
841                     @"Track", trackNo, @"of", trackCount];
842             }
843         }
844
845         if ( [df boolForKey:@"showTrackRating"] ) {
846             float currentRating = 0;
847             
848             NS_DURING
849                 currentRating = [[self currentRemote] currentSongRating];
850             NS_HANDLER
851                 [self networkError:localException];
852             NS_ENDHANDLER
853             
854             if (currentRating >= 0.0) {
855                 rating = ( currentRating * 5 );
856             }
857         }
858     } else {
859         title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
860     }
861     ITDebugLog(@"Showing current track info status window.");
862     [statusWindowController showSongInfoWindowWithSource:source
863                                                    title:title
864                                                    album:album
865                                                   artist:artist
866                                                 composer:composer
867                                                     time:time
868                                                    track:track
869                                                   rating:rating
870                                                    image:art];
871 }
872
873 - (void)showUpcomingSongs
874 {
875     int numSongs = 0;
876     NS_DURING
877         numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
878     NS_HANDLER
879         [self networkError:localException];
880     NS_ENDHANDLER
881     
882     ITDebugLog(@"Showing upcoming songs status window.");
883     NS_DURING
884         if (numSongs > 0) {
885             int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
886             NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
887             int curTrack = [[self currentRemote] currentSongIndex];
888             int i;
889     
890             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
891                 if (i <= numSongs) {
892                     [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
893                 }
894             }
895             
896             if ([songList count] == 0) {
897                 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
898             }
899             
900             [statusWindowController showUpcomingSongsWindowWithTitles:songList];
901         } else {
902             [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
903         }
904     NS_HANDLER
905         [self networkError:localException];
906     NS_ENDHANDLER
907 }
908
909 - (void)incrementVolume
910 {
911     NS_DURING
912         float volume  = [[self currentRemote] volume];
913         float dispVol = volume;
914         ITDebugLog(@"Incrementing volume.");
915         volume  += 0.110;
916         dispVol += 0.100;
917         
918         if (volume > 1.0) {
919             volume  = 1.0;
920             dispVol = 1.0;
921         }
922     
923         ITDebugLog(@"Setting volume to %f", volume);
924         [[self currentRemote] setVolume:volume];
925     
926         // Show volume status window
927         [statusWindowController showVolumeWindowWithLevel:dispVol];
928     NS_HANDLER
929         [self networkError:localException];
930     NS_ENDHANDLER
931 }
932
933 - (void)decrementVolume
934 {
935     NS_DURING
936         float volume  = [[self currentRemote] volume];
937         float dispVol = volume;
938         ITDebugLog(@"Decrementing volume.");
939         volume  -= 0.090;
940         dispVol -= 0.100;
941     
942         if (volume < 0.0) {
943             volume  = 0.0;
944             dispVol = 0.0;
945         }
946         
947         ITDebugLog(@"Setting volume to %f", volume);
948         [[self currentRemote] setVolume:volume];
949         
950         //Show volume status window
951         [statusWindowController showVolumeWindowWithLevel:dispVol];
952     NS_HANDLER
953         [self networkError:localException];
954     NS_ENDHANDLER
955 }
956
957 - (void)incrementRating
958 {
959     NS_DURING
960         float rating = [[self currentRemote] currentSongRating];
961         ITDebugLog(@"Incrementing rating.");
962         
963         if ([[self currentRemote] currentPlaylistIndex] == 0) {
964             ITDebugLog(@"No song playing, rating change aborted.");
965             return;
966         }
967         
968         rating += 0.2;
969         if (rating > 1.0) {
970             rating = 1.0;
971         }
972         ITDebugLog(@"Setting rating to %f", rating);
973         [[self currentRemote] setCurrentSongRating:rating];
974         
975         //Show rating status window
976         [statusWindowController showRatingWindowWithRating:rating];
977     NS_HANDLER
978         [self networkError:localException];
979     NS_ENDHANDLER
980 }
981
982 - (void)decrementRating
983 {
984     NS_DURING
985         float rating = [[self currentRemote] currentSongRating];
986         ITDebugLog(@"Decrementing rating.");
987         
988         if ([[self currentRemote] currentPlaylistIndex] == 0) {
989             ITDebugLog(@"No song playing, rating change aborted.");
990             return;
991         }
992         
993         rating -= 0.2;
994         if (rating < 0.0) {
995             rating = 0.0;
996         }
997         ITDebugLog(@"Setting rating to %f", rating);
998         [[self currentRemote] setCurrentSongRating:rating];
999         
1000         //Show rating status window
1001         [statusWindowController showRatingWindowWithRating:rating];
1002     NS_HANDLER
1003         [self networkError:localException];
1004     NS_ENDHANDLER
1005 }
1006
1007 - (void)toggleLoop
1008 {
1009     NS_DURING
1010         ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
1011         ITDebugLog(@"Toggling repeat mode.");
1012         switch (repeatMode) {
1013             case ITMTRemotePlayerRepeatOff:
1014                 repeatMode = ITMTRemotePlayerRepeatAll;
1015             break;
1016             case ITMTRemotePlayerRepeatAll:
1017                 repeatMode = ITMTRemotePlayerRepeatOne;
1018             break;
1019             case ITMTRemotePlayerRepeatOne:
1020                 repeatMode = ITMTRemotePlayerRepeatOff;
1021             break;
1022         }
1023         ITDebugLog(@"Setting repeat mode to %i", repeatMode);
1024         [[self currentRemote] setRepeatMode:repeatMode];
1025         
1026         //Show loop status window
1027         [statusWindowController showRepeatWindowWithMode:repeatMode];
1028     NS_HANDLER
1029         [self networkError:localException];
1030     NS_ENDHANDLER
1031 }
1032
1033 - (void)toggleShuffle
1034 {
1035     NS_DURING
1036         BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
1037         ITDebugLog(@"Toggling shuffle mode.");
1038         [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
1039         //Show shuffle status window
1040         ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
1041         [statusWindowController showShuffleWindow:newShuffleEnabled];
1042     NS_HANDLER
1043         [self networkError:localException];
1044     NS_ENDHANDLER
1045 }
1046
1047 - (void)registerNowOK
1048 {
1049     [[StatusWindow sharedWindow] setLocked:NO];
1050     [[StatusWindow sharedWindow] vanish:self];
1051     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1052
1053     [self blingNow];
1054 }
1055
1056 - (void)registerNowCancel
1057 {
1058     [[StatusWindow sharedWindow] setLocked:NO];
1059     [[StatusWindow sharedWindow] vanish:self];
1060     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1061
1062     [NSApp terminate:self];
1063 }
1064
1065 /*************************************************************************/
1066 #pragma mark -
1067 #pragma mark NETWORK HANDLERS
1068 /*************************************************************************/
1069
1070 - (void)setServerStatus:(BOOL)newStatus
1071 {
1072     if (newStatus) {
1073         //Turn on
1074         [networkController setServerStatus:YES];
1075     } else {
1076         //Tear down
1077         [networkController setServerStatus:NO];
1078     }
1079 }
1080
1081 - (int)connectToServer
1082 {
1083     int result;
1084     ITDebugLog(@"Attempting to connect to shared remote.");
1085     result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1086     //Connect
1087     if (result == 1) {
1088         [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1089         currentRemote = [[[networkController networkObject] remote] retain];
1090         
1091         [self setupHotKeys];
1092         //playerRunningState = ITMTRemotePlayerRunning;
1093         playerRunningState = [[self currentRemote] playerRunningState];
1094         
1095         [refreshTimer invalidate];
1096         refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1097                                 target:self
1098                                 selector:@selector(timerUpdate)
1099                                 userInfo:nil
1100                                 repeats:YES] retain];
1101         [self timerUpdate];
1102         ITDebugLog(@"Connection successful.");
1103         return 1;
1104     } else if (result == 0) {
1105         ITDebugLog(@"Connection failed.");
1106         currentRemote = [remoteArray objectAtIndex:0];
1107         return 0;
1108     } else {
1109         //Do something about the password being invalid
1110         ITDebugLog(@"Connection failed.");
1111         currentRemote = [remoteArray objectAtIndex:0];
1112         return -1;
1113     }
1114 }
1115
1116 - (BOOL)disconnectFromServer
1117 {
1118     ITDebugLog(@"Disconnecting from shared remote.");
1119     //Disconnect
1120     [currentRemote release];
1121     currentRemote = [remoteArray objectAtIndex:0];
1122     [networkController disconnect];
1123     
1124     if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
1125         [self applicationLaunched:nil];
1126     } else {
1127         [self applicationTerminated:nil];
1128     }
1129     [self timerUpdate];
1130     return YES;
1131 }
1132
1133 - (void)checkForRemoteServer
1134 {
1135     [self checkForRemoteServerAndConnectImmediately:NO];
1136 }
1137
1138 - (void)checkForRemoteServerAndConnectImmediately:(BOOL)connectImmediately
1139 {
1140     ITDebugLog(@"Checking for remote server.");
1141     if (!_checkingForServer) {
1142         if (!_serverCheckLock) {
1143             _serverCheckLock = [[NSLock alloc] init];
1144         }
1145         [_serverCheckLock lock];
1146         _checkingForServer = YES;
1147         [_serverCheckLock unlock];
1148         [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:[NSNumber numberWithBool:connectImmediately]];
1149     }
1150 }
1151
1152 - (void)runRemoteServerCheck:(id)sender
1153 {
1154     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1155     ITDebugLog(@"Remote server check running.");
1156     if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1157         ITDebugLog(@"Remote server found.");
1158         if ([sender boolValue]) {
1159             [self performSelectorOnMainThread:@selector(connectToServer) withObject:nil waitUntilDone:NO];
1160         } else {
1161             [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO];
1162         }
1163     } else {
1164         ITDebugLog(@"Remote server not found.");
1165         [self performSelectorOnMainThread:@selector(remoteServerNotFound:) withObject:nil waitUntilDone:NO];
1166     }
1167     [_serverCheckLock lock];
1168     _checkingForServer = NO;
1169     [_serverCheckLock unlock];
1170     [pool release];
1171 }
1172
1173 - (void)remoteServerFound:(id)sender
1174 {
1175     if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1176         [[StatusWindowController sharedController] showReconnectQueryWindow];
1177     }
1178 }
1179
1180 - (void)remoteServerNotFound:(id)sender
1181 {
1182     if (![[NetworkController sharedController] isConnectedToServer]) {
1183         [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1184     }
1185 }
1186
1187 - (void)networkError:(NSException *)exception
1188 {
1189     ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1190     if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1191         //NSRunCriticalAlertPanel(@"Remote MenuTunes Disconnected", @"The MenuTunes server you were connected to stopped responding or quit. MenuTunes will revert back to the local player.", @"OK", nil, nil);
1192         [[StatusWindowController sharedController] showNetworkErrorQueryWindow];
1193         if ([self disconnectFromServer]) {
1194             [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1195             [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1196         } else {
1197             ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1198         }
1199     }
1200 }
1201
1202 - (void)reconnect
1203 {
1204     /*if ([self connectToServer] == 0) {
1205         [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1206     }*/
1207     [self checkForRemoteServerAndConnectImmediately:YES];
1208     [[StatusWindow sharedWindow] setLocked:NO];
1209     [[StatusWindow sharedWindow] vanish:self];
1210     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1211 }
1212
1213 - (void)cancelReconnect
1214 {
1215     [[StatusWindow sharedWindow] setLocked:NO];
1216     [[StatusWindow sharedWindow] vanish:self];
1217     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1218 }
1219
1220 /*************************************************************************/
1221 #pragma mark -
1222 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1223 /*************************************************************************/
1224
1225 - (void)applicationLaunched:(NSNotification *)note
1226 {
1227     NS_DURING
1228         if (!note || ([[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer])) {
1229             ITDebugLog(@"Remote application launched.");
1230             playerRunningState = ITMTRemotePlayerRunning;
1231             [[self currentRemote] begin];
1232             [self setLatestSongIdentifier:@""];
1233             [self timerUpdate];
1234             refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1235                                 target:self
1236                                 selector:@selector(timerUpdate)
1237                                 userInfo:nil
1238                                 repeats:YES] retain];
1239             //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1240             [self setupHotKeys];
1241         }
1242     NS_HANDLER
1243         [self networkError:localException];
1244     NS_ENDHANDLER
1245 }
1246
1247  - (void)applicationTerminated:(NSNotification *)note
1248  {
1249     NS_DURING
1250         if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer]) {
1251             ITDebugLog(@"Remote application terminated.");
1252             playerRunningState = ITMTRemotePlayerNotRunning;
1253             [[self currentRemote] halt];
1254             [refreshTimer invalidate];
1255             [refreshTimer release];
1256             refreshTimer = nil;
1257             [self clearHotKeys];
1258             
1259             if ([df objectForKey:@"ShowPlayer"] != nil) {
1260                 ITHotKey *hotKey;
1261                 ITDebugLog(@"Setting up show player hot key.");
1262                 hotKey = [[ITHotKey alloc] init];
1263                 [hotKey setName:@"ShowPlayer"];
1264                 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1265                 [hotKey setTarget:self];
1266                 [hotKey setAction:@selector(showPlayer)];
1267                 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1268             }
1269         }
1270     NS_HANDLER
1271         [self networkError:localException];
1272     NS_ENDHANDLER
1273  }
1274
1275
1276 /*************************************************************************/
1277 #pragma mark -
1278 #pragma mark NSApplication DELEGATE METHODS
1279 /*************************************************************************/
1280
1281 - (void)applicationWillTerminate:(NSNotification *)note
1282 {
1283     [networkController stopRemoteServerSearch];
1284     [self clearHotKeys];
1285     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1286 }
1287
1288
1289 /*************************************************************************/
1290 #pragma mark -
1291 #pragma mark DEALLOCATION METHOD
1292 /*************************************************************************/
1293
1294 - (void)dealloc
1295 {
1296     [self applicationTerminated:nil];
1297     [bling release];
1298     [statusItem release];
1299     [statusWindowController release];
1300     [menuController release];
1301     [networkController release];
1302     [_serverCheckLock release];
1303     [super dealloc];
1304 }
1305
1306 @end