Added another debug statement to see if I cornered that stupid crash bug
[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 ([networkController isConnectedToServer]) {
338         [statusItem setMenu:[menuController menu]];
339     }
340     
341     if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
342         ITDebugLog(@"The song changed.");
343         
344         if ([df boolForKey:@"runScripts"]) {
345             NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
346             NSEnumerator *scriptsEnum = [scripts objectEnumerator];
347             NSString *nextScript;
348             ITDebugLog(@"Running AppleScripts for song change.");
349             while ( (nextScript = [scriptsEnum nextObject]) ) {
350                 NSDictionary *error;
351                 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
352                 ITDebugLog(@"Running script: %@", nextScript);
353                 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
354                     ITDebugLog(@"Error running script %@.", nextScript);
355                 }
356                 [currentScript release];
357             }
358         }
359         
360         timerUpdating = YES;
361         
362         NS_DURING
363             latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
364             [menuController rebuildSubmenus];
365     
366             if ( [df boolForKey:@"showSongInfoOnChange"] ) {
367                 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
368             }
369             
370             [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
371             
372             //Create the tooltip for the status item
373             if ( [df boolForKey:@"showToolTip"] ) {
374                 NSString *artist = [[self currentRemote] currentSongArtist];
375                 NSString *title = [[self currentRemote] currentSongTitle];
376                 NSString *toolTip;
377                 ITDebugLog(@"Creating status item tooltip.");
378                 if (artist) {
379                     toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
380                 } else if (title) {
381                     toolTip = title;
382                 } else {
383                     toolTip = @"No Song Playing";
384                 }
385                 [statusItem setToolTip:toolTip];
386             } else {
387                 [statusItem setToolTip:nil];
388             }
389         NS_HANDLER
390             [self networkError:localException];
391         NS_ENDHANDLER
392         
393         timerUpdating = NO;
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                 path = [[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
781         if ( [df boolForKey:@"showAlbum"] ) {
782             NS_DURING
783                 album = [[self currentRemote] currentSongAlbum];
784             NS_HANDLER
785                 [self networkError:localException];
786             NS_ENDHANDLER
787         }
788
789         if ( [df boolForKey:@"showArtist"] ) {
790             NS_DURING
791                 artist = [[self currentRemote] currentSongArtist];
792             NS_HANDLER
793                 [self networkError:localException];
794             NS_ENDHANDLER
795         }
796
797         if ( [df boolForKey:@"showComposer"] ) {
798             NS_DURING
799                 composer = [[self currentRemote] currentSongComposer];
800             NS_HANDLER
801                 [self networkError:localException];
802             NS_ENDHANDLER
803         }
804
805         if ( [df boolForKey:@"showTime"] ) {
806             NS_DURING
807                 time = [NSString stringWithFormat:@"%@: %@ / %@",
808                 @"Time",
809                 [[self currentRemote] currentSongElapsed],
810                 [[self currentRemote] currentSongLength]];
811             NS_HANDLER
812                 [self networkError:localException];
813             NS_ENDHANDLER
814         }
815
816         if ( [df boolForKey:@"showTrackNumber"] ) {
817             int trackNo    = 0;
818             int trackCount = 0;
819             
820             NS_DURING
821                 trackNo    = [[self currentRemote] currentSongTrack];
822                 trackCount = [[self currentRemote] currentAlbumTrackCount];
823             NS_HANDLER
824                 [self networkError:localException];
825             NS_ENDHANDLER
826             
827             if ( (trackNo > 0) || (trackCount > 0) ) {
828                 track = [NSString stringWithFormat:@"%@: %i %@ %i",
829                     @"Track", trackNo, @"of", trackCount];
830             }
831         }
832
833         if ( [df boolForKey:@"showTrackRating"] ) {
834             float currentRating = 0;
835             
836             NS_DURING
837                 currentRating = [[self currentRemote] currentSongRating];
838             NS_HANDLER
839                 [self networkError:localException];
840             NS_ENDHANDLER
841             
842             if (currentRating >= 0.0) {
843                 rating = ( currentRating * 5 );
844             }
845         }
846         
847         if ( [df boolForKey:@"showAlbumArtwork"] ) {
848             NSSize oldSize, newSize;
849              NS_DURING
850                  art = [[self currentRemote] currentSongAlbumArt];
851                  oldSize = [art size];
852                  if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
853                  else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
854                 art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
855             NS_HANDLER
856                 [self networkError:localException];
857             NS_ENDHANDLER
858         }
859         
860     } else {
861         title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
862     }
863     ITDebugLog(@"Showing current track info status window.");
864     [statusWindowController showSongInfoWindowWithSource:source
865                                                    title:title
866                                                    album:album
867                                                   artist:artist
868                                                 composer:composer
869                                                     time:time
870                                                    track:track
871                                                   rating:rating
872                                                    image:art];
873 }
874
875 - (void)showUpcomingSongs
876 {
877     int numSongs = 0;
878     NS_DURING
879         numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
880     NS_HANDLER
881         [self networkError:localException];
882     NS_ENDHANDLER
883     
884     ITDebugLog(@"Showing upcoming songs status window.");
885     NS_DURING
886         if (numSongs > 0) {
887             int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
888             NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
889             int curTrack = [[self currentRemote] currentSongIndex];
890             int i;
891     
892             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
893                 if (i <= numSongs) {
894                     [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
895                 }
896             }
897             
898             if ([songList count] == 0) {
899                 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
900             }
901             
902             [statusWindowController showUpcomingSongsWindowWithTitles:songList];
903         } else {
904             [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
905         }
906     NS_HANDLER
907         [self networkError:localException];
908     NS_ENDHANDLER
909 }
910
911 - (void)incrementVolume
912 {
913     NS_DURING
914         float volume  = [[self currentRemote] volume];
915         float dispVol = volume;
916         ITDebugLog(@"Incrementing volume.");
917         volume  += 0.110;
918         dispVol += 0.100;
919         
920         if (volume > 1.0) {
921             volume  = 1.0;
922             dispVol = 1.0;
923         }
924     
925         ITDebugLog(@"Setting volume to %f", volume);
926         [[self currentRemote] setVolume:volume];
927     
928         // Show volume status window
929         [statusWindowController showVolumeWindowWithLevel:dispVol];
930     NS_HANDLER
931         [self networkError:localException];
932     NS_ENDHANDLER
933 }
934
935 - (void)decrementVolume
936 {
937     NS_DURING
938         float volume  = [[self currentRemote] volume];
939         float dispVol = volume;
940         ITDebugLog(@"Decrementing volume.");
941         volume  -= 0.090;
942         dispVol -= 0.100;
943     
944         if (volume < 0.0) {
945             volume  = 0.0;
946             dispVol = 0.0;
947         }
948         
949         ITDebugLog(@"Setting volume to %f", volume);
950         [[self currentRemote] setVolume:volume];
951         
952         //Show volume status window
953         [statusWindowController showVolumeWindowWithLevel:dispVol];
954     NS_HANDLER
955         [self networkError:localException];
956     NS_ENDHANDLER
957 }
958
959 - (void)incrementRating
960 {
961     NS_DURING
962         float rating = [[self currentRemote] currentSongRating];
963         ITDebugLog(@"Incrementing rating.");
964         
965         if ([[self currentRemote] currentPlaylistIndex] == 0) {
966             ITDebugLog(@"No song playing, rating change aborted.");
967             return;
968         }
969         
970         rating += 0.2;
971         if (rating > 1.0) {
972             rating = 1.0;
973         }
974         ITDebugLog(@"Setting rating to %f", rating);
975         [[self currentRemote] setCurrentSongRating:rating];
976         
977         //Show rating status window
978         [statusWindowController showRatingWindowWithRating:rating];
979     NS_HANDLER
980         [self networkError:localException];
981     NS_ENDHANDLER
982 }
983
984 - (void)decrementRating
985 {
986     NS_DURING
987         float rating = [[self currentRemote] currentSongRating];
988         ITDebugLog(@"Decrementing rating.");
989         
990         if ([[self currentRemote] currentPlaylistIndex] == 0) {
991             ITDebugLog(@"No song playing, rating change aborted.");
992             return;
993         }
994         
995         rating -= 0.2;
996         if (rating < 0.0) {
997             rating = 0.0;
998         }
999         ITDebugLog(@"Setting rating to %f", rating);
1000         [[self currentRemote] setCurrentSongRating:rating];
1001         
1002         //Show rating status window
1003         [statusWindowController showRatingWindowWithRating:rating];
1004     NS_HANDLER
1005         [self networkError:localException];
1006     NS_ENDHANDLER
1007 }
1008
1009 - (void)toggleLoop
1010 {
1011     NS_DURING
1012         ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
1013         ITDebugLog(@"Toggling repeat mode.");
1014         switch (repeatMode) {
1015             case ITMTRemotePlayerRepeatOff:
1016                 repeatMode = ITMTRemotePlayerRepeatAll;
1017             break;
1018             case ITMTRemotePlayerRepeatAll:
1019                 repeatMode = ITMTRemotePlayerRepeatOne;
1020             break;
1021             case ITMTRemotePlayerRepeatOne:
1022                 repeatMode = ITMTRemotePlayerRepeatOff;
1023             break;
1024         }
1025         ITDebugLog(@"Setting repeat mode to %i", repeatMode);
1026         [[self currentRemote] setRepeatMode:repeatMode];
1027         
1028         //Show loop status window
1029         [statusWindowController showRepeatWindowWithMode:repeatMode];
1030     NS_HANDLER
1031         [self networkError:localException];
1032     NS_ENDHANDLER
1033 }
1034
1035 - (void)toggleShuffle
1036 {
1037     NS_DURING
1038         BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
1039         ITDebugLog(@"Toggling shuffle mode.");
1040         [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
1041         //Show shuffle status window
1042         ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
1043         [statusWindowController showShuffleWindow:newShuffleEnabled];
1044     NS_HANDLER
1045         [self networkError:localException];
1046     NS_ENDHANDLER
1047 }
1048
1049 - (void)registerNowOK
1050 {
1051     [[StatusWindow sharedWindow] setLocked:NO];
1052     [[StatusWindow sharedWindow] vanish:self];
1053     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1054
1055     [self blingNow];
1056 }
1057
1058 - (void)registerNowCancel
1059 {
1060     [[StatusWindow sharedWindow] setLocked:NO];
1061     [[StatusWindow sharedWindow] vanish:self];
1062     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1063
1064     [NSApp terminate:self];
1065 }
1066
1067 /*************************************************************************/
1068 #pragma mark -
1069 #pragma mark NETWORK HANDLERS
1070 /*************************************************************************/
1071
1072 - (void)setServerStatus:(BOOL)newStatus
1073 {
1074     if (newStatus) {
1075         //Turn on
1076         [networkController setServerStatus:YES];
1077     } else {
1078         //Tear down
1079         [networkController setServerStatus:NO];
1080     }
1081 }
1082
1083 - (int)connectToServer
1084 {
1085     int result;
1086     ITDebugLog(@"Attempting to connect to shared remote.");
1087     result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1088     //Connect
1089     if (result == 1) {
1090         [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1091         currentRemote = [[[networkController networkObject] remote] retain];
1092         [refreshTimer invalidate];
1093         refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1094                                 target:self
1095                                 selector:@selector(timerUpdate)
1096                                 userInfo:nil
1097                                 repeats:YES] retain];
1098         [self timerUpdate];
1099         ITDebugLog(@"Connection successful.");
1100         return 1;
1101     } else if (result == 0) {
1102         ITDebugLog(@"Connection failed.");
1103         currentRemote = [remoteArray objectAtIndex:0];
1104         return 0;
1105     } else {
1106         //Do something about the password being invalid
1107         ITDebugLog(@"Connection failed.");
1108         currentRemote = [remoteArray objectAtIndex:0];
1109         return -1;
1110     }
1111 }
1112
1113 - (BOOL)disconnectFromServer
1114 {
1115     ITDebugLog(@"Disconnecting from shared remote.");
1116     //Disconnect
1117     [currentRemote release];
1118     currentRemote = [remoteArray objectAtIndex:0];
1119     [networkController disconnect];
1120     [self timerUpdate];
1121     return YES;
1122 }
1123
1124 - (void)checkForRemoteServer:(NSTimer *)timer
1125 {
1126     ITDebugLog(@"Checking for remote server.");
1127     if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1128         ITDebugLog(@"Remote server found.");
1129         [timer invalidate];
1130         if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1131             [[StatusWindowController sharedController] showReconnectQueryWindow];
1132         }
1133     } else {
1134         ITDebugLog(@"Remote server not found.");
1135     }
1136 }
1137
1138 - (void)networkError:(NSException *)exception
1139 {
1140     ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1141     if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1142         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);
1143         if ([self disconnectFromServer]) {
1144             [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1145             [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1146         } else {
1147             ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1148         }
1149     }
1150 }
1151
1152 - (void)reconnect
1153 {
1154     if ([self connectToServer] == 0) {
1155         [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1156     }
1157     [[StatusWindow sharedWindow] setLocked:NO];
1158     [[StatusWindow sharedWindow] vanish:self];
1159     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1160 }
1161
1162 - (void)cancelReconnect
1163 {
1164     [[StatusWindow sharedWindow] setLocked:NO];
1165     [[StatusWindow sharedWindow] vanish:self];
1166     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1167 }
1168
1169 /*************************************************************************/
1170 #pragma mark -
1171 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1172 /*************************************************************************/
1173
1174 - (void)applicationLaunched:(NSNotification *)note
1175 {
1176     NS_DURING
1177         if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1178             ITDebugLog(@"Remote application launched.");
1179             playerRunningState = ITMTRemotePlayerRunning;
1180             [[self currentRemote] begin];
1181             [self setLatestSongIdentifier:@""];
1182             [self timerUpdate];
1183             refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1184                                 target:self
1185                                 selector:@selector(timerUpdate)
1186                                 userInfo:nil
1187                                 repeats:YES] retain];
1188             //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1189             [self setupHotKeys];
1190         }
1191     NS_HANDLER
1192         [self networkError:localException];
1193     NS_ENDHANDLER
1194 }
1195
1196  - (void)applicationTerminated:(NSNotification *)note
1197  {
1198     NS_DURING
1199         if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1200             ITDebugLog(@"Remote application terminated.");
1201             playerRunningState = ITMTRemotePlayerNotRunning;
1202             [[self currentRemote] halt];
1203             [refreshTimer invalidate];
1204             [refreshTimer release];
1205             refreshTimer = nil;
1206             [self clearHotKeys];
1207             
1208             if ([df objectForKey:@"ShowPlayer"] != nil) {
1209                 ITHotKey *hotKey;
1210                 ITDebugLog(@"Setting up show player hot key.");
1211                 hotKey = [[ITHotKey alloc] init];
1212                 [hotKey setName:@"ShowPlayer"];
1213                 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1214                 [hotKey setTarget:self];
1215                 [hotKey setAction:@selector(showPlayer)];
1216                 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1217             }
1218         }
1219     NS_HANDLER
1220         [self networkError:localException];
1221     NS_ENDHANDLER
1222  }
1223
1224
1225 /*************************************************************************/
1226 #pragma mark -
1227 #pragma mark NSApplication DELEGATE METHODS
1228 /*************************************************************************/
1229
1230 - (void)applicationWillTerminate:(NSNotification *)note
1231 {
1232     [networkController stopRemoteServerSearch];
1233     [self clearHotKeys];
1234     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1235 }
1236
1237
1238 /*************************************************************************/
1239 #pragma mark -
1240 #pragma mark DEALLOCATION METHOD
1241 /*************************************************************************/
1242
1243 - (void)dealloc
1244 {
1245     [self applicationTerminated:nil];
1246     [bling release];
1247     [statusItem release];
1248     [statusWindowController release];
1249     [menuController release];
1250     [networkController release];
1251     [super dealloc];
1252 }
1253
1254 @end