Positioning and hidden ratings for radio
[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             float currentRating = [currentRemote currentSongRating];
537             if (currentRating >= 0.0) {
538                 rating = ( currentRating * 5 );
539             }
540         }
541         
542     } else {
543         title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
544     }
545
546     [statusWindowController showSongInfoWindowWithSource:source
547                                                    title:title
548                                                    album:album
549                                                   artist:artist
550                                                     time:time
551                                                    track:track
552                                                   rating:rating];
553 }
554
555 - (void)showUpcomingSongs
556 {
557     int curPlaylist = [currentRemote currentPlaylistIndex];
558     int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
559     ITDebugLog(@"Showing upcoming songs status window.");
560     if (numSongs > 0) {
561         NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5];
562         int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
563         int curTrack = [currentRemote currentSongIndex];
564         int i;
565
566         for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
567             if (i <= numSongs) {
568                 [songList addObject:[currentRemote songTitleAtIndex:i]];
569             }
570         }
571         
572         [statusWindowController showUpcomingSongsWindowWithTitles:songList];
573         
574     } else {
575         [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
576     }
577 }
578
579 - (void)incrementVolume
580 {
581     float volume  = [currentRemote volume];
582     float dispVol = volume;
583     ITDebugLog(@"Incrementing volume.");
584     volume  += 0.110;
585     dispVol += 0.100;
586     
587     if (volume > 1.0) {
588         volume  = 1.0;
589         dispVol = 1.0;
590     }
591
592     ITDebugLog(@"Setting volume to %f", volume);
593     [currentRemote setVolume:volume];
594
595     // Show volume status window
596     [statusWindowController showVolumeWindowWithLevel:dispVol];
597 }
598
599 - (void)decrementVolume
600 {
601     float volume  = [currentRemote volume];
602     float dispVol = volume;
603     ITDebugLog(@"Decrementing volume.");
604     volume  -= 0.090;
605     dispVol -= 0.100;
606
607     if (volume < 0.0) {
608         volume  = 0.0;
609         dispVol = 0.0;
610     }
611     
612     ITDebugLog(@"Setting volume to %f", volume);
613     [currentRemote setVolume:volume];
614     
615     //Show volume status window
616     [statusWindowController showVolumeWindowWithLevel:dispVol];
617 }
618
619 - (void)incrementRating
620 {
621     float rating = [currentRemote currentSongRating];
622     ITDebugLog(@"Incrementing rating.");
623     rating += 0.2;
624     if (rating > 1.0) {
625         rating = 1.0;
626     }
627     ITDebugLog(@"Setting rating to %f", rating);
628     [currentRemote setCurrentSongRating:rating];
629     
630     //Show rating status window
631     [statusWindowController showRatingWindowWithRating:rating];
632 }
633
634 - (void)decrementRating
635 {
636     float rating = [currentRemote currentSongRating];
637     ITDebugLog(@"Decrementing rating.");
638     rating -= 0.2;
639     if (rating < 0.0) {
640         rating = 0.0;
641     }
642     ITDebugLog(@"Setting rating to %f", rating);
643     [currentRemote setCurrentSongRating:rating];
644     
645     //Show rating status window
646     [statusWindowController showRatingWindowWithRating:rating];
647 }
648
649 - (void)toggleLoop
650 {
651     ITMTRemotePlayerRepeatMode repeatMode = [currentRemote repeatMode];
652     ITDebugLog(@"Toggling repeat mode.");
653     switch (repeatMode) {
654         case ITMTRemotePlayerRepeatOff:
655             repeatMode = ITMTRemotePlayerRepeatAll;
656         break;
657         case ITMTRemotePlayerRepeatAll:
658             repeatMode = ITMTRemotePlayerRepeatOne;
659         break;
660         case ITMTRemotePlayerRepeatOne:
661             repeatMode = ITMTRemotePlayerRepeatOff;
662         break;
663     }
664     ITDebugLog(@"Setting repeat mode to %i", repeatMode);
665     [currentRemote setRepeatMode:repeatMode];
666     
667     //Show loop status window
668     [statusWindowController showRepeatWindowWithMode:repeatMode];
669 }
670
671 - (void)toggleShuffle
672 {
673     BOOL newShuffleEnabled = ( ! [currentRemote shuffleEnabled] );
674     ITDebugLog(@"Toggling shuffle mode.");
675     [currentRemote setShuffleEnabled:newShuffleEnabled];
676     //Show shuffle status window
677     ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
678     [statusWindowController showShuffleWindow:newShuffleEnabled];
679 }
680
681 /*************************************************************************/
682 #pragma mark -
683 #pragma mark WORKSPACE NOTIFICATION HANDLERS
684 /*************************************************************************/
685
686 - (void)applicationLaunched:(NSNotification *)note
687 {
688     if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
689         ITDebugLog(@"Remote application launched.");
690         [currentRemote begin];
691         [self setLatestSongIdentifier:@""];
692         [self timerUpdate];
693         refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
694                              target:self
695                              selector:@selector(timerUpdate)
696                              userInfo:nil
697                              repeats:YES] retain];
698         //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
699         [self setupHotKeys];
700         playerRunningState = ITMTRemotePlayerRunning;
701     }
702 }
703
704  - (void)applicationTerminated:(NSNotification *)note
705  {
706      if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
707         ITDebugLog(@"Remote application terminated.");
708         [currentRemote halt];
709         [refreshTimer invalidate];
710         [refreshTimer release];
711         refreshTimer = nil;
712         [self clearHotKeys];
713         playerRunningState = ITMTRemotePlayerNotRunning;
714      }
715  }
716
717
718 /*************************************************************************/
719 #pragma mark -
720 #pragma mark NSApplication DELEGATE METHODS
721 /*************************************************************************/
722
723 - (void)applicationWillTerminate:(NSNotification *)note
724 {
725     [self clearHotKeys];
726     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
727 }
728
729
730 /*************************************************************************/
731 #pragma mark -
732 #pragma mark DEALLOCATION METHOD
733 /*************************************************************************/
734
735 - (void)dealloc
736 {
737     [self applicationTerminated:nil];
738     [bling release];
739     [statusItem release];
740     [statusWindowController release];
741     [menuController release];
742     [super dealloc];
743 }
744
745
746 @end