/*
 * Simple default handler.
 * Do 'openDefaults()' at -appDidInit time, reference the global default
 * variables defined here, and write the values back out with 'writeDefaults()'
 * 
 * M. J. Hawley
 * mike@media-lab.mit.edu
 */
/*
    Copyright (c) 1991, 1992 by the MIT Media Laboratory
    
    This software is distributed by Michael Hawley of the MIT Media Laboratory. 
    We hope it will be useful to you.
    
    Permission to use, copy, or modify this software for educational 
    and research purposes only and without fee is hereby granted 
    provided this notice appears on all copies, and provided you send 
    us your improvements.  Any other use of this software, in original 
    or modified form, in whole or in part, requires specific permission 
    from MIT.  This software shall not be used, rewritten, or adapted 
    for use in a commercial product without first obtaining appropriate 
    licenses from MIT.  MIT makes no representations about the suitability 
    of this software for any purpose: it is provided "as is" without any 
    warranty and any risk, damage, or liability incurred through your use 
    of this software is yours alone.
    
    Michael Hawley
    MIT Media Laboratory
    20 Ames Street, 
    Cambridge, MA 02139
    mike@media-lab.mit.edu
*/
#import "Defaults.h"
#import <appkit/defaults.h>
#import <appkit/Application.h>

extern id NXApp;

@implementation Defaults

#define appname [NXApp appName]

char FirstUsed[1024] = "";
char DefaultFont[256] = "Ohlfs";
char DefaultFontSize[32] = "10.";
char FetchText[3][128] = { "", "", "" };
char Site[80] = "downwind.sprl.umich.edu";

static NXDefaultsVector DefaultV = {
    {"FirstUsed",  FirstUsed},
    {"Site",  Site},
    {"Font",  DefaultFont},
    {"FontSize", DefaultFontSize },
    {"Fetch1", FetchText[0] },
    {"Fetch2", FetchText[1] },
    {"Fetch3", FetchText[2] },
    {NULL}
};

char *
deflt(char *s){
    return (char *)NXGetDefaultValue(appname,s);
}

static id _defaults = (id)0;

+ loadDefaults {
    char *s;
    extern char *strcpy();
#define l(x,xs) s = deflt(xs); if (s && *s) strcpy(x,s)
    l(FirstUsed,"FirstUsed");
    l(Site,"Site");
    l(DefaultFont,"Font");
    l(DefaultFontSize,"FontSize");
    l(FetchText[0],"Fetch1");
    l(FetchText[1],"Fetch2");
    l(FetchText[2],"Fetch3");
    return self;
}

+ initialize {
    NXRegisterDefaults(appname, DefaultV);
    [self loadDefaults];
    return self;
}

- writeDefaults:sender {
    NXWriteDefaults(appname, DefaultV);
    return self;
}

- free {
    [self writeDefaults:self];
    [super free];
    return self;
}

void
openDefaults(){
    _defaults = [Defaults new];
}

void
writeDefaults(){
    [_defaults writeDefaults:_defaults];
}

@end
