Intermediary checkin, just to mark my place and back up, in case Panther decides...
[MenuTunes.git] / PreferencesController.m
index 0ee689d..9690e0b 100755 (executable)
 #import <arpa/inet.h>
 #import <openssl/sha.h>
 
+#import <ITFoundation/ITFoundation.h>
+
 #import <ITKit/ITHotKeyCenter.h>
 #import <ITKit/ITKeyCombo.h>
 #import <ITKit/ITKeyComboPanel.h>
 #import <ITKit/ITWindowPositioning.h>
 #import <ITKit/ITKeyBroadcaster.h>
 
+#import <ITKit/ITTSWBackgroundView.h>
+#import <ITKit/ITWindowEffect.h>
 #import <ITKit/ITCutWindowEffect.h>
 #import <ITKit/ITDissolveWindowEffect.h>
 #import <ITKit/ITSlideHorizontallyWindowEffect.h>
@@ -35,6 +39,8 @@
 - (void)setupCustomizationTables;
 - (void)setupMenuItems;
 - (void)setupUI;
+- (NSArray *)effectNamesFromDictionary:(NSDictionary *)infoDict;
+- (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update;
 - (IBAction)changeMenus:(id)sender;
 - (void)setLaunchesAtLogin:(BOOL)flag;
 @end
@@ -100,6 +106,7 @@ static PreferencesController *prefs = nil;
         controller = nil;
         
         [self setupWindow];  // Load in the nib, and perform any initial setup.
+        [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
     }
     return self;
 }
@@ -162,7 +169,7 @@ static PreferencesController *prefs = nil;
 - (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 setupCustomizationTables];  // Setup the DnD manu config tables.
         [self setupMenuItems];  // Setup the arrays of menu items
@@ -184,6 +191,11 @@ static PreferencesController *prefs = nil;
     [window performSelector:@selector(makeKeyAndOrderFront:) withObject:self afterDelay:0.0];
 }
 
+- (IBAction)showTestWindow:(id)sender
+{
+    [controller showTestWindow];
+}
+
 - (IBAction)changeGeneralSetting:(id)sender
 {
     ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
@@ -252,7 +264,7 @@ 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 {
@@ -264,10 +276,22 @@ static PreferencesController *prefs = nil;
             
         }
     } 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 ) {
@@ -276,13 +300,20 @@ 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];
@@ -315,7 +346,7 @@ static PreferencesController *prefs = nil;
             }
         }
         
-        if ([controller connectToServer]) {
+        if ([controller connectToServer] == 1) {
             [useSharedMenuTunesCheckbox setState:NSOnState];
             [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
             [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
@@ -345,6 +376,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
@@ -400,10 +433,64 @@ static PreferencesController *prefs = nil;
         [sw setExitDelay:[sender floatValue]];
     } else if ( [sender tag] == 2080) {
         [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
+    } else if ( [sender tag] == 2090) {
+        
+        int setting = [sender indexOfSelectedItem];
+        
+        if ( setting == 0 ) {
+            [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundApple];
+            [backgroundColorWell  setEnabled:NO];
+            [backgroundColorPopup setEnabled:NO];
+        } else if ( setting == 1 ) {
+            [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundReadable];
+            [backgroundColorWell  setEnabled:NO];
+            [backgroundColorPopup setEnabled:NO];
+        } else if ( setting == 2 ) {
+            [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundColored];
+            [backgroundColorWell  setEnabled:YES];
+            [backgroundColorPopup setEnabled:YES];
+        }
+
+        [df setInteger:setting forKey:@"statusWindowBackgroundMode"];
+        
+    } else if ( [sender tag] == 2091) {
+        [self setCustomColor:[sender color] updateWell:NO];
+    } else if ( [sender tag] == 2092) {
+        
+        int selectedItem = [sender indexOfSelectedItem];
+        
+        if ( selectedItem == 1 ) { // An NSPopUpButton in PullDown mode uses item 0 as its title.  Its first selectable item is 1.
+            [self setCustomColor:[NSColor colorWithCalibratedRed:0.92549 green:0.686275 blue:0.0 alpha:1.0] updateWell:YES];
+        } else if ( selectedItem == 2 ) {
+            [self setCustomColor:[NSColor colorWithCalibratedRed:0.380392 green:0.670588 blue:0.0 alpha:1.0] updateWell:YES];
+        } else if ( selectedItem == 3 ) {
+            [self setCustomColor:[NSColor colorWithCalibratedRed:0.443137 green:0.231373 blue:0.619608 alpha:1.0] updateWell:YES];
+        } else if ( selectedItem == 4 ) {
+            [self setCustomColor:[NSColor colorWithCalibratedRed:0.831373 green:0.12549 blue:0.509804 alpha:1.0] updateWell:YES];
+        } else if ( selectedItem == 5 ) {
+            [self setCustomColor:[NSColor colorWithCalibratedRed:0.00784314 green:0.611765 blue:0.662745 alpha:1.0] updateWell:YES];
+        } else {
+            [self setCustomColor:[NSColor colorWithCalibratedWhite:0.15 alpha:0.70] updateWell:YES];
+        }
+
+    } else if ( [sender tag] == 2095) {
+        [sw vanish:self];
+        [df setInteger:[sender indexOfSelectedItem] forKey:@"statusWindowSizing"];
     }
+    
     [df synchronize];
 }
 
+- (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update
+{
+    [(ITTSWBackgroundView *)[[StatusWindow sharedWindow] contentView] setBackgroundColor:color];
+    [df setObject:[NSArchiver archivedDataWithRootObject:color] forKey:@"statusWindowBackgroundColor"];
+    
+    if ( update ) {
+        [backgroundColorWell setColor:color];
+    }
+}
+
 - (void)registerDefaults
 {
     BOOL found = NO;
@@ -427,18 +514,22 @@ static PreferencesController *prefs = nil;
         nil] forKey:@"menu"];
 
     [df setInteger:5 forKey:@"SongsInAdvance"];
-    // [df setBool:YES forKey:@"showName"];  // Song info will always show song title.
+//  [df setBool:YES forKey:@"showName"];  // Song info will always show song title.
     [df setBool:YES forKey:@"showArtist"];
     [df setBool:NO forKey:@"showAlbum"];
     [df setBool:NO forKey:@"showTime"];
 
-    [df setInteger:2100 forKey:@"statusWindowAppearanceEffect"];
-    [df setInteger:2101 forKey:@"statusWindowVanishEffect"];
+    [df setObject:@"ITCutWindowEffect" forKey:@"statusWindowAppearanceEffect"];
+    [df setObject:@"ITDissolveWindowEffect" forKey:@"statusWindowVanishEffect"];
     [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
     [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
     [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
+    [df setInteger:(int)ITWindowPositionBottom forKey:@"statusWindowVerticalPosition"];
+    [df setInteger:(int)ITWindowPositionLeft forKey:@"statusWindowHorizontalPosition"];
     [df setBool:YES forKey:@"showSongInfoOnChange"];
-
+    
+    [df setObject:[NSArchiver archivedDataWithRootObject:[NSColor blueColor]] forKey:@"statusWindowBackgroundColor"];
+    
     [df synchronize];
     
     loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
@@ -645,8 +736,13 @@ static PreferencesController *prefs = nil;
 {
     NSMutableDictionary *loginwindow;
     NSMutableArray *loginarray;
-    NSEnumerator *loginEnum, *keyArrayEnum;
-    NSString *serverName;
+    NSEnumerator   *loginEnum;
+    NSEnumerator   *keyArrayEnum;
+    NSString       *serverName;
+    NSData         *colorData;
+    NSArray        *effectClasses = [ITWindowEffect effectClasses];
+//  NSEnumerator   *effectEnum = [effectList objectEnumerator];
+    int selectedBGStyle;
     id anItem;
     
     ITDebugLog(@"Setting up preferences UI.");
@@ -695,13 +791,46 @@ static PreferencesController *prefs = nil;
     // Setup the positioning controls
     
     // Setup effects controls
+    // Populate the effects popups
+    [appearanceEffectPopup removeItemAtIndex:0];
+    [vanishEffectPopup     removeItemAtIndex:0];
+    [appearanceEffectPopup addItemsWithTitles:[[effectList allValues] objectsForKey:@"Name"]];  // category method
+    [vanishEffectPopup     addItemsWithTitles:[[effectList allValues] objectsForKey:@"Name"]];  // category method
+    
+    // Attempt to find the pref'd effect in the list.
+    // If it's not there, use cut/dissolve.
+
+    
     [appearanceEffectPopup selectItem:[appearanceEffectPopup itemAtIndex:[appearanceEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowAppearanceEffect"]]]];
     [vanishEffectPopup     selectItem:[vanishEffectPopup     itemAtIndex:[vanishEffectPopup     indexOfItemWithTag:[df integerForKey:@"statusWindowVanishEffect"]]]];
     [appearanceSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowAppearanceSpeed"])];
     [vanishSpeedSlider     setFloatValue:-([df floatForKey:@"statusWindowVanishSpeed"])];
     [vanishDelaySlider     setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
-    [showOnChangeCheckbox  setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
+
+    // Setup General Controls
+    selectedBGStyle = [df integerForKey:@"statusWindowBackgroundMode"];
+    [backgroundStylePopup selectItem:[backgroundStylePopup itemAtIndex:[backgroundStylePopup indexOfItemWithTag:selectedBGStyle]]];
+
+    if ( selectedBGStyle == ITTSWBackgroundColored ) {
+        [backgroundColorWell  setEnabled:YES];
+        [backgroundColorPopup setEnabled:YES];
+    } else {
+        [backgroundColorWell  setEnabled:NO];
+        [backgroundColorPopup setEnabled:NO];
+    }
+
+    colorData = [df dataForKey:@"statusWindowBackgroundColor"];
+
+    if ( colorData ) {
+        [backgroundColorWell setColor:(NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
+    } else {
+        [backgroundColorWell setColor:[NSColor blueColor]];
+    }
+    
+    [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
     
+    [windowSizingPopup selectItem:[windowSizingPopup itemAtIndex:[windowSizingPopup indexOfItemWithTag:[df integerForKey:@"statusWindowSizing"]]]];
+
     // Setup the sharing controls
     if ([df boolForKey:@"enableSharing"]) {
         [shareMenuTunesCheckbox setState:NSOnState];
@@ -742,6 +871,11 @@ static PreferencesController *prefs = nil;
     }
 }
 
+- (NSArray *)effectNamesFromDictionary:(NSDictionary *)infoDict
+{
+    
+}
+
 - (IBAction)changeMenus:(id)sender
 {
     ITDebugLog(@"Synchronizing menus");
@@ -790,6 +924,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 -
@@ -815,7 +964,7 @@ static PreferencesController *prefs = nil;
         NSString *object = [myItems objectAtIndex:rowIndex];
         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
             if ([object isEqualToString:@"showPlayer"]) {
-                NSString *string;
+                NSString *string = nil;
                 NS_DURING
                     string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
                 NS_HANDLER
@@ -836,7 +985,7 @@ static PreferencesController *prefs = nil;
         NSString *object = [availableItems objectAtIndex:rowIndex];
         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
             if ([object isEqualToString:@"showPlayer"]) {
-                NSString *string;
+                NSString *string = nil;
                 NS_DURING
                     string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
                 NS_HANDLER