More bugfixes in networking. Added password panels. Fixed bug with 0:60 time.
[MenuTunes.git] / PreferencesController.m
index 01820ab..4141086 100755 (executable)
@@ -1,10 +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>
@@ -93,6 +98,8 @@ static PreferencesController *prefs = nil;
                                                        nil];
         hotKeysDictionary = [[NSMutableDictionary alloc] init];
         controller = nil;
+        
+        [self setupWindow];  // Load in the nib, and perform any initial setup.
     }
     return self;
 }
@@ -120,12 +127,37 @@ static PreferencesController *prefs = nil;
 #pragma mark INSTANCE METHODS
 /*************************************************************************/
 
+- (BOOL)showPasswordPanel
+{
+    [passwordPanel setLevel:NSStatusWindowLevel];
+    [passwordPanelOKButton setTitle:@"OK"];
+    [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"Please enter a password for access to the MenuTunes player named %@ at %@.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
+    [passwordPanel makeKeyAndOrderFront:nil];
+    if ([NSApp runModalForWindow:passwordPanel]) {
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
+- (BOOL)showInvalidPasswordPanel
+{
+    [passwordPanel setLevel:NSStatusWindowLevel];
+    [passwordPanelOKButton setTitle:@"Retry"];
+    [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"The password entered for access to the MenuTunes player named %@ at %@ is invalid.  Please provide a new password.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
+    [passwordPanel makeKeyAndOrderFront:nil];
+    if ([NSApp runModalForWindow:passwordPanel]) {
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
 - (IBAction)showPrefsWindow:(id)sender
 {
     ITDebugLog(@"Showing preferences window.");
     if (! window) {  // If window does not exist yet, then the nib hasn't been loaded.
         ITDebugLog(@"Window doesn't exist, initial setup.");
-        [self setupWindow];  // Load in the nib, and perform any initial setup.
         [self setupCustomizationTables];  // Setup the DnD manu config tables.
         [self setupMenuItems];  // Setup the arrays of menu items
         [self setupUI]; // Sets up additional UI
@@ -181,31 +213,54 @@ static PreferencesController *prefs = nil;
         [df setBool:state forKey:@"enableSharing"];
         //Disable/enable the use of shared player options
         [useSharedMenuTunesCheckbox setEnabled:!state];
-        [sharePasswordCheckbox setEnabled:!state];
-        [sharePasswordTextField 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 ) {
-        [df setObject:[sender stringValue] forKey:@"sharingPassword"];
+        //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"];
         //Disable/enable the use of sharing options
         [shareMenuTunesCheckbox setEnabled:!state];
-        [sharePasswordCheckbox setEnabled:!state];
-        [sharePasswordTextField setEnabled:!state];
+        [usePasswordCheckbox setEnabled:NO];
+        [passwordTextField setEnabled:NO];
+        [nameTextField setEnabled:NO];
+        [selectSharedPlayerButton setEnabled:state];
         
-        if (state) {
-            [controller connectToServer];
+        if (state && [controller connectToServer]) {
+            [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
+            [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
         } else {
-            [controller disconnectFromServer];
+            [selectedPlayerTextField setStringValue:@"No shared player selected."];
+            [locationTextField setStringValue:@"-"];
+            if ([[NetworkController sharedController] isConnectedToServer]) {
+                [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];
@@ -215,15 +270,22 @@ static PreferencesController *prefs = nil;
             NSRect frame = [selectPlayerSheet frame];
             frame.origin.y -= 58;
             frame.size.height = 273;
-            [selectPlayerSheet setFrame:frame display:YES animate:YES];
             [selectPlayerBox setContentView:zeroConfView];
-        } else if ([selectPlayerBox contentView] != manualView) {
+            [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;
-            [selectPlayerSheet setFrame:frame display:YES animate:YES];
+            //[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];
@@ -237,16 +299,35 @@ static PreferencesController *prefs = nil;
         [NSApp endSheet:selectPlayerSheet];
         [selectPlayerSheet orderOut: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 {
-            [useSharedMenuTunesCheckbox setState:NSOnState];
-        }
+        [self changeSharingSetting:clientPasswordTextField];
         
         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]] addresses] objectAtIndex:0] 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);
         }
+    } else if ( [sender tag] == 6010 ) {
+        //Cancel password entry
+        [passwordPanel orderOut:nil];
+        [NSApp stopModalWithCode:0];
+    } else if ( [sender tag] == 6020 ) {
+        //OK password entry, retry connect
+        const char *instring = [[passwordPanelTextField stringValue] UTF8String];
+        unsigned char *result;
+        result = SHA1(instring, strlen(instring), NULL);
+        [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
+        [passwordPanel orderOut:nil];
+        [NSApp stopModalWithCode:1];
     }
     [df synchronize];
 }
@@ -275,7 +356,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]];
         }
 
@@ -549,6 +629,7 @@ static PreferencesController *prefs = nil;
     NSMutableDictionary *loginwindow;
     NSMutableArray *loginarray;
     NSEnumerator *loginEnum, *keyArrayEnum;
+    NSString *serverName;
     id anItem;
     
     ITDebugLog(@"Setting up preferences UI.");
@@ -609,22 +690,39 @@ static PreferencesController *prefs = nil;
         [shareMenuTunesCheckbox setState:NSOnState];
         [useSharedMenuTunesCheckbox setEnabled:NO];
         [selectSharedPlayerButton setEnabled:NO];
-        [hostTextField setEnabled:NO];
+        [passwordTextField setEnabled:YES];
+        [usePasswordCheckbox setEnabled:YES];
+        [nameTextField setEnabled:YES];
     } else if ([df boolForKey:@"useSharedPlayer"]) {
         [useSharedMenuTunesCheckbox setState:NSOnState];
         [shareMenuTunesCheckbox setEnabled:NO];
-        [sharePasswordCheckbox setEnabled:NO];
-        [sharePasswordTextField 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];
-    [sharePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
-    //[sharePasswordTextField setStringValue:@""]; //DO THIS LATER
+    [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
@@ -744,11 +842,7 @@ static PreferencesController *prefs = nil;
             return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
         }
     } else {
-        if ([[aTableColumn identifier] isEqualToString:@"name"]) {
-            return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] objectForKey:@"name"];
-        } else {
-            return @"X";
-        }
+        return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
     }
 }