You can now connect through the new NetworkObject. Password
[MenuTunes.git] / PreferencesController.m
index 85370d9..96c19e8 100755 (executable)
@@ -1,11 +1,14 @@
 #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 <netinet/in.h>
+#import <arpa/inet.h>
+#import <openssl/sha.h>
 
 #import <ITKit/ITHotKeyCenter.h>
 #import <ITKit/ITKeyCombo.h>
@@ -193,7 +196,19 @@ static PreferencesController *prefs = nil;
     } else if ( [sender tag] == 5020 ) {
         [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
     } else if ( [sender tag] == 5030 ) {
-        [df setObject:[sender stringValue] forKey:@"sharedPlayerPassword"];
+        //Set the server password
+        const char *instring = [[sender stringValue] UTF8String];
+        const char *password = "password";
+        unsigned char *result;
+        NSData *hashedPass, *passwordStringHash;
+        result = SHA1(instring, strlen(instring), NULL);
+        hashedPass = [NSData dataWithBytes:result length:strlen(result)];
+        result = SHA1(password, strlen(password), NULL);
+        passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
+        if (![hashedPass isEqualToData:passwordStringHash]) {
+            [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
+            [sender setStringValue:@"password"];
+        }
     } else if ( [sender tag] == 5040 ) {
         BOOL state = SENDER_STATE;
         [df setBool:state forKey:@"useSharedPlayer"];
@@ -210,10 +225,9 @@ static PreferencesController *prefs = nil;
             [controller disconnectFromServer];
         }
     } else if ( [sender tag] == 5050 ) {
-        if ([sender clickedRow] > -1) {
-            //Set sharedPlayerHost
-            //[df setObject:[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sender clickedRow]] objectForKey:@"ip"] forKey:@"sharedPlayerHost"];
-        }
+        //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];
@@ -225,13 +239,20 @@ static PreferencesController *prefs = nil;
             frame.size.height = 273;
             [selectPlayerBox setContentView:zeroConfView];
             [selectPlayerSheet setFrame:frame display:YES animate:YES];
-        } else if ([selectPlayerBox contentView] != manualView) {
+        } 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;
+        result = SHA1(instring, strlen(instring), NULL);
+        [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
     } else if ( [sender tag] == 5110 ) {
         //Cancel
         [NSApp endSheet:selectPlayerSheet];
@@ -253,10 +274,12 @@ static PreferencesController *prefs = nil;
             }
         }
         
-        if (![controller connectToServer]) {
-            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);
-        } else {
+        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];
@@ -286,7 +309,6 @@ static PreferencesController *prefs = nil;
         } else if ( effectTag == 2103 ) {
             [sw setEntryEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
         } else if ( effectTag == 2104 ) {
-            NSLog(@"dflhgldf");
             [sw setEntryEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
         }
 
@@ -627,6 +649,7 @@ static PreferencesController *prefs = nil;
     } 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];
@@ -639,12 +662,20 @@ static PreferencesController *prefs = nil;
     
     [selectPlayerBox setContentView:zeroConfView];
     [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
-    if ([df stringForKey:@"sharedPlayerPassword"]) {
-        [passwordTextField setStringValue:@"*************"];
+    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
@@ -764,11 +795,7 @@ static PreferencesController *prefs = nil;
             return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
         }
     } else {
-        if ([[aTableColumn identifier] isEqualToString:@"name"]) {
-            return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
-        } else {
-            return @"X";
-        }
+        return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
     }
 }