2011-07-02 16:39:32 +02:00
|
|
|
/*
|
2013-01-17 20:22:42 +01:00
|
|
|
Copyright 2010-2013 Grégory Soutadé
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
#include <QXmlStreamWriter>
|
|
|
|
#include <QFile>
|
2011-08-20 11:43:12 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2011-08-20 14:02:47 +02:00
|
|
|
#include "XMLExportEngine.hpp"
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
static XMLExportEngine xmlExportEngine;
|
|
|
|
|
|
|
|
XMLExportEngine::XMLExportEngine()
|
|
|
|
{
|
|
|
|
KissCount::RegisterExportEngine(this);
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_shortExt = ".xml";
|
|
|
|
_longExt = _("KissCount XML files (*.xml)");
|
2011-07-02 16:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLExportEngine::~XMLExportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
bool XMLExportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
|
2011-07-02 16:39:32 +02:00
|
|
|
{
|
|
|
|
return ExportEngine::HandleFile(path, user, db, kiss);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLExportEngine::SaveAccounts()
|
|
|
|
{
|
|
|
|
Account account;
|
2011-08-27 18:35:36 +02:00
|
|
|
std::map<int, int>::iterator it;
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
for(it=_accounts.begin(); it!=_accounts.end(); it++)
|
|
|
|
{
|
2012-05-06 11:40:13 +02:00
|
|
|
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;
|
|
|
|
}
|
2011-07-02 16:39:32 +02:00
|
|
|
|
2011-07-03 07:56:48 +02:00
|
|
|
ESCAPE_CHARS(account.name);
|
2011-07-04 20:23:00 +02:00
|
|
|
ESCAPE_CHARS(account.number);
|
2011-07-03 07:56:48 +02:00
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_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();
|
2011-07-02 16:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLExportEngine::SaveAccountAmounts()
|
|
|
|
{
|
2012-05-12 09:56:33 +02:00
|
|
|
std::map<AccountAmount, int, AccountAmount>::iterator it;
|
2012-02-12 10:20:02 +01:00
|
|
|
QString v;
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
for(it=_accountAmounts.begin(); it!=_accountAmounts.end(); it++)
|
|
|
|
{
|
2012-02-12 10:20:02 +01:00
|
|
|
_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));
|
2012-05-12 09:56:33 +02:00
|
|
|
_writer->writeAttribute("amount", v.sprintf("%d", it->second));
|
2012-02-12 10:20:02 +01:00
|
|
|
_writer->writeEndElement();
|
2011-07-02 16:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLExportEngine::SaveCategories()
|
|
|
|
{
|
|
|
|
Category category;
|
2011-08-27 18:35:36 +02:00
|
|
|
std::map<int, int>::iterator it;
|
2011-07-02 16:39:32 +02:00
|
|
|
int rgb;
|
2012-02-12 10:20:02 +01:00
|
|
|
QString v;
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
for(it=_categories.begin(); it!=_categories.end(); it++)
|
|
|
|
{
|
|
|
|
category = _user->GetCategory(it->first);
|
|
|
|
|
2011-07-03 07:56:48 +02:00
|
|
|
ESCAPE_CHARS(category.name);
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_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);
|
2011-08-27 18:35:36 +02:00
|
|
|
rgb = category.backcolor.blue();
|
|
|
|
rgb |= category.backcolor.green() << 8;
|
|
|
|
rgb |= category.backcolor.red() << 16;
|
2012-02-12 10:20:02 +01:00
|
|
|
_writer->writeAttribute("backcolor", v.sprintf("0x%08X", rgb));
|
2011-08-27 18:35:36 +02:00
|
|
|
rgb = category.forecolor.blue();
|
|
|
|
rgb |= category.forecolor.green() << 8;
|
|
|
|
rgb |= category.forecolor.red() << 16;
|
2012-02-12 10:20:02 +01:00
|
|
|
_writer->writeAttribute("forecolor", v.sprintf("0x%08X", rgb));
|
|
|
|
_writer->writeAttribute("fix_cost", (category.fix_cost ? "1" : "0"));
|
|
|
|
_writer->writeEndElement();
|
2011-07-02 16:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLExportEngine::SaveOperations(std::vector<Operation>* operations)
|
|
|
|
{
|
|
|
|
std::vector<Operation>::iterator it;
|
2012-02-12 10:20:02 +01:00
|
|
|
QString v;
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
for(it=operations->begin(); it!=operations->end(); it++)
|
|
|
|
{
|
2011-07-03 07:56:48 +02:00
|
|
|
ESCAPE_CHARS(it->description);
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_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));
|
2012-05-12 09:56:33 +02:00
|
|
|
_writer->writeAttribute("amount", v.sprintf("%d", it->amount));
|
2012-02-12 10:20:02 +01:00
|
|
|
_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();
|
2011-07-02 16:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLExportEngine::SaveFile(std::vector<Operation>* operations)
|
|
|
|
{
|
2012-02-12 10:20:02 +01:00
|
|
|
QFile file(_path);
|
2011-07-02 16:39:32 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ExportEngine::SaveFile(operations);
|
|
|
|
|
|
|
|
if (!rc) return false;
|
|
|
|
|
2013-01-17 20:22:42 +01:00
|
|
|
if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
|
2011-07-02 16:39:32 +02:00
|
|
|
{
|
2012-02-12 10:20:02 +01:00
|
|
|
std::cout << "Error can't open the file " << _path.toStdString() << std::endl;
|
2011-07-02 16:39:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_writer = new QXmlStreamWriter(&file);
|
2011-07-02 16:39:32 +02:00
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_writer->setCodec("UTF-8");
|
|
|
|
_writer->setAutoFormatting(true);
|
|
|
|
_writer->setAutoFormattingIndent(4);
|
|
|
|
_writer->writeStartDocument("1.0");
|
|
|
|
_writer->writeStartElement("kisscount");
|
|
|
|
_writer->writeAttribute("version", "1");
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
SaveAccounts();
|
|
|
|
SaveAccountAmounts();
|
|
|
|
SaveCategories();
|
|
|
|
SaveOperations(operations);
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_writer->writeEndElement();
|
|
|
|
_writer->writeEndDocument();
|
2011-07-02 16:39:32 +02:00
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
file.flush();
|
|
|
|
file.close();
|
2011-07-02 16:39:32 +02:00
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
delete _writer;
|
2011-07-02 16:39:32 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|