/*
Copyright 2010-2013 Grégory Soutadé
This file is part of KissCount.
KissCount is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KissCount 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.
You should have received a copy of the GNU General Public License
along with KissCount. If not, see .
*/
#include
#include
#include
#include "XMLExportEngine.hpp"
static XMLExportEngine xmlExportEngine;
XMLExportEngine::XMLExportEngine()
{
KissCount::RegisterExportEngine(this);
_shortExt = ".xml";
_longExt = _("KissCount XML files (*.xml)");
}
XMLExportEngine::~XMLExportEngine()
{
}
bool XMLExportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
{
return ExportEngine::HandleFile(path, user, db, kiss);
}
bool XMLExportEngine::SaveAccounts()
{
Account account;
std::map::iterator it;
for(it=_accounts.begin(); it!=_accounts.end(); it++)
{
try
{
account = _user->GetAccount(it->first);
}
catch(User::AccountNotFound)
{
account.id = it->first;
account.name = "Unknown";
account.blocked = false;
account._virtual = false;
account.hidden = false;
}
ESCAPE_CHARS(account.name);
ESCAPE_CHARS(account.number);
_writer->writeStartElement("account");
_writer->writeAttribute("id", QString::number(account.id));
_writer->writeAttribute("name", account.name);
_writer->writeAttribute("number", account.number);
// _writer->writeAttribute("shared", (account.shared ? "1" : "0"));
_writer->writeAttribute("blocked", (account.blocked ? "1" : "0"));
// _writer->writeAttribute("default", (account._default ? "1" : "0"));
// _writer->writeAttribute("is_owner", (account.is_owner ? "1" : "0"));
_writer->writeAttribute("virtual", (account._virtual ? "1" : "0"));
_writer->writeAttribute("hidden", (account.hidden ? "1" : "0"));
_writer->writeEndElement();
}
return true;
}
bool XMLExportEngine::SaveAccountAmounts()
{
std::map::iterator it;
QString v;
for(it=_accountAmounts.begin(); it!=_accountAmounts.end(); it++)
{
_writer->writeStartElement("account_amount");
_writer->writeAttribute("account", QString::number(it->first.account));
_writer->writeAttribute("month", QString::number(it->first.month));
_writer->writeAttribute("year", QString::number(it->first.year));
_writer->writeAttribute("amount", v.sprintf("%d", it->second));
_writer->writeEndElement();
}
return true;
}
bool XMLExportEngine::SaveCategories()
{
Category category;
std::map::iterator it;
int rgb;
QString v;
for(it=_categories.begin(); it!=_categories.end(); it++)
{
category = _user->GetCategory(it->first);
ESCAPE_CHARS(category.name);
_writer->writeStartElement("category");
_writer->writeAttribute("id", QString::number(category.id));
_writer->writeAttribute("parent", QString::number(category.parent));
_writer->writeAttribute("name", category.name);
_writer->writeAttribute("font", category.font);
rgb = category.backcolor.blue();
rgb |= category.backcolor.green() << 8;
rgb |= category.backcolor.red() << 16;
_writer->writeAttribute("backcolor", v.sprintf("0x%08X", rgb));
rgb = category.forecolor.blue();
rgb |= category.forecolor.green() << 8;
rgb |= category.forecolor.red() << 16;
_writer->writeAttribute("forecolor", v.sprintf("0x%08X", rgb));
_writer->writeAttribute("fix_cost", (category.fix_cost ? "1" : "0"));
_writer->writeEndElement();
}
return true;
}
bool XMLExportEngine::SaveOperations(std::vector* operations)
{
std::vector::iterator it;
QString v;
for(it=operations->begin(); it!=operations->end(); it++)
{
ESCAPE_CHARS(it->description);
_writer->writeStartElement("operation");
_writer->writeAttribute("id", QString::number(it->id));
_writer->writeAttribute("parent", QString::number(it->parent));
_writer->writeAttribute("day", QString::number(it->day));
_writer->writeAttribute("month", QString::number(it->month));
_writer->writeAttribute("year", QString::number(it->year));
_writer->writeAttribute("amount", v.sprintf("%d", it->amount));
_writer->writeAttribute("description", it->description);
_writer->writeAttribute("category", QString::number(it->category));
_writer->writeAttribute("fix_cost", (it->fix_cost ? "1" : "0"));
_writer->writeAttribute("account", QString::number(it->account));
_writer->writeAttribute("checked", (it->checked ? "1" : "0"));
_writer->writeAttribute("transfert", QString::number(it->transfert));
_writer->writeAttribute("formula", it->formula);
_writer->writeAttribute("meta", (it->meta ? "1" : "0"));
_writer->writeAttribute("virtual", (it->_virtual ? "1" : "0"));
_writer->writeEndElement();
}
return true;
}
bool XMLExportEngine::SaveFile(std::vector* operations)
{
QFile file(_path);
int rc;
rc = ExportEngine::SaveFile(operations);
if (!rc) return false;
if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
{
std::cout << "Error can't open the file " << _path.toStdString() << std::endl;
return false;
}
_writer = new QXmlStreamWriter(&file);
_writer->setCodec("UTF-8");
_writer->setAutoFormatting(true);
_writer->setAutoFormattingIndent(4);
_writer->writeStartDocument("1.0");
_writer->writeStartElement("kisscount");
_writer->writeAttribute("version", "1");
SaveAccounts();
SaveAccountAmounts();
SaveCategories();
SaveOperations(operations);
_writer->writeEndElement();
_writer->writeEndDocument();
file.flush();
file.close();
delete _writer;
return true;
}