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

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

#include "CaOptions.h"

CaOptions::CaOptions()
{
}

CaOptions::~CaOptions()
{
}

void
CaOptions::serialize(QIODevice &dev)
{
	QXmlStreamWriter xml(&dev);
	xml.setAutoFormatting(true);

	xml.writeStartDocument();
	xml.writeStartElement("CA_XML_CONFIG");

	mailConfig.serialize(xml);
	ftpConfig.serialize(xml);

	xml.writeEndElement();
	xml.writeEndDocument();
}

int
CaOptions::FTPConfig::getActiveCount()
{
	int active = 0;
	for(int i=0; i<mAccounts.size(); i++)
	{
		if(mAccounts[i].mUseAccount)
		{
			active ++;
		}
	}

	return active;
}

bool
CaOptions::FTPConfig::deserialize(QXmlStreamReader &xml)
{
	mAccounts.clear();

	while (!xml.atEnd())
	{
		xml.readNext();
		QString xname = xml.name().toString();

		if(xml.isStartElement())
		{
			if(xname == "FTP_ACCOUNTS")
			{
				while (!xml.atEnd())
				{
					xml.readNext();
					xname = xml.name().toString();
					
					if(xml.isStartElement())
					{
						if(xname == "FTP_ACCOUNT")
						{
							FTPAccount acc;
							if(acc.deserialize(xml))
								mAccounts.append(acc);
						}
					}					
					else if(xml.isEndElement() && xname == "FTP_ACCOUNTS")
						return true;
				}
			}
		}
		else if(xml.isEndElement() && xname == "FTP_CONFIG")
			return true;
	}

	return false;
}

bool
CaOptions::FTPConfig::FTPAccount::deserialize(QXmlStreamReader &xml)
{
	mPaths.clear();

	QXmlStreamAttributes atts = xml.attributes();

	mName = atts.value("name").toString();
	mServer = atts.value("server").toString();
	mUser = atts.value("user").toString();
	QString pass = atts.value("pass").toString();
	mPass = QString(QByteArray::fromBase64(pass.toUtf8())); // must be unencrypted
	mUseAccount = atts.value("use").toString() == "1" ? true : false;

	while (!xml.atEnd())
	{
		xml.readNext();
		QString xname = xml.name().toString();

		if(xml.isStartElement())
		{
			if(xname == "PATH")
			{
				QString path = xml.readElementText();
				mPaths.append(path);
			}
		}
		else if(xml.isEndElement() && xname == "FTP_ACCOUNT")
			return true;
	}
	
	return false;
}

void
CaOptions::FTPConfig::serialize(QXmlStreamWriter &xml)
{
	xml.writeStartElement("FTP_CONFIG");
	xml.writeStartElement("FTP_ACCOUNTS");

	foreach(FTPAccount acc, mAccounts)
	{
		acc.serialize(xml);
	}

	xml.writeEndElement();
	xml.writeEndElement();
}

void
CaOptions::FTPConfig::FTPAccount::serialize(QXmlStreamWriter &xml)
{
	xml.writeStartElement("FTP_ACCOUNT");

	xml.writeAttribute("name", mName);
	xml.writeAttribute("server", mServer);
	xml.writeAttribute("user", mUser);
	xml.writeAttribute("pass", QString(QByteArray(mPass.toAscii()).toBase64())); // must be encrypted
	xml.writeAttribute("use", mUseAccount ? "1" : "0");

	foreach(QString path, mPaths)
	{
		xml.writeTextElement("PATH", path);
	}

	xml.writeEndElement();
}

bool
CaOptions::deserialize(QIODevice &dev)
{
	bool result = false;

	QXmlStreamReader xml(&dev);
	QString xname;
	
	while (!xml.atEnd())
	{
		xml.readNext();
		xname = xml.name().toString();

		if(xml.isStartElement())
		{
			if(xname == "CA_XML_CONFIG" && !result)
			{
				result = true;
			}
			else if(result)
			{
				if(xname == "MAIL_CONFIG")
					mailConfig.deserialize(xml);
				else if(xname == "FTP_CONFIG")
					ftpConfig.deserialize(xml);
			}
		}
		else if(xml.isEndElement() && xname == "CA_XML_CONFIG")
			break;
	}
	if (xml.hasError())
	{
		result = false;
	}

	return result;
}

