/*
   ODBCLoginPanel.m

   Copyright (c) 1996 NeXT Software, Inc.  All rights reserved.

   IMPORTANT:  This NeXT software is supplied to you in consideration of your agreement
   to the terms of the NeXT Software License Agreement stated in the file ODBC_LICENSE.rtf
   provided with the software.  Your use of the software is governed by such terms.
   Do not use, install, or make copies of the software if you do not agree to such terms.

   THIS SOFTWARE IS FURNISHED ON AN "AS-IS" BASIS. NeXT MAKES NO WARRANTIES OF ANY KIND,
   EITHER EXPRESS OR IMPLIED, AS TO ANY MATTER WHATSOEVER, INCLUDING WITHOUT LIMITATION
   THE CONDITION, MERCHANTABILITY, OR FITNESS FOR ANY PARTICULAR PURPOSE OF THIS SOFTWARE.
   NeXT DOES NOT ASSUME ANY LIABILITY REGARDING USE OF, OR ANY DEFECT IN, THIS SOFTWARE.
   IN NO EVENT SHALL NeXT BE LIABLE FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
   DAMAGES, EVEN IF IT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "ODBCLoginPanel.h"
#import <ODBCEOAdaptor/ODBCAdaptor.h>

@implementation ODBCLoginPanel

extern const char ODBC_VERS_NUM[];

- (void)_resetAccordingToState
{
    if([useConnectionString state]) {
        [datasourceName setEnabled:NO];
        [userName setEnabled:NO];
        [password setEnabled:NO];
        [connectionString setEnabled:YES];
    } else {
        [datasourceName setEnabled:YES];
        [userName setEnabled:YES];
        [password setEnabled:YES];
        [connectionString setEnabled:NO];
    }
}

- (void)_setConnectionDictionary:(NSDictionary *)connectionInfo
{
    id value;

    _connectionInfo = [connectionInfo mutableCopy];

    value = [connectionInfo valueForKey:dataSourceKey];
    [datasourceName setStringValue:value ? value : @""];
    [_connectionInfo removeObjectForKey:dataSourceKey];
    
    value = [connectionInfo valueForKey:userNameKey];
    [userName setStringValue:value ? value : @""];
    [_connectionInfo removeObjectForKey:userNameKey];

    value = [connectionInfo valueForKey:passwordKey];
    [password setStringValue:value ? value : @""];
    [_connectionInfo removeObjectForKey:passwordKey];

    value = [connectionInfo valueForKey:connectionStringKey];
    [connectionString setStringValue:value ? value : @""];
    [_connectionInfo removeObjectForKey:connectionStringKey];

    [useConnectionString setState:(value ? 1 : 0)];

    [self _resetAccordingToState];
}

- (NSDictionary *)_connectionDictionary
{
    id value;
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    value = [datasourceName stringValue];
    if(value && ![value isEqual:@""])
        [dictionary setObject:value forKey:dataSourceKey];

    value = [userName stringValue];
    if(value && ![value isEqual:@""])
        [dictionary setObject:value forKey:userNameKey];

    value = [password stringValue];
    if(value && ![value isEqual:@""])
        [dictionary setObject:value forKey:passwordKey];

    if([useConnectionString state]) {
        value = [connectionString stringValue];
        if(value && ![value isEqual:@""])
            [dictionary setObject:value forKey:connectionStringKey];
    }

    if(_connectionInfo) [dictionary addEntriesFromDictionary:_connectionInfo];
    return dictionary;
}

- init
{
    NSWindow *window;
    NSString *versionString = [NSString stringWithCString:ODBC_VERS_NUM];

    [super init];
    [NSBundle loadNibNamed:@"ODBCLoginPanel" owner:self];

    window = [userName window];

    if(!versionString || ![versionString length]) {
        versionString = @"Development version";
    } else {
        versionString = [NSString stringWithFormat:@"v.%@", versionString];
    }

    [window setTitle:[NSString stringWithFormat:@"%@ (%@)", [window title], versionString]];

    return self;
}

- (NSDictionary *)runPanelForAdaptor:(EOAdaptor *)adaptor validate:(BOOL)validate
{
    NSDictionary *result;
    BOOL success;

    [self _setConnectionDictionary:[adaptor connectionDictionary]];
    do {
        if ([NSApp runModalForWindow:[datasourceName window]] != NSRunContinuesResponse) {
            result = nil;
            [[datasourceName window] orderOut:nil];
            break;
        }
        [[datasourceName window] orderOut:nil];
        result = [self _connectionDictionary];

        if (!validate) break;

        success = YES;
        [adaptor setConnectionDictionary:result];
        NS_DURING
            [adaptor assertConnectionDictionaryIsValid];
        NS_HANDLER
            NSRunAlertPanel (NULL, @"%@", NULL, NULL, NULL, [localException reason]);
            [self _setConnectionDictionary:[adaptor connectionDictionary]];
            success = NO;
        NS_ENDHANDLER
    } while (!success);

    return result;
}

- (void)cancel:sender
{
    [NSApp stopModalWithCode:NSRunAbortedResponse];
}

- (void)login:sender
{
    [NSApp stopModalWithCode:NSRunContinuesResponse];
}

- (void)switch:sender
{
    [self _resetAccordingToState];
}
@end
