/* AppController.m created by andrew on Fri 13-Jun-1997 */


#import <AppKit/AppKit.h>
#import "AppController.h"

#import "ResolverWindowController.h"
#import <Foundation/Foundation.h>
#import <NIAccess/NIAccess.h>
#import <NIInterface/NIInterface.h>
// for geteuid
#import <System/bsd/sys/types.h>
#import <System/bsd/unistd.h>


@interface AppController (Private)
+ (NIDomain *)_currentResolverDomain;
@end


@implementation AppController

static NSString *netinfoAuthenticationBugString = nil;
static NSString *noCurrentResolverConfigurationString = nil;

+ (void)initialize;
{
    static BOOL initialized = NO;

    [super initialize];

    if (initialized)
        return;

    initialized = YES;

    netinfoAuthenticationBugString = NSLocalizedString (@"NetInfoAuthenticationBug", user has hit up against the netinfo authentication crasher);
    noCurrentResolverConfigurationString = NSLocalizedString (@"NoCurrentResolverConfiguration", no existing resolver directory was found in NetInfo);
}

- (void)showConfigurationPanel:sender;
{
    [configurationPanel makeKeyAndOrderFront:sender];
}

- (void)showHelpWindow:sender;
{
    [helpPanel makeKeyAndOrderFront:sender];
}

- (void)openResolver:sender;
{
    NIDomainPanel *domainPanel;
    NIDomain *domain;
    NSEnumerator *enumerator;

    domainPanel = [NIDomainPanel domainPanel];
    [domainPanel setAllowsMultipleSelection:YES];
    if (![domainPanel runModalForDomain:[NIDomain localDomain]])
        return;

    enumerator = [[domainPanel domains] objectEnumerator];
    while ((domain = [enumerator nextObject]))
        [[ResolverWindowController resolverWindowControllerForDomain:domain] display];
}

- (void)openFirstResolver:sender;
{
    NIDomain *domain;

    domain = [[self class] _currentResolverDomain];
    if (!domain) {
        NSRunAlertPanel (nil, noCurrentResolverConfigurationString, nil, nil, nil);
        return;
    }

    [[ResolverWindowController resolverWindowControllerForDomain:domain] display];
}

- (void)openLocalResolver:sender;
{
    [[ResolverWindowController resolverWindowControllerForLocalDomain] display];
}

- (void)openRootResolver:sender;
{
    [[ResolverWindowController resolverWindowControllerForRootDomain] display];
}

@end


@implementation AppController (Private)

+ (NIDomain *)_currentResolverDomain;
{
    return [[[NINetInfo netinfo] firstDirectoryWithPath:@"/locations/resolver"] domain];
}

@end


@implementation AppController (NSApplicationDelegate)

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
{
    NSBundle *mainBundle;
    NSString *helpfilePath;
    NSDictionary *infoDictionary;

    mainBundle = [NSBundle mainBundle];
    helpfilePath = [mainBundle pathForResource:@"OmniDNSHelper.README" ofType:@"rtf"];
    infoDictionary = [mainBundle infoDictionary];
    [versionField setStringValue:[infoDictionary objectForKey:@"applicationVersionString"]];
    [helpTextView readRTFDFromFile:helpfilePath];
    [self showConfigurationPanel:nil];
    if ([[infoDictionary objectForKey:@"useNetInfoAuthenticationHack"] isEqualToString:@"YES"])
        if (([[NIDomain localDomain] isRootDomain]) && (geteuid() != 0))
            NSRunAlertPanel(nil, netinfoAuthenticationBugString, nil, nil, nil);
}

- (BOOL)applicationShouldTerminate:(NSApplication *)application;
{
    return [ResolverWindowController applicationShouldTerminate:application];
}

@end
