X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/beebd024ae0457a22ab8f289f890ead9f6db399e..d7c39a05d1ea7368700d2140c7f042cc72cf0d0d:/MainController.m diff --git a/MainController.m b/MainController.m index 3b9342b..294e52c 100755 --- a/MainController.m +++ b/MainController.m @@ -9,6 +9,7 @@ #import #import "StatusWindow.h" #import "StatusWindowController.h" +#import "AudioscrobblerController.h" #import "StatusItemHack.h" @interface NSMenu (MenuImpl) @@ -64,6 +65,8 @@ - (void)setLatestSongIdentifier:(NSString *)newIdentifier; - (void)applicationLaunched:(NSNotification *)note; - (void)applicationTerminated:(NSNotification *)note; + +- (void)invalidateStatusWindowUpdateTimer; @end static MainController *sharedController; @@ -85,6 +88,9 @@ static MainController *sharedController; if ( ( self = [super init] ) ) { sharedController = self; + _statusWindowUpdateTimer = nil; + _audioscrobblerTimer = nil; + remoteArray = [[NSMutableArray alloc] initWithCapacity:1]; [[PreferencesController sharedPrefs] setController:self]; statusWindowController = [StatusWindowController sharedController]; @@ -98,11 +104,32 @@ static MainController *sharedController; - (void)applicationDidFinishLaunching:(NSNotification *)note { + NSString *iTunesPath = [df stringForKey:@"CustomPlayerPath"]; + NSDictionary *iTunesInfoPlist; + float iTunesVersion; + //Turn on debug mode if needed - if ([df boolForKey:@"ITDebugMode"]) { + /*if ((GetCurrentKeyModifiers() & (controlKey | rightControlKey)) != 0) + if ((GetCurrentKeyModifiers() & (optionKey | rightOptionKey)) != 0) + if ((GetCurrentKeyModifiers() & (shiftKey | rightShiftKey)) != 0)*/ + if ([df boolForKey:@"ITDebugMode"] || ((GetCurrentKeyModifiers() & (controlKey | rightControlKey)) != 0)) { SetITDebugMode(YES); + [[StatusWindowController sharedController] showDebugModeEnabledWindow]; } - + + //Check if iTunes 4.7 or later is installed + if (!iTunesPath) { + iTunesPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"]; + } + iTunesInfoPlist = [[NSBundle bundleWithPath:iTunesPath] infoDictionary]; + iTunesVersion = [[iTunesInfoPlist objectForKey:@"CFBundleVersion"] floatValue]; + ITDebugLog(@"iTunes version found: %f.", iTunesVersion); + if (iTunesVersion >= 4.7) { + _needsPolling = NO; + } else { + _needsPolling = YES; + } + if (([df integerForKey:@"appVersion"] < 1200) && ([df integerForKey:@"SongsInAdvance"] > 0)) { [df removePersistentDomainForName:@"com.ithinksw.menutunes"]; [df synchronize]; @@ -113,6 +140,8 @@ static MainController *sharedController; currentRemote = [self loadRemote]; [[self currentRemote] begin]; + [[self currentRemote] currentSongElapsed]; + //Turn on network stuff if needed networkController = [[NetworkController alloc] init]; if ([df boolForKey:@"enableSharing"]) { @@ -147,13 +176,13 @@ static MainController *sharedController; withLength:NSSquareStatusItemLength]; } - bling = [[MTBlingController alloc] init]; + /*bling = [[MTBlingController alloc] init]; [self blingTime]; registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(blingTime) userInfo:nil - repeats:YES] retain]; + repeats:YES] retain];*/ NS_DURING if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) { @@ -171,9 +200,23 @@ static MainController *sharedController; [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]]; [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]]; + if ([df boolForKey:@"audioscrobblerEnabled"]) { + if ([PreferencesController getKeychainItemPasswordForUser:[df stringForKey:@"audioscrobblerUser"]] != nil) { + [[AudioscrobblerController sharedController] attemptHandshake:NO]; + } + } + [networkController startRemoteServerSearch]; [NSApp deactivate]; [self performSelector:@selector(rawr) withObject:nil afterDelay:1.0]; + + bling = [[MTBlingController alloc] init]; + [self blingTime]; + registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0 + target:self + selector:@selector(blingTime) + userInfo:nil + repeats:YES] retain]; } - (void)rawr @@ -181,13 +224,6 @@ static MainController *sharedController; _open = YES; } -- (void)applicationWillBecomeActive:(NSNotification *)note -{ - if (_open) { - [[MainController sharedController] showPreferences]; - } -} - - (ITMTRemote *)loadRemote { NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath]; @@ -288,7 +324,7 @@ static MainController *sharedController; if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) { blinged = YES; [statusItem setEnabled:NO]; - [self clearHotKeys]; + [[ITHotKeyCenter sharedCenter] setEnabled:NO]; if ([refreshTimer isValid]) { [refreshTimer invalidate]; [refreshTimer release]; @@ -299,8 +335,8 @@ static MainController *sharedController; } else { if (blinged) { [statusItem setEnabled:YES]; - [self setupHotKeys]; - if (![refreshTimer isValid]) { + [[ITHotKeyCenter sharedCenter] setEnabled:YES]; + if (_needsPolling && ![refreshTimer isValid]) { [refreshTimer release]; refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5) target:self @@ -376,18 +412,21 @@ static MainController *sharedController; - (void)timerUpdate { - NSString *identifier = [[self currentRemote] playerStateUniqueIdentifier]; - if (identifier == nil) { + NSString *identifier = nil; + NS_DURING + identifier = [[self currentRemote] playerStateUniqueIdentifier]; + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + if (refreshTimer && identifier == nil) { if ([statusItem isEnabled]) { [statusItem setToolTip:@"iTunes not responding."]; - [[ITHotKeyCenter sharedCenter] setEnabled:NO]; } [statusItem setEnabled:NO]; return; } else if (![statusItem isEnabled]) { [statusItem setEnabled:YES]; [statusItem setToolTip:_toolTip]; - [[ITHotKeyCenter sharedCenter] setEnabled:YES]; return; } @@ -430,13 +469,26 @@ static MainController *sharedController; } else if (title) { _toolTip = title; } else { - _toolTip = @"No Song Playing"; + _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing."); } [statusItem setToolTip:_toolTip]; } else { [statusItem setToolTip:nil]; } } + + if ([df boolForKey:@"audioscrobblerEnabled"]) { + int length = [[self currentRemote] currentSongDuration]; + if (length > 30) { + _audioscrobblerInterval = ((length / 2 < 240) ? length / 2 : 240); + [_audioscrobblerTimer invalidate]; + [_audioscrobblerTimer release]; + _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:nil target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode]; + } + } else { + _audioscrobblerTimer = nil; + } NS_HANDLER [self networkError:localException]; NS_ENDHANDLER @@ -456,20 +508,18 @@ static MainController *sharedController; if (([[self currentRemote] playerStateUniqueIdentifier] == nil) && playerRunningState == ITMTRemotePlayerRunning) { if (refreshTimer) { if ([statusItem isEnabled]) { - [statusItem setToolTip:@"iTunes not responding."]; - [[ITHotKeyCenter sharedCenter] setEnabled:NO]; + [statusItem setToolTip:NSLocalizedString(@"iTunesNotResponding", @"iTunes is not responding.")]; } [statusItem setEnabled:NO]; } else { NSMenu *menu = [[NSMenu alloc] init]; - [menu addItemWithTitle:@"iTunes Not Responding" action:nil keyEquivalent:@""]; + [menu addItemWithTitle:NSLocalizedString(@"iTunesNotResponding", @"iTunes is not responding.") action:nil keyEquivalent:@""]; [statusItem setMenu:[menu autorelease]]; } return; } else if (![statusItem isEnabled]) { [statusItem setEnabled:YES]; [statusItem setToolTip:_toolTip]; - [[ITHotKeyCenter sharedCenter] setEnabled:YES]; return; } @@ -492,12 +542,12 @@ static MainController *sharedController; - (void)trackChanged:(NSNotification *)note { //If we're running the timer, shut it off since we don't need it! - if (refreshTimer && [refreshTimer isValid]) { + /*if (refreshTimer && [refreshTimer isValid]) { ITDebugLog(@"Invalidating refresh timer."); [refreshTimer invalidate]; [refreshTimer release]; refreshTimer = nil; - } + }*/ if (![self songChanged]) { return; @@ -546,13 +596,26 @@ static MainController *sharedController; } else if (title) { _toolTip = title; } else { - _toolTip = @"No Song Playing"; + _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing.");; } [statusItem setToolTip:_toolTip]; } else { [statusItem setToolTip:nil]; } } + + if ([df boolForKey:@"audioscrobblerEnabled"]) { + int length = [[self currentRemote] currentSongDuration]; + if (length > 30) { + _audioscrobblerInterval = ((length / 2 < 240) ? length / 2 : 240); + [_audioscrobblerTimer invalidate]; + [_audioscrobblerTimer release]; + _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:1.0 target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode]; + } + } else { + _audioscrobblerTimer = nil; + } NS_HANDLER [self networkError:localException]; NS_ENDHANDLER @@ -564,6 +627,39 @@ static MainController *sharedController; } } +- (void)submitAudioscrobblerTrack:(NSTimer *)timer +{ + ITDebugLog(@"Audioscrobbler: Attempting to submit current track"); + [timer invalidate]; + if ([df boolForKey:@"audioscrobblerEnabled"]) { + NS_DURING + int elapsed = [[self currentRemote] currentSongPlayed], length = [[self currentRemote] currentSongDuration], requiredInterval = ((length / 2 < 240) ? length / 2 : 240); + if ((abs(elapsed - requiredInterval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) { + NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist]; + NSLog(@"Audioscrobbler: Submitting"); + if (title && artist) { + ITDebugLog(@"Audioscrobbler: Submitting current track"); + [[AudioscrobblerController sharedController] submitTrack:title + artist:artist + album:[[self currentRemote] currentSongAlbum] + length:[[self currentRemote] currentSongDuration]]; + } + } else if (requiredInterval - elapsed > 0) { + _audioscrobblerInterval = requiredInterval - elapsed; + [_audioscrobblerTimer release]; + NSLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", _audioscrobblerInterval); + ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", _audioscrobblerInterval); + _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:1.0 target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode]; + } else { + NSLog(@"Audioscrobbler: Skipping submission. %i, %i", requiredInterval, elapsed); + } + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + } +} + // // // Menu Selectors @@ -750,7 +846,7 @@ static MainController *sharedController; { ITDebugLog(@"Show preferences."); [[PreferencesController sharedPrefs] showPrefsWindow:self]; - [[StatusWindow sharedWindow] setLocked:NO]; + [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO]; [[StatusWindow sharedWindow] vanish:self]; [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; } @@ -883,10 +979,20 @@ static MainController *sharedController; [hotKey setName:@"TrackInfo"]; [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]]; [hotKey setTarget:self]; - [hotKey setAction:@selector(showCurrentTrackInfo)]; + [hotKey setAction:@selector(showCurrentTrackInfoHotKey)]; [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; } + if ([df objectForKey:@"AlbumArt"] != nil) { + ITDebugLog(@"Setting up album art hot key."); + hotKey = [[ITHotKey alloc] init]; + [hotKey setName:@"AlbumArt"]; + [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"AlbumArt"]]]; + [hotKey setTarget:self]; + [hotKey setAction:@selector(showCurrentAlbumArtHotKey)]; + [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; + } + if ([df objectForKey:@"UpcomingSongs"] != nil) { ITDebugLog(@"Setting up upcoming songs hot key."); hotKey = [[ITHotKey alloc] init]; @@ -957,6 +1063,16 @@ static MainController *sharedController; [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; } + if ([df objectForKey:@"ToggleShufflability"] != nil) { + ITDebugLog(@"Setting up toggle song shufflability hot key."); + hotKey = [[ITHotKey alloc] init]; + [hotKey setName:@"ToggleShufflability"]; + [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShufflability"]]]; + [hotKey setTarget:self]; + [hotKey setAction:@selector(toggleSongShufflable)]; + [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; + } + if ([df objectForKey:@"PopupMenu"] != nil) { ITDebugLog(@"Setting up popup menu hot key."); hotKey = [[ITHotKey alloc] init]; @@ -983,6 +1099,69 @@ static MainController *sharedController; ITDebugLog(@"Finished setting up hot keys."); } +- (void)showCurrentTrackInfoHotKey +{ + //If we're already visible and the setting says so, vanish instead of displaying again. + if ([df boolForKey:@"ToggleTrackInfoWithHotKey"] && [statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [[StatusWindow sharedWindow] visibilityState] == ITWindowVisibleState) { + ITDebugLog(@"Track window is already visible, hiding track window."); + [self invalidateStatusWindowUpdateTimer]; + [[StatusWindow sharedWindow] vanish:nil]; + return; + } + [self showCurrentTrackInfo]; +} + +- (void)showCurrentAlbumArtHotKey +{ + //If we're already visible and the setting says so, vanish instead of displaying again. + if ([df boolForKey:@"ToggleTrackInfoWithHotKey"] && [statusWindowController currentStatusWindowType] == StatusWindowAlbumArtType && [[StatusWindow sharedWindow] visibilityState] == ITWindowVisibleState) { + ITDebugLog(@"Art window is already visible, hiding track window."); + [[StatusWindow sharedWindow] vanish:nil]; + return; + } + [self showCurrentAlbumArt]; +} + +- (void)showCurrentAlbumArt +{ + NSImage *art = nil; + NS_DURING + art = [[self currentRemote] currentSongAlbumArt]; + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + + if (art) { + NSSize oldSize = [art size], newSize; + if (oldSize.width > 300 && oldSize.height > 300) { + if (oldSize.width > oldSize.height) { + newSize = NSMakeSize(300, oldSize.height * (300.0f / oldSize.width)); + } else { + newSize = NSMakeSize(oldSize.width * (300.0f / oldSize.height), 300); + } + } else { + newSize = oldSize; + } + + art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize]; + + [statusWindowController showAlbumArtWindowWithImage:art]; + } else { + NSString *string = nil; + NS_DURING + if ([[self currentRemote] currentSongTitle]) { + string = NSLocalizedString(@"noAlbumArt", @"No art for current song."); + } else { + string = NSLocalizedString(@"noSongPlaying", @"No song is playing."); + } + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + //Show the no song playing window if there is no album art or no track is playing + [statusWindowController showAlbumArtWindowWithErrorText:string]; + } +} + - (void)showCurrentTrackInfo { ITMTRemotePlayerSource source = 0; @@ -995,7 +1174,7 @@ static MainController *sharedController; NSImage *art = nil; int rating = -1; int playCount = -1; - + ITDebugLog(@"Showing track info status window."); NS_DURING @@ -1007,16 +1186,18 @@ static MainController *sharedController; if ( title ) { if ( [df boolForKey:@"showAlbumArtwork"] ) { - NSSize oldSize, newSize; - NS_DURING - art = [[self currentRemote] currentSongAlbumArt]; - oldSize = [art size]; - if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width)); - else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110); - art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize]; - NS_HANDLER - [self networkError:localException]; - NS_ENDHANDLER + NSSize oldSize, newSize; + NS_DURING + art = [[self currentRemote] currentSongAlbumArt]; + oldSize = [art size]; + if (oldSize.width > oldSize.height) { + newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width)); + } + else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110); + art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize]; + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER } if ( [df boolForKey:@"showAlbum"] ) { @@ -1052,6 +1233,9 @@ static MainController *sharedController; NS_HANDLER [self networkError:localException]; NS_ENDHANDLER + _timeUpdateCount = 0; + [self invalidateStatusWindowUpdateTimer]; + _statusWindowUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; } if ( [df boolForKey:@"showTrackNumber"] ) { @@ -1108,6 +1292,37 @@ static MainController *sharedController; image:art]; } +- (void)updateTime:(NSTimer *)timer +{ + StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow]; + _timeUpdateCount++; + if ([statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [sw visibilityState] != ITWindowHiddenState) { + NSString *time = nil, *length; + NS_DURING + length = [[self currentRemote] currentSongLength]; + if (length) { + time = [NSString stringWithFormat:@"%@: %@ / %@", + NSLocalizedString(@"time", @"Time"), + [[self currentRemote] currentSongElapsed], + length]; + [[StatusWindowController sharedController] updateTime:time]; + } + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + } else { + [self invalidateStatusWindowUpdateTimer]; + } +} + +- (void)invalidateStatusWindowUpdateTimer +{ + if (_statusWindowUpdateTimer) { + [_statusWindowUpdateTimer invalidate]; + _statusWindowUpdateTimer = nil; + } +} + - (void)showUpcomingSongs { int numSongs = 0; @@ -1117,6 +1332,8 @@ static MainController *sharedController; [self networkError:localException]; NS_ENDHANDLER + [self invalidateStatusWindowUpdateTimer]; + ITDebugLog(@"Showing upcoming songs status window."); NS_DURING if (numSongs > 0) { @@ -1125,10 +1342,12 @@ static MainController *sharedController; int curTrack = [[self currentRemote] currentSongIndex]; int i; - for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) { - if (i <= numSongs) { + for (i = curTrack + 1; i <= curTrack + numSongsInAdvance && i <= numSongs; i++) { + if ([[self currentRemote] songEnabledAtIndex:i]) { [songList addObject:[[self currentRemote] songTitleAtIndex:i]]; - } + } else { + numSongsInAdvance++; + } } if ([songList count] == 0) { @@ -1173,6 +1392,7 @@ static MainController *sharedController; [[self currentRemote] setVolume:volume]; // Show volume status window + [self invalidateStatusWindowUpdateTimer]; [statusWindowController showVolumeWindowWithLevel:dispVol]; NS_HANDLER [self networkError:localException]; @@ -1197,6 +1417,7 @@ static MainController *sharedController; [[self currentRemote] setVolume:volume]; //Show volume status window + [self invalidateStatusWindowUpdateTimer]; [statusWindowController showVolumeWindowWithLevel:dispVol]; NS_HANDLER [self networkError:localException]; @@ -1222,6 +1443,7 @@ static MainController *sharedController; [[self currentRemote] setCurrentSongRating:rating]; //Show rating status window + [self invalidateStatusWindowUpdateTimer]; [statusWindowController showRatingWindowWithRating:rating]; NS_HANDLER [self networkError:localException]; @@ -1247,6 +1469,7 @@ static MainController *sharedController; [[self currentRemote] setCurrentSongRating:rating]; //Show rating status window + [self invalidateStatusWindowUpdateTimer]; [statusWindowController showRatingWindowWithRating:rating]; NS_HANDLER [self networkError:localException]; @@ -1255,9 +1478,11 @@ static MainController *sharedController; - (void)setRating:(ITHotKey *)sender { - int stars = [[sender name] characterAtIndex:9] - 48; - [self selectSongRating:stars * 20]; - [statusWindowController showRatingWindowWithRating:(float)stars / 5.0]; + if ([self songIsPlaying]) { + int stars = [[sender name] characterAtIndex:9] - 48; + [self selectSongRating:stars * 20]; + [statusWindowController showRatingWindowWithRating:(float)stars / 5.0]; + } } - (void)toggleLoop @@ -1280,6 +1505,7 @@ static MainController *sharedController; [[self currentRemote] setRepeatMode:repeatMode]; //Show loop status window + [self invalidateStatusWindowUpdateTimer]; [statusWindowController showRepeatWindowWithMode:repeatMode]; NS_HANDLER [self networkError:localException]; @@ -1294,15 +1520,32 @@ static MainController *sharedController; [[self currentRemote] setShuffleEnabled:newShuffleEnabled]; //Show shuffle status window ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled); + [self invalidateStatusWindowUpdateTimer]; [statusWindowController showShuffleWindow:newShuffleEnabled]; NS_HANDLER [self networkError:localException]; NS_ENDHANDLER } +- (void)toggleSongShufflable +{ + if ([self songIsPlaying]) { + NS_DURING + BOOL flag = ![[self currentRemote] currentSongShufflable]; + ITDebugLog(@"Toggling shufflability."); + [[self currentRemote] setCurrentSongShufflable:flag]; + //Show song shufflability status window + [self invalidateStatusWindowUpdateTimer]; + [statusWindowController showSongShufflabilityWindow:flag]; + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + } +} + - (void)registerNowOK { - [[StatusWindow sharedWindow] setLocked:NO]; + [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO]; [[StatusWindow sharedWindow] vanish:self]; [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; @@ -1311,7 +1554,7 @@ static MainController *sharedController; - (void)registerNowCancel { - [[StatusWindow sharedWindow] setLocked:NO]; + [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO]; [[StatusWindow sharedWindow] vanish:self]; [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; @@ -1347,14 +1590,18 @@ static MainController *sharedController; [self setupHotKeys]; //playerRunningState = ITMTRemotePlayerRunning; playerRunningState = [[self currentRemote] playerRunningState]; - if (refreshTimer) { - [refreshTimer invalidate]; + if (_needsPolling) { + if (refreshTimer) { + [refreshTimer invalidate]; + } } - refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5) - target:self - selector:@selector(timerUpdate) - userInfo:nil - repeats:YES] retain]; + + refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5) + target:self + selector:@selector(timerUpdate) + userInfo:nil + repeats:YES] retain]; + [self timerUpdate]; ITDebugLog(@"Connection successful."); return 1; @@ -1379,10 +1626,12 @@ static MainController *sharedController; [networkController disconnect]; if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) { + refreshTimer = nil; [self applicationLaunched:nil]; } else { [self applicationTerminated:nil]; } + if (refreshTimer) { [self timerUpdate]; }; @@ -1432,6 +1681,7 @@ static MainController *sharedController; - (void)remoteServerFound:(id)sender { if (![networkController isServerOn] && ![networkController isConnectedToServer]) { + [self invalidateStatusWindowUpdateTimer]; [[StatusWindowController sharedController] showReconnectQueryWindow]; } } @@ -1448,6 +1698,7 @@ static MainController *sharedController; ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]); if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) { //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); + [self invalidateStatusWindowUpdateTimer]; [[StatusWindowController sharedController] showNetworkErrorQueryWindow]; if ([self disconnectFromServer]) { [[PreferencesController sharedPrefs] resetRemotePlayerTextFields]; @@ -1464,14 +1715,14 @@ static MainController *sharedController; [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO]; }*/ [self checkForRemoteServerAndConnectImmediately:YES]; - [[StatusWindow sharedWindow] setLocked:NO]; + [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO]; [[StatusWindow sharedWindow] vanish:self]; [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; } - (void)cancelReconnect { - [[StatusWindow sharedWindow] setLocked:NO]; + [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO]; [[StatusWindow sharedWindow] vanish:self]; [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; } @@ -1490,11 +1741,13 @@ static MainController *sharedController; [[self currentRemote] begin]; [self setLatestSongIdentifier:@""]; [self timerUpdate]; - refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5) - target:self - selector:@selector(timerUpdate) - userInfo:nil - repeats:YES] retain]; + if (_needsPolling) { + refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5) + target:self + selector:@selector(timerUpdate) + userInfo:nil + repeats:YES] retain]; + } //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil]; if (![df boolForKey:@"UsePollingOnly"]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackChanged:) name:@"ITMTTrackChanged" object:nil]; @@ -1516,7 +1769,7 @@ static MainController *sharedController; [refreshTimer invalidate]; [refreshTimer release]; refreshTimer = nil; - [[NSNotificationCenter defaultCenter] removeObserver:self]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ITMTTrackChanged" object:nil]; [statusItem setEnabled:YES]; [statusItem setToolTip:@"iTunes not running."]; [self clearHotKeys]; @@ -1552,6 +1805,13 @@ static MainController *sharedController; [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; } +- (void)applicationDidBecomeActive:(NSNotification *)note +{ + //This appears to not work in 10.4 + if (_open && !blinged && ![[ITAboutWindowController sharedController] isVisible] && ![NSApp mainWindow] && ([[StatusWindow sharedWindow] exitMode] == ITTransientStatusWindowExitAfterDelay)) { + [[MainController sharedController] showPreferences]; + } +} /*************************************************************************/ #pragma mark -