/******************************************************************************
 SyGGlobals.cc

	Access to testjx global objects and factories.

	Written by Glenn W. Bach.

 ******************************************************************************/

#include "SyGGlobals.h"
#include "SyGApplication.h"
#include "SyGPrefsMgr.h"
#include "SyGBindingsManager.h"
#include "SyGHelpText.h"
#include "SyGHelpPrefObject.h"
#include "SyGChooseSavePrefObject.h"
#include "SyGViewManPageDialog.h"
#include "SyGChooseSaveFile.h"

#include <JXWindow.h>

#include <JSubstitute.h>
#include <JSimpleProcess.h>

#include <jStreamUtil.h>
#include <jDirUtil.h>
#include <jFileUtil.h>
#include <jTime.h>
#include <jProcessUtil.h>

#include <unistd.h>
#include <stdlib.h>
#include <jAssert.h>

static SyGApplication*		theApplication 		= NULL;		// owns itself
static SyGPrefsMgr*			itsPrefsMgr			= NULL;
static SyGBindingsManager*	itsBindingsMgr		= NULL;
static SyGHelpPrefObject*	itsHelpPrefsObj		= NULL;

static SyGChooseSavePrefObject* itsCSPrefsObj		= NULL;
static SyGViewManPageDialog*	itsManPageDialog	= NULL;
static SyGChooseSaveFile*	itsAltChooseSaveFile	= NULL;

/******************************************************************************
 SyGCreateGlobals

	server can be NULL

 ******************************************************************************/

JBoolean
SyGCreateGlobals
	(
	SyGApplication*		app
	)
{
	theApplication = app;
	
	JBoolean isNew;
	itsPrefsMgr = new SyGPrefsMgr(&isNew);
	assert(itsPrefsMgr != NULL);

	itsBindingsMgr = new SyGBindingsManager(itsPrefsMgr, kSBindingsMgrID);
	assert(itsBindingsMgr != NULL);

	itsBindingsMgr->SetHelpSection(kBindingEditorHelpName);
	
	JXInitHelp(kTOCHelpName, kHelpSectionCount,
			   kHelpSectionName, kHelpSectionTitle, kHelpSectionText,
			   JXMenu::kMacintoshStyle);

	itsHelpPrefsObj = new SyGHelpPrefObject(itsPrefsMgr, kSHelpSetupID);
	assert(itsHelpPrefsObj != NULL);

	itsCSPrefsObj = new SyGChooseSavePrefObject(itsPrefsMgr, kSCSSetupID);
	assert(itsCSPrefsObj != NULL);

	itsManPageDialog = new SyGViewManPageDialog(app);
	assert(itsManPageDialog != NULL);

	itsAltChooseSaveFile = new SyGChooseSaveFile(itsPrefsMgr, kSAltCSSetupID);
	assert(itsAltChooseSaveFile != NULL);
	
	return isNew;	
}

/******************************************************************************
 SyGDeleteGlobals

 ******************************************************************************/

void
SyGDeleteGlobals()
{	
	delete itsHelpPrefsObj;
	delete itsCSPrefsObj;
	delete itsAltChooseSaveFile;
	delete itsBindingsMgr;
	delete itsPrefsMgr;
	theApplication = NULL;
}

/******************************************************************************
 SyGGetApplication

 ******************************************************************************/

SyGApplication*
SyGGetApplication()
{
	assert( theApplication != NULL );
	return theApplication;
}

/******************************************************************************
 GGetPrefsMgr

 ******************************************************************************/

SyGPrefsMgr*
SyGGetPrefsMgr()
{
	assert(itsPrefsMgr != NULL);
	return itsPrefsMgr;
}

/******************************************************************************
 SyGGetBindingsMgr

 ******************************************************************************/

SyGBindingsManager*
SyGGetBindingsMgr()
{
	assert(itsBindingsMgr != NULL);
	return itsBindingsMgr;
}

/******************************************************************************
 SyGHandleFileAction

 ******************************************************************************/

void
SyGHandleFileAction
	(
	const JString& fileName, 
	JBoolean metaOn
	)
{
	JString sysCmd(SyGGetBindingsMgr()->GetAppForFile(fileName, metaOn));
	SyGHandleCustomFileAction(fileName, sysCmd);
}

/******************************************************************************
 SyGHandleCustomFileAction

 ******************************************************************************/

void
SyGHandleCustomFileAction
	(
	const JString& fileName, 
	const JString& cmd
	)
{
	theApplication->DisplayBusyCursor();

	JString sysCmd 		= cmd;
	JIndex index;
	if (sysCmd.LocateSubstring("$f", &index))
		{
		sysCmd.ReplaceSubstring(index, index + 1, fileName);
		}
	else
		{
		sysCmd += " \"" + fileName + "\"";
		}
	JString name;
	JString path;
	JSplitPathAndName(fileName, &path, &name);
	JString currentDir	= JGetCurrentDirectory();
	JError err 			= JChangeDirectory(path);
	if (err.OK())
		{
		JSimpleProcess::Create(sysCmd, kTrue);
//		pid_t pid;
//		JExecute(sysCmd, &pid);
		}
}

/******************************************************************************
 SyGHandleExecutable

 ******************************************************************************/

void
SyGHandleExecutable
	(
	const JString& fileName
	)
{
	theApplication->DisplayBusyCursor();
	
	JString name;
	JString path;
	JSplitPathAndName(fileName, &path, &name);
	JString currentDir = JGetCurrentDirectory();
	JError err = JChangeDirectory(path);
	if (err.OK())
		{
		JSimpleProcess::Create(fileName, kTrue);
//		pid_t pid;
//		JExecute(fileName, &pid);
		}
}

/******************************************************************************
 SyGGetManPageDialog

 ******************************************************************************/

SyGViewManPageDialog*
SyGGetManPageDialog()
{
	return itsManPageDialog;
}

/******************************************************************************
 SyGGetChooseSaveFile

 ******************************************************************************/

SyGChooseSaveFile*
SyGGetChooseSaveFile()
{
	return itsAltChooseSaveFile;
}
