/*
 *  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.
 */

// MailConfig.cpp: implementation of the CaOptions class.
//
//////////////////////////////////////////////////////////////////////

#include "MailConfig.h"

MailConfig::MailConfig()
{
	mUseMail = true;
	mOutgoingAuth = false;
	mUseIncomming = true;
}

bool
MailConfig::checkOutgoingConfig()
{
	if(mSmtpServer.isEmpty())
		return false;
	
	if(mOutgoingAuth)
	{
		if(!mUseIncomming && (mSmtpUser.isEmpty() || mSmtpPass.isEmpty()))
			return false;
			
		if(mUseIncomming && (mPop3User.isEmpty() || mPop3Pass.isEmpty()))
			return false;
	}
	
	return true;
}

bool
MailConfig::checkIncommingConfig()
{
	if(mPop3Server.isEmpty() || mPop3User.isEmpty() || mPop3Pass.isEmpty())
		return false;
	
	return true;
}

void
MailConfig::serialize(QXmlStreamWriter &xml)
{
	xml.writeStartElement("MAIL_CONFIG");
	xml.writeAttribute("user", mUser);
	xml.writeAttribute("email", mMail);
	xml.writeAttribute("smtp", mSmtpServer);
	xml.writeAttribute("smtpuser", mSmtpUser);
	xml.writeAttribute("smtppass", QString(QByteArray(mSmtpPass.toAscii()).toBase64())); // must be encrypted
	xml.writeAttribute("pop3", mPop3Server);
	xml.writeAttribute("pop3user", mPop3User);
	xml.writeAttribute("pop3pass", QString(QByteArray(mPop3Pass.toAscii()).toBase64())); // must be encrypted

	xml.writeAttribute("auth", mOutgoingAuth ? "1" : "0");
	xml.writeAttribute("useinc", mUseIncomming ? "1" : "0");

	xml.writeTextElement("REQ_FILTER", mReqFilter);
	xml.writeTextElement("USE_MAIL", mUseMail ? "1" : "0");

	xml.writeEndElement();
}

bool
MailConfig::deserialize(QXmlStreamReader &xml)
{
	QXmlStreamAttributes atts = xml.attributes();

	mUser = atts.value("user").toString();
	mMail = atts.value("email").toString();
	
	mSmtpServer = atts.value("smtp").toString();
	mSmtpUser = atts.value("smtpuser").toString();
	QString pass = atts.value("smtppass").toString();
	mSmtpPass = QString(QByteArray::fromBase64(pass.toUtf8())); // must be unencrypted
	
	mPop3Server = atts.value("pop3").toString();
	mPop3User = atts.value("pop3user").toString();
	pass = atts.value("pop3pass").toString();
	mPop3Pass = QString(QByteArray::fromBase64(pass.toUtf8())); // must be unencrypted

	mOutgoingAuth = atts.value("auth").toString() == "1";
	mUseIncomming = atts.value("useinc").toString() == "1";
		
	while (!xml.atEnd())
	{
		xml.readNext();
		QString xname = xml.name().toString();

		if(xml.isStartElement())
		{
			if(xname == "USE_MAIL")
				mUseMail = xml.readElementText() == "1" ? true : false;
			else if(xname == "REQ_FILTER")
				mReqFilter = xml.readElementText();
		}
		else if(xml.isEndElement() && xname == "MAIL_CONFIG")
			return true;
	}

	return false;
}
