Fixing the bug preventing iPod playlists from showing in the menu
[MenuTunes.git] / PreferencesController.m
index 5f38b44..01ca498 100755 (executable)
@@ -98,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;
 }
@@ -125,12 +127,43 @@ static PreferencesController *prefs = nil;
 #pragma mark INSTANCE METHODS
 /*************************************************************************/
 
+- (BOOL)showPasswordPanel
+{
+    [passwordPanel setLevel:NSStatusWindowLevel];
+    [passwordPanelOKButton setTitle:@"Connect"];
+    [passwordPanelTitle setStringValue:@"Password Required"];
+    [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"Please enter a password for access to the MenuTunes player named %@ at %@.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
+    [passwordPanel center];
+    [passwordPanel setLevel:NSStatusWindowLevel];
+    [passwordPanel makeKeyAndOrderFront:nil];
+    if ([NSApp runModalForWindow:passwordPanel]) {
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
+- (BOOL)showInvalidPasswordPanel
+{
+    [passwordPanel setLevel:NSStatusWindowLevel];
+    [passwordPanelOKButton setTitle:@"Retry"];
+    [passwordPanelTitle setStringValue:@"Invalid Password"];
+    [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 center];
+    [passwordPanel setLevel:NSStatusWindowLevel];
+    [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.
+    if (!myItems) {  // If menu array does not exist yet, then the window hasn't been setup.
         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
@@ -199,14 +232,15 @@ static PreferencesController *prefs = nil;
         //Set the server password
         const char *instring = [[sender stringValue] UTF8String];
         const char *password = "password";
-        unsigned char result;
+        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)];
+        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;
@@ -218,16 +252,34 @@ static PreferencesController *prefs = nil;
         [nameTextField setEnabled:NO];
         [selectSharedPlayerButton setEnabled:state];
         
-        if (state) {
-            [controller connectToServer];
+        if (state && ([controller connectToServer] == 1)) {
+            [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 ) {
-        //Do nothing on table view click
+        //If no player is selected in the table view, turn off OK button.
+        if ([sender clickedRow] == -1 ) {
+            [sharingPanelOKButton setEnabled:NO];
+        } else {
+            [sharingPanelOKButton setEnabled:YES];
+        }
     } else if ( [sender tag] == 5051 ) {
         [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
     } else if ( [sender tag] == 5060 ) {
+        //Set OK button state
+        if (([selectPlayerBox contentView] == zeroConfView && [sharingTableView selectedRow] == -1) ||
+            ([selectPlayerBox contentView] == manualView && [[hostTextField stringValue] length] == 0)) {
+            [sharingPanelOKButton setEnabled:NO];
+        } else {
+            [sharingPanelOKButton setEnabled:YES];
+        }
         //Show selection sheet
         [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
     } else if ( [sender tag] == 5100 ) {
@@ -236,22 +288,29 @@ static PreferencesController *prefs = nil;
             NSRect frame = [selectPlayerSheet frame];
             frame.origin.y -= 58;
             frame.size.height = 273;
+            if ([sharingTableView selectedRow] == -1) {
+                [sharingPanelOKButton setEnabled:NO];
+            }
             [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];
+            if ([[hostTextField stringValue] length] == 0) {
+                [sharingPanelOKButton setEnabled:NO];
+            } else {
+                [sharingPanelOKButton setEnabled:YES];
+            }
             [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"];
+        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];
@@ -265,21 +324,35 @@ static PreferencesController *prefs = nil;
         [NSApp endSheet:selectPlayerSheet];
         [selectPlayerSheet orderOut:nil];
         
+        [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]] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
+                [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]) {
+        if ([controller connectToServer] == 1) {
             [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];
 }
@@ -291,6 +364,8 @@ static PreferencesController *prefs = nil;
     if ( [sender tag] == 2010) {
         [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
         [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
+        [sw setHorizontalPosition:[sender selectedColumn]];
+        [sw setVerticalPosition:[sender selectedRow]];
         // update the window's position here
     } else if ( [sender tag] == 2020) {
         // update screen selection
@@ -308,7 +383,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]];
         }
 
@@ -444,6 +518,17 @@ static PreferencesController *prefs = nil;
     }
 }
 
+- (void)resetRemotePlayerTextFields
+{
+    if ([[NetworkController sharedController] isConnectedToServer]) {
+        [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
+        [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
+    } else {
+        [selectedPlayerTextField setStringValue:@"No shared player selected."];
+        [locationTextField setStringValue:@"-"];
+    }
+}
+
 /*************************************************************************/
 #pragma mark -
 #pragma mark HOTKEY SUPPORT METHODS
@@ -726,6 +811,21 @@ static PreferencesController *prefs = nil;
     [(MainController *)controller closePreferences]; 
 }
 
+/*************************************************************************/
+#pragma mark -
+#pragma mark NSTextField DELEGATE METHODS
+/*************************************************************************/
+
+- (void)controlTextDidChange:(NSNotification*)note
+{
+    if ([note object] == hostTextField) {
+        if ([[hostTextField stringValue] length] == 0) {
+            [sharingPanelOKButton setEnabled:NO];
+        } else {
+            [sharingPanelOKButton setEnabled:YES];
+        }
+    }
+}
 
 /*************************************************************************/
 #pragma mark -
@@ -795,11 +895,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];
     }
 }