+- (BOOL)disconnectFromServer
+{
+ ITDebugLog(@"Disconnecting from shared remote.");
+ //Disconnect
+ [currentRemote release];
+ currentRemote = [remoteArray objectAtIndex:0];
+ [networkController disconnect];
+
+ if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
+ [self applicationLaunched:nil];
+ } else {
+ [self applicationTerminated:nil];
+ }
+ if (refreshTimer) {
+ [self timerUpdate];
+ };
+ return YES;
+}
+
+- (void)checkForRemoteServer
+{
+ [self checkForRemoteServerAndConnectImmediately:NO];
+}
+
+- (void)checkForRemoteServerAndConnectImmediately:(BOOL)connectImmediately
+{
+ ITDebugLog(@"Checking for remote server.");
+ 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
+{
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ ITDebugLog(@"Remote server check running.");
+ if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
+ ITDebugLog(@"Remote server found.");
+ 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];
+}
+
+- (void)remoteServerFound:(id)sender
+{
+ if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
+ [self invalidateStatusWindowUpdateTimer];
+ [[StatusWindowController sharedController] showReconnectQueryWindow];
+ }
+}
+
+- (void)remoteServerNotFound:(id)sender
+{
+ 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);
+ [self invalidateStatusWindowUpdateTimer];
+ [[StatusWindowController sharedController] showNetworkErrorQueryWindow];
+ if ([self disconnectFromServer]) {
+ [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
+ [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
+ } else {
+ ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
+ }
+ }
+}
+
+- (void)reconnect
+{
+ /*if ([self connectToServer] == 0) {
+ [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
+ }*/
+ [self checkForRemoteServerAndConnectImmediately:YES];
+ [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
+ [[StatusWindow sharedWindow] vanish:self];
+ [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
+}
+
+- (void)cancelReconnect
+{
+ [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
+ [[StatusWindow sharedWindow] vanish:self];
+ [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
+}
+