Fixed the bug where hotkeys would not activate if iTunes was launched
[MenuTunes.git] / MainController.m
index 523936c..dd8c51d 100755 (executable)
@@ -4,6 +4,7 @@
 #import <ITKit/ITHotKeyCenter.h>
 #import <ITKit/ITHotKey.h>
 #import <ITKit/ITKeyCombo.h>
+#import "StatusWindow.h"
 #import "StatusWindowController.h"
 #import "StatusItemHack.h"
 
@@ -39,6 +40,8 @@ static MainController *sharedController;
         statusWindowController = [StatusWindowController sharedController];
         menuController = [[MenuController alloc] init];
         df = [[NSUserDefaults standardUserDefaults] retain];
+        timerUpdating = NO;
+        blinged = NO;
     }
     return self;
 }
@@ -50,9 +53,6 @@ static MainController *sharedController;
         SetITDebugMode(YES);
     }
     
-    bling = [[MTBlingController alloc] init];
-    blingDate = nil;
-    
     currentRemote = [self loadRemote];
     [currentRemote begin];
     
@@ -78,6 +78,14 @@ static MainController *sharedController;
             initWithStatusBar:[NSStatusBar systemStatusBar]
             withLength:NSSquareStatusItemLength];
     
+    bling = [[MTBlingController alloc] init];
+    [self blingTime];
+    registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
+                             target:self
+                             selector:@selector(blingTime)
+                             userInfo:nil
+                             repeats:YES] retain];
+    
     if ([currentRemote playerRunningState] == ITMTRemotePlayerRunning) {
         [self applicationLaunched:nil];
     } else {
@@ -147,21 +155,64 @@ static MainController *sharedController;
     [pool release];
 }*/
 
+- (void)setBlingTime:(NSDate*)date
+{
+    NSMutableDictionary *globalPrefs;
+    [df synchronize];
+    globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
+    if (date) {
+        [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
+    } else {
+        [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
+    }
+    [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
+    [df synchronize];
+    [globalPrefs release];
+}
+
+- (NSDate*)getBlingTime
+{
+    [df synchronize];
+    return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
+}
+
 - (void)blingTime
 {
     NSDate *now = [NSDate date];
-    if ( (! blingDate) || ([now timeIntervalSinceDate:blingDate] >= 86400) ) {
-        [bling showPanelIfNeeded];
-        [blingDate autorelease];
-        blingDate = [now retain];
+    if (![self blingBling]) {
+        if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
+            [self setBlingTime:now];
+        }
+        if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
+            blinged = YES;
+            [statusItem setEnabled:NO];
+            [self clearHotKeys];
+            if ([refreshTimer isValid]) {
+                [refreshTimer invalidate];
+            }
+            [statusWindowController showRegistrationQueryWindow];
+        }
+    } else {
+        if (blinged) {
+            [statusItem setEnabled:YES];
+            [self setupHotKeys];
+            if (![refreshTimer isValid]) {
+                [refreshTimer release];
+                refreshTimer = refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
+                             target:self
+                             selector:@selector(timerUpdate)
+                             userInfo:nil
+                             repeats:YES] retain];
+            }
+            blinged = NO;
+        }
+        [self setBlingTime:nil];
     }
 }
 
 - (void)blingNow
 {
     [bling showPanel];
-    [blingDate autorelease];
-    blingDate = [[NSDate date] retain];
 }
 
 - (BOOL)blingBling
@@ -202,15 +253,19 @@ static MainController *sharedController;
 
 - (void)timerUpdate
 {
-    if ( [self songChanged] ) {
+    if ( [self songChanged] && (timerUpdating != YES) ) {
         ITDebugLog(@"The song changed.");
-        [self setLatestSongIdentifier:[currentRemote playerStateUniqueIdentifier]];
+        timerUpdating = YES;
         latestPlaylistClass = [currentRemote currentPlaylistClass];
         [menuController rebuildSubmenus];
 
         if ( [df boolForKey:@"showSongInfoOnChange"] ) {
             [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
         }
+        
+        [self setLatestSongIdentifier:[currentRemote playerStateUniqueIdentifier]];
+        
+        timerUpdating = NO;
     }
 }
 
@@ -366,6 +421,11 @@ static MainController *sharedController;
 {
     ITHotKey *hotKey;
     ITDebugLog(@"Setting up hot keys.");
+    
+    if (playerRunningState == ITMTRemotePlayerNotRunning) {
+        return;
+    }
+    
     if ([df objectForKey:@"PlayPause"] != nil) {
         ITDebugLog(@"Setting up play pause hot key.");
         hotKey = [[ITHotKey alloc] init];
@@ -528,7 +588,10 @@ static MainController *sharedController;
         }
 
         if ( [df boolForKey:@"showTrackRating"] ) {
-            rating = ( [currentRemote currentSongRating] * 5 );
+            float currentRating = [currentRemote currentSongRating];
+            if (currentRating >= 0.0) {
+                rating = ( currentRating * 5 );
+            }
         }
         
     } else {
@@ -612,6 +675,12 @@ static MainController *sharedController;
 {
     float rating = [currentRemote currentSongRating];
     ITDebugLog(@"Incrementing rating.");
+    
+    if ([currentRemote currentPlaylistIndex] == 0) {
+        ITDebugLog(@"No song playing, rating change aborted.");
+        return;
+    }
+    
     rating += 0.2;
     if (rating > 1.0) {
         rating = 1.0;
@@ -627,6 +696,12 @@ static MainController *sharedController;
 {
     float rating = [currentRemote currentSongRating];
     ITDebugLog(@"Decrementing rating.");
+    
+    if ([currentRemote currentPlaylistIndex] == 0) {
+        ITDebugLog(@"No song playing, rating change aborted.");
+        return;
+    }
+    
     rating -= 0.2;
     if (rating < 0.0) {
         rating = 0.0;
@@ -670,6 +745,25 @@ static MainController *sharedController;
     [statusWindowController showShuffleWindow:newShuffleEnabled];
 }
 
+- (void)registerNowOK
+{
+    [[StatusWindow sharedWindow] setLocked:NO];
+    [[StatusWindow sharedWindow] vanish:self];
+    [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
+
+    [self blingNow];
+}
+
+- (void)registerNowCancel
+{
+    [[StatusWindow sharedWindow] setLocked:NO];
+    [[StatusWindow sharedWindow] vanish:self];
+    [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
+
+    [NSApp terminate:self];
+}
+
+
 /*************************************************************************/
 #pragma mark -
 #pragma mark WORKSPACE NOTIFICATION HANDLERS
@@ -679,6 +773,7 @@ static MainController *sharedController;
 {
     if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
         ITDebugLog(@"Remote application launched.");
+        playerRunningState = ITMTRemotePlayerRunning;
         [currentRemote begin];
         [self setLatestSongIdentifier:@""];
         [self timerUpdate];
@@ -689,7 +784,6 @@ static MainController *sharedController;
                              repeats:YES] retain];
         //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
         [self setupHotKeys];
-        playerRunningState = ITMTRemotePlayerRunning;
     }
 }