#import "PopUpButton.h"
#import "Defaults.h"
#import "DefaultsController.h"
#import "ModuleMgr.h"
#import "MasterController.h"

@implementation DefaultsController

- init
{
   [super init];
   [self setDocType: "defaults"];
   return self;
}

- awakeFromNib
{
   defaults = [[NXApp delegate] defaults];
   [self revert: self];
   return self;
}

- revert: sender
{
   [replyTo setStringValue: [defaults get: "ReplyTo"]];
   [sendType setPopUpTitle: [defaults get: "DefaultSendType"]];
   [enclosureType setPopUpTitle: [defaults get: "DefaultEnclosureType"]];
   [sigFile setStringValue: [defaults get: "SigFile"]];
   [autoSig setState: ((*[defaults get: "AutoSig"] == 'Y') ? 1 : 0)];
   return self;
}

- set: sender
{
   [defaults writeDB: "ReplyTo" as: [replyTo stringValue]];
   [defaults writeDB: "DefaultSendType" as: [sendType title]];
   [defaults writeDB: "DefaultEnclosureType" as: [enclosureType title]];
   [defaults writeDB: "SigFile" as: [sigFile stringValue]];
   [defaults writeDB: "AutoSig" as: ([autoSig state]? "YES" : "NO")];
   return self;
}

- setSendType:anObject
{
   id sendBundles = [[NXApp delegate] sendBundles];
   const char *key;
   id popUp = [anObject target];

   /* select first item in matrix and on button */
   sendType = anObject;

   [sendBundles initState]; 
   while (key = [sendBundles nextKey])
       [popUp addItem: (const char *)key];

   [popUp removeItemAt: 0];		     /* clear out the .nibs' item */
   return self;
}

- setEnclosureType: anObject
{
   id sendBundles = [[NXApp delegate] sendBundles];
   const char *key;
   id popUp = [anObject target];

   /* select first item in matrix and on button */
   enclosureType = anObject;

   [sendBundles initState]; 
   while (key = [sendBundles nextKey])
       [popUp addItem: (const char *)key];

   [popUp removeItemAt: 0];		     /* clear out the .nibs' item */
   return self;
}

- getSigFile: sender
{
   id op = [OpenPanel new];

   [op allowMultipleFiles: NO];
   if([op runModalForDirectory: "~/" file: (char *)0 types: (char **)0])
       [sigFile setStringValue: [op filename]];

   return self;
}

@end
