X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/92cf4bdca5f1a76e6feec10a0d9ea32b51f286e8..7c8b58558998c9369bbf1953c49a883eabea5e73:/MainController.m diff --git a/MainController.m b/MainController.m index f47c01b..fbeae9d 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 @@ -13,7 +14,6 @@ - (ITMTRemote *)loadRemote; - (void)timerUpdate; - (void)setLatestSongIdentifier:(NSString *)newIdentifier; -- (void)showCurrentTrackInfo; - (void)applicationLaunched:(NSNotification *)note; - (void)applicationTerminated:(NSNotification *)note; @end @@ -38,6 +38,7 @@ static MainController *sharedController; sharedController = self; remoteArray = [[NSMutableArray alloc] initWithCapacity:1]; + [[PreferencesController sharedPrefs] setController:self]; statusWindowController = [StatusWindowController sharedController]; menuController = [[MenuController alloc] init]; df = [[NSUserDefaults standardUserDefaults] retain]; @@ -61,8 +62,10 @@ static MainController *sharedController; networkController = [[NetworkController alloc] init]; if ([df boolForKey:@"enableSharing"]) { [self setServerStatus:YES]; - } else if ([df boolForKey:@"useSharedPlayer"] && [df boolForKey:@"alwaysUseSharedPlayer"]) { - [self connectToServer]; + } else if ([df boolForKey:@"useSharedPlayer"]) { + if ([self connectToServer] == 0) { + [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES]; + } } //Setup for notification of the remote player launching or quitting @@ -240,7 +243,7 @@ static MainController *sharedController; - (BOOL)songIsPlaying { - NSString *identifier; + NSString *identifier = nil; NS_DURING identifier = [[self currentRemote] playerStateUniqueIdentifier]; NS_HANDLER @@ -251,7 +254,7 @@ static MainController *sharedController; - (BOOL)radioIsPlaying { - ITMTRemotePlayerPlaylistClass class; + ITMTRemotePlayerPlaylistClass class = nil; NS_DURING class = [[self currentRemote] currentPlaylistClass]; NS_HANDLER @@ -262,7 +265,7 @@ static MainController *sharedController; - (BOOL)songChanged { - NSString *identifier; + NSString *identifier = nil; NS_DURING identifier = [[self currentRemote] playerStateUniqueIdentifier]; NS_HANDLER @@ -287,6 +290,9 @@ static MainController *sharedController; { if ([networkController isConnectedToServer]) { [statusItem setMenu:[menuController menu]]; + if ([[networkController networkObject] remote] == nil) { + [self networkError:nil]; + } } if ( [self songChanged] && (timerUpdating != YES) ) { @@ -469,10 +475,14 @@ static MainController *sharedController; - (void)showPreferences { ITDebugLog(@"Show preferences."); - [[PreferencesController sharedPrefs] setController:self]; [[PreferencesController sharedPrefs] showPrefsWindow:self]; } +- (void)showTestWindow +{ + [self showCurrentTrackInfo]; +} + - (void)quitMenuTunes { ITDebugLog(@"Quitting MenuTunes."); @@ -645,8 +655,8 @@ static MainController *sharedController; - (void)showCurrentTrackInfo { - ITMTRemotePlayerSource source; - NSString *title; + ITMTRemotePlayerSource source = 0; + NSString *title = nil; NSString *album = nil; NSString *artist = nil; NSString *time = nil; @@ -692,8 +702,8 @@ static MainController *sharedController; } if ( [df boolForKey:@"showTrackNumber"] ) { - int trackNo; - int trackCount; + int trackNo = 0; + int trackCount = 0; NS_DURING trackNo = [[self currentRemote] currentSongTrack]; @@ -709,7 +719,7 @@ static MainController *sharedController; } if ( [df boolForKey:@"showTrackRating"] ) { - float currentRating; + float currentRating = 0; NS_DURING currentRating = [[self currentRemote] currentSongRating]; @@ -737,8 +747,7 @@ static MainController *sharedController; - (void)showUpcomingSongs { - int numSongs; - + int numSongs = 0; NS_DURING numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]]; NS_HANDLER @@ -748,8 +757,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; @@ -759,6 +768,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.")]]; @@ -940,38 +953,93 @@ 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]; + if (result == 1) { + [[PreferencesController sharedPrefs] resetRemotePlayerTextFields]; + currentRemote = [[[networkController networkObject] remote] retain]; [refreshTimer invalidate]; - return YES; + 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; + } 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; } } - (BOOL)disconnectFromServer { + ITDebugLog(@"Disconnecting from shared remote."); //Disconnect + [currentRemote release]; currentRemote = [remoteArray objectAtIndex:0]; [networkController disconnect]; [self timerUpdate]; return YES; } +- (void)checkForRemoteServer:(NSTimer *)timer +{ + ITDebugLog(@"Checking for remote server."); + if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) { + ITDebugLog(@"Remote server found."); + [timer invalidate]; + if (![networkController isServerOn] && ![networkController isConnectedToServer]) { + [[StatusWindowController sharedController] showReconnectQueryWindow]; + } + } else { + ITDebugLog(@"Remote server not found."); + } +} + - (void)networkError:(NSException *)exception { ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]); - NSRunAlertPanel(@"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]) { - } else { - ITDebugLog(@"CRITICAL ERROR DISCONNECTING!"); + 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!"); + } } } +- (void)reconnect +{ + if ([self connectToServer] == 0) { + [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES]; + } + [[StatusWindow sharedWindow] setLocked:NO]; + [[StatusWindow sharedWindow] vanish:self]; + [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; +} + +- (void)cancelReconnect +{ + [[StatusWindow sharedWindow] setLocked:NO]; + [[StatusWindow sharedWindow] vanish:self]; + [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES]; +} + /*************************************************************************/ #pragma mark - #pragma mark WORKSPACE NOTIFICATION HANDLERS @@ -1010,6 +1078,17 @@ static MainController *sharedController; refreshTimer = nil; [self clearHotKeys]; playerRunningState = ITMTRemotePlayerNotRunning; + + if ([df objectForKey:@"ShowPlayer"] != nil) { + ITHotKey *hotKey; + ITDebugLog(@"Setting up show player hot key."); + hotKey = [[ITHotKey alloc] init]; + [hotKey setName:@"ShowPlayer"]; + [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]]; + [hotKey setTarget:self]; + [hotKey setAction:@selector(showPlayer)]; + [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; + } } NS_HANDLER [self networkError:localException]; @@ -1024,8 +1103,8 @@ static MainController *sharedController; - (void)applicationWillTerminate:(NSNotification *)note { - [self clearHotKeys]; [networkController stopRemoteServerSearch]; + [self clearHotKeys]; [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; } @@ -1046,5 +1125,4 @@ static MainController *sharedController; [super dealloc]; } - @end \ No newline at end of file