KissCount/src/model/import/XMLImportEngine.cpp

229 lines
5.5 KiB
C++

/*
Copyright 2010-2012 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 <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <QXmlInputSource>
#include "XMLImportEngine.hpp"
static XMLImportEngine xmlImportEngine;
XMLImportEngine::XMLImportEngine()
{
KissCount::RegisterImportEngine(this);
_shortExt = ".xml";
_longExt = _("KissCount xml files (*.xml)");
_sax.setContentHandler(this);
}
XMLImportEngine::~XMLImportEngine()
{
}
bool XMLImportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
{
bool res = false;
QFile file(path);
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
if (!file.open(QIODevice::ReadOnly))
{
std::cout << "Error can't open the file " << path.toStdString() << std::endl;
return false;
}
try
{
res = _sax.parse(&file);
LinkChilds();
}
catch (const char* s)
{
std::cout << "XMLImportEngine :: " << s << std::endl;
res = false;
}
file.close();
return res;
}
bool XMLImportEngine::startElement (const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& attrs)
{
if (qName == "kisscount")
{
if (attrs.value("version") != "1")
throw "Unsupported version !";
}
else if (qName == "account")
LoadAccount(attrs);
else if (qName == "account_amount")
LoadAccountAmount(attrs);
else if (qName == "category")
LoadCategory(attrs);
else if (qName == "operation")
LoadOperation(attrs);
else
std::cout << "Unknown element : '" << qName.toStdString() << "'" << std::endl;
return true;
}
void XMLImportEngine::LoadAccount(const QXmlAttributes& attrs)
{
int id;
Account ac;
static int unknownAccount = 0;
ac.id = 0;
ac.name = attrs.value("name");
ac.id = id = attrs.value("id").toInt();
ac.number = attrs.value("number");
ac.blocked = (attrs.value("blocked") == "1");
ac._virtual = (attrs.value("virtual") == "1");
ac.hidden = (attrs.value("hidden") == "1");
UNESCAPE_CHARS(ac.name);
UNESCAPE_CHARS(ac.number);
ac._default = false;
ac.shared = false;
ac.is_owner = true;
if (ac.number.size())
{
try {
_accounts[ac.id] = _user->GetAccountIdFromAccountNumber(ac.number);
return;
}
catch (User::AccountNotFound)
{
}
}
if (ac.name.size())
{
try {
_accounts[ac.id] = _user->GetAccountId(ac.name);
return;
}
catch (User::AccountNotFound)
{
}
}
_accounts[id] = --unknownAccount;
_unresolvedAccounts.push_back(ac);
}
void XMLImportEngine::LoadAccountAmount(const QXmlAttributes& attrs)
{
AccountAmount accountAmount;
double amount;
accountAmount.account = attrs.value("account").toInt();
accountAmount.month = attrs.value("month").toInt();
accountAmount.year = attrs.value("year").toInt();
amount = attrs.value("amount").toDouble();
_accountAmounts[accountAmount] = amount;
}
void XMLImportEngine::LoadCategory(const QXmlAttributes& attrs)
{
QString name;
int id;
long rgb;
Category cat;
static int unknownCategory = 0;
cat.fix_cost = false;
cat.name = name = attrs.value("name");
cat.id = id = attrs.value("id").toInt();
cat.parent = attrs.value("parent").toInt();
cat.font = attrs.value("font");
if (attrs.value("backcolor") != "")
{
rgb = attrs.value("backcolor").toInt(0, 16);
cat.backcolor = QColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
else
cat.backcolor = view::OWN_GREEN;
if (attrs.value("forecolor") != "")
{
rgb = attrs.value("forecolor").toInt(0, 16);
cat.forecolor = QColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
else
cat.forecolor = Qt::black;
cat.fix_cost = (attrs.value("fix_cost") == "1");
UNESCAPE_CHARS(cat.name);
int catId = _user->GetCategoryId(name);
if (catId)
{
_categories[id] = catId;
return;
}
_categories[id] = --unknownCategory;
_unresolvedCategories.push_back(cat);
}
void XMLImportEngine::LoadOperation(const QXmlAttributes& attrs)
{
Operation op;
op.id = attrs.value("id").toInt();
op.parent = attrs.value("parent").toInt();
op.day = attrs.value("day").toInt();
op.month = attrs.value("month").toInt();
op.year = attrs.value("year").toInt();
op.amount = attrs.value("amount").toDouble();
op.description = attrs.value("description");
op.category = attrs.value("category").toInt();
op.category = (attrs.value("fix_cost") == "1");
op.account = attrs.value("account").toInt();
op.checked = (attrs.value("checked") == "1");
op.transfert = attrs.value("transfert").toInt();
op.formula = attrs.value("formula");
op.meta = (attrs.value("meta") == "1");
op._virtual = (attrs.value("virtual") == "1");
UNESCAPE_CHARS(op.description);
_operations.push_back(op);
_descriptions[op.id] = op.description;
MatchPattern(op.description, op);
}