// MiniSQLLoginPanel:Panel

#import "MiniSQLLoginPanel.h"
#import "MiniSQLAdaptor.h"

@implementation MiniSQLLoginPanel

- doCancel:sender
{
    [NXApp stopModal:NX_CANCELTAG];
    return self;
}

- doLogin:sender
{
    [NXApp stopModal:NX_OKTAG];
    return self;
}

- (void)setConnectionDictionary:(NSDictionary *)aDictionary
{
    NSString *hostname;
    NSString *database;
    NSString *name;
    NSString *password;
    
    [self disableFlushWindow];
    
    database = [aDictionary objectForKey:DATABASE];
    if (!database) {
    	database = @"";
    }
    
    hostname = [aDictionary objectForKey:HOSTNAME];
    if (!hostname) {
    	hostname = @"";
    }
    
    name = [aDictionary objectForKey:NAME];
    if (!name) {
    	name = @"";
    }
    
    password = [aDictionary objectForKey:PASSWORD];
    if (!password) {
    	password = @"";
    }
    
    [nameField     setStringValue:[name     cString]];
    [passwordField setStringValue:[password cString]];
    [databaseField setStringValue:[database cString]];
    [hostnameField setStringValue:[hostname cString]];
    

 // select the first empty field, or the password field if none 
 // are empty. 

    if ([database length] == 0) {
	[databaseField selectText:self];
    } else if ([hostname length] == 0) {
	[hostnameField selectText:self];
    } else if ([name length] == 0) {
	[nameField selectText:self];
    } else {
	[passwordField selectText:self];
    }

    [self reenableFlushWindow];
    [self flushWindowIfNeeded];
}


- (NSDictionary *) connectionDictionary
{
    NSString           *name;
    NSString           *password;
    NSString           *hostname;
    NSString           *database;

    NSMutableDictionary *info;

    info = [NSMutableDictionary dictionaryWithCapacity:4];

    database = [NSString stringWithCString:[databaseField stringValue]];
    if (database) {
	[info setObject:database forKey:DATABASE];
    }

    hostname = [NSString stringWithCString:[hostnameField stringValue]];
    if (hostname) {
	[info setObject:hostname forKey:HOSTNAME];
    }

    name = [NSString stringWithCString:[nameField stringValue]];
    if (name) {
	[info setObject:name forKey:NAME];
    }

    password = [NSString stringWithCString:[passwordField stringValue]];
    if (password) {
	[info setObject:password forKey:PASSWORD];
    }
    return info;
}

@end
