Fixing crashes :D
[MenuTunes.git] / MainController.m
1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import <ITKit/ITHotKeyCenter.h>
5 #import <ITKit/ITHotKey.h>
6 #import <ITKit/ITKeyCombo.h>
7 #import "StatusWindowController.h"
8 #import "StatusItemHack.h"
9
10 @interface MainController(Private)
11 - (ITMTRemote *)loadRemote;
12 - (void)timerUpdate;
13 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
14 - (void)showCurrentTrackInfo;
15 - (void)applicationLaunched:(NSNotification *)note;
16 - (void)applicationTerminated:(NSNotification *)note;
17 @end
18
19 static MainController *sharedController;
20
21 @implementation MainController
22
23 + (MainController *)sharedController
24 {
25     return sharedController;
26 }
27
28 /*************************************************************************/
29 #pragma mark -
30 #pragma mark INITIALIZATION/DEALLOCATION METHODS
31 /*************************************************************************/
32
33 - (id)init
34 {
35     if ( ( self = [super init] ) ) {
36         sharedController = self;
37         
38         remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
39         statusWindowController = [StatusWindowController sharedController];
40         menuController = [[MenuController alloc] init];
41         df = [[NSUserDefaults standardUserDefaults] retain];
42         timerUpdating = NO;
43     }
44     return self;
45 }
46
47 - (void)applicationDidFinishLaunching:(NSNotification *)note
48 {
49     //Turn on debug mode if needed
50     if ([df boolForKey:@"ITDebugMode"]) {
51         SetITDebugMode(YES);
52     }
53     
54     bling = [[MTBlingController alloc] init];
55     blingDate = nil;
56     
57     currentRemote = [self loadRemote];
58     [currentRemote begin];
59     
60     //Setup for notification of the remote player launching or quitting
61     [[[NSWorkspace sharedWorkspace] notificationCenter]
62             addObserver:self
63             selector:@selector(applicationTerminated:)
64             name:NSWorkspaceDidTerminateApplicationNotification
65             object:nil];
66     
67     [[[NSWorkspace sharedWorkspace] notificationCenter]
68             addObserver:self
69             selector:@selector(applicationLaunched:)
70             name:NSWorkspaceDidLaunchApplicationNotification
71             object:nil];
72     
73     if ( ! [df objectForKey:@"menu"] ) {  // If this is nil, defaults have never been registered.
74         [[PreferencesController sharedPrefs] registerDefaults];
75     }
76     
77     [StatusItemHack install];
78     statusItem = [[ITStatusItem alloc]
79             initWithStatusBar:[NSStatusBar systemStatusBar]
80             withLength:NSSquareStatusItemLength];
81     
82     if ([currentRemote playerRunningState] == ITMTRemotePlayerRunning) {
83         [self applicationLaunched:nil];
84     } else {
85         if ([df boolForKey:@"LaunchPlayerWithMT"])
86             [self showPlayer];
87         else
88             [self applicationTerminated:nil];
89     }
90     
91     [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
92     [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
93
94     [NSApp deactivate];
95 }
96
97 - (ITMTRemote *)loadRemote
98 {
99     NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
100     ITDebugLog(@"Gathering remotes.");
101     if (folderPath) {
102         NSArray      *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
103         NSEnumerator *enumerator     = [bundlePathList objectEnumerator];
104         NSString     *bundlePath;
105
106         while ( (bundlePath = [enumerator nextObject]) ) {
107             NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
108
109             if (remoteBundle) {
110                 Class remoteClass = [remoteBundle principalClass];
111
112                 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
113                     [remoteClass isKindOfClass:[NSObject class]]) {
114                     id remote = [remoteClass remote];
115                     ITDebugLog(@"Adding remote at path %@", bundlePath);
116                     [remoteArray addObject:remote];
117                 }
118             }
119         }
120
121 //      if ( [remoteArray count] > 0 ) {  // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
122 //          if ( [remoteArray count] > 1 ) {
123 //              [remoteArray sortUsingSelector:@selector(sortAlpha:)];
124 //          }
125 //          [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
126 //      }
127     }
128 //  NSLog(@"%@", [remoteArray objectAtIndex:0]);  //DEBUG
129     return [remoteArray objectAtIndex:0];
130 }
131
132 /*************************************************************************/
133 #pragma mark -
134 #pragma mark INSTANCE METHODS
135 /*************************************************************************/
136
137 /*- (void)startTimerInNewThread
138 {
139     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
140     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
141     refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
142                              target:self
143                              selector:@selector(timerUpdate)
144                              userInfo:nil
145                              repeats:YES] retain];
146     [runLoop run];
147     ITDebugLog(@"Timer started.");
148     [pool release];
149 }*/
150
151 - (void)blingTime
152 {
153     NSDate *now = [NSDate date];
154     if ( (! blingDate) || ([now timeIntervalSinceDate:blingDate] >= 86400) ) {
155         [bling showPanelIfNeeded];
156         [blingDate autorelease];
157         blingDate = [now retain];
158     }
159 }
160
161 - (void)blingNow
162 {
163     [bling showPanel];
164     [blingDate autorelease];
165     blingDate = [[NSDate date] retain];
166 }
167
168 - (BOOL)blingBling
169 {
170     if ( ! ([bling checkDone] == 2475) ) {
171         return NO;
172     } else {
173         return YES;
174     }
175 }
176
177 - (BOOL)songIsPlaying
178 {
179     return ( ! ([[currentRemote playerStateUniqueIdentifier] isEqualToString:@"0-0"]) );
180 }
181
182 - (BOOL)radioIsPlaying
183 {
184     return ( [currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist );
185 }
186
187 - (BOOL)songChanged
188 {
189     return ( ! [[currentRemote playerStateUniqueIdentifier] isEqualToString:_latestSongIdentifier] );
190 }
191
192 - (NSString *)latestSongIdentifier
193 {
194     return _latestSongIdentifier;
195 }
196
197 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
198 {
199     ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
200     [_latestSongIdentifier autorelease];
201     _latestSongIdentifier = [newIdentifier copy];
202 }
203
204 - (void)timerUpdate
205 {
206     if ( [self songChanged] && (timerUpdating != YES) ) {
207         ITDebugLog(@"The song changed.");
208         timerUpdating = YES;
209         latestPlaylistClass = [currentRemote currentPlaylistClass];
210         [menuController rebuildSubmenus];
211
212         if ( [df boolForKey:@"showSongInfoOnChange"] ) {
213             [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
214         }
215         
216         [self setLatestSongIdentifier:[currentRemote playerStateUniqueIdentifier]];
217         
218         timerUpdating = NO;
219     }
220 }
221
222 - (void)menuClicked
223 {
224     ITDebugLog(@"Menu clicked.");
225     if ([currentRemote playerRunningState] == ITMTRemotePlayerRunning) {
226         [statusItem setMenu:[menuController menu]];
227     } else {
228         [statusItem setMenu:[menuController menuForNoPlayer]];
229     }
230 }
231
232 //
233 //
234 // Menu Selectors
235 //
236 //
237
238 - (void)playPause
239 {
240     ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
241     ITDebugLog(@"Play/Pause toggled");
242     if (state == ITMTRemotePlayerPlaying) {
243         [currentRemote pause];
244     } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
245         [currentRemote pause];
246         [currentRemote play];
247     } else {
248         [currentRemote play];
249     }
250     [self timerUpdate];
251 }
252
253 - (void)nextSong
254 {
255     ITDebugLog(@"Going to next song.");
256     [currentRemote goToNextSong];
257     [self timerUpdate];
258 }
259
260 - (void)prevSong
261 {
262     ITDebugLog(@"Going to previous song.");
263     [currentRemote goToPreviousSong];
264     [self timerUpdate];
265 }
266
267 - (void)fastForward
268 {
269     ITDebugLog(@"Fast forwarding.");
270     [currentRemote forward];
271     [self timerUpdate];
272 }
273
274 - (void)rewind
275 {
276     ITDebugLog(@"Rewinding.");
277     [currentRemote rewind];
278     [self timerUpdate];
279 }
280
281 - (void)selectPlaylistAtIndex:(int)index
282 {
283     ITDebugLog(@"Selecting playlist %i", index);
284     [currentRemote switchToPlaylistAtIndex:index];
285     [self timerUpdate];
286 }
287
288 - (void)selectSongAtIndex:(int)index
289 {
290     ITDebugLog(@"Selecting song %i", index);
291     [currentRemote switchToSongAtIndex:index];
292     [self timerUpdate];
293 }
294
295 - (void)selectSongRating:(int)rating
296 {
297     ITDebugLog(@"Selecting song rating %i", rating);
298     [currentRemote setCurrentSongRating:(float)rating / 100.0];
299     [self timerUpdate];
300 }
301
302 - (void)selectEQPresetAtIndex:(int)index
303 {
304     ITDebugLog(@"Selecting EQ preset %i", index);
305     [currentRemote switchToEQAtIndex:index];
306     [self timerUpdate];
307 }
308
309 - (void)showPlayer
310 {
311     ITDebugLog(@"Beginning show player.");
312     if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
313         ITDebugLog(@"Showing player interface.");
314         [currentRemote showPrimaryInterface];
315     } else {
316         ITDebugLog(@"Launching player.");
317         if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
318             ITDebugLog(@"Error Launching Player");
319         }
320     }
321     ITDebugLog(@"Finished show player.");
322 }
323
324 - (void)showPreferences
325 {
326     ITDebugLog(@"Show preferences.");
327     [[PreferencesController sharedPrefs] setController:self];
328     [[PreferencesController sharedPrefs] showPrefsWindow:self];
329 }
330
331 - (void)quitMenuTunes
332 {
333     ITDebugLog(@"Quitting MenuTunes.");
334     [NSApp terminate:self];
335 }
336
337 //
338 //
339
340 - (void)closePreferences
341 {
342     ITDebugLog(@"Preferences closed.");
343     if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
344         [self setupHotKeys];
345     }
346 }
347
348 - (ITMTRemote *)currentRemote
349 {
350     return currentRemote;
351 }
352
353 //
354 //
355 // Hot key setup
356 //
357 //
358
359 - (void)clearHotKeys
360 {
361     NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
362     ITHotKey *nextHotKey;
363     ITDebugLog(@"Clearing hot keys.");
364     while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
365         [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
366     }
367     ITDebugLog(@"Done clearing hot keys.");
368 }
369
370 - (void)setupHotKeys
371 {
372     ITHotKey *hotKey;
373     ITDebugLog(@"Setting up hot keys.");
374     if ([df objectForKey:@"PlayPause"] != nil) {
375         ITDebugLog(@"Setting up play pause hot key.");
376         hotKey = [[ITHotKey alloc] init];
377         [hotKey setName:@"PlayPause"];
378         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
379         [hotKey setTarget:self];
380         [hotKey setAction:@selector(playPause)];
381         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
382     }
383     
384     if ([df objectForKey:@"NextTrack"] != nil) {
385         ITDebugLog(@"Setting up next track hot key.");
386         hotKey = [[ITHotKey alloc] init];
387         [hotKey setName:@"NextTrack"];
388         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
389         [hotKey setTarget:self];
390         [hotKey setAction:@selector(nextSong)];
391         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
392     }
393     
394     if ([df objectForKey:@"PrevTrack"] != nil) {
395         ITDebugLog(@"Setting up previous track hot key.");
396         hotKey = [[ITHotKey alloc] init];
397         [hotKey setName:@"PrevTrack"];
398         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
399         [hotKey setTarget:self];
400         [hotKey setAction:@selector(prevSong)];
401         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
402     }
403     
404     if ([df objectForKey:@"ShowPlayer"] != nil) {
405         ITDebugLog(@"Setting up show player hot key.");
406         hotKey = [[ITHotKey alloc] init];
407         [hotKey setName:@"ShowPlayer"];
408         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
409         [hotKey setTarget:self];
410         [hotKey setAction:@selector(showPlayer)];
411         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
412     }
413     
414     if ([df objectForKey:@"TrackInfo"] != nil) {
415         ITDebugLog(@"Setting up track info hot key.");
416         hotKey = [[ITHotKey alloc] init];
417         [hotKey setName:@"TrackInfo"];
418         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
419         [hotKey setTarget:self];
420         [hotKey setAction:@selector(showCurrentTrackInfo)];
421         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
422     }
423     
424     if ([df objectForKey:@"UpcomingSongs"] != nil) {
425         ITDebugLog(@"Setting up upcoming songs hot key.");
426         hotKey = [[ITHotKey alloc] init];
427         [hotKey setName:@"UpcomingSongs"];
428         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
429         [hotKey setTarget:self];
430         [hotKey setAction:@selector(showUpcomingSongs)];
431         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
432     }
433     
434     if ([df objectForKey:@"ToggleLoop"] != nil) {
435         ITDebugLog(@"Setting up toggle loop hot key.");
436         hotKey = [[ITHotKey alloc] init];
437         [hotKey setName:@"ToggleLoop"];
438         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
439         [hotKey setTarget:self];
440         [hotKey setAction:@selector(toggleLoop)];
441         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
442     }
443     
444     if ([df objectForKey:@"ToggleShuffle"] != nil) {
445         ITDebugLog(@"Setting up toggle shuffle hot key.");
446         hotKey = [[ITHotKey alloc] init];
447         [hotKey setName:@"ToggleShuffle"];
448         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
449         [hotKey setTarget:self];
450         [hotKey setAction:@selector(toggleShuffle)];
451         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
452     }
453     
454     if ([df objectForKey:@"IncrementVolume"] != nil) {
455         ITDebugLog(@"Setting up increment volume hot key.");
456         hotKey = [[ITHotKey alloc] init];
457         [hotKey setName:@"IncrementVolume"];
458         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
459         [hotKey setTarget:self];
460         [hotKey setAction:@selector(incrementVolume)];
461         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
462     }
463     
464     if ([df objectForKey:@"DecrementVolume"] != nil) {
465         ITDebugLog(@"Setting up decrement volume hot key.");
466         hotKey = [[ITHotKey alloc] init];
467         [hotKey setName:@"DecrementVolume"];
468         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
469         [hotKey setTarget:self];
470         [hotKey setAction:@selector(decrementVolume)];
471         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
472     }
473     
474     if ([df objectForKey:@"IncrementRating"] != nil) {
475         ITDebugLog(@"Setting up increment rating hot key.");
476         hotKey = [[ITHotKey alloc] init];
477         [hotKey setName:@"IncrementRating"];
478         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
479         [hotKey setTarget:self];
480         [hotKey setAction:@selector(incrementRating)];
481         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
482     }
483     
484     if ([df objectForKey:@"DecrementRating"] != nil) {
485         ITDebugLog(@"Setting up decrement rating hot key.");
486         hotKey = [[ITHotKey alloc] init];
487         [hotKey setName:@"DecrementRating"];
488         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
489         [hotKey setTarget:self];
490         [hotKey setAction:@selector(decrementRating)];
491         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
492     }
493     ITDebugLog(@"Finished setting up hot keys.");
494 }
495
496 - (void)showCurrentTrackInfo
497 {
498     ITMTRemotePlayerSource  source      = [currentRemote currentSource];
499     NSString               *title       = [currentRemote currentSongTitle];
500     NSString               *album       = nil;
501     NSString               *artist      = nil;
502     NSString               *time        = nil;
503     NSString               *track       = nil;
504     int                     rating      = -1;
505     
506     ITDebugLog(@"Showing track info status window.");
507     
508     if ( title ) {
509
510         if ( [df boolForKey:@"showAlbum"] ) {
511             album = [currentRemote currentSongAlbum];
512         }
513
514         if ( [df boolForKey:@"showArtist"] ) {
515             artist = [currentRemote currentSongArtist];
516         }
517
518         if ( [df boolForKey:@"showTime"] ) {
519             time = [NSString stringWithFormat:@"%@: %@ / %@",
520                 @"Time",
521                 [currentRemote currentSongElapsed],
522                 [currentRemote currentSongLength]];
523         }
524
525         if ( [df boolForKey:@"showTrackNumber"] ) {
526             int trackNo    = [currentRemote currentSongTrack];
527             int trackCount = [currentRemote currentAlbumTrackCount];
528             
529             if ( (trackNo > 0) || (trackCount > 0) ) {
530                 track = [NSString stringWithFormat:@"%@: %i %@ %i",
531                     @"Track", trackNo, @"of", trackCount];
532             }
533         }
534
535         if ( [df boolForKey:@"showTrackRating"] ) {
536             rating = ( [currentRemote currentSongRating] * 5 );
537         }
538         
539     } else {
540         title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
541     }
542
543     [statusWindowController showSongInfoWindowWithSource:source
544                                                    title:title
545                                                    album:album
546                                                   artist:artist
547                                                     time:time
548                                                    track:track
549                                                   rating:rating];
550 }
551
552 - (void)showUpcomingSongs
553 {
554     int curPlaylist = [currentRemote currentPlaylistIndex];
555     int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
556     ITDebugLog(@"Showing upcoming songs status window.");
557     if (numSongs > 0) {
558         NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5];
559         int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
560         int curTrack = [currentRemote currentSongIndex];
561         int i;
562
563         for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
564             if (i <= numSongs) {
565                 [songList addObject:[currentRemote songTitleAtIndex:i]];
566             }
567         }
568         
569         [statusWindowController showUpcomingSongsWindowWithTitles:songList];
570         
571     } else {
572         [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
573     }
574 }
575
576 - (void)incrementVolume
577 {
578     float volume  = [currentRemote volume];
579     float dispVol = volume;
580     ITDebugLog(@"Incrementing volume.");
581     volume  += 0.110;
582     dispVol += 0.100;
583     
584     if (volume > 1.0) {
585         volume  = 1.0;
586         dispVol = 1.0;
587     }
588
589     ITDebugLog(@"Setting volume to %f", volume);
590     [currentRemote setVolume:volume];
591
592     // Show volume status window
593     [statusWindowController showVolumeWindowWithLevel:dispVol];
594 }
595
596 - (void)decrementVolume
597 {
598     float volume  = [currentRemote volume];
599     float dispVol = volume;
600     ITDebugLog(@"Decrementing volume.");
601     volume  -= 0.090;
602     dispVol -= 0.100;
603
604     if (volume < 0.0) {
605         volume  = 0.0;
606         dispVol = 0.0;
607     }
608     
609     ITDebugLog(@"Setting volume to %f", volume);
610     [currentRemote setVolume:volume];
611     
612     //Show volume status window
613     [statusWindowController showVolumeWindowWithLevel:dispVol];
614 }
615
616 - (void)incrementRating
617 {
618     float rating = [currentRemote currentSongRating];
619     ITDebugLog(@"Incrementing rating.");
620     rating += 0.2;
621     if (rating > 1.0) {
622         rating = 1.0;
623     }
624     ITDebugLog(@"Setting rating to %f", rating);
625     [currentRemote setCurrentSongRating:rating];
626     
627     //Show rating status window
628     [statusWindowController showRatingWindowWithRating:rating];
629 }
630
631 - (void)decrementRating
632 {
633     float rating = [currentRemote currentSongRating];
634     ITDebugLog(@"Decrementing rating.");
635     rating -= 0.2;
636     if (rating < 0.0) {
637         rating = 0.0;
638     }
639     ITDebugLog(@"Setting rating to %f", rating);
640     [currentRemote setCurrentSongRating:rating];
641     
642     //Show rating status window
643     [statusWindowController showRatingWindowWithRating:rating];
644 }
645
646 - (void)toggleLoop
647 {
648     ITMTRemotePlayerRepeatMode repeatMode = [currentRemote repeatMode];
649     ITDebugLog(@"Toggling repeat mode.");
650     switch (repeatMode) {
651         case ITMTRemotePlayerRepeatOff:
652             repeatMode = ITMTRemotePlayerRepeatAll;
653         break;
654         case ITMTRemotePlayerRepeatAll:
655             repeatMode = ITMTRemotePlayerRepeatOne;
656         break;
657         case ITMTRemotePlayerRepeatOne:
658             repeatMode = ITMTRemotePlayerRepeatOff;
659         break;
660     }
661     ITDebugLog(@"Setting repeat mode to %i", repeatMode);
662     [currentRemote setRepeatMode:repeatMode];
663     
664     //Show loop status window
665     [statusWindowController showRepeatWindowWithMode:repeatMode];
666 }
667
668 - (void)toggleShuffle
669 {
670     BOOL newShuffleEnabled = ( ! [currentRemote shuffleEnabled] );
671     ITDebugLog(@"Toggling shuffle mode.");
672     [currentRemote setShuffleEnabled:newShuffleEnabled];
673     //Show shuffle status window
674     ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
675     [statusWindowController showShuffleWindow:newShuffleEnabled];
676 }
677
678 /*************************************************************************/
679 #pragma mark -
680 #pragma mark WORKSPACE NOTIFICATION HANDLERS
681 /*************************************************************************/
682
683 - (void)applicationLaunched:(NSNotification *)note
684 {
685     if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
686         ITDebugLog(@"Remote application launched.");
687         [currentRemote begin];
688         [self setLatestSongIdentifier:@""];
689         [self timerUpdate];
690         refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
691                              target:self
692                              selector:@selector(timerUpdate)
693                              userInfo:nil
694                              repeats:YES] retain];
695         //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
696         [self setupHotKeys];
697         playerRunningState = ITMTRemotePlayerRunning;
698     }
699 }
700
701  - (void)applicationTerminated:(NSNotification *)note
702  {
703      if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
704         ITDebugLog(@"Remote application terminated.");
705         [currentRemote halt];
706         [refreshTimer invalidate];
707         [refreshTimer release];
708         refreshTimer = nil;
709         [self clearHotKeys];
710         playerRunningState = ITMTRemotePlayerNotRunning;
711      }
712  }
713
714
715 /*************************************************************************/
716 #pragma mark -
717 #pragma mark NSApplication DELEGATE METHODS
718 /*************************************************************************/
719
720 - (void)applicationWillTerminate:(NSNotification *)note
721 {
722     [self clearHotKeys];
723     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
724 }
725
726
727 /*************************************************************************/
728 #pragma mark -
729 #pragma mark DEALLOCATION METHOD
730 /*************************************************************************/
731
732 - (void)dealloc
733 {
734     [self applicationTerminated:nil];
735     [bling release];
736     [statusItem release];
737     [statusWindowController release];
738     [menuController release];
739     [super dealloc];
740 }
741
742
743 @end