/*
 * EKDefaultsManager
 * description: a generic object for reading/writing/accessing defaults/prefs
 * history:
 *	5/19/93 [Erik Kay] - created
 *	5/23/93 [Erik Kay] - writing the defaults out
 */

/*
 * NOTE: This is probably the most questionable object in the code here.
 *	There is a lot of missing functionailty, and some of the existing
 *	stuff needs a bit of work.  I think that the basic idea has potential
 *	though, so send me your suggestions/fixes! -EK
 */

#import "EKDefaultsManager.h"

@implementation EKDefaultsManager

- init
{
    defaultsTable = [[NXStringTable alloc] init];
    return self;
}

// add a new default to the hash table
- addDefault:(const char *)key
{
    static const char *dummyStr = "";
    [defaultsTable insertKey:(void *)key value:(void *)dummyStr];
    return self;
}

// override this method to initialize a custom defaults vector
- loadDefaults
{
    const   void  *key; 
	    void  *value;
    const   char  *def;
    NXHashState  state = [defaultsTable initState]; 
    while ([defaultsTable nextState: &state key: &key value: &value])  {
	def = NXGetDefaultValue([NXApp appName],(const char *)key);
	[self setDefault:(const char *)key value:def];
    }
    return self;
}

// write out all of the defaults in the hash table
//! This is kind of bogus because *all* of the defaults are being written out
//! rather than keeping track of the ones that have changed, and simply writing
//! those out.
- writeDefaults
{
    const   void  *key; 
	    void  *value;
    NXHashState  state = [defaultsTable initState]; 
    while ([defaultsTable nextState: &state key: &key value: &value])  {
	NXWriteDefault([NXApp appName],(const char *)key,(const char *)value);
    }
    return self;
}

// Set the value of a default that is already in the hash table
// Note: override this method when you want specific actions to happen when
// a certain default is set or changed
- setDefault:(const char *)key value:(const char *)val
{
    if ([defaultsTable isKey:key])
	[defaultsTable insertKey:(void *)key value:(void *)val];
    return self;
}

// get the value for a default
- (const char *)getDefault:(const char *)key
{
    if ([defaultsTable isKey:key])
	return [defaultsTable valueForStringKey:key];
    return NULL;
}

// we manage a preferences panel (UI to set defaults), so it's only natural
// that someone might actually want to see the panel.  Here, we load the nib
// file that has the preferences UI in it.
- loadPreferencesPanel
{
    if (!prefsPanel) {
	[NXApp loadNibSection:"Preferences.nib" owner:self withNames:NO];
    }
    return self;
}

// now actually show the panel
- showPreferencesPanel:sender
{
    // make sure it's loaded first
    [self loadPreferencesPanel];
    [prefsPanel makeKeyAndOrderFront:self];
    return self;
}


@end
