/******************************************************************************
 GMailPrefsDialog.cc

	BASE CLASS = JXDialogDirector

	Copyright @ 1997 by Glenn W. Bach.

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

#include "GMailPrefsDialog.h"
#include <GHelpText.h>

#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXInputField.h>
#include <JXTextCheckbox.h>
#include <JXStaticText.h>
#include <JXIntegerInput.h>
#include <JXChooseSaveFile.h>
#include <JXHelpManager.h>
#include <JXChooseMonoFont.h>
#include <GMCustomFromInput.h>
#include <JXRadioGroup.h>
#include <JXTextRadioButton.h>

#include <JColormap.h>
#include <JString.h>

#include <jXGlobals.h>

#include <jAssert.h>

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

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

GMailPrefsDialog::GMailPrefsDialog
	(
	JXDirector* 	supervisor,
	const JString& 	inbox,
	const JString& 	smtp,
	const JString& 	smtpuser,
	const JString& 	repltyto,
	const JBoolean 	showstate,
	const JString& 	pop,
	const JString& 	account,
	const JBoolean 	usePop,
	const JBoolean 	leave,
	const JBoolean 	save,
	const JBoolean 	useAPop,
	const JBoolean 	autoQuote,
	const JBoolean 	autoCheck,
	const JInteger	minutes,
	const JBoolean 	showSender,
	const JString& 	senderString,
	const JBoolean 	useFrom,
	const JString& 	fromLine,
	const JString& 	fontname,
	const JSize 	fontsize,
	const JSize		maxQuotedReply,
	const JBoolean	reportSCErrors,
	const JSize		spacesPerTab,
	const JBoolean 	beepOnNewMail,
	const JIndex	encType
	)
	:
	JXDialogDirector(supervisor, kTrue)
{
	BuildWindow(inbox, smtp, smtpuser, repltyto, showstate, pop, account, usePop, leave, 
					save, useAPop, autoQuote, autoCheck, minutes, showSender,
					senderString, useFrom, fromLine, fontname, fontsize, 
					maxQuotedReply, reportSCErrors, spacesPerTab, beepOnNewMail,
					encType);
}

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

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

GMailPrefsDialog::~GMailPrefsDialog()
{
}

/******************************************************************************
 BuildWindow (private)

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

void
GMailPrefsDialog::BuildWindow
	(
	const JString& 	inbox,
	const JString& 	smtp,
	const JString& 	smtpuser,
	const JString& 	replyto,
	const JBoolean 	showstate,
	const JString& 	pop,
	const JString& 	account,
	const JBoolean 	usePop,
	const JBoolean 	leave,
	const JBoolean 	save,
	const JBoolean 	useAPop,
	const JBoolean 	autoQuote,
	const JBoolean 	autoCheck,
	const JInteger	minutes,
	const JBoolean 	showSender,
	const JString& 	senderString,
	const JBoolean 	useFrom,
	const JString& 	fromLine,
	const JString& 	fontname,
	const JSize 	fontsize,
	const JSize		maxQuotedReply,
	const JBoolean	reportSCErrors,
	const JSize		spacesPerTab,
	const JBoolean 	beepOnNewMail,
	const JIndex	encType
	)
{
	
// begin JXLayout

    JXWindow* window = new JXWindow(this, 740,470, "");
    assert( window != NULL );
    SetWindow(window);

    itsSMTPServerInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 110,50, 240,20);
    assert( itsSMTPServerInput != NULL );

    itsSMTPUserName =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 110,80, 240,20);
    assert( itsSMTPUserName != NULL );

    itsReplyToAddress =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 110,110, 240,20);
    assert( itsReplyToAddress != NULL );

    itsMaxReplyInput =
        new JXIntegerInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 270,300, 40,20);
    assert( itsMaxReplyInput != NULL );

    itsSenderStringInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 110,360, 240,20);
    assert( itsSenderStringInput != NULL );

    itsSpacesPerTabInput =
        new JXIntegerInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 485,100, 40,20);
    assert( itsSpacesPerTabInput != NULL );

    itsCheckMailDelay =
        new JXIntegerInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 595,337, 40,20);
    assert( itsCheckMailDelay != NULL );

    itsPopServerInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 480,375, 230,20);
    assert( itsPopServerInput != NULL );

    itsPopAccountInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 480,405, 230,20);
    assert( itsPopAccountInput != NULL );

    itsChooseInboxButton =
        new JXTextButton("Choose", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 290,20, 60,20);
    assert( itsChooseInboxButton != NULL );
    itsChooseInboxButton->SetShortcuts("#O");

    JXStaticText* obj1 =
        new JXStaticText("Default inbox:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,20, 90,20);
    assert( obj1 != NULL );

    JXStaticText* obj2 =
        new JXStaticText("SMTP server:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,53, 90,20);
    assert( obj2 != NULL );

    itsPOPText3 =
        new JXStaticText("POP server:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 380,378, 85,20);
    assert( itsPOPText3 != NULL );

    itsUsePopButton =
        new JXTextCheckbox("Use POP server", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 380,230, 120,20);
    assert( itsUsePopButton != NULL );

    itsLeaveMailButton =
        new JXTextCheckbox("Leave mail on server", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 390,260, 205,20);
    assert( itsLeaveMailButton != NULL );

    itsSavePasswdButton =
        new JXTextCheckbox("Save password", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 390,285, 205,20);
    assert( itsSavePasswdButton != NULL );

    itsPOPText4 =
        new JXStaticText("POP account:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 380,408, 90,20);
    assert( itsPOPText4 != NULL );

    JXTextButton* okButton =
        new JXTextButton("OK", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 529,439, 72,22);
    assert( okButton != NULL );
    okButton->SetShortcuts("^M");

    JXTextButton* cancelButton =
        new JXTextButton("Cancel", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 140,440, 70,20);
    assert( cancelButton != NULL );
    cancelButton->SetShortcuts("^[");

    itsInboxText =
        new JXStaticText("", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 110,20, 180,20);
    assert( itsInboxText != NULL );

    itsShowStateInTitle =
        new JXTextCheckbox("Show mailbox state in title", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,270, 250,20);
    assert( itsShowStateInTitle != NULL );

    itsAutoQuote =
        new JXTextCheckbox("Quote message on reply if smaller than", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,300, 250,20);
    assert( itsAutoQuote != NULL );

    itsUseAPop =
        new JXTextCheckbox("Use APOP", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 390,310, 205,20);
    assert( itsUseAPop != NULL );

    itsAutoCheckMail =
        new JXTextCheckbox("Automatically check mail every", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 390,335, 205,20);
    assert( itsAutoCheckMail != NULL );

    itsPOPText2 =
        new JXStaticText("minutes", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 642,338, 55,19);
    assert( itsPOPText2 != NULL );

    JXStaticText* obj3 =
        new JXStaticText("SMTP user:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,83, 90,20);
    assert( obj3 != NULL );

    JXStaticText* obj4 =
        new JXStaticText("Reply to:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,113, 90,20);
    assert( obj4 != NULL );

    itsShowSenderString =
        new JXTextCheckbox("Show sender string on reply", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,330, 250,20);
    assert( itsShowSenderString != NULL );

    itsSenderText =
        new JXStaticText("String:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 55,363, 50,20);
    assert( itsSenderText != NULL );

    itsFromLineCB =
        new JXTextCheckbox("Use custom from line", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,155, 230,20);
    assert( itsFromLineCB != NULL );

    itsFromLineText =
        new JXStaticText("From line:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 30,188, 70,20);
    assert( itsFromLineText != NULL );

    itsHelpButton =
        new JXTextButton("Help", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 335,440, 70,20);
    assert( itsHelpButton != NULL );

    itsFontMenu =
        new JXChooseMonoFont(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 380,20, 330,70);
    assert( itsFontMenu != NULL );

    JXStaticText* obj5 =
        new JXStaticText("KB", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 320,303, 30,20);
    assert( obj5 != NULL );

    itsReportSCErrorsCB =
        new JXTextCheckbox("Report no errors on spell check", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,405, 230,20);
    assert( itsReportSCErrorsCB != NULL );

    JXStaticText* obj6 =
        new JXStaticText("Spaces per tab:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 380,103, 100,20);
    assert( obj6 != NULL );

    itsFromLineInput =
        new GMCustomFromInput(itsFromLineCB, window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 110,185, 240,20);
    assert( itsFromLineInput != NULL );

    itsBeepOnNewMailCB =
        new JXTextCheckbox("Beep when new mail arrives", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,240, 250,20);
    assert( itsBeepOnNewMailCB != NULL );

    itsEncryptionType =
        new JXRadioGroup(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 435,140, 145,70);
    assert( itsEncryptionType != NULL );

    JXTextRadioButton* obj7 =
        new JXTextRadioButton(1, "PGP 2.6.2", itsEncryptionType,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,15, 90,20);
    assert( obj7 != NULL );

    JXTextRadioButton* obj8 =
        new JXTextRadioButton(2, "GnuPG 1.0", itsEncryptionType,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,35, 90,20);
    assert( obj8 != NULL );

    JXStaticText* obj9 =
        new JXStaticText("Encryption program", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 445,133, 120,20);
    assert( obj9 != NULL );

// end JXLayout

	window->SetTitle("Mail Preferences");
	SetButtons(okButton, cancelButton);

	itsInboxText->SetBorderWidth(2);

	ListenTo(itsChooseInboxButton);
	ListenTo(itsUsePopButton);
	ListenTo(itsShowSenderString);
	ListenTo(itsFromLineCB);
	ListenTo(itsHelpButton);
	ListenTo(itsAutoQuote);
	ListenTo(itsAutoCheckMail);

	itsInboxText->SetText(inbox);
	itsSMTPServerInput->SetText(smtp);
	itsSMTPUserName->SetText(smtpuser);
	itsReplyToAddress->SetText(replyto);
	itsPopServerInput->SetText(pop);
	itsPopAccountInput->SetText(account);

	itsShowStateInTitle->SetState(showstate);
	itsUsePopButton->SetState(usePop);
	itsLeaveMailButton->SetState(leave);
	itsSavePasswdButton->SetState(save);
	itsUseAPop->SetState(useAPop);

	itsAutoQuote->SetState(autoQuote);
	itsAutoCheckMail->SetState(autoCheck);
	itsCheckMailDelay->SetValue(minutes);

	itsSenderStringInput->SetText(senderString);
	itsShowSenderString->SetState(showSender);

	itsFromLineInput->SetText(fromLine);
	itsFromLineCB->SetState(useFrom);

	itsFontMenu->SetFont(fontname, fontsize);

	itsMaxReplyInput->SetValue(maxQuotedReply);

	itsSpacesPerTabInput->SetValue(spacesPerTab);

	itsReportSCErrorsCB->SetState(reportSCErrors);

	itsBeepOnNewMailCB->SetState(beepOnNewMail);

	AdjustForPOP(usePop);
	AdjustForAutoCheck(autoCheck);
	AdjustForSenderString(showSender);
	AdjustForFromLine(useFrom);
	AdjustForQuotedReply(autoQuote);

	itsEncryptionType->SelectItem(encType);
}

/******************************************************************************
 GetValues 

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

void
GMailPrefsDialog::GetValues
	(
	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*	minutes,
	JBoolean*	showSender,
	JString*	senderString,
	JBoolean*	useFrom,
	JString*	fromLine,
	JString*	fontname,
	JSize*		fontsize,
	JSize*		maxQuotedReply,
	JBoolean* 	reportSCErrors,
	JSize*		spacesPerTab,
	JBoolean* 	beepOnNewMail,
	JIndex*		encType
	)
{
	*inbox 		= itsInboxText->GetText();
	*smtp 		= itsSMTPServerInput->GetText();
	*smtpuser 	= itsSMTPUserName->GetText();
	*replyto 	= itsReplyToAddress->GetText();
	*showstate 	= itsShowStateInTitle->IsChecked();
	*pop 		= itsPopServerInput->GetText();
	*account 	= itsPopAccountInput->GetText();

	*usePop 	= itsUsePopButton->IsChecked();
	*leave 		= itsLeaveMailButton->IsChecked();
	*save 		= itsSavePasswdButton->IsChecked();

	*useAPop 	= itsUseAPop->IsChecked();
	*autoQuote 	= itsAutoQuote->IsChecked();

	*autoCheck		= itsAutoCheckMail->IsChecked();
	itsCheckMailDelay->GetValue(minutes);

	*showSender 	= itsShowSenderString->IsChecked();
	*senderString 	= itsSenderStringInput->GetText();

	*useFrom	 	= itsFromLineCB->IsChecked();
	*fromLine	 	= itsFromLineInput->GetText();

	*beepOnNewMail	= itsBeepOnNewMailCB->IsChecked();

	JInteger value;
	if (itsMaxReplyInput->GetValue(&value))
		{
		*maxQuotedReply = value;
		}
	else
		{
		*maxQuotedReply = 0;
		}

	itsFontMenu->GetFont(fontname, fontsize);

	*reportSCErrors	= itsReportSCErrorsCB->IsChecked();

	if (itsSpacesPerTabInput->GetValue(&value))
		{
		*spacesPerTab	= value;
		}
	else
		{
		*spacesPerTab	= 0;
		}

	*encType		= itsEncryptionType->GetSelectedItem();
}

/******************************************************************************
 Receive 

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

void
GMailPrefsDialog::Receive
	(
	JBroadcaster* 	sender, 
	const Message& 	message
	)
{
	if (sender == itsChooseInboxButton && message.Is(JXButton::kPushed))
		{
		JString mbox;
		if (JXGetChooseSaveFile()->ChooseFile("Choose default inbox:", "", &mbox))
			{
			itsInboxText->SetText(mbox);
			}
		}
	else if (sender == itsHelpButton && message.Is(JXButton::kPushed))
		{
		(JXGetHelpManager())->ShowSection(kPrefsHelpName);
		}
	else if (sender == itsUsePopButton && message.Is(JXCheckbox::kPushed))
		{
		const JXCheckbox::Pushed* info = 
			dynamic_cast(const JXCheckbox::Pushed*, &message);
		assert(info != NULL);
		AdjustForPOP(info->IsChecked());
		}
	else if (sender == itsShowSenderString && message.Is(JXCheckbox::kPushed))
		{
		const JXCheckbox::Pushed* info = 
			dynamic_cast(const JXCheckbox::Pushed*, &message);
		assert(info != NULL);
		AdjustForSenderString(info->IsChecked());
		}
	else if (sender == itsFromLineCB && message.Is(JXCheckbox::kPushed))
		{
		const JXCheckbox::Pushed* info = 
			dynamic_cast(const JXCheckbox::Pushed*, &message);
		assert(info != NULL);
		AdjustForFromLine(info->IsChecked());
		}
	else if (sender == itsAutoQuote && message.Is(JXCheckbox::kPushed))
		{
		const JXCheckbox::Pushed* info = 
			dynamic_cast(const JXCheckbox::Pushed*, &message);
		assert(info != NULL);
		AdjustForQuotedReply(info->IsChecked());
		}
	else if (sender == itsAutoCheckMail && message.Is(JXCheckbox::kPushed))
		{
		const JXCheckbox::Pushed* info = 
			dynamic_cast(const JXCheckbox::Pushed*, &message);
		assert(info != NULL);
		AdjustForAutoCheck(info->IsChecked());
		}
	else
		{
		JXDialogDirector::Receive(sender, message);
		}
}

/******************************************************************************
 AdjustForPOP (private)

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

void
GMailPrefsDialog::AdjustForPOP
	(
	const JBoolean usePOP
	)
{
	JFontStyle style(kFalse, kFalse, 0, kFalse, JGetCurrColormap()->GetInactiveLabelColor());
	if (usePOP)
		{
		itsPopServerInput->Activate();
		itsLeaveMailButton->Activate();
		itsSavePasswdButton->Activate();
		itsPopAccountInput->Activate();
		itsUseAPop->Activate();
		itsAutoCheckMail->Activate();
		itsCheckMailDelay->Activate();
		itsPOPText2->SetFontStyle(JFontStyle());
		itsPOPText3->SetFontStyle(JFontStyle());
		itsPOPText4->SetFontStyle(JFontStyle());
		itsCheckMailDelay->SetFontStyle(JFontStyle());
		itsPopAccountInput->SetFontStyle(JFontStyle());
		itsPopServerInput->SetFontStyle(JFontStyle());
		}
	else
		{
		itsPopServerInput->Deactivate();
		itsLeaveMailButton->Deactivate();
		itsSavePasswdButton->Deactivate();
		itsPopAccountInput->Deactivate();
		itsUseAPop->Deactivate();
		itsAutoCheckMail->Deactivate();
		itsCheckMailDelay->Deactivate();
		itsPOPText2->SetFontStyle(style);
		itsPOPText3->SetFontStyle(style);
		itsPOPText4->SetFontStyle(style);
		itsCheckMailDelay->SetFontStyle(style);
		itsPopAccountInput->SetFontStyle(style);
		itsPopServerInput->SetFontStyle(style);
		}
}

/******************************************************************************
 AdjustForSenderString (private)

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

void
GMailPrefsDialog::AdjustForSenderString
	(
	const JBoolean showSender
	)
{
	JFontStyle style(kFalse, kFalse, 0, kFalse, JGetCurrColormap()->GetInactiveLabelColor());
	if (showSender)
		{
		itsSenderStringInput->Activate();
		itsSenderText->SetFontStyle(JFontStyle());
		itsSenderStringInput->SetFontStyle(JFontStyle());
		}
	else
		{
		itsSenderStringInput->Deactivate();
		itsSenderText->SetFontStyle(style);
		itsSenderStringInput->SetFontStyle(style);
		}
}

/******************************************************************************
 AdjustForFromLine (private)

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

void
GMailPrefsDialog::AdjustForFromLine
	(
	const JBoolean useFrom
	)
{
	JFontStyle style(kFalse, kFalse, 0, kFalse, JGetCurrColormap()->GetInactiveLabelColor());
	if (useFrom)
		{
		itsFromLineInput->Activate();
		itsFromLineText->SetFontStyle(JFontStyle());
		itsFromLineInput->SetFontStyle(JFontStyle());
		}
	else
		{
		itsFromLineInput->Deactivate();
		itsFromLineText->SetFontStyle(style);
		itsFromLineInput->SetFontStyle(style);
		}
}

/******************************************************************************
 AdjustForAutoCheck (private)

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

void
GMailPrefsDialog::AdjustForAutoCheck
	(
	const JBoolean check
	)
{
	if (check)
		{
		itsCheckMailDelay->Activate();
		}
	else
		{
		itsCheckMailDelay->Deactivate();
		}
}

/******************************************************************************
 AdjustForQuotedReply (private)

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

void
GMailPrefsDialog::AdjustForQuotedReply
	(
	const JBoolean quote
	)
{
	if (quote)
		{
		itsMaxReplyInput->Activate();
		}
	else
		{
		itsMaxReplyInput->Deactivate();
		}
}
