//*****************************************************************************
//
//	OptionsEditor.m.  
//		
//		Edit the options file contained within a Gate doc 
// 
//			by	Felipe A. Rodriguez		
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//*****************************************************************************

#import "GKdefs.h"
#import "OptionsEditor.h"
#import "options.h"
#import "Coordinator.h"



	char *devNames[] = { "cua", "cufa", "cudfa", "cub",	"cufb", "cudfb", "cuc",
							 "cufc", "cudfc", "cud", "cufd", "cudfd", NULL};


@implementation OptionsEditor

//*****************************************************************************
//
// 		init -- reset ivars
//
//*****************************************************************************

- init
{
    [super init];										
	debug = NO;
	crtscts = NO;
	lockFlag = NO;
	defaultroute = NO;
	shouldSave = NO;

    return self;
}
//*****************************************************************************
//
// 		Open file to parse and search 
//
//*****************************************************************************

- parseOptionsFile:(const char *)filePath
{
struct stat statbuf;
char **devNamePtr;

	setDelegate(self);

			// check for installed ports, add those found to device popup List
	devNamePtr = devNames;
  	if (![dvPopup isKindOf:[PopUpList class]]) 
		dvPopupList = [dvPopup target];		// get popuplist from trig button
	while(*devNamePtr)
		{
		strcpy(devName, "/dev/");
		strcat(devName, *devNamePtr);
    	if (stat(devName, &statbuf) == 0)
			[dvPopupList addItem:*devNamePtr];
		devNamePtr++;
		}
	devName[1] = 0;
	options_from_file(filePath, 0, 1);
	strcpy(path, filePath);

	if(defaultroute)		
		[[swMatrix findCellWithTag:0] setIntValue:1];
	if(lockFlag)		
		[[swMatrix findCellWithTag:1] setIntValue:1];
	if(crtscts)		
		[[swMatrix findCellWithTag:2] setIntValue:1];
	if(debug)		
		[[swMatrix findCellWithTag:3] setIntValue:1];


    return self;
}
//*****************************************************************************
//
// 		Open file to parse and search without setting GUI controls
//
//*****************************************************************************

- parseOptions:(const char *)filePath
{
	setDelegate(self);
	[self init];										// reset ivars 
	options_from_file(filePath, 0, 1);

    return self;
}
//*****************************************************************************
//
// 		defaultroute -- return status of or set from info in options file
//
//*****************************************************************************

- setDefaultRoute
{
	defaultroute = YES;

    return self;
}
- (BOOL)defaultRoute
{
	return defaultroute;
}
//*****************************************************************************
//
// 		lockFlag -- return status of or set from info in options file
//
//*****************************************************************************

- setLockFlag
{
	lockFlag = YES;

    return self;
}
- (BOOL)lockFlag
{
	return lockFlag;
}
//*****************************************************************************
//
// 		defaultroute -- return status of or set from info in options file
//
//*****************************************************************************

- setDebug
{
	debug = YES;

    return self;
}
- (BOOL)debug
{
	return debug;
}
//*****************************************************************************
//
// 		defaultroute -- return status of or set from info in options file
//
//*****************************************************************************

- setCrtscts
{
	crtscts = YES;

    return self;
}
- (BOOL)crtscts
{
	return crtscts;
}
//*****************************************************************************
//
// 		if set, GUI settings will be saved to options file when it is parsed
//
//*****************************************************************************

- (BOOL)shouldSave
{
    return shouldSave;
}
//************************************************************************
//
//		Target method of save switch
//
//************************************************************************

- setShouldSave:sender
{
	shouldSave = YES;
	setDelegate(self);
	options_from_file(path, 0, 1);
	shouldSave = NO;

    return self;
}
//************************************************************************
//
//		Target method of switches
//		check switches based on the tag assigned in IB to each switch.
//
//************************************************************************

- switch:sender
{
	switch([sender selectedTag])
		{							
		case 0: 				// defaultroute
			defaultroute = (BOOL)[sender intValue];
			[self setShouldSave:self];
			break;	
			
		case 1: 				// lock
			lockFlag = (BOOL)[sender intValue];
			[self setShouldSave:self];
			break;	

		case 2: 				// crtscts
			crtscts = (BOOL)[sender intValue];
			[self setShouldSave:self];
			break;	

		case 3: 				// debug
			debug = (BOOL)[sender intValue];
			[self setShouldSave:self];
			break;	

		default:	// parent --  vfork returns pid of the child  		
			break;
		}

    return self;
}
//*****************************************************************************
//
// 		devName -- set, return and target of 
//
//*****************************************************************************

- setDevName:(const char *)device
{
const char *sPtr;
static int i;

	sPtr = device + 5;
	if(dvPopup != nil)		// if not connected we are just parsing for port
		{
		strncpy(devName, device, MAXPATHLEN);
		if (![dvPopup isKindOf:[PopUpList class]]) 
			dvPopupList = [dvPopup target];	// get popuplist from trig button
		dvPopMtrx = [dvPopupList itemList];		// get matrix from popuplist
		for (i = 0; i < [dvPopMtrx cellCount]; i++)	// scan matrix for device
			{										// str found in options
			if(strcmp(sPtr,[[[dvPopMtrx selectCellAt:i :0] 
													selectedCell] title])==0)
				break;
			}						// set button title to that of sel'd cell
		[dvPopup setTitle:[[[dvPopMtrx selectCellAt:i :0]selectedCell] title]];
		[dvPopupList setTarget:self];
		[dvPopupList setAction:@selector(devPopUpAction:)];
		}
	else
		strncpy(devName, sPtr, MAXPATHLEN);
	devName[MAXPATHLEN-1] = 0;

    return self;
}

- (const char *)devName
{
	return devName;
}

- devPopUpAction:sender
{
	strcpy(devName, "/dev/");
	strncat(devName, [[sender selectedCell] title], MAXPATHLEN - 5);
	devName[MAXPATHLEN-1] = 0;
	[self setShouldSave:self];

	return self;
}
//*****************************************************************************
//
// 		speed -- set, return and target of 
//
//*****************************************************************************

- setSpeed:(const char *)aSpeed
{
static int i;

	strncpy(speed, aSpeed, MAXPATHLEN);
	speed[MAXPATHLEN-1] = 0;
	if(spPopup)
		{
    	if (![spPopup isKindOf:[PopUpList class]]) 
			spPopupList = [spPopup target];
		spPopMtrx = [spPopupList itemList];
		for (i = 0; i < [spPopMtrx cellCount]; i++)	// scan matrix for speed
			{										// str found in options
			if(strcmp(speed,[[[spPopMtrx selectCellAt:i :0] 
													selectedCell]title])==0)
			break;
			}						// set button title to that of sel'd cell
		[spPopupList setTarget:self];
		[spPopupList setAction:@selector(speedPopUpAction:)];
		if(i < [spPopMtrx cellCount])
			{
			[spPopup setTitle:[[[spPopMtrx selectCellAt:i :0] 
														selectedCell] title]];
    		return self;				// aSpeed was in popuplist, success
			}
		}

    return nil;							// aSpeed was not in popuplist, fail
}

- (const char *)speed
{
	return speed;
}

- speedPopUpAction:sender
{
	strncpy(speed, [[sender selectedCell] title], MAXPATHLEN);
	speed[MAXPATHLEN-1] = 0;
	[self setShouldSave:self];

	return self;
}
//*****************************************************************************
//
// 		return status of or set from info in options file
//
//*****************************************************************************

- editorWindow
{
    return editWin;
}
//*****************************************************************************
//
// 		Edit options file in Edit.app
//
//*****************************************************************************

- editOptions:sender
{
	if([[NXApp delegate] readable:path])
						// call edit to open radioButtons selected file
		[[Application workspace] openFile:path withApplication:"Edit"];
	
    return self;
}
//*****************************************************************************
//
// 		Edit Gate pppup file in Edit.app
//
//*****************************************************************************

- editPppup:sender
{
static char *sPtr;
							
	strcpy(Path, path);
	sPtr = Path + (strlen(path) - 8);
	strcpy(sPtr, PPPUP);
	if([[NXApp delegate] readable:Path])
						// call edit to open radioButtons selected file
		[[Application workspace] openFile:Path withApplication:"Edit"];

    return self;
}
//*****************************************************************************
//
// 		set our owner
//
//*****************************************************************************

- setOwner:anObject
{
	owner = anObject;

    return self;
}
//******************* Gate doc panel delegate methods *************************
//*****************************************************************************
//
// 		called whenever the user minituriazes our status window.
//
//*****************************************************************************

- windowWillMiniaturize:sender toMiniwindow:miniwindow 
{
    return [sender setMiniwindowIcon:".dir"];
}


@end

