/******************************************************************************
 GPrefsMgr.cc

	<Description>

	BASE CLASS = public JUNIXPrefsManager

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

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

#include <GPrefsMgr.h>
#include <GMailPrefsDialog.h>
#include <GMGlobals.h>

#include <JXSRTextDialogBase.h>
#include <JXFontManager.h>
#include <JXApplication.h>
#include <JXDisplay.h>
#include <JXChooseSaveFile.h>
#include <JXHelpManager.h>
#include <JXWindow.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>

const JFileVersion kCurrentPrefsFileVersion 	= 6;

const JCharacter* GPrefsMgr::kInboxesChanged 	= "GPrefsMgr::kInboxesChanged";
const JCharacter* GPrefsMgr::kInboxAdded 		= "GPrefsMgr::kInboxAdded";
const JCharacter* GPrefsMgr::kInboxRemoved		= "GPrefsMgr::kInboxRemoved";
const JCharacter* GPrefsMgr::kShowStateChanged 	= "GPrefsMgr::kShowStateChanged";
const JCharacter* GPrefsMgr::kPopChanged 		= "GPrefsMgr::kPopChanged";
const JCharacter* GPrefsMgr::kFontChanged 		= "GPrefsMgr::kFontChanged";

const JCharacter* kGMDefaultMailtoString		= "arrow --mailto \"$a\"";

const JCharacter* kGMAppSig 					= "gArrow";

extern const JCharacter* kArrowVersionNumberStr;

const JSize kGDefaultFontSize					= 12;		
const JSize	kGDefaultTabCharWidth				= 8;
const JSize kGDefaultMaxQuote					= 10;

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

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

GPrefsMgr::GPrefsMgr
	(
	JBoolean* isNew
	)
	:
   JUNIXPrefsManager(kGMAppSig, kCurrentPrefsFileVersion, kTrue)
{
	itsDialog	= NULL;
	
	*isNew = JPrefsManager::UpgradeData();
}

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

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

GPrefsMgr::~GPrefsMgr()
{
	SetData(kGProgramVersionID, kArrowVersionNumberStr);

	SaveToDisk();
}

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

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

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

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

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

void
GPrefsMgr::UpgradeData
	(
	const JBoolean		isNew,
	const JFileVersion	currentVersion
	)
{
	if (isNew)
		{
		JString homeDir;
		JBoolean fileok = JGetHomeDirectory(&homeDir);
		JAppendDirSeparator(&homeDir);
		JString outbox = homeDir + ".outbox";
		SetCurrentOutbox(outbox);

		JString username;
		char* loginname = getlogin();
		if (loginname != NULL)
			{
			username = loginname;
			}
		else
			{
			username = JGetUserName();
			}
		
		JString inbox = "/var/spool/mail/" + username;
		SetDefaultInbox(inbox);

		ShowStateInTitle(kTrue);
		SetPopServer(JString("pop-server"));
		UsePopServer(kFalse);
		SetLeaveOnServer(kFalse);
		SetPopUserName(JString("name"));
		SetPopPasswd(JString(""));
		SetRememberPasswd(kFalse);
		UseAPop(kFalse);
		AutoInsertQuote(kTrue);
		AutoCheckMail(kFalse);
		SetCheckMailDelay(30);
		SetDefaultMonoFont("6x13");
		JCharacter* var = getenv("REPLYTO");
		if (var != NULL)
			{
			JString varStr(var);
			SetReplyTo(varStr);
			}
		ShowSenderString(kTrue);
		SetSenderString("On $d, $f wrote:");
		JXGetHelpManager()->SetSendMailCmd(kGMDefaultMailtoString);
		}
	else
		{
		if (currentVersion < 2)
			{
			ShowStateInTitle(kTrue);
			}
		if (currentVersion < 3)
			{
			UseAPop(kFalse);
			AutoInsertQuote(kTrue);
			AutoCheckMail(kFalse);
			SetCheckMailDelay(30);
			}
		if (currentVersion < 4)
			{
			JCharacter* var = getenv("REPLYTO");
			if (var != NULL)
				{
				JString varStr(var);
				SetReplyTo(varStr);
				}
			ShowSenderString(kTrue);
			SetSenderString("On $d, $f wrote:");
			}
		if (currentVersion < 5)
			{
			RemoveData(kGHelpSetupID);
			JXGetHelpManager()->SetSendMailCmd(kGMDefaultMailtoString);
			}
		if (currentVersion == 5)
			{
			RemoveData(kGSpellCheckSetupID);
			}
		}
}

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

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

void
GPrefsMgr::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())
			{
			JString  inbox;
			JString  smtp;
			JString  smtpuser;
			JString  replyto;
			JBoolean showstate;
			JString  pop;
			JString  account;
			JBoolean usePop;
			JBoolean leave;
			JBoolean save;
			JBoolean useAPop;
			JBoolean autoQuote;
			JBoolean autoCheck;
			JInteger checkDelay;
			JBoolean showSender;
			JString	 senderString;
			JBoolean useFrom;
			JString	 fromLine;
			JString  fontname;
			JSize    fontsize;
			JSize	 maxQuote;
			JBoolean report;
			JSize	 spacesPerTab;
			JBoolean beeping;
			JIndex	 encType;
			itsDialog->GetValues(&inbox, &smtp, &smtpuser, &replyto, 
									&showstate, &pop, 
									&account, &usePop,&leave, &save, 
									&useAPop, &autoQuote, &autoCheck, 
									&checkDelay, &showSender, &senderString,
									&useFrom, &fromLine, &fontname, &fontsize,
									&maxQuote, &report, &spacesPerTab, &beeping,
									&encType);
			SetDefaultInbox(inbox);
			SetSMTPHost(smtp);
			SetSMTPUser(smtpuser);
			SetReplyTo(replyto);
			ShowStateInTitle(showstate);
			SetPopServer(pop);
			SetPopUserName(account);
			UsePopServer(usePop);
			SetLeaveOnServer(leave);
			SetRememberPasswd(save);
			UseAPop(useAPop);
			AutoInsertQuote(autoQuote);
			AutoCheckMail(autoCheck);
			SetCheckMailDelay(checkDelay);
			ShowSenderString(showSender);
			SetSenderString(senderString);
			UseCustomFromLine(useFrom);
			SetCustomFromString(fromLine);
			SetDefaultMonoFont(fontname);
			SetDefaultFontSize(fontsize);
			SetMaxQuote(maxQuote);
			ReportSCErrors(report);
			SetTabCharWidth(spacesPerTab);
			BeepOnNewMail(beeping);
			SetEncryptionType((EncryptionType)encType);
			}
		itsDialog = NULL;
		}
}

/******************************************************************************
 GetInboxes

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

void
GPrefsMgr::GetInboxes
	(
	JPtrArray<JString>& inboxes
	)
{
	if (IDValid(kGInboxesID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGInboxesID, &data);
		assert( ok );

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

		JSize count;
		dataStream >> count;

		inboxes.SetCompareFunction(CompareFileNames);

		for (JIndex i=1; i<=count; i++)
			{
			JString* str = new JString;
			assert( str != NULL );
			dataStream >> *str;
			if (!inboxes.InsertSorted(str, kFalse))
				{
				delete str;
				}
			}
		}
}

/******************************************************************************
 SetInboxes

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

void
GPrefsMgr::SetInboxes
	(
	const JPtrArray<JString>& inboxes
	)
{
	const JSize count = inboxes.GetElementCount();

	ostrstream data;
	data << count;

	for (JIndex i=1; i<=count; i++)
		{
		data << ' ' << *(inboxes.NthElement(i));
		}

	SetData(kGInboxesID, data);
	Broadcast(InboxesChanged());
}

/******************************************************************************
 AddInbox

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

void
GPrefsMgr::AddInbox
	(
	const JString& inbox
	)
{
	JPtrArray<JString> inboxes;
	GetInboxes(inboxes);
	JString* str = new JString(inbox);
	assert(str != NULL);
	JIndex index;
	inboxes.SetCompareFunction(CompareFileNames);
	JBoolean found = inboxes.SearchSorted(str, JOrderedSetT::kAnyMatch, &index);
	if (!found)
		{
		if (inboxes.InsertSorted(str, kFalse, &index))
			{
			SetInboxes(inboxes);
			Broadcast(InboxAdded(index));
			}
		else
			{
			delete str;
			}
		}
	else
		{
		delete str;
		}
}

/******************************************************************************
 DeleteInbox

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

void
GPrefsMgr::DeleteInbox
	(
	const JString& inbox
	)
{
	JPtrArray<JString> inboxes;
	GetInboxes(inboxes);
	JIndex index;
	JString str(inbox);
	inboxes.SetCompareFunction(CompareFileNames);
	if (inboxes.SearchSorted(&str, JOrderedSetT::kAnyMatch, &index))
		{
		Broadcast(InboxRemoved(index));
		}
	JBoolean found = kFalse;
	for (JSize i = 1; i <= inboxes.GetElementCount(); i++)
		{
		if (*(inboxes.NthElement(i)) == inbox)
			{
			inboxes.DeleteElement(i);
			found = kTrue;
			}
		}
	if (found)
		{
		SetInboxes(inboxes);
		}
}

/******************************************************************************
 DeleteAllInboxes

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

void
GPrefsMgr::DeleteAllInboxes()
{
	JPtrArray<JString> inboxes;
	GetInboxes(inboxes);
	if (inboxes.GetElementCount() == 0)
		{
		return;
		}
	inboxes.DeleteAll();
	SetInboxes(inboxes);
}

/******************************************************************************
 GetSMTPHost


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

JString
GPrefsMgr::GetSMTPHost()
{
	if (IDValid(kGSMTPHostID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGSMTPHostID, &data);
		assert( ok );

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

		JString host;
		dataStream >> host;
		if (!host.IsEmpty())
			{
			return host;
			}
		}
	JCharacter hostname[255];
	gethostname(hostname, 255);
	JString host(hostname);
	return host;
}

/******************************************************************************
 SetSMTPHost


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

void
GPrefsMgr::SetSMTPHost
	(
	const JString& host
	)
{
	ostrstream data;
	data << host;

	SetData(kGSMTPHostID, data);
}

/******************************************************************************
 GetSMTPUser


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

JString
GPrefsMgr::GetSMTPUser()
{
	if (IDValid(kSMTPUserID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kSMTPUserID, &data);
		assert( ok );

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

		JString user;
		dataStream >> user;
		if (!user.IsEmpty())
			{
			return user;
			}
		}
	JCharacter* name = getenv("LOGNAME");
	if (name == NULL)
		{
		return JString();
		}
	return JString(name);
}

/******************************************************************************
 SetSMTPUser


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

void
GPrefsMgr::SetSMTPUser
	(
	const JString& user
	)
{
	ostrstream data;
	data << user;

	SetData(kSMTPUserID, data);
}

/******************************************************************************
 ShowingStateInTitle


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

JBoolean 
GPrefsMgr::ShowingStateInTitle()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGShowStateInTitleID, &data);
	assert( ok );

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

	JBoolean show;
	dataStream >> show;
	return show;
}

/******************************************************************************
 ShowStateInTitle


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

void 
GPrefsMgr::ShowStateInTitle
	(
	const JBoolean show
	)
{
	ostrstream data;
	data << show;

	SetData(kGShowStateInTitleID, data);
	Broadcast(ShowStateChanged());
}

/******************************************************************************
 GetPopServer


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

JString	 
GPrefsMgr::GetPopServer()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGPOPServerID, &data);
	assert( ok );

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

	JString pop;
	dataStream >> pop;
	return pop;
}

/******************************************************************************
 SetPopServer


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

void
GPrefsMgr::SetPopServer
	(
	const JString& pop
	)
{
	ostrstream data;
	data << pop;

	SetData(kGPOPServerID, data);
}

/******************************************************************************
 UsingPopServer


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

JBoolean 
GPrefsMgr::UsingPopServer()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGUsePopID, &data);
	assert( ok );

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

	JBoolean use;
	dataStream >> use;
	return use;
}

/******************************************************************************
 UsePopServer


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

void 
GPrefsMgr::UsePopServer
	(
	const JBoolean use
	)
{
	ostrstream data;
	data << use;

	SetData(kGUsePopID, data);
	Broadcast(PopChanged());
}

/******************************************************************************
 LeaveOnServer


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

JBoolean 
GPrefsMgr::LeaveOnServer()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGPopLeaveOnServerID, &data);
	assert( ok );

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

	JBoolean leave;
	dataStream >> leave;
	return leave;
}

/******************************************************************************
 SetLeaveOnServer


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

void 
GPrefsMgr::SetLeaveOnServer
	(
	const JBoolean leave
	)
{
	ostrstream data;
	data << leave;

	SetData(kGPopLeaveOnServerID, data);
}

/******************************************************************************
 GetPopUserName


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

JString
GPrefsMgr::GetPopUserName()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGPopUserNameID, &data);
	assert( ok );

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

	JString name;
	dataStream >> name;
	return name;
}

/******************************************************************************
 SetPopUserName


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

void
GPrefsMgr::SetPopUserName
	(
	const JString& name
	)
{
	ostrstream data;
	data << name;

	SetData(kGPopUserNameID, data);
}

/******************************************************************************
 GetPopPasswd


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

JString
GPrefsMgr::GetPopPasswd()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGPopPasswdID, &data);
	assert( ok );

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

	JString passwd;
	dataStream >> passwd;
	return passwd;
}

/******************************************************************************
 SetPopPasswd


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

void
GPrefsMgr::SetPopPasswd
	(
	const JString& passwd
	)
{
	ostrstream data;
	data << passwd;

	SetData(kGPopPasswdID, data);
}

/******************************************************************************
 RememberPasswd


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

JBoolean 
GPrefsMgr::RememberPasswd()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGPopRememberPasswdID, &data);
	assert( ok );

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

	JBoolean remember;
	dataStream >> remember;
	return remember;
}

/******************************************************************************
 SetRememberPasswd


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

void 
GPrefsMgr::SetRememberPasswd
	(
	const JBoolean remember
	)
{
	ostrstream data;
	data << remember;

	SetData(kGPopRememberPasswdID, data);
}

/******************************************************************************
 UsingAPop


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

JBoolean 
GPrefsMgr::UsingAPop()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGUseAPopID, &data);
	assert( ok );

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

	JBoolean use;
	dataStream >> use;
	return use;
}

/******************************************************************************
 UseAPop


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

void 
GPrefsMgr::UseAPop
	(
	const JBoolean use
	)
{
	ostrstream data;
	data << use;

	SetData(kGUseAPopID, data);
	Broadcast(PopChanged());
}

/******************************************************************************
 AutoCheckingMail


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

JBoolean 
GPrefsMgr::AutoCheckingMail()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kAutoCheckMailID, &data);
	assert( ok );

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

	JBoolean check;
	dataStream >> check;
	return check;
}

/******************************************************************************
 AutoCheckMail


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

void 
GPrefsMgr::AutoCheckMail
	(
	const JBoolean check
	)
{
	ostrstream data;
	data << check;

	SetData(kAutoCheckMailID, data);
	Broadcast(PopChanged());
}

/******************************************************************************
 CheckMailDelay


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

JInteger 
GPrefsMgr::CheckMailDelay()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kCheckMailDelayID, &data);
	assert( ok );

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

	JInteger delay;
	dataStream >> delay;
	return delay;
}

/******************************************************************************
 SetCheckMailDelay


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

void 
GPrefsMgr::SetCheckMailDelay
	(
	const JInteger delay
	)
{
	ostrstream data;
	data << delay;

	SetData(kCheckMailDelayID, data);
	Broadcast(PopChanged());
}

/******************************************************************************
 GetReplyTo


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

JString
GPrefsMgr::GetReplyTo()
{
	JString replyto;
	if (IDValid(kGReplyToID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGReplyToID, &data);
		assert( ok );

		jistrstream(dataStream, data.GetData(), strlen(data));
		dataStream >> replyto;
		}
	return replyto;
}

/******************************************************************************
 SetReplyTo


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

void
GPrefsMgr::SetReplyTo
	(
	const JString& replyto
	)
{
	ostrstream data;
	data << replyto;

	SetData(kGReplyToID, data);
}


/******************************************************************************
 GetUIDList

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

void
GPrefsMgr::GetUIDList
	(
	JPtrArray<JString>& list
	)
{
	if (IDValid(kGUIDListID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGUIDListID, &data);
		assert( ok );

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

		JSize count;
		dataStream >> count;

		list.SetCompareFunction(JCompareStringsCaseSensitive);

		for (JIndex i=1; i<=count; i++)
			{
			JString* str = new JString;
			assert( str != NULL );
			dataStream >> *str;
			if (!list.InsertSorted(str, kFalse))
				{
				delete str;
				}
			}
		}
}

/******************************************************************************
 SetUIDList

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

void
GPrefsMgr::SetUIDList
	(
	const JPtrArray<JString>& list
	)
{
	const JSize count = list.GetElementCount();

	ostrstream data;
	data << count;

	for (JIndex i=1; i<=count; i++)
		{
		data << ' ' << *(list.NthElement(i));
		}

	SetData(kGUIDListID, data);
}

void
GPrefsMgr::ClearUIDList()
{
	const JSize count = 0;

	ostrstream data;
	data << count;

	SetData(kGUIDListID, data);
}

/******************************************************************************
 AutoInsertingQuote


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

JBoolean 
GPrefsMgr::AutoInsertingQuote()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kAutoInsertQuoteID, &data);
	assert( ok );

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

	JBoolean insert;
	dataStream >> insert;
	return insert;
}

/******************************************************************************
 AutoInsertQuote


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

void 
GPrefsMgr::AutoInsertQuote
	(
	const JBoolean insert
	)
{
	ostrstream data;
	data << insert;

	SetData(kAutoInsertQuoteID, data);
}

/******************************************************************************
 GetCurrentOutbox


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

JString
GPrefsMgr::GetCurrentOutbox()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGCurrentOutboxID, &data);
	assert( ok );

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

	JString mbox;
	dataStream >> mbox;
	return mbox;
}

/******************************************************************************
 SetCurrentOutbox


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

void
GPrefsMgr::SetCurrentOutbox
	(
	const JString& mbox
	)
{
	ostrstream data;
	data << mbox;

	SetData(kGCurrentOutboxID, data);
}

/******************************************************************************
 GetDefaultInbox


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

JString
GPrefsMgr::GetDefaultInbox()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGDefaultInboxID, &data);
	assert( ok );

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

	JString mbox;
	dataStream >> mbox;
	return mbox;
}

/******************************************************************************
 SetDefaultInbox


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

void
GPrefsMgr::SetDefaultInbox
	(
	const JString& mbox
	)
{
	ostrstream data;
	data << mbox;

	SetData(kGDefaultInboxID, data);
}

/******************************************************************************
 GetDefaultMonoFont


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

JFontID 
GPrefsMgr::GetDefaultMonoFontID()
{
	if (IDValid(kGDefaultMonoFontID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kGDefaultMonoFontID, &data);
		assert( ok );

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

		JString name;
		dataStream >> name;
		JFontID id;
		ok = JXGetApplication()->GetCurrentDisplay()->GetXFontManager()->GetFontID(name, &id);
		if (ok)
			{
			return id;
			}
		ok = JXGetApplication()->GetCurrentDisplay()->GetXFontManager()->GetFontID("6x13", &id);
		assert(ok);
		SetDefaultMonoFont("6x13");
		return id;
		}
	SetDefaultMonoFont("6x13");
	return GetDefaultMonoFontID();
}

JString 
GPrefsMgr::GetDefaultMonoFont()
{
	if (IDValid(kGDefaultMonoFontID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kGDefaultMonoFontID, &data);
		assert( ok );

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

		JString name;
		dataStream >> name;
		return name;
		}
	SetDefaultMonoFont("6x13");
	return GetDefaultMonoFont();
}

/******************************************************************************
 SetDefaultMonoFont


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

void 
GPrefsMgr::SetDefaultMonoFont
	(
	const JCharacter* fontName
	)
{
	ostrstream data;
	data << JString(fontName);

	SetData(kGDefaultMonoFontID, data);
	Broadcast(FontChanged());
}

/******************************************************************************
 GetDefaultFontSize

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

JSize 
GPrefsMgr::GetDefaultFontSize()
{
	if (IDValid(kGDefaultFontSizeID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kGDefaultFontSizeID, &data);
		assert( ok );

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

		JSize size;
		dataStream >> size;
		return size;
		}
	return kGDefaultFontSize;
}

/******************************************************************************
 SetDefaultFontSize


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

void 
GPrefsMgr::SetDefaultFontSize
	(
	const JSize size
	)
{
	ostrstream data;
	data << size;

	SetData(kGDefaultFontSizeID, data);
}

/******************************************************************************
 GetTabCharWidth

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

JSize 
GPrefsMgr::GetTabCharWidth()
{
	if (IDValid(kGTabCharWidthID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kGTabCharWidthID, &data);
		assert( ok );

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

		JSize size;
		dataStream >> size;
		return size;
		}
	return kGDefaultTabCharWidth;
}

/******************************************************************************
 SetTabCharWidth


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

void 
GPrefsMgr::SetTabCharWidth
	(
	const JSize size
	)
{
	ostrstream data;
	data << size;

	SetData(kGTabCharWidthID, data);
	Broadcast(FontChanged());
}

/******************************************************************************
 IsReportingSCErrors


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

JBoolean 
GPrefsMgr::IsReportingSCErrors()
	const
{
	if (IDValid(kGShowSpellCheckFinishedID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGShowSpellCheckFinishedID, &data);
		assert( ok );

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

		JBoolean report;
		dataStream >> report;
		return report;
		}
	return kTrue;
}

/******************************************************************************
 ReportSCErrors


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

void 
GPrefsMgr::ReportSCErrors
	(
	const JBoolean report
	)
{
	ostrstream data;
	data << report;

	SetData(kGShowSpellCheckFinishedID, data);
}


/******************************************************************************
 ShowingSenderString


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

JBoolean 
GPrefsMgr::ShowingSenderString()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGShowSenderStringID, &data);
	assert( ok );

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

	JBoolean show;
	dataStream >> show;
	return show;
}

/******************************************************************************
 ShowSenderString


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

void 
GPrefsMgr::ShowSenderString
	(
	const JBoolean show
	)
{
	ostrstream data;
	data << show;

	SetData(kGShowSenderStringID, data);
}

/******************************************************************************
 GetSenderString


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

JString
GPrefsMgr::GetSenderString()
{
	JStaticBuffer data;
	const JBoolean ok = GetData(kGSenderStringID, &data);
	assert( ok );

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

	JString sender;
	dataStream >> sender;
	return sender;
}

/******************************************************************************
 SetSenderString


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

void
GPrefsMgr::SetSenderString
	(
	const JString& senderString
	)
{
	ostrstream data;
	data << senderString;

	SetData(kGSenderStringID, data);
}

/******************************************************************************
 UsingCustomFromLine


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

JBoolean 
GPrefsMgr::UsingCustomFromLine()
{
	if (IDValid(kGUseCustomFromLineID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGUseCustomFromLineID, &data);
		assert( ok );

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

		JBoolean use;
		dataStream >> use;
		return use;
		}
	return kFalse;
}

/******************************************************************************
 UseCustomFromLine


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

void 
GPrefsMgr::UseCustomFromLine
	(
	const JBoolean useFrom
	)
{
	ostrstream data;
	data << useFrom;

	SetData(kGUseCustomFromLineID, data);
}

/******************************************************************************
 GetCustomFromString


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

JString
GPrefsMgr::GetCustomFromString()
{
	if (IDValid(kGCustomFromLineID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGCustomFromLineID, &data);
		assert( ok );

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

		JString from;
		dataStream >> from;
		return from;
		}
	return JString();
}

/******************************************************************************
 SetCustomFromString


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

void
GPrefsMgr::SetCustomFromString
	(
	const JString& fromLine
	)
{
	ostrstream data;
	data << fromLine;

	SetData(kGCustomFromLineID, data);
}

/******************************************************************************
 MaxQuote

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

JSize
GPrefsMgr::GetMaxQuote()
{
	if (IDValid(kGMaxQuoteID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGMaxQuoteID, &data);
		assert( ok );

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

		JSize max;
		dataStream >> max;
		return max * 1024;
		}
	return kGDefaultMaxQuote * 1024;
}

void
GPrefsMgr::SetMaxQuote
	(
	const JSize max
	)
{
	ostrstream data;
	data << max;

	SetData(kGMaxQuoteID, data);
}

/******************************************************************************
 EncryptionType

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

GPrefsMgr::EncryptionType 
GPrefsMgr::GetEncryptionType()
	const
{
	if (IDValid(kGEncryptionTypeID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kGEncryptionTypeID, &data);
		assert( ok );

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

		int type;
		dataStream >> type;
		return (EncryptionType)type;
		}
	return kPGP2_6;
}

void 
GPrefsMgr::SetEncryptionType
	(
	const EncryptionType type
	)
{
	ostrstream data;
	data << (int)type;

	SetData(kGEncryptionTypeID, data);
}


/******************************************************************************
 EditPrefs


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

void
GPrefsMgr::EditPrefs()
{
	assert(itsDialog == NULL);
	itsDialog = 
		new GMailPrefsDialog(JXGetApplication(), GetDefaultInbox(), 
								GetSMTPHost(), GetSMTPUser(),
								GetReplyTo(), ShowingStateInTitle(), 
								GetPopServer(), GetPopUserName(),
								UsingPopServer(), LeaveOnServer(), 
								RememberPasswd(), UsingAPop(), 
								AutoInsertingQuote(), AutoCheckingMail(),
								CheckMailDelay(), ShowingSenderString(),
								GetSenderString(), UsingCustomFromLine(),
								GetCustomFromString(), GetDefaultMonoFont(),
								GetDefaultFontSize(), GetMaxQuote()/1024,
								IsReportingSCErrors(), GetTabCharWidth(),
								IsBeepingOnNewMail(), (JIndex)GetEncryptionType());
	assert(itsDialog != NULL);
	ListenTo(itsDialog);
	itsDialog->BeginDialog();
}

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


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

void
GPrefsMgr::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
GPrefsMgr::WriteFindDialogPrefs
	(
	JXSRTextDialogBase* dialog
	)
{
	ostrstream data;
	dialog->WriteSetup(data);
	SetData(kFindDialogPrefsID, data);
}

/******************************************************************************
 ReadChooseSaveDialogPrefs


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

void
GPrefsMgr::ReadChooseSaveDialogPrefs()
{
	if (IDValid(kChooseSaveDialogPrefsID))
		{
		JStaticBuffer data;
		JBoolean ok = GetData(kChooseSaveDialogPrefsID, &data);
		assert( ok );

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

		JXGetChooseSaveFile()->ReadSetup(dataStream);
		}
}

/******************************************************************************
 WriteChooseSaveDialogPrefs


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

void
GPrefsMgr::WriteChooseSaveDialogPrefs()
{
	ostrstream data;
	JXGetChooseSaveFile()->WriteSetup(data);
	SetData(kChooseSaveDialogPrefsID, data);
}

/******************************************************************************
 CompareFileNames (static private)

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

JOrderedSetT::CompareResult
GPrefsMgr::CompareFileNames
	(
	JString* const & n1,
	JString* const & n2
	)
{
	JString s1(*n1);
	JString s2(*n2);
	JIndex index;
	if (s1.LocateLastSubstring("/",&index))
		{
		s1.RemoveSubstring(1, index);
		}
	if (s2.LocateLastSubstring("/",&index))
		{
		s2.RemoveSubstring(1, index);
		}
	const int r = JStringCompare(s1, s2, kFalse);

	if (r > 0)
		{
		return JOrderedSetT::kFirstGreaterSecond;
		}
	else if (r < 0)
		{
		return JOrderedSetT::kFirstLessSecond;
		}
	else
		{
		return JOrderedSetT::kFirstEqualSecond;
		}
}

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

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

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

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

/******************************************************************************
 TableWindowSize


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

void
GPrefsMgr::GetTableWindowSize
	(
	JXWindow* window
	)
	const
{
	GetWindowSize(kGTableWindowSizeID, window);
}

void
GPrefsMgr::SaveTableWindowSize
	(
	JXWindow* window
	)
{
	SaveWindowSize(kGTableWindowSizeID, window);
}

/******************************************************************************
 ViewWindowSize


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

void
GPrefsMgr::GetViewWindowSize
	(
	JXWindow* window
	)
	const
{
	GetWindowSize(kGViewWindowSizeID, window);
}

void
GPrefsMgr::SaveViewWindowSize
	(
	JXWindow* window
	)
{
	SaveWindowSize(kGViewWindowSizeID, window);
}

/******************************************************************************
 EditorWindowSize


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

void
GPrefsMgr::GetEditorWindowSize
	(
	JXWindow* window
	)
	const
{
	GetWindowSize(kGEditorWindowSizeID, window);
}

void
GPrefsMgr::SaveEditorWindowSize
	(
	JXWindow* window
	)
{
	SaveWindowSize(kGEditorWindowSizeID, window);
}


/******************************************************************************
 WindowSize (private)

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

void
GPrefsMgr::GetWindowSize
	(
	const JPrefID	id,
	JXWindow*		window
	)
	const
{
	if (IDValid(id))
		{
		JStaticBuffer data;
		GetData(id, &data);
		jistrstream(dataStream, data.GetData(), strlen(data));
		window->ReadGeometry(dataStream);
		}
}

void
GPrefsMgr::SaveWindowSize
	(
	const JPrefID	id,
	JXWindow*		window
	)
{
	ostrstream data;
	window->WriteGeometry(data);
//	JPoint dtl = window->GetDesktopLocation();
//	window->Place(dtl.x, dtl.y);

	SetData(id, data);
}

/******************************************************************************
 IsBeepingOnNewMail


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

JBoolean 
GPrefsMgr::IsBeepingOnNewMail()
{
	if (IDValid(kGBeepOnNewMailID))
		{
		JStaticBuffer data;
		const JBoolean ok = GetData(kGBeepOnNewMailID, &data);
		assert( ok );

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

		JBoolean beep;
		dataStream >> beep;
		return beep;
		}
	return kTrue;
}

/******************************************************************************
 BeepOnNewMail


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

void 
GPrefsMgr::BeepOnNewMail
	(
	const JBoolean beep
	)
{
	ostrstream data;
	data << beep;

	SetData(kGBeepOnNewMailID, data);
}
