X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/1241f4136652ac9629457a83953d8f8bf3889bdd..bc4fa9cf6435c18c42de06c478480c6f6673ca4b:/MainController.m diff --git a/MainController.m b/MainController.m index c2acaf1..65c718c 100755 --- a/MainController.m +++ b/MainController.m @@ -33,7 +33,6 @@ @interface MainController(Private) - (ITMTRemote *)loadRemote; - (void)setLatestSongIdentifier:(NSString *)newIdentifier; -- (void)checkForRemoteServer; - (void)applicationLaunched:(NSNotification *)note; - (void)applicationTerminated:(NSNotification *)note; @end @@ -90,7 +89,7 @@ static MainController *sharedController; if ([df boolForKey:@"enableSharing"]) { [self setServerStatus:YES]; } else if ([df boolForKey:@"useSharedPlayer"]) { - [self checkForRemoteServer]; + [self checkForRemoteServerAndConnectImmediately:YES]; } //Setup for notification of the remote player launching or quitting @@ -778,7 +777,19 @@ static MainController *sharedController; NS_ENDHANDLER 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 + } + if ( [df boolForKey:@"showAlbum"] ) { NS_DURING album = [[self currentRemote] currentSongAlbum]; @@ -844,20 +855,6 @@ static MainController *sharedController; rating = ( currentRating * 5 ); } } - - 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 - } - } else { title = NSLocalizedString(@"noSongPlaying", @"No song is playing."); } @@ -1134,9 +1131,22 @@ static MainController *sharedController; } - (void)checkForRemoteServer +{ + [self checkForRemoteServerAndConnectImmediately:NO]; +} + +- (void)checkForRemoteServerAndConnectImmediately:(BOOL)connectImmediately { ITDebugLog(@"Checking for remote server."); - [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:nil]; + if (!_checkingForServer) { + if (!_serverCheckLock) { + _serverCheckLock = [[NSLock alloc] init]; + } + [_serverCheckLock lock]; + _checkingForServer = YES; + [_serverCheckLock unlock]; + [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:[NSNumber numberWithBool:connectImmediately]]; + } } - (void)runRemoteServerCheck:(id)sender @@ -1145,11 +1155,18 @@ static MainController *sharedController; ITDebugLog(@"Remote server check running."); if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) { ITDebugLog(@"Remote server found."); - [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO]; + if ([sender boolValue]) { + [self performSelectorOnMainThread:@selector(connectToServer) withObject:nil waitUntilDone:NO]; + } else { + [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO]; + } } else { ITDebugLog(@"Remote server not found."); [self performSelectorOnMainThread:@selector(remoteServerNotFound:) withObject:nil waitUntilDone:NO]; } + [_serverCheckLock lock]; + _checkingForServer = NO; + [_serverCheckLock unlock]; [pool release]; } @@ -1162,17 +1179,20 @@ static MainController *sharedController; - (void)remoteServerNotFound:(id)sender { - [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO]; + if (![[NetworkController sharedController] isConnectedToServer]) { + [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO]; + } } - (void)networkError:(NSException *)exception { 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); + //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); + [[StatusWindowController sharedController] showNetworkErrorQueryWindow]; if ([self disconnectFromServer]) { [[PreferencesController sharedPrefs] resetRemotePlayerTextFields]; - [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:YES]; + [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO]; } else { ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!"); } @@ -1181,9 +1201,10 @@ static MainController *sharedController; - (void)reconnect { - if ([self connectToServer] == 0) { - [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:YES]; - } + /*if ([self connectToServer] == 0) { + [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO]; + }*/ + [self checkForRemoteServerAndConnectImmediately:YES]; [[StatusWindow sharedWindow] setLocked:NO]; [[StatusWindow sharedWindow] vanish:self]; [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; @@ -1278,6 +1299,7 @@ static MainController *sharedController; [statusWindowController release]; [menuController release]; [networkController release]; + [_serverCheckLock release]; [super dealloc]; }