// Copyright 1997-2000 Omni Development, Inc.  All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.

#import "CDDBPreferences.h"
#import "CDDBDatabase.h"

NSString *CDDBServerAutoQueryDefaultKey    = @"CDDBAutoQuery";
NSString *CDDBServerProtocolDefaultKey     = @"CDDBProtocol";
NSString *CDDBServerHostNameDefaultKey     = @"CDDBServerHostName";
NSString *CDDBServerPortDefaultKey         = @"CDDBServerPort";
NSString *CDDBHTTPFormPathDefaultKey       = @"CDDBHTTPFormPath";
NSString *CDDBHTTPUseProxyDefaultKey       = @"CDDBHTTPUseProxyServer";
NSString *CDDBHTTPProxyHostNameDefaultKey  = @"CDDBHTTPProxyServerHostName";
NSString *CDDBHTTPProxyPortDefaultKey      = @"CDDBHTTPProxyPort";

NSString *CDDDCDDBPProtocolName = @"cddbp";
NSString *CDDDHTTPProtocolName  = @"http";

@interface CDDBPreferences (PrivateAPI)

- (NSArray *) _cddbServers;

@end

@implementation CDDBPreferences

- (IBAction) selectServer: (id) sender;
{
    unsigned int   row;
    NSArray       *servers;
    NSDictionary  *server;
    int            ret;
    NSString      *protocol;
    
    [cddbServerListWindow center];
    [cddbServerListWindow orderFront: nil];

    ret = [NSApp runModalForWindow: cddbServerListWindow];
    [cddbServerListWindow orderOut: nil];

    if (!ret)
        // cancel
        return;

    if ([cddbServerListTableView numberOfSelectedRows] != 1)
        return;

    row = [cddbServerListTableView selectedRow];
    servers = [self _cddbServers];
    if (row >= [servers count])
        return;

    server = [servers objectAtIndex: row];
    [defaults setObject: [server objectForKey: CDDBServerHostNameKey]
                 forKey: CDDBServerHostNameDefaultKey];
    [defaults setInteger: [[server objectForKey: CDDBServerPortKey] intValue]
                  forKey: CDDBServerPortDefaultKey];

    protocol = [server objectForKey: CDDBServerProtocolKey];
    [defaults setObject: protocol forKey: CDDBServerProtocolDefaultKey];

    if ([protocol isEqualToString: CDDDHTTPProtocolName])
        [defaults setObject: [server objectForKey: CDDBServerFormPathKey]
                     forKey: CDDBHTTPFormPathDefaultKey];
    else
        [defaults setObject: @"" forKey: CDDBHTTPFormPathDefaultKey];

    [self updateUI];
}

- (IBAction) selectServerOk: (id) sender;
{
    [NSApp stopModalWithCode: 1];
}

- (IBAction) selectServerCancel: (id) sender;
{
    [NSApp stopModalWithCode: 0];
}

- (IBAction) refreshServerList: (id) sender;
{
    [_cddbServers release];
    _cddbServers = nil;

    [cddbServerListTableView deselectAll: nil];
    [cddbServerListTableView reloadData];
}

//
// OAPreferenceClient subclass methods
//

- (void)updateUI;
{
    NSString *protocol;
    BOOL      useProxy, autoQuery;
    BOOL      httpEnabled, proxyEnabled;

    autoQuery = [defaults boolForKey: CDDBServerAutoQueryDefaultKey];
    [cddbAutoQueryButton setState: autoQuery];

    [cddbServerHostNameField setStringValue: [defaults objectForKey: CDDBServerHostNameDefaultKey]];
    [cddbServerPortField setIntValue: [defaults integerForKey: CDDBServerPortDefaultKey]];

    protocol = [defaults objectForKey: CDDBServerProtocolDefaultKey];
    [cddbProtocolPopUpButton selectItemWithTitle: protocol];

    [httpFormPathField setStringValue: [defaults objectForKey: CDDBHTTPFormPathDefaultKey]];

    useProxy = [defaults boolForKey: CDDBHTTPUseProxyDefaultKey];
    [useHTTPProxyButton setState: useProxy];

    [httpProxyHostNameField setStringValue: [defaults objectForKey: CDDBHTTPProxyHostNameDefaultKey]];
    [httpProxyPortField setStringValue: [defaults objectForKey: CDDBHTTPProxyPortDefaultKey]];

    if ([protocol isEqualToString: CDDDHTTPProtocolName]) {
        httpEnabled = YES;
        proxyEnabled = useProxy;
    } else {
        httpEnabled = NO;
        proxyEnabled = NO;
    }

    [httpFormPathField setEditable: httpEnabled];
    [httpFormPathField setEnabled: httpEnabled];
    [useHTTPProxyButton setEnabled: httpEnabled];

    [httpProxyHostNameField setEditable: proxyEnabled];
    [httpProxyHostNameField setEnabled: proxyEnabled];
    [httpProxyPortField setEditable: proxyEnabled];
    [httpProxyPortField setEnabled: proxyEnabled];
        
    [super updateUI];
}

- (void)setValueForSender:(id)sender;
{
    if (sender == cddbAutoQueryButton) {
        [defaults setBool: [cddbAutoQueryButton state]
                   forKey: CDDBServerAutoQueryDefaultKey];
    } if (sender == cddbServerHostNameField) {
        [defaults setObject: [cddbServerHostNameField stringValue]
                     forKey: CDDBServerHostNameDefaultKey];
    } else if (sender == cddbServerPortField) {
        [defaults setInteger: [CDDBServerPortDefaultKey intValue]
                      forKey: CDDBServerPortDefaultKey];
    } else if (sender == cddbProtocolPopUpButton) {
        [defaults setObject: [cddbProtocolPopUpButton titleOfSelectedItem]
                     forKey: CDDBServerProtocolDefaultKey];
    } else if (sender == httpFormPathField) {
        [defaults setObject: [httpFormPathField stringValue]
                     forKey: CDDBHTTPFormPathDefaultKey];
    } else if (sender == useHTTPProxyButton) {
        [defaults setBool: [useHTTPProxyButton state]
                   forKey: CDDBHTTPUseProxyDefaultKey];
    } else if (sender == httpProxyHostNameField) {
        [defaults setObject: [httpProxyHostNameField stringValue]
                     forKey: CDDBHTTPProxyHostNameDefaultKey];
    } else if (sender == httpProxyPortField) {
        [defaults setObject: [httpProxyPortField stringValue]
                     forKey: CDDBHTTPProxyPortDefaultKey];
    }

    [self updateUI];
}


//
// NSTableView dataSource methods
//
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
{
    if (tableView == cddbServerListTableView)
        return [[self _cddbServers] count];
    return 0;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
{
    if (tableView == cddbServerListTableView) {
        NSArray      *servers;
        NSDictionary *server;

        servers = [self _cddbServers];
        if (row >= [servers count])
            return nil;
        server = [servers objectAtIndex: row];

        if (tableColumn == cddbServerHostNameTableColumn)
            return [server objectForKey: CDDBServerHostNameKey];
        else if (tableColumn == cddbServerPortTableColumn)
            return [server objectForKey: CDDBServerPortKey];
        else if (tableColumn == cddbServerDescriptionTableColumn)
            return [server objectForKey: CDDBServerDescriptionKey];
        else if (tableColumn == cddbServerProtocolTableColumn)
            return [server objectForKey: CDDBServerProtocolKey];
    }

    return nil;
}

@end

@implementation CDDBPreferences (PrivateAPI)

- (NSArray *) _cddbServers;
{
    if (!_cddbServers) {
        NS_DURING {
            CDDBDatabase *database;

            database = [[[CDDBDatabase alloc] init] autorelease];
            [database connect];
            _cddbServers = [[database serverList] retain];
            [database disconnect];
        } NS_HANDLER {
            NSRunAlertPanel(@"CD Database Error", [localException reason], @"Ok", nil, nil);
            _cddbServers = [NSArray array];
        } NS_ENDHANDLER;
    }

    return _cddbServers;
}

@end
