/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

// MailAccountWidget.cpp: implementation of the MailAccountWidget class.
//
//////////////////////////////////////////////////////////////////////

#include "MailAccountWidget.h"

MailAccountWidget::MailAccountWidget(bool displayFilter, bool usePop3, QWidget *parent) : QWidget(parent)
{
	mUsePop3 = usePop3;
	QGridLayout *layout = new QGridLayout();
	
	mReqFilter = 0;
	setLayout(layout);
	
	mBox = new QGroupBox(tr("E-mail account setup"));
	QGridLayout *boxLayout = new QGridLayout();
	
	int row = 0;
	mBox->setLayout(boxLayout);
	
	boxLayout->addWidget(new QLabel(tr("Name")), row, 0);
	mUser = new QLineEdit();
	boxLayout->addWidget(mUser, row ++, 1);

	boxLayout->addWidget(new QLabel(tr("E-mail address")), row, 0);
	mMail = new QLineEdit();
	boxLayout->addWidget(mMail, row ++, 1);
	
	// incoming
	mPop3Server = new QLineEdit();
	mPop3User = new QLineEdit();
	mPop3Pass = new QLineEdit();

	if(mUsePop3)
	{
//		boxLayout->addWidget(new QLabel(), row ++, 0, 1, 2);
		boxLayout->addWidget(new QLabel(tr("<u><b>Incoming mail server (POP3)</b></u>")), row ++, 0, 1, 2);

		boxLayout->addWidget(new QLabel(tr("Server address")), row, 0);
		boxLayout->addWidget(mPop3Server, row ++, 1);
		
		boxLayout->addWidget(new QLabel(tr("Account name")), row, 0);
		boxLayout->addWidget(mPop3User, row ++, 1);
		
		boxLayout->addWidget(new QLabel(tr("Password")), row, 0);
		mPop3Pass->setEchoMode(QLineEdit::Password);
		boxLayout->addWidget(mPop3Pass, row ++, 1);
	}

	if(displayFilter)
	{
		boxLayout->addWidget(new QLabel(tr("Request message<br>filter mask")), row, 0);
		mReqFilter = new QLineEdit();
		boxLayout->addWidget(mReqFilter, row ++, 1);
	}

	// outgoing
//	boxLayout->addWidget(new QLabel(), row ++, 0, 1, 2);
	boxLayout->addWidget(new QLabel(tr("<u><b>Outgoing mail server (SMTP)</b></u>")), row ++, 0, 1, 2);
	
	boxLayout->addWidget(new QLabel(tr("Server address")), row, 0);
	mSmtpServer = new QLineEdit();
	boxLayout->addWidget(mSmtpServer, row ++, 1);
	
//	boxLayout->addWidget(new QLabel(), row ++, 0, 1, 2);

	mOutgoingAuth = new QCheckBox(tr("Server requires authentication"));
	boxLayout->addWidget(mOutgoingAuth, row ++, 0, 1, 2);
	connect(mOutgoingAuth, SIGNAL(stateChanged(int)), this, SLOT(autentStateChanged(int)));

	mUseIncomming = new QCheckBox(tr("Use same settings as my incoming mail server"));
	boxLayout->addWidget(mUseIncomming, row ++, 0, 1, 2);
	connect(mUseIncomming, SIGNAL(stateChanged(int)), this, SLOT(useIncomingStateChanged(int)));
	if(!mUsePop3)
		mUseIncomming->hide();

	mIncName = new QLabel(tr("Account name"));
	boxLayout->addWidget(mIncName, row, 0);
	mSmtpUser = new QLineEdit();
	boxLayout->addWidget(mSmtpUser, row ++, 1);
	
	mIncPass = new QLabel(tr("Password"));
	boxLayout->addWidget(mIncPass, row, 0);
	mSmtpPass = new QLineEdit();
	mSmtpPass->setEchoMode(QLineEdit::Password);
	boxLayout->addWidget(mSmtpPass, row ++, 1);

	boxLayout->setColumnStretch(1, 1);
	
	layout->addWidget(mBox, 0, 0, Qt::AlignTop);
	
	// use account
	mUseMail = new QCheckBox(tr("Use mail account"));
	layout->addWidget(mUseMail, 1, 0, Qt::AlignTop);
	connect(mUseMail, SIGNAL(stateChanged(int)), this, SLOT(useMailStateChanged(int)));
}

MailAccountWidget::~MailAccountWidget()
{
}

void
MailAccountWidget::autentStateChanged(int state)
{
	updateAutentState();
}

void
MailAccountWidget::updateAutentState()
{
	mUseIncomming->setEnabled(mOutgoingAuth->checkState() == Qt::Checked);
	updateUseIncomingState();
}

void
MailAccountWidget::useIncomingStateChanged(int state)
{
	updateUseIncomingState();
}

void
MailAccountWidget::updateUseIncomingState()
{
	bool on = mUseIncomming->checkState() == Qt::Unchecked && mUseIncomming->isEnabled();
	mIncName->setEnabled(on);
	mIncPass->setEnabled(on);
	mSmtpUser->setEnabled(on);
	mSmtpPass->setEnabled(on);
}

void
MailAccountWidget::useMailStateChanged(int state)
{
	updateMailState();
}

void
MailAccountWidget::updateMailState()
{
	mBox->setEnabled(mUseMail->checkState() == Qt::Checked);
}

void
MailAccountWidget::setData(MailConfig &data)
{
	if(!mUsePop3)
		data.mUseIncomming = false;

	mUser->setText(data.mUser);
	mMail->setText(data.mMail);
	
	mSmtpServer->setText(data.mSmtpServer);
	mSmtpUser->setText(data.mSmtpUser);
	mSmtpPass->setText(data.mSmtpPass);

	if(mReqFilter)
		mReqFilter->setText(data.mReqFilter);

	mPop3Server->setText(data.mPop3Server);
	mPop3User->setText(data.mPop3User);
	mPop3Pass->setText(data.mPop3Pass);

	mOutgoingAuth->setCheckState(data.mOutgoingAuth ? Qt::Checked : Qt::Unchecked);
	mUseIncomming->setCheckState(data.mUseIncomming ? Qt::Checked : Qt::Unchecked);
	
	mUseMail->setCheckState(data.mUseMail ? Qt::Checked : Qt::Unchecked);
	
	updateAutentState();
	updateMailState();
}

void
MailAccountWidget::getData(MailConfig &data)
{
	data.mUser = mUser->text();
	data.mMail = mMail->text();

	data.mSmtpServer = mSmtpServer->text();
	data.mSmtpUser = mSmtpUser->text();
	data.mSmtpPass = mSmtpPass->text();

	if(mReqFilter)
		data.mReqFilter = mReqFilter->text();

	data.mPop3Server = mPop3Server->text();
	data.mPop3User = mPop3User->text();
	data.mPop3Pass = mPop3Pass->text();

	data.mOutgoingAuth = mOutgoingAuth->checkState() == Qt::Checked ? true : false;
	data.mUseIncomming = mUseIncomming->checkState() == Qt::Checked ? true : false;

	data.mUseMail = mUseMail->checkState() == Qt::Checked ? true : false;
}

void
MailAccountWidget::setEmail(const QString &mail)
{
	mMail->setText(mail);
}

void
MailAccountWidget::setUser(const QString &user)
{
	mUser->setText(user);
}
