/*
 *    Filename:	PrefMgr.m 
 *    Created :	Wed Jan  1 02:47:53 1992 
 *    Author  :	Vince DeMarco
 *		<vince@whatnxt.cuc.ab.ca>
 */


/* Generated by Interface Builder */

#import <stdlib.h>
#import <strings.h>

#import "PrefMgr.h"
#import "ScreenDim.h"
#import "Controller.h"
#import "EyeView.h"

#import <appkit/Application.h>
#import <appkit/Window.h>
#import <appkit/Control.h>
#import <appkit/ActionCell.h>
#import <appkit/Matrix.h>
#import <appkit/defaults.h>

@implementation PrefMgr

- updateScreenDim
{

    dimtime     = atoi(NXGetDefaultValue("SpotLight","DimTime"));
    savetype    = atoi(NXGetDefaultValue("SpotLight","DimType"));
    passwdState = atoi(NXGetDefaultValue("SpotLight","PasswdLock"));

    [timeset setIntValue:dimtime];
    [timedisplay setIntValue:(dimtime*6 <= 1) ? 1 : (dimtime*6)];
    [passwd setState: passwdState];

    if (savetype == SPOTLIGHT){
	[savetypeSelect selectCellAt:0 : 0];
    }else{
	[savetypeSelect selectCellAt:0 : 1];
    }

    return self;
}

- updateServices
{
    wrapWidth = atoi(NXGetDefaultValue("SpotLight","Width"));
    strcpy(quoteString,NXGetDefaultValue("SpotLight","Quote"));
    strcpy(sigFile,NXGetDefaultValue("SpotLight","Sigfile"));
    strcpy(rtfSigFile,NXGetDefaultValue("SpotLight","Rtfsigfile"));

    [gWrapWidth setIntValue: wrapWidth];
    [gWrapWidthfield setIntValue: wrapWidth];
    [gQuoteString setStringValue: quoteString];
    [gSigFileName setStringValue: sigFile];  
    [gRtfSigFileName setStringValue: rtfSigFile];

    return self;
}

- updateEyes
{
    eyeUpdateTime = atof(NXGetDefaultValue("SpotLight","EyeUpdateTime"));
    eyeOnState    = atoi(NXGetDefaultValue("SpotLight","EyesON"));

    [eyeUpdate setDoubleValue: eyeUpdateTime];
    [eyeUpdatefield setDoubleValue: eyeUpdateTime];
    [eyeOn setState: eyeOnState];

    return self;
}

- pref:sender
{
    if (!preferencesWindow){
	[NXApp loadNibSection:"Pref.nib" owner:self withNames:NO fromZone:[self zone]];
	[self updateScreenDim];
	[self updateServices];
	[self updateEyes];
    }

    [preferencesWindow makeKeyAndOrderFront:sender];

    return self;
}

- setEyes:anObject
{
    eyes = anObject;
    return self;
}

- okayServices:sender
{
    char value[10];

    strcpy(quoteString,[gQuoteString stringValue]);
    strcpy(sigFile,[gSigFileName stringValue]);
    strcpy(rtfSigFile, [gRtfSigFileName stringValue]);
    wrapWidth   = [gWrapWidthfield intValue];

    sprintf(value,"%d",wrapWidth);
    NXWriteDefault("SpotLight", "Width", value);
    NXWriteDefault("SpotLight", "Quote", quoteString);
    NXWriteDefault("SpotLight", "Sigfile", sigFile);
    NXWriteDefault("SpotLight", "Rtfsigfile", rtfSigFile);
    [controller setQuoteString:quoteString];
    [controller setSigFile:sigFile];
    [controller setRtfSigFile:rtfSigFile];
    [controller setWrapWidth:wrapWidth];
    return self;
}

- revertServices:sender
{
    [self updateServices];
    return self;
}


- okayDim:sender
{
    char value[10];

    passwdState    = [passwd state];
    [screenDim setpasswdOn:passwdState];
    [screenDim setSaveType:savetype];
    [screenDim setDimTime:dimtime];

    sprintf(value,"%d",passwdState);
    NXWriteDefault("SpotLight","PasswdLock",value);
    sprintf(value,"%d",dimtime);
    NXWriteDefault("SpotLight","DimTime",value);
    sprintf(value, "%d", savetype);
    NXWriteDefault("SpotLight", "DimType", value);
    return self;
}

- setScreenSaveType:sender
{
    switch ([sender selectedCol]){
	case 0: 
	    savetype = SPOTLIGHT;
	    break;
	case 1 :
	    savetype = BLACKOUT;
	    break;
	}
    [screenDim setSaveType:savetype];
    return self;
}

- revertDim:sender
{
    [self updateScreenDim];
    return self;
}


- okayEyes:sender
{
    char value[10];

    eyeUpdateTime = [eyeUpdate doubleValue];
    eyeOnState    = [eyeOn state];

    [eyes setUpdateTime: eyeUpdateTime];
    sprintf(value,"%f",eyeUpdateTime);
    NXWriteDefault("SpotLight", "EyeUpdateTime", value);
    sprintf(value,"%d",eyeOnState);
    NXWriteDefault("SpotLight", "EyesON", value);
    return self;
}

- revertEyes:sender
{
    [self updateEyes];
    return self;
}

- inspectorview:sender
{
    [currentView removeFromSuperview];
    switch ([[sender selectedCell] tag]){
     case 1:
	 [[preferencesWindow contentView] addSubview:screenDimView];
	 currentView = screenDimView;
	 break;
     case 2: 
	 [[preferencesWindow contentView] addSubview:mailHelperView];
	 currentView = mailHelperView;
	 break;
     case 3: 
	 [[preferencesWindow contentView] addSubview:eyeView];
	 currentView = eyeView;
	 break;

    }
    [[preferencesWindow contentView] display];
    return self;
}

- timeset:sender
{
    dimtime = [sender intValue];
    [timedisplay setIntValue:(dimtime*6 <= 1) ? 1 : (dimtime*6)];
    return self;
}


@end
