/******************************************************************************
 GNBPrefsMgr.cc

	<Description>

	BASE CLASS = public JUNIXPrefsManager

	Copyright © 1998 by Glenn W. Bach.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

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

#include <GNBPrefsMgr.h>
#include <GNBGlobals.h>
#include <GNBApp.h>

//#include <JXSRTextDialogBase.h>
#include <JXFontManager.h>
#include <JXApplication.h>
#include <JXDisplay.h>
#include <JXChooseSaveFile.h>
#include <JXFontManager.h>
#include <JXPTPrinter.h>

#include <JStaticBuffer.h>
#include <JPtrArray-JString.h>
#include <JOrderedSet.h>

#include <jUNIXUtil.h>
#include <jTypes.h>
#include <jStrStreamUtil.h>
#include <jDirUtil.h>

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

static const JCharacter* kDefaultFontName = "6x13";

const JFileVersion kCurrentPrefsFileVersion 	= 1;

const JCharacter* kGNBAppSig 					= "gNotebook";

extern const JCharacter* kGNBFileName;
extern const JCharacter* kNotebookVersionNumberStr;

/******************************************************************************
 Constructor

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

GNBPrefsMgr::GNBPrefsMgr
	(
	JBoolean* isNew
	)
	:
   JUNIXPrefsManager(kGNBAppSig, kCurrentPrefsFileVersion, kTrue)
{
//	itsDialog	= NULL;
	
	*isNew = JPrefsManager::UpgradeData();
}

/******************************************************************************
 Destructor

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

GNBPrefsMgr::~GNBPrefsMgr()
{
	SetData(kGProgramVersionID, kNotebookVersionNumberStr);

	SaveToDisk();
}

/******************************************************************************
 GetJCCVersionStr

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

JString
GNBPrefsMgr::GetNotebookVersionStr()
	const
{
	JStaticBuffer data;
	if (GetData(kGProgramVersionID, &data))
		{
		return JString(data);
		}
	else
		{
		return JString("< 0.1.0");		// didn't exist before this version
		}
}

/******************************************************************************
 UpgradeData (virtual protected)

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

void
GNBPrefsMgr::UpgradeData
	(
	const JBoolean		isNew,
	const JFileVersion	currentVersion
	)
{
	JXDisplay* display = (GNBGetApplication())->GetDisplay(1);
	
	if (isNew)
		{
		JString home;
		if (JGetHomeDirectory(&home))
			{
			JString fullname = JCombinePathAndName(home, kGNBFileName);
			SetNotesDatabaseFile(fullname);
			}
		}

	if (currentVersion < 1)
		{
		JFontID id;
		if ((display->GetXFontManager())->GetFontID(kDefaultFontName, &id))
			{
			SetDefaultFont(kDefaultFontName, kJXDefaultFontSize);
			}
		else
			{
			SetDefaultFont(JGetMonospaceFontName(), kJXDefaultFontSize);
			}
		}
}

/******************************************************************************
 Receive (virtual protected)

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

void
GNBPrefsMgr::Receive
	(
	JBroadcaster* 	sender, 
	const Message&	message
	)
{
/*	if (sender == itsDialog && message.Is(JXDialogDirector::kDeactivated))
		{
		const JXDialogDirector::Deactivated* info = 
			dynamic_cast(const JXDialogDirector::Deactivated*, &message);
		assert(info != NULL);
		if (info->Successful())
			{
			}
		itsDialog = NULL;
		}*/
}

/******************************************************************************
 GetNotesDatabaseFile (public)

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

JBoolean	 
GNBPrefsMgr::GetNotesDatabaseFile
	(
	JString* filename
	)
{
	if (IDValid(kGNotesDatabaseFileID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGNotesDatabaseFileID, &data);
		assert( ok );

		jistrstream(dataStream, data.GetData(), strlen(data));

		dataStream >> *filename;
		return kTrue;
		}
	return kFalse;
}

/******************************************************************************
 SetNotesDatabaseFile (public)

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

void
GNBPrefsMgr::SetNotesDatabaseFile
	(
	const JString& filename
	)
{
	ostrstream data;
	data << filename;

	SetData(kGNotesDatabaseFileID, data);
}

/******************************************************************************
 EditPrefs (private)

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

void
GNBPrefsMgr::EditPrefs()
{
/*	assert(itsDialog == NULL);
	itsDialog = 
		new GMailPrefsDialog(JXGetApplication(), GetDefaultInbox(), 
								GetSMTPHost(), GetSMTPUser(),
								GetReplyTo(), ShowingStateInTitle(), 
								GetPopServer(), GetPopUserName(),
								UsingPopServer(), LeaveOnServer(), 
								RememberPasswd(), UsingAPop(), 
								AutoInsertingQuote(), AutoCheckingMail(),
								CheckMailDelay(), ShowingSenderString(),
								GetSenderString());
	assert(itsDialog != NULL);
	ListenTo(itsDialog);
	itsDialog->BeginDialog();*/
}

/******************************************************************************
 ReadFindDialogPrefs


 ******************************************************************************/
/*
void
GNBPrefsMgr::ReadFindDialogPrefs
	(
	JXSRTextDialogBase* dialog
	)
{
	if (IDValid(kFindDialogPrefsID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kFindDialogPrefsID, &data);
		assert( ok );

		jistrstream(dataStream, data.GetData(), strlen(data));

		dialog->ReadSetup(dataStream);
		}
}*/

/******************************************************************************
 WriteFindDialogPrefs


 ******************************************************************************/
/*
void
GNBPrefsMgr::WriteFindDialogPrefs
	(
	JXSRTextDialogBase* dialog
	)
{
	ostrstream data;
	dialog->WriteSetup(data);
	SetData(kFindDialogPrefsID, data);
}*/

/******************************************************************************
 Default font

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

void
GNBPrefsMgr::GetDefaultFont
	(
	JString*	name,
	JSize*		size
	)
	const
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kCBDefFontID, &data);
	assert( ok );

	jistrstream(dataStream, data.GetData(), strlen(data));
	dataStream >> *name >> *size;
}

void
GNBPrefsMgr::SetDefaultFont
	(
	const JCharacter*	name,
	const JSize			size
	)
{
	ostrstream data;
	data << JString(name) << ' ' << size;
	SetData(kCBDefFontID, data);
}

/******************************************************************************
 PrinterSetup

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

void
GNBPrefsMgr::WritePrinterSetup
	(
	JXPTPrinter* printer
	)
{
	ostrstream data;
	printer->WriteXPTSetup(data);
	SetData(kPTPrinterSetupID, data);
}

void
GNBPrefsMgr::ReadPrinterSetup
	(
	JXPTPrinter* printer
	)
{
	if (IDValid(kPTPrinterSetupID))
		{
		JStaticBuffer data;
		GetData(kPTPrinterSetupID, &data);
		jistrstream(dataStream, data.GetData(), strlen(data));
		printer->ReadXPTSetup(dataStream);
		}
}
