KissCount/src/model/import/XMLImportEngine.cpp

288 lines
6.7 KiB
C++
Raw Normal View History

/*
Copyright 2010-2011 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/>.
*/
2011-08-27 18:35:36 +02:00
#include <iostream>
2011-08-20 14:02:47 +02:00
#include "XMLImportEngine.hpp"
static XMLImportEngine xmlImportEngine;
void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs)
{
2011-08-27 18:35:36 +02:00
int i, id;
QString account_number, name;
Account ac;
2011-08-27 18:35:36 +02:00
static int unknownAccount = 0;
2011-08-27 18:35:36 +02:00
ac.id = 0;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "name"))
2011-08-27 18:35:36 +02:00
ac.name = attrs[i+1];
else if (!strcmp(attrs[i], "id"))
2011-08-27 18:35:36 +02:00
ac.id = id = QString(attrs[i+1]).toInt();
else if (!strcmp(attrs[i], "number"))
2011-08-27 18:35:36 +02:00
ac.number = account_number = attrs[i+1];
else if (!strcmp(attrs[i], "blocked"))
2011-08-27 18:35:36 +02:00
ac.blocked = (QString(attrs[i+1]) == "1");
else if (!strcmp(attrs[i], "virtual"))
2011-08-27 18:35:36 +02:00
ac._virtual = (QString(attrs[i+1]) == "1");
}
UNESCAPE_CHARS(ac.name);
UNESCAPE_CHARS(ac.number);
ac._default = false;
ac.shared = false;
ac.is_owner = true;
2011-08-27 18:35:36 +02:00
if (account_number.size())
{
2011-08-14 19:26:54 +02:00
try {
2011-08-27 18:35:36 +02:00
_this->_accounts[ac.id] = _this->_user->GetAccountIdFromAccountNumber(account_number);
2011-08-14 19:26:54 +02:00
return;
}
catch (User::AccountNotFound)
{
}
}
2011-08-27 18:35:36 +02:00
_this->_accounts[id] = --unknownAccount;
_this->_unresolvedAccounts.push_back(ac);
}
void XMLImportEngine::LoadAccountAmount(XMLImportEngine* _this, const char** attrs)
{
AccountAmount accountAmount;
int i;
double amount;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "account"))
2011-08-27 18:35:36 +02:00
accountAmount.account = QString(attrs[i+1]).toInt();
2011-08-25 17:45:41 +02:00
else if (!strcmp(attrs[i], "month"))
{
2011-08-27 18:35:36 +02:00
accountAmount.month = QString(attrs[i+1]).toInt();
}
else if (!strcmp(attrs[i], "year"))
{
2011-08-27 18:35:36 +02:00
accountAmount.year = QString(attrs[i+1]).toInt();
}
else if (!strcmp(attrs[i], "amount"))
2011-08-27 18:35:36 +02:00
amount = QString(attrs[i+1]).toDouble();
}
_this->_accountAmounts[accountAmount] = amount;
}
void XMLImportEngine::LoadCategory(XMLImportEngine* _this, const char** attrs)
{
2011-08-27 18:35:36 +02:00
QString name;
int i, id;
long rgb;
Category cat;
2011-08-27 18:35:36 +02:00
static int unknownCategory = 0;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "name"))
2011-08-27 18:35:36 +02:00
cat.name = name = QString(attrs[i+1]);
else if (!strcmp(attrs[i], "id"))
2011-08-27 18:35:36 +02:00
cat.id = id = QString(attrs[i+1]).toInt();
else if (!strcmp(attrs[i], "font"))
2011-08-27 18:35:36 +02:00
cat.font = QString(attrs[i+1]);
else if (!strcmp(attrs[i], "backcolor"))
{
2011-08-27 18:35:36 +02:00
rgb = QString(attrs[i+1]).toInt(0, 16);
cat.backcolor = QColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
else if (!strcmp(attrs[i], "forecolor"))
{
2011-08-27 18:35:36 +02:00
rgb = QString(attrs[i+1]).toInt(0, 16);
cat.forecolor = QColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
else if (!strcmp(attrs[i], "fix_cost"))
2011-08-27 18:35:36 +02:00
cat.fix_cost = (QString(attrs[i+1]) == "1");
}
UNESCAPE_CHARS(cat.name);
2011-08-27 18:35:36 +02:00
int catId = _this->_user->GetCategoryId(name);
2011-08-14 19:26:54 +02:00
2011-08-27 18:35:36 +02:00
if (catId)
{
2011-08-14 19:26:54 +02:00
_this->_categories[id] = catId;
return;
}
2011-08-27 18:35:36 +02:00
_this->_categories[id] = --unknownCategory;
_this->_unresolvedCategories.push_back(cat);
}
void XMLImportEngine::LoadOperation(XMLImportEngine* _this, const char** attrs)
{
int i;
Operation op;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "id"))
2011-08-27 18:35:36 +02:00
op.id = QString(attrs[i+1]).toInt();
else if (!strcmp(attrs[i], "parent"))
2011-08-27 18:35:36 +02:00
op.parent = QString(attrs[i+1]).toInt();
else if (!strcmp(attrs[i], "day"))
{
2011-08-27 18:35:36 +02:00
op.day = QString(attrs[i+1]).toInt();
}
2011-08-25 17:45:41 +02:00
else if (!strcmp(attrs[i], "month"))
{
2011-08-27 18:35:36 +02:00
op.month = QString(attrs[i+1]).toInt();
}
else if (!strcmp(attrs[i], "year"))
{
2011-08-27 18:35:36 +02:00
op.year = QString(attrs[i+1]).toInt();
}
else if (!strcmp(attrs[i], "amount"))
{
2011-08-27 18:35:36 +02:00
op.amount = QString(attrs[i+1]).toDouble();
}
else if (!strcmp(attrs[i], "description"))
2011-08-27 18:35:36 +02:00
op.description = QString(attrs[i+1]);
else if (!strcmp(attrs[i], "category"))
2011-08-27 18:35:36 +02:00
op.category = _this->_categories[QString(attrs[i+1]).toInt()];
else if (!strcmp(attrs[i], "fix_cost"))
2011-08-27 18:35:36 +02:00
op.fix_cost = (QString(attrs[i+1]) == "1");
else if (!strcmp(attrs[i], "account"))
2011-08-27 18:35:36 +02:00
op.account = _this->_accounts[QString(attrs[i+1]).toInt()];
else if (!strcmp(attrs[i], "checked"))
2011-08-27 18:35:36 +02:00
op.checked = (QString(attrs[i+1]) == "1");
else if (!strcmp(attrs[i], "transfert"))
2011-08-27 18:35:36 +02:00
op.transfert = QString(attrs[i+1]).toInt();
else if (!strcmp(attrs[i], "formula"))
2011-08-27 18:35:36 +02:00
op.formula = QString(attrs[i+1]);
else if (!strcmp(attrs[i], "meta"))
2011-08-27 18:35:36 +02:00
op.meta = (QString(attrs[i+1]) == "1");
else if (!strcmp(attrs[i], "virtual"))
2011-08-27 18:35:36 +02:00
op._virtual = (QString(attrs[i+1]) == "1");
}
UNESCAPE_CHARS(op.description);
_this->_operations.push_back(op);
_this->_descriptions[op.id] = op.description;
_this->MatchPattern(op.description, op);
}
void XMLImportEngine::XmlStartElement(void* user_data, const xmlChar* name_, const xmlChar** attrs_)
{
XMLImportEngine* _this = (XMLImportEngine*) user_data;
int i;
const char** attrs = (const char**) attrs_;
const char* name = (const char*) name_;
// if (!first && strcmp(name, "Xml"))
// {
// throw "Invalid file !";
// }
// else
// first = 1;
if (!strcmp(name, "kisscount"))
{
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "version") && strcmp(attrs[i+1], "1"))
throw "Unsupported version !";
}
}
else if (!strcmp(name, "account"))
LoadAccount(_this, attrs);
else if (!strcmp(name, "account_amount"))
LoadAccountAmount(_this, attrs);
else if (!strcmp(name, "category"))
LoadCategory(_this, attrs);
else if (!strcmp(name, "operation"))
LoadOperation(_this, attrs);
}
XMLImportEngine::XMLImportEngine()
{
KissCount::RegisterImportEngine(this);
2011-08-27 18:35:36 +02:00
_shortExt = "xml";
_longExt = _("KissCount xml files (*.xml)|*.xml");
_sax.startElement = XmlStartElement ;
}
XMLImportEngine::~XMLImportEngine()
{
}
2011-08-27 18:35:36 +02:00
bool XMLImportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
{
int res = -1 ;
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
try
{
2011-08-27 18:35:36 +02:00
res = xmlSAXUserParseFile(&_sax, this, path.toStdString().c_str()) >= 0;
}
catch (const char* s)
{
std::cout << "XMLImportEngine :: " << s << std::endl;
res = -1;
}
return res >= 0;
}