2011-04-23 20:16:31 +02:00
|
|
|
/*
|
2012-02-01 11:02:54 +01:00
|
|
|
Copyright 2010-2012 Grégory Soutadé
|
2011-04-23 20:16:31 +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/>.
|
|
|
|
*/
|
2011-08-27 18:35:36 +02:00
|
|
|
#include <iostream>
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2011-08-20 14:02:47 +02:00
|
|
|
#include "GrisbiImportEngine.hpp"
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2011-08-20 14:02:47 +02:00
|
|
|
#include <view/view.hpp>
|
2011-08-20 11:43:12 +02:00
|
|
|
|
2011-04-23 20:16:31 +02:00
|
|
|
static GrisbiImportEngine grisbiImportEngine;
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
void GrisbiImportEngine::LoadAccount(const QXmlAttributes& attrs)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
int id, i;
|
2011-08-27 18:35:36 +02:00
|
|
|
QString account_number, name, key;
|
2011-04-25 17:36:58 +02:00
|
|
|
Account ac;
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
name = attrs.value("name");
|
|
|
|
id = attrs.value("Number").toInt();
|
|
|
|
account_number = attrs.value("Bank_account_number");
|
|
|
|
key = attrs.value("Key");
|
2011-04-23 20:16:31 +02:00
|
|
|
|
|
|
|
account_number += key;
|
|
|
|
|
2011-07-04 20:23:00 +02:00
|
|
|
UNESCAPE_CHARS(name);
|
|
|
|
UNESCAPE_CHARS(account_number);
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
for (i=0; i<(int)_user->_accounts.size(); i++)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
if (_user->_accounts[i].number == account_number)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
_accounts[id] = _user->_accounts[i].id;
|
2011-04-23 20:16:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
_accounts[id] = 0;
|
2011-04-25 17:36:58 +02:00
|
|
|
ac.number = account_number;
|
|
|
|
ac.name = name;
|
2011-07-04 20:23:00 +02:00
|
|
|
ac.shared = false;
|
|
|
|
ac.blocked = false;
|
|
|
|
ac._default = false;
|
|
|
|
ac.is_owner = true;
|
|
|
|
ac._virtual = false;
|
2012-02-11 14:02:30 +01:00
|
|
|
ac.hidden = false;
|
2012-02-19 17:14:36 +01:00
|
|
|
_unresolvedAccounts.push_back(ac);
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
void GrisbiImportEngine::LoadCategory(const QXmlAttributes& attrs)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
QString name;
|
2012-02-19 17:14:36 +01:00
|
|
|
int id, i;
|
2011-04-25 19:55:31 +02:00
|
|
|
Category cat;
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
name = attrs.value("Na");
|
|
|
|
id = attrs.value("Nb").toInt();
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2011-07-04 20:23:00 +02:00
|
|
|
UNESCAPE_CHARS(name);
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
for (i=0; i<(int)_user->_categories.size(); i++)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
if (_user->_categories[i].name == name)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
_categories[id] = _user->_categories[i].id;
|
2011-04-23 20:16:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
_categories[id] = 0;
|
2011-04-25 19:55:31 +02:00
|
|
|
cat.id = id;
|
|
|
|
cat.name = name;
|
2011-08-27 18:35:36 +02:00
|
|
|
cat.parent = 0;
|
2011-08-16 18:22:35 +02:00
|
|
|
cat.backcolor = view::OWN_GREEN ;
|
2011-08-27 18:35:36 +02:00
|
|
|
cat.forecolor = Qt::black;
|
2011-07-04 20:23:00 +02:00
|
|
|
cat.fix_cost = false;
|
2012-02-19 17:14:36 +01:00
|
|
|
_unresolvedCategories.push_back(cat);
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
void GrisbiImportEngine::LoadOperation(const QXmlAttributes& attrs)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
|
|
|
static int id=0;
|
|
|
|
Operation op;
|
2011-08-27 18:35:36 +02:00
|
|
|
QDate date;
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
op.id = ++id;
|
|
|
|
op.parent = 0;
|
|
|
|
op.account = 0;
|
|
|
|
op.category = 0;
|
2014-11-10 11:54:28 +01:00
|
|
|
op.tag = 0;
|
2011-04-23 20:16:31 +02:00
|
|
|
op.fix_cost = false;
|
|
|
|
op.checked = false;
|
2011-08-27 18:35:36 +02:00
|
|
|
op.transfert = 0;
|
|
|
|
op.formula = "";
|
2011-04-23 20:16:31 +02:00
|
|
|
op.meta = false;
|
|
|
|
op._virtual = false;
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
op.account = _accounts[attrs.value("Ac").toInt()];
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-20 21:27:51 +01:00
|
|
|
date = QDate::fromString(attrs.value("Dt"), "MM/dd/yyyy");
|
2012-02-19 17:14:36 +01:00
|
|
|
op.day = date.day();
|
|
|
|
op.month = date.month();
|
2012-02-20 21:27:51 +01:00
|
|
|
op.year = date.year();
|
2012-04-30 21:15:51 +02:00
|
|
|
op.amount = attrs.value("Am").toInt();
|
2012-02-19 17:14:36 +01:00
|
|
|
op.category = _categories[attrs.value("Ca").toInt()];
|
|
|
|
op.description = attrs.value("No");
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2011-07-04 20:23:00 +02:00
|
|
|
UNESCAPE_CHARS(op.description);
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
_operations.push_back(op);
|
|
|
|
_descriptions[op.id] = op.description;
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
MatchPattern(op.description, op);
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
bool GrisbiImportEngine::startElement (const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& attrs)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
|
|
|
static char first = 0;
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
if (!first && qName != "Grisbi")
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
|
|
|
throw "Invalid file !";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
first = 1;
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
if (qName == "General")
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
if (attrs.value("File_version") < "0.6.0")
|
|
|
|
throw "Unsupported version !";
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
if (attrs.value("Crypt_file") == "1")
|
|
|
|
throw "Crypted file !";
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
else if (qName == "Account")
|
|
|
|
LoadAccount(attrs);
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
else if (qName == "Category")
|
|
|
|
LoadCategory(attrs);
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
else if (qName == "Transaction")
|
|
|
|
LoadOperation(attrs);
|
|
|
|
|
|
|
|
return true;
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GrisbiImportEngine::GrisbiImportEngine()
|
|
|
|
{
|
|
|
|
KissCount::RegisterImportEngine(this);
|
|
|
|
|
2012-02-12 10:20:02 +01:00
|
|
|
_shortExt = ".gsb";
|
|
|
|
_longExt = _("Grisbi files (*.gsb)");
|
2011-04-23 20:16:31 +02:00
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
_sax.setContentHandler(this);
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GrisbiImportEngine::~GrisbiImportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
bool GrisbiImportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
|
2011-04-23 20:16:31 +02:00
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
bool res = false;
|
|
|
|
QFile file(path);
|
2011-07-04 20:23:00 +02:00
|
|
|
|
2011-04-23 20:16:31 +02:00
|
|
|
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
|
|
|
|
|
2011-07-04 20:23:00 +02:00
|
|
|
try
|
|
|
|
{
|
2012-02-19 17:14:36 +01:00
|
|
|
res = _sax.parse(&file);
|
2012-02-26 08:59:59 +01:00
|
|
|
LinkChilds();
|
2011-07-04 20:23:00 +02:00
|
|
|
}
|
|
|
|
catch (const char* s)
|
|
|
|
{
|
|
|
|
std::cout << "GrisbiImportEngine :: " << s << std::endl;
|
2012-02-19 17:14:36 +01:00
|
|
|
res = false;
|
2011-07-04 20:23:00 +02:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:14:36 +01:00
|
|
|
file.close();
|
|
|
|
|
|
|
|
return res;
|
2011-04-23 20:16:31 +02:00
|
|
|
}
|