/* 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 . */ #include #include "GrisbiImportEngine.hpp" #include static GrisbiImportEngine grisbiImportEngine; void GrisbiImportEngine::LoadAccount(GrisbiImportEngine* _this, const char** attrs) { int i, id; QString account_number, name, key; Account ac; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "Name")) name = attrs[i+1]; else if (!strcmp(attrs[i], "Number")) id = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "Bank_account_number")) account_number = attrs[i+1]; else if (!strcmp(attrs[i], "Key")) key = attrs[i+1]; } account_number += key; UNESCAPE_CHARS(name); UNESCAPE_CHARS(account_number); for (i=0; i<(int)_this->_user->_accounts.size(); i++) { if (_this->_user->_accounts[i].number == account_number) { _this->_accounts[id] = _this->_user->_accounts[i].id; return; } } _this->_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; _this->_unresolvedAccounts.push_back(ac); } void GrisbiImportEngine::LoadCategory(GrisbiImportEngine* _this, const char** attrs) { QString name; int i, id; Category cat; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "Na")) name = attrs[i+1]; else if (!strcmp(attrs[i], "Nb")) id = QString(attrs[i+1]).toInt(); } UNESCAPE_CHARS(name); for (i=0; i<(int)_this->_user->_categories.size(); i++) { if (_this->_user->_categories[i].name == name) { _this->_categories[id] = _this->_user->_categories[i].id; return; } } _this->_categories[id] = 0; cat.id = id; cat.name = name; cat.parent = 0; cat.backcolor = view::OWN_GREEN ; cat.forecolor = Qt::black; cat.fix_cost = false; _this->_unresolvedCategories.push_back(cat); } void GrisbiImportEngine::LoadOperation(GrisbiImportEngine* _this, const char** attrs) { int i; static int id=0; Operation op; QDate date; op.id = ++id; op.parent = 0; op.account = 0; op.category = 0; op.fix_cost = false; op.checked = false; op.transfert = 0; op.formula = ""; op.meta = false; op._virtual = false; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "Ac")) op.account = _this->_accounts[QString(attrs[i+1]).toInt()]; else if (!strcmp(attrs[i], "Dt")) { date.fromString(attrs[i+1], "MM/%dd/%yyyy"); op.day = date.day(); op.month = date.month(); op.year = date.year(); } else if (!strcmp(attrs[i], "Am")) { op.amount = QString(attrs[i+1]).toDouble(); } else if (!strcmp(attrs[i], "Ca")) op.category = _this->_categories[QString(attrs[i+1]).toInt()]; else if (!strcmp(attrs[i], "No")) op.description = attrs[i+1]; } UNESCAPE_CHARS(op.description); _this->_operations.push_back(op); _this->_descriptions[op.id] = op.description; _this->MatchPattern(op.description, op); } void GrisbiImportEngine::GrisbiStartElement(void* user_data, const xmlChar* name_, const xmlChar** attrs_) { GrisbiImportEngine* _this = (GrisbiImportEngine*) user_data; static char first = 0; int i; const char** attrs = (const char**) attrs_; const char* name = (const char*) name_; if (!first && strcmp(name, "Grisbi")) { throw "Invalid file !"; } else first = 1; if (!strcmp(name, "General")) { for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "File_version") && strcmp(attrs[i+1], "0.6.0") < 0) throw "Unsupported version !"; else if (!strcmp(attrs[i], "Crypt_file") && !strcmp(attrs[i+1], "1")) throw "Crypted file !"; } } else if (!strcmp(name, "Account")) LoadAccount(_this, attrs); else if (!strcmp(name, "Category")) LoadCategory(_this, attrs); else if (!strcmp(name, "Transaction")) LoadOperation(_this, attrs); } GrisbiImportEngine::GrisbiImportEngine() { KissCount::RegisterImportEngine(this); _shortExt = "gsb"; _longExt = _("Grisbi files (*.gsb)|*.gsb"); _sax.startElement = GrisbiStartElement ; } GrisbiImportEngine::~GrisbiImportEngine() { } bool GrisbiImportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss) { int res = -1; if (!ImportEngine::HandleFile(path, user, db, kiss)) return false; try { res = xmlSAXUserParseFile(&_sax, this, path.toStdString().c_str()); } catch (const char* s) { std::cout << "GrisbiImportEngine :: " << s << std::endl; res = -1; } return res >= 0; }