Adding a new proxy object for the networking. It's broken right now, but
[MenuTunes.git] / PreferencesController.m
index b66520b..5f38b44 100755 (executable)
@@ -1,9 +1,15 @@
 #import "PreferencesController.h"
 #import "MainController.h"
+#import "NetworkController.h"
+#import "NetworkObject.h"
 #import "StatusWindow.h"
 #import "StatusWindowController.h"
 #import "CustomMenuTableView.h"
 
+#import <netinet/in.h>
+#import <arpa/inet.h>
+#import <openssl/sha.h>
+
 #import <ITKit/ITHotKeyCenter.h>
 #import <ITKit/ITKeyCombo.h>
 #import <ITKit/ITKeyComboPanel.h>
@@ -133,7 +139,11 @@ static PreferencesController *prefs = nil;
         [hotKeysTableView setDoubleAction:@selector(hotKeysTableViewDoubleClicked:)];
         
         //Change the launch player checkbox to the proper name
-        [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
+        NS_DURING
+            [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
+        NS_HANDLER
+            [controller networkError:localException];
+        NS_ENDHANDLER
     }
 
     [window center];
@@ -168,6 +178,112 @@ static PreferencesController *prefs = nil;
     [df synchronize];
 }
 
+- (IBAction)changeSharingSetting:(id)sender
+{
+    ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
+    if ( [sender tag] == 5010 ) {
+        BOOL state = SENDER_STATE;
+        [df setBool:state forKey:@"enableSharing"];
+        //Disable/enable the use of shared player options
+        [useSharedMenuTunesCheckbox setEnabled:!state];
+        [usePasswordCheckbox setEnabled:state];
+        [passwordTextField setEnabled:state];
+        [nameTextField setEnabled:state];
+        [selectSharedPlayerButton setEnabled:NO];
+        [controller setServerStatus:state]; //Set server status
+    } else if ( [sender tag] == 5015 ) {
+        [df setObject:[sender stringValue] forKey:@"sharedPlayerName"];
+    } else if ( [sender tag] == 5020 ) {
+        [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
+    } else if ( [sender tag] == 5030 ) {
+        //Set the server password
+        const char *instring = [[sender stringValue] UTF8String];
+        const char *password = "password";
+        unsigned char result;
+        NSData *hashedPass, *passwordStringHash;
+        SHA1(instring, strlen(instring), &result);
+        hashedPass = [NSData dataWithBytes:&result length:strlen(&result)];
+        SHA1(password, strlen(password), &result);
+        passwordStringHash = [NSData dataWithBytes:&result length:strlen(&result)];
+        if (![hashedPass isEqualToData:passwordStringHash]) {
+            [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
+        }
+    } else if ( [sender tag] == 5040 ) {
+        BOOL state = SENDER_STATE;
+        [df setBool:state forKey:@"useSharedPlayer"];
+        //Disable/enable the use of sharing options
+        [shareMenuTunesCheckbox setEnabled:!state];
+        [usePasswordCheckbox setEnabled:NO];
+        [passwordTextField setEnabled:NO];
+        [nameTextField setEnabled:NO];
+        [selectSharedPlayerButton setEnabled:state];
+        
+        if (state) {
+            [controller connectToServer];
+        } else {
+            [controller disconnectFromServer];
+        }
+    } else if ( [sender tag] == 5050 ) {
+        //Do nothing on table view click
+    } else if ( [sender tag] == 5051 ) {
+        [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
+    } else if ( [sender tag] == 5060 ) {
+        //Show selection sheet
+        [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
+    } else if ( [sender tag] == 5100 ) {
+        //Change view
+        if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
+            NSRect frame = [selectPlayerSheet frame];
+            frame.origin.y -= 58;
+            frame.size.height = 273;
+            [selectPlayerBox setContentView:zeroConfView];
+            [selectPlayerSheet setFrame:frame display:YES animate:YES];
+        } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
+            NSRect frame = [selectPlayerSheet frame];
+            frame.origin.y += 58;
+            frame.size.height = 215;
+            //[window makeFirstResponder:hostTextField];
+            [selectPlayerBox setContentView:manualView];
+            [selectPlayerSheet setFrame:frame display:YES animate:YES];
+            [hostTextField selectText:nil];
+        }
+    } else if ( [sender tag] == 5150 ) {
+        const char *instring = [[sender stringValue] UTF8String];
+        unsigned char result;
+        SHA1(instring, strlen(instring), &result);
+        [df setObject:[NSData dataWithBytes:&result length:strlen(&result)] forKey:@"connectPassword"];
+    } else if ( [sender tag] == 5110 ) {
+        //Cancel
+        [NSApp endSheet:selectPlayerSheet];
+        [selectPlayerSheet orderOut:nil];
+        if ([selectPlayerBox contentView] == manualView) {
+            [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
+        } else {
+        }
+    } else if ( [sender tag] == 5120 ) {
+        //OK, try to connect
+        [NSApp endSheet:selectPlayerSheet];
+        [selectPlayerSheet orderOut:nil];
+        
+        if ([selectPlayerBox contentView] == manualView) {
+            [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
+        } else {
+            if ([sharingTableView selectedRow] > -1) {
+                [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
+            }
+        }
+        
+        if ([controller connectToServer]) {
+            [useSharedMenuTunesCheckbox setState:NSOnState];
+            [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
+            [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
+        } else {
+            NSRunAlertPanel(@"Connection error.", @"The MenuTunes server you attempted to connect to was not responding. MenuTunes will revert back to the local player.", @"OK", nil, nil);
+        }
+    }
+    [df synchronize];
+}
+
 - (IBAction)changeStatusWindowSetting:(id)sender
 {
     StatusWindow *sw = [StatusWindow sharedWindow];
@@ -466,6 +582,7 @@ static PreferencesController *prefs = nil;
     NSMutableDictionary *loginwindow;
     NSMutableArray *loginarray;
     NSEnumerator *loginEnum, *keyArrayEnum;
+    NSString *serverName;
     id anItem;
     
     ITDebugLog(@"Setting up preferences UI.");
@@ -520,6 +637,45 @@ static PreferencesController *prefs = nil;
     [vanishSpeedSlider     setFloatValue:-([df floatForKey:@"statusWindowVanishSpeed"])];
     [vanishDelaySlider     setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
     [showOnChangeCheckbox  setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
+    
+    // Setup the sharing controls
+    if ([df boolForKey:@"enableSharing"]) {
+        [shareMenuTunesCheckbox setState:NSOnState];
+        [useSharedMenuTunesCheckbox setEnabled:NO];
+        [selectSharedPlayerButton setEnabled:NO];
+        [passwordTextField setEnabled:YES];
+        [usePasswordCheckbox setEnabled:YES];
+        [nameTextField setEnabled:YES];
+    } else if ([df boolForKey:@"useSharedPlayer"]) {
+        [useSharedMenuTunesCheckbox setState:NSOnState];
+        [shareMenuTunesCheckbox setEnabled:NO];
+        [selectSharedPlayerButton setEnabled:YES];
+    }
+    
+    [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
+    
+    serverName = [df stringForKey:@"sharedPlayerName"];
+    if (!serverName || [serverName length] == 0) {
+        serverName = @"MenuTunes Shared Player";
+    }
+    [nameTextField setStringValue:serverName];
+    
+    [selectPlayerBox setContentView:zeroConfView];
+    [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
+    if ([df dataForKey:@"sharedPlayerPassword"]) {
+        [passwordTextField setStringValue:@"password"];
+    }
+    if ([df stringForKey:@"sharedPlayerHost"]) {
+        [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
+    }
+    
+    if ([[NetworkController sharedController] isConnectedToServer]) {
+        [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
+        [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
+    } else {
+        [selectedPlayerTextField setStringValue:@"No shared player selected."];
+        [locationTextField setStringValue:@"-"];
+    }
 }
 
 - (IBAction)changeMenus:(id)sender
@@ -582,8 +738,10 @@ static PreferencesController *prefs = nil;
         return [myItems count];
     } else if (aTableView == allTableView) {
         return [availableItems count];
-    } else {
+    } else if (aTableView == hotKeysTableView) {
         return [hotKeysArray count];
+    } else {
+        return [[[NetworkController sharedController] remoteServices] count];
     }
 }
 
@@ -593,7 +751,13 @@ static PreferencesController *prefs = nil;
         NSString *object = [myItems objectAtIndex:rowIndex];
         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
             if ([object isEqualToString:@"showPlayer"]) {
-                return [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
+                NSString *string;
+                NS_DURING
+                    string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
+                NS_HANDLER
+                    [controller networkError:localException];
+                NS_ENDHANDLER
+                return string;
             }
             return NSLocalizedString(object, @"ERROR");
         } else {
@@ -608,7 +772,13 @@ static PreferencesController *prefs = nil;
         NSString *object = [availableItems objectAtIndex:rowIndex];
         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
             if ([object isEqualToString:@"showPlayer"]) {
-                return [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
+                NSString *string;
+                NS_DURING
+                    string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
+                NS_HANDLER
+                    [controller networkError:localException];
+                NS_ENDHANDLER
+                return string;
             }
             return NSLocalizedString(object, @"ERROR");
         } else {
@@ -618,12 +788,18 @@ static PreferencesController *prefs = nil;
                 return nil;
             }
         }
-    } else {
+    } else if (aTableView == hotKeysTableView) {
         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
             return [hotKeyNamesArray objectAtIndex:rowIndex];
         } else {
             return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
         }
+    } else {
+        if ([[aTableColumn identifier] isEqualToString:@"name"]) {
+            return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
+        } else {
+            return @"X";
+        }
     }
 }