X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/d11efc8d287ca1643e9ac6a7b561cad7cd659bda..a1367a46347952261b0e04a09dc061695a6511d0:/MainController.m?ds=inline diff --git a/MainController.m b/MainController.m index 2f4f820..1c7905b 100755 --- a/MainController.m +++ b/MainController.m @@ -2,6 +2,7 @@ #import "MenuController.h" #import "PreferencesController.h" #import "NetworkController.h" +#import "NetworkObject.h" #import #import #import @@ -41,6 +42,7 @@ static MainController *sharedController; statusWindowController = [StatusWindowController sharedController]; menuController = [[MenuController alloc] init]; df = [[NSUserDefaults standardUserDefaults] retain]; + [[PreferencesController sharedPrefs] setController:self]; timerUpdating = NO; blinged = NO; } @@ -62,7 +64,7 @@ static MainController *sharedController; if ([df boolForKey:@"enableSharing"]) { [self setServerStatus:YES]; } else if ([df boolForKey:@"useSharedPlayer"]) { - if (![self connectToServer]) { + if ([self connectToServer] == 0) { [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES]; } } @@ -471,7 +473,6 @@ static MainController *sharedController; - (void)showPreferences { ITDebugLog(@"Show preferences."); - [[PreferencesController sharedPrefs] setController:self]; [[PreferencesController sharedPrefs] showPrefsWindow:self]; } @@ -740,7 +741,6 @@ static MainController *sharedController; - (void)showUpcomingSongs { int numSongs; - NS_DURING numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]]; NS_HANDLER @@ -750,8 +750,8 @@ static MainController *sharedController; ITDebugLog(@"Showing upcoming songs status window."); NS_DURING if (numSongs > 0) { - NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5]; int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"]; + NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance]; int curTrack = [[self currentRemote] currentSongIndex]; int i; @@ -761,6 +761,10 @@ static MainController *sharedController; } } + if ([songList count] == 0) { + [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]; + } + [statusWindowController showUpcomingSongsWindowWithTitles:songList]; } else { [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]]; @@ -942,19 +946,27 @@ static MainController *sharedController; } } -- (BOOL)connectToServer +- (int)connectToServer { + int result; ITDebugLog(@"Attempting to connect to shared remote."); + result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]; //Connect - if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) { - currentRemote = [networkController sharedRemote]; - [refreshTimer invalidate]; + if (result == 1) { + [[PreferencesController sharedPrefs] resetRemotePlayerTextFields]; + currentRemote = [[[networkController networkObject] remote] retain]; + [self timerUpdate]; ITDebugLog(@"Connection successful."); - return YES; + return 1; + } else if (result == 0) { + ITDebugLog(@"Connection failed."); + currentRemote = [remoteArray objectAtIndex:0]; + return 0; } else { + //Do something about the password being invalid ITDebugLog(@"Connection failed."); currentRemote = [remoteArray objectAtIndex:0]; - return NO; + return -1; } } @@ -962,6 +974,7 @@ static MainController *sharedController; { ITDebugLog(@"Disconnecting from shared remote."); //Disconnect + [currentRemote release]; currentRemote = [remoteArray objectAtIndex:0]; [networkController disconnect]; [self timerUpdate]; @@ -974,7 +987,9 @@ static MainController *sharedController; if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) { ITDebugLog(@"Remote server found."); [timer invalidate]; - [[StatusWindowController sharedController] showReconnectQueryWindow]; + if (![networkController isConnectedToServer]) { + [[StatusWindowController sharedController] showReconnectQueryWindow]; + } } else { ITDebugLog(@"Remote server not found."); } @@ -983,20 +998,21 @@ static MainController *sharedController; - (void)networkError:(NSException *)exception { ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]); - NSLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]); - 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); - if ([networkController isConnectedToServer] && [self disconnectFromServer]) { - if ([[exception name] isEqualToString:NSPortTimeoutException]) { + NSLog(@"%@", [exception reason]); + if ([[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); + if ([self disconnectFromServer]) { + [[PreferencesController sharedPrefs] resetRemotePlayerTextFields]; [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES]; + } else { + ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!"); } - } else { - ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!"); } } - (void)reconnect { - if (![self connectToServer]) { + if ([self connectToServer] == 0) { [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES]; } [[StatusWindow sharedWindow] setLocked:NO]; @@ -1063,8 +1079,8 @@ static MainController *sharedController; - (void)applicationWillTerminate:(NSNotification *)note { - [self clearHotKeys]; [networkController stopRemoteServerSearch]; + [self clearHotKeys]; [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; } @@ -1085,5 +1101,4 @@ static MainController *sharedController; [super dealloc]; } - @end \ No newline at end of file