/******************************************************************************
 GNBGlobals.cc

	Access to testjx global objects and factories.

	Written by Glenn W. Bach.

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

#include "GNBGlobals.h"
#include "GNBApp.h"
#include "GNBPrefsMgr.h"
#include "GNBTrashDir.h"
#include "GNBHelpText.h"
#include <GNBHelpPrefObject.h>

#include <JXWindow.h>
#include <JXDocumentManager.h>
#include <JXPTPrinter.h>

#include <JUserNotification.h>
#include <JUNIXDirEntry.h>
#include <JInPipeStream.h>
#include <JRegex.h>

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

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

static GNBApp*				theApplication 		= NULL;		// owns itself
static GNBPrefsMgr*			itsPrefsMgr			= NULL;
static GNBTrashDir*			itsTrashDir			= NULL;
static JXDocumentManager*	itsDocMgr			= NULL;
static JXPTPrinter*			itsPrinter			= NULL;
static GNBHelpPrefObject*	itsHelpPrefsObj		= NULL;

/******************************************************************************
 GNBCreateGlobals

	server can be NULL

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

JBoolean
GNBCreateGlobals
	(
	GNBApp*		app
	)
{
	theApplication = app;
	
	JBoolean isNew;
	itsPrefsMgr = new GNBPrefsMgr(&isNew);
	assert(itsPrefsMgr != NULL);

	JXInitHelp(kTOCHelpName, kHelpSectionCount,
			   kHelpSectionName, kHelpSectionTitle, kHelpSectionText,
			   JXMenu::kMacintoshStyle);

	itsDocMgr = 
		new JXDocumentManager(JXDocumentManager::kMetaShortcuts, kFalse);
	assert(itsDocMgr != NULL);

	itsPrinter = new JXPTPrinter();
	assert(itsPrinter != NULL);

	itsPrefsMgr->ReadPrinterSetup(itsPrinter);

	itsHelpPrefsObj = new GNBHelpPrefObject(itsPrefsMgr, kGHelpSetupID);
	assert(itsHelpPrefsObj != NULL);
		
	return isNew;	
}

/******************************************************************************
 GNBDeleteGlobals

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

void
GNBDeleteGlobals()
{	
	delete itsHelpPrefsObj;
	itsPrefsMgr->WritePrinterSetup(itsPrinter);
	delete itsPrinter;
	delete itsPrefsMgr;
//	delete itsDocMgr;
	theApplication = NULL;
}

/******************************************************************************
 GNBGetApplication

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

GNBApp*
GNBGetApplication()
{
	assert( theApplication != NULL );
	return theApplication;
}

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

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

GNBPrefsMgr*
GNBGetPrefsMgr()
{
	assert(itsPrefsMgr != NULL);
	return itsPrefsMgr;
}

/******************************************************************************
 GNBSetTrashDir

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

void
GNBSetTrashDir
	(
	GNBTrashDir* dir
	)
{
	assert(itsTrashDir == NULL);
	itsTrashDir = dir;
}

/******************************************************************************
 GNBGetTrashDir

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

GNBTrashDir*
GNBGetTrashDir()
{
	assert(itsTrashDir != NULL);
	return itsTrashDir;
}

/******************************************************************************
 GNBGetPTPrinter

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

JXPTPrinter*
GNBGetPTPrinter()
{
	return itsPrinter;
}
