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