/* 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 "XMLImportEngine.hpp" static XMLImportEngine xmlImportEngine; void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs) { int i, id; QString account_number, name; Account ac; static int unknownAccount = 0; ac.id = 0; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "name")) ac.name = attrs[i+1]; else if (!strcmp(attrs[i], "id")) ac.id = id = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "number")) ac.number = account_number = attrs[i+1]; else if (!strcmp(attrs[i], "blocked")) ac.blocked = (QString(attrs[i+1]) == "1"); else if (!strcmp(attrs[i], "virtual")) 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; if (account_number.size()) { try { _this->_accounts[ac.id] = _this->_user->GetAccountIdFromAccountNumber(account_number); return; } catch (User::AccountNotFound) { } } _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")) accountAmount.account = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "month")) { accountAmount.month = QString(attrs[i+1]).toInt(); } else if (!strcmp(attrs[i], "year")) { accountAmount.year = QString(attrs[i+1]).toInt(); } else if (!strcmp(attrs[i], "amount")) amount = QString(attrs[i+1]).toDouble(); } _this->_accountAmounts[accountAmount] = amount; } void XMLImportEngine::LoadCategory(XMLImportEngine* _this, const char** attrs) { QString name; int i, id; long rgb; Category cat; static int unknownCategory = 0; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "name")) cat.name = name = QString(attrs[i+1]); else if (!strcmp(attrs[i], "id")) cat.id = id = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "font")) cat.font = QString(attrs[i+1]); else if (!strcmp(attrs[i], "backcolor")) { 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")) { 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")) cat.fix_cost = (QString(attrs[i+1]) == "1"); } UNESCAPE_CHARS(cat.name); int catId = _this->_user->GetCategoryId(name); if (catId) { _this->_categories[id] = catId; return; } _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")) op.id = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "parent")) op.parent = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "day")) { op.day = QString(attrs[i+1]).toInt(); } else if (!strcmp(attrs[i], "month")) { op.month = QString(attrs[i+1]).toInt(); } else if (!strcmp(attrs[i], "year")) { op.year = QString(attrs[i+1]).toInt(); } else if (!strcmp(attrs[i], "amount")) { op.amount = QString(attrs[i+1]).toDouble(); } else if (!strcmp(attrs[i], "description")) op.description = QString(attrs[i+1]); else if (!strcmp(attrs[i], "category")) op.category = _this->_categories[QString(attrs[i+1]).toInt()]; else if (!strcmp(attrs[i], "fix_cost")) op.fix_cost = (QString(attrs[i+1]) == "1"); else if (!strcmp(attrs[i], "account")) op.account = _this->_accounts[QString(attrs[i+1]).toInt()]; else if (!strcmp(attrs[i], "checked")) op.checked = (QString(attrs[i+1]) == "1"); else if (!strcmp(attrs[i], "transfert")) op.transfert = QString(attrs[i+1]).toInt(); else if (!strcmp(attrs[i], "formula")) op.formula = QString(attrs[i+1]); else if (!strcmp(attrs[i], "meta")) op.meta = (QString(attrs[i+1]) == "1"); else if (!strcmp(attrs[i], "virtual")) 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); _shortExt = "xml"; _longExt = _("KissCount xml files (*.xml)|*.xml"); _sax.startElement = XmlStartElement ; } XMLImportEngine::~XMLImportEngine() { } 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 { res = xmlSAXUserParseFile(&_sax, this, path.toStdString().c_str()) >= 0; } catch (const char* s) { std::cout << "XMLImportEngine :: " << s << std::endl; res = -1; } return res >= 0; }