/***********************************************************************\
Controller class for Convert MacPaint which converts MacPaint files to PostScript files.
Copyright (C) 1993 David John Burrowes

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

The author, David John Burrowes, can be reached at:
	davidjohn@kira.net.netcom.com
	David John Burrowes
	1926 Ivy #10
	San Mateo, CA 94403-1367
\***********************************************************************/


#import "macpaintController.h"
#import <stdio.h>
#import <libc.h>			// for mkdir
#import	<string.h>		// for strrchr
#include <sys/vnode.h>
#import "PSFile.h"
#import "macpaintConverter.h"
#import <appkit/Matrix.h>	// For matrix stuff for preferences setting
#import <appkit/Cell.h>	// for menu command work

@implementation macpaintController


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Routine:		init
//	Parameters:	none
//	Returns:		self
//	Stores:		none
//	Description:
//		This overrides the defalut init method.  The purpose of this overriding
//		is to give ourselves a chance to set up some internal variables which are used
//		later on
//	Bugs:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- init
{	
	static NXDefaultsVector theDefaults =
	{
		{"DoPacking", "YES"},
		{NULL, NULL}
	};

	[super init];

	ConversionString = "Converting MacPaint file";
	SourcePrompt = "Paint file:";
	DestPrompt = "eps file:";
	DestExtension = ".eps";
	DefaultsOwner = "MacPaint_to_eps";

	NXRegisterDefaults(DefaultsOwner, theDefaults);
	
	PackTheImage = [self   GetBoolPref: "DoPacking"];

	converterInst = [[macpaintConverter   alloc]  init];

	return self;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Routine:		displayPreferences: 
//	Parameters:	the object that called us
//	Description:
//	This is called whenever we need to bring the preferences panel onto the screen.
//	Using the value in our internal PackTheImage variable, then we set the buttons
//	in the preference panel to reflect these, and then we show the panel.
//	Bugs:
//		Well, strictly speaking, this is only needed the first time this is called,
//		because thereafter the buttons maintain their own highlite properly.
//		Oh well.  This whole scheme is a bit awkward, given my trivial use, and the fact
//		that it really seems to be designed for bigger things.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-displayPreferences: sender
{
	//
	//	Get the default preference values, and compare judge what buttons to
	//	highlight for the user.
	//
	if (PackTheImage == YES)
		[PackingButton  selectCellWithTag: 1];
	else
		[PackingButton  selectCellWithTag: 0];
	[prefPanel	makeKeyAndOrderFront:self];
	return self;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Routine: ChangePacking: 
//	Parameters: the object that called us
//	Description:
//		This method gets called whenever the user clicks on a button to change
//		the setting of whether the output should be packed or not.  If the button they
//		clicked on had a key of '1', then we will pack the image, otherwise we don't.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-ChangePacking: target
{
	if ( [[target selectedCell] tag] == 1)
		PackTheImage = YES;
	else
		PackTheImage = NO;

	[self   SetBoolPref: "DoPacking" To: PackTheImage];

	return self;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Routine:		openDestFile
//	Parameters:	the path to the file to be opened
//	Returns:		the opened file object
//	Stores:		none
//	Description:
//		This is used to open the destination file object (a PS file).  This is needed so
//		that our superclass, when it is doing some of the work that we don' want to,
//		will open an instance of the right class.
//	Bugs:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- openDestFile: (roCString) theFile
{	
	Instance	fileInstance;
	[self   ResetResults];
	
	fileInstance = [[PSFile alloc] initAndUse: theFile];
	if ([fileInstance   GetErrorCode] == ERR_OK)
		[fileInstance   CreateAndOpenFor: FILE_WRITE];
	if ([fileInstance   GetErrorCode] != ERR_OK)
	{
		[self   StoreErrorCode: ERR_OPENFAILED
			AndText: "We failed to open the file"];
		[fileInstance   free];
		fileInstance = NullInstance;
	}
	return fileInstance;
}




//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Routine:		ConvertFrom:To: 
//	Parameters:	The file to be converted from, and the file to be converted to
//	Returns:		self
//	Stores:		error code
//	Description:
//		This merely acts (like the rest of this object) as a kinda minimal interface
//		shell.  This merely creates the converting object, asks it to convert the stuff,
//		stores whether there were any results, and returns.
//	Bugs
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ConvertFrom: sourceFile To: destinationFile
{
	CString	tempString;
	
	[super   ConvertFrom: sourceFile To: destinationFile];
	[ConvertCommand	setEnabled: NO];
	[converterInst   SetPacking: PackTheImage AndPath: AppHome];
	[converterInst   Convert: sourceFile To: destinationFile];
	[ConvertCommand	setEnabled: YES];
	//
	//	93.01.31	djb	YOW!!! I was not storing the error code here!!!
	//
	if ( [converterInst   GetErrorCode] < ERR_OK)
	{
		tempString =   [converterInst   GetErrorText];
		[self   StoreErrorCode:  [converterInst   GetErrorCode]
			AndText: tempString];
		FreeCString(tempString);
	}
	else
		[self   StoreErrorCode: ERR_OK
			AndText: "Converted Successfully" ];

	return self;
}

@end
