//*****************************************************************************
//
//	GateDocEditor.m.  
//		
//		Create, edit and link using 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 "GateDocEditor.h"
#import "options.h"
#import "Coordinator.h"
#import "OptionsEditor.h"
#import <dbkit/DBImageView.h>

				// filter for Gate doc path's
	const char *filter[2] = {"Gate", NULL};


@implementation GateDocEditor

//*****************************************************************************
//
//		create new Gate Document with default files inside.
//
//*****************************************************************************

- newDoc:sender
{
	theSavePanel = [SavePanel new];
	[theSavePanel setTitle:[[NXApp delegate] localString:"New"]];
	[theSavePanel setRequiredFileType:filter[0]]; // append ext if not supplied
	if(NX_OKTAG == [theSavePanel runModalForDirectory:
						NXGetDefaultValue([NXApp appName], "path") file:NULL])
		{										// create .Gate doc wrapper
    	mkdir([theSavePanel filename], (_S_IRUSR | _S_IWUSR | _S_IXUSR));		
		chown([theSavePanel filename], getuid(), getgid());
		[self addDefaultFiles:[theSavePanel filename]];
		[self editGateDoc:[theSavePanel filename]];
		}

    return self;
}
//*****************************************************************************
//
//		execute pppd with info from .Gate open document
//
//*****************************************************************************

- linkDoc:sender
{
	if((strcmp([[[NXApp mainMenu] findCellWithTag:2] title], 
				[[NXApp delegate] localString:"Connect"]) == 0) && 
						[[[NXApp mainMenu] findCellWithTag:2] isEnabled])
		{										// is link menu item enabled?
		[[sender window] close];
	  	[[NXApp delegate] 
					linkWithFile:NXGetDefaultValue([NXApp appName], "path")];
		}
	else
		[[NXApp delegate] showAlert:"A ppp session is already active."];

    return self;
}
//*****************************************************************************
//
//		open an existing .Gate document
//
//*****************************************************************************

- openDoc:sender
{
	theOpenPanel = [OpenPanel new];
	[theOpenPanel setTitle:[[NXApp delegate] localString:"Open"]];
	if(NX_OKTAG == [theOpenPanel runModalForDirectory:
						NXGetDefaultValue([NXApp appName], "path") file:NULL 	
							types:filter])
		[self editGateDoc:[theOpenPanel filename]];

    return self;
}
//*****************************************************************************
//
//		edit an existing .Gate document using built in editor
//
//*****************************************************************************

- editGateDoc:(const char *)aGateDoc
{
id image;

	if(!NXWriteDefault([NXApp appName],"path", aGateDoc))
		[[NXApp delegate] showAlert:"ddbWriteError"];
	strcpy(Path, aGateDoc);
	strcat(Path, PPPUP);						// pppup file inside of dir
	if([[NXApp delegate] readable:Path])
		{
		if(!theOptionsEditor)					// open edit doc window
			[NXApp loadNibSection:"EditDoc.nib" owner:self withNames:NO];
		[docPath setStringValue:aGateDoc];		// display name
		[docName setStringValue:[[NXApp delegate] extractName:aGateDoc]];	
		strcpy(Path, aGateDoc);
		strcat(Path, OPTION);					// file name inside of dir
		[theOptionsEditor parseOptionsFile:Path];

		strcpy(Path, aGateDoc);
		strcat(Path, "/Icon.tiff");
		[[GDEImageView setEditable:NO] setStyle:DB_ImageNoFrame];
		if(!(image = [[NXImage alloc] initFromFile:Path]))		// if imge
			image = [NXImage findImageNamed: ".dir"];		// else use blank
		[GDEImageView setImage:image];

		[[theOptionsEditor editorWindow] center];
		[[theOptionsEditor editorWindow] makeKeyAndOrderFront:self];
		}

    return self;
}
//*****************************************************************************
//
//		add our default options and pppup files to our new Gate document
//
//		paths for pppup file set from pref editor path
//
//*****************************************************************************

- addDefaultFiles:(const char *)nameOfFile
{
NXStream *Stream;
							// add example options
	if(![[NXBundle mainBundle]getPath:Path forResource:"Examples" ofType:NULL])
		[[NXApp delegate] showAlert:"Error getting examples path"];
	strncat(Path, XOPTION,  MAXPATHLEN - strlen(XOPTION) - strlen(Path) + 1);
	if((Stream = NXMapFile(Path, NX_READONLY)) == NULL)
		[[NXApp delegate] showAlert:"Unable to open example options File"];
	else
		{
		strncpy(Path, nameOfFile, MAXPATHLEN - strlen(OPTION));
		strncat(Path, OPTION, (strlen(OPTION) + 1));
		if((NXSaveToFile(Stream, Path)) == -1)
			[[NXApp delegate] showAlert:"Error saving options to Gate doc"];
		else
			chown(Path, getuid(), getgid());
		NXCloseMemory(Stream, NX_FREEBUFFER);
		}
							// add example pppup
	if(![[NXBundle mainBundle]getPath:Path forResource:"Examples" ofType:NULL])
		[[NXApp delegate] showAlert:"Error getting examples path"];
	strncat(Path, PPPUP,  MAXPATHLEN - strlen(PPPUP) - strlen(Path) + 1);
	if((Stream = NXMapFile(Path, NX_READONLY)) == NULL)
		[[NXApp delegate] showAlert:"Unable to open pppup example File"];
	else
		{
		strncpy(Path, nameOfFile, MAXPATHLEN - strlen(PPPUP));
		strncat(Path, PPPUP, (strlen(PPPUP) + 1));
		if((NXSaveToFile(Stream, Path)) == -1)
			[[NXApp delegate] showAlert:"Error saving pppup to .Gate doc"];
		else
			{
			chmod(Path, (_S_IRUSR | _S_IWUSR));		// set to owner read/write
			chown(Path, getuid(), getgid());
			}
		NXCloseMemory(Stream, NX_FREEBUFFER);
		}
						// add default Icon.tiff
	if(![[NXBundle mainBundle] getPath:Path 
										forResource:"Icon.tiff" ofType:NULL])
		[[NXApp delegate] showAlert:"Error getting Icon.tiff path"];
	if((Stream = NXMapFile(Path, NX_READONLY)) == NULL)
		[[NXApp delegate] showAlert:"Unable to open Icon.tiff File"];
	strncpy(Path, nameOfFile, MAXPATHLEN - strlen(IMAGE));
	strncat(Path, IMAGE, (strlen(IMAGE) + 1));
	if((NXSaveToFile(Stream, Path)) == -1)
	   	[[NXApp delegate] showAlert:"Unable to save Icon.tiff to Gate doc"];
	else
		chown(Path, getuid(), getgid());
	NXCloseMemory(Stream, NX_FREEBUFFER);

    return self;
}


@end

