KissCount/src/model/import/GrisbiImportEngine.cpp

198 lines
4.4 KiB
C++
Raw Normal View History

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