/* 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 . */ #include #include "GrisbiImportEngine.hpp" #include static GrisbiImportEngine grisbiImportEngine; void GrisbiImportEngine::LoadAccount(const QXmlAttributes& attrs) { int id, i; 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) { 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; cat.parent = 0; cat.backcolor = view::OWN_GREEN ; cat.forecolor = Qt::black; cat.fix_cost = false; _unresolvedCategories.push_back(cat); } void GrisbiImportEngine::LoadOperation(const QXmlAttributes& attrs) { static int id=0; Operation op; QDate date; op.id = ++id; op.parent = 0; op.account = 0; op.category = 0; op.tag = 0; op.fix_cost = false; op.checked = false; 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(); 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() { } 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; QXmlInputSource *source = new QXmlInputSource(&file); try { res = _sax.parse(source, false); LinkChilds(); } catch (const char* s) { std::cout << "GrisbiImportEngine :: " << s << std::endl; res = false; } file.close(); delete source; return res; }