/* 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 "XMLImportEngine.hpp" static XMLImportEngine xmlImportEngine; void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs) { int i; wxString account_number, name, id; Account ac; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "name")) ac.name = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "id")) ac.id = id = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "number")) ac.number = account_number = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "blocked")) ac.blocked = (wxString(attrs[i+1], wxConvUTF8) == wxT("1")); else if (!strcmp(attrs[i], "virtual")) ac._virtual = (wxString(attrs[i+1], wxConvUTF8) == wxT("1")); } UNESCAPE_CHARS(ac.name); UNESCAPE_CHARS(ac.number); ac._default = false; ac.shared = false; ac.is_owner = true; if (account_number.Length()) { try { Account ac = _this->_user->GetAccount(account_number); _this->_accounts[id] = ac.id; return; } catch (User::AccountNotFound) { } } _this->_accounts[id] = wxT("unknown-") + account_number; _this->_unresolvedAccounts.push_back(ac); } void XMLImportEngine::LoadAccountAmount(XMLImportEngine* _this, const char** attrs) { AccountAmount accountAmount; int i; long v; double amount; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "account")) accountAmount.account = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "month")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&v); accountAmount.month = v; } else if (!strcmp(attrs[i], "year")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&v); accountAmount.year = v; } else if (!strcmp(attrs[i], "amount")) wxString(attrs[i+1], wxConvUTF8).ToDouble(&amount); } _this->_accountAmounts[accountAmount] = amount; } void XMLImportEngine::LoadCategory(XMLImportEngine* _this, const char** attrs) { wxString name, id; int i; long rgb; Category cat; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "name")) cat.name = name = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "id")) cat.id = id = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "font")) cat.font = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "backcolor")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&rgb, 16); cat.backcolor = wxColour((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF); } else if (!strcmp(attrs[i], "forecolor")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&rgb, 16); cat.forecolor = wxColour((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF); } else if (!strcmp(attrs[i], "fix_cost")) cat.fix_cost = (wxString(attrs[i+1], wxConvUTF8) == wxT("1")); } UNESCAPE_CHARS(cat.name); wxString catId = _this->_user->GetCategoryId(name); if (catId != wxT("0")) { _this->_categories[id] = catId; return; } _this->_categories[id] = wxT("unknown-") + name; _this->_unresolvedCategories.push_back(cat); } void XMLImportEngine::LoadOperation(XMLImportEngine* _this, const char** attrs) { int i; Operation op; long v; double amount; wxString id; for (i=0; attrs[i]; i+=2) { if (!strcmp(attrs[i], "id")) op.id = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "parent")) op.parent = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "day")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&v); op.day = v; } else if (!strcmp(attrs[i], "month")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&v); op.month = v; } else if (!strcmp(attrs[i], "year")) { wxString(attrs[i+1], wxConvUTF8).ToLong(&v); op.year = v; } else if (!strcmp(attrs[i], "amount")) { wxString(attrs[i+1], wxConvUTF8).ToDouble(&amount); op.amount = amount; } else if (!strcmp(attrs[i], "description")) op.description = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "category")) op.category = _this->_categories[wxString(attrs[i+1], wxConvUTF8)]; else if (!strcmp(attrs[i], "fix_cost")) op.fix_cost = (wxString(attrs[i+1], wxConvUTF8) == wxT("1")); else if (!strcmp(attrs[i], "account")) op.account = _this->_accounts[wxString(attrs[i+1], wxConvUTF8)]; else if (!strcmp(attrs[i], "checked")) op.checked = (wxString(attrs[i+1], wxConvUTF8) == wxT("1")); else if (!strcmp(attrs[i], "transfert")) op.transfert = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "formula")) op.formula = wxString(attrs[i+1], wxConvUTF8); else if (!strcmp(attrs[i], "meta")) op.meta = (wxString(attrs[i+1], wxConvUTF8) == wxT("1")); else if (!strcmp(attrs[i], "virtual")) op._virtual = (wxString(attrs[i+1], wxConvUTF8) == wxT("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 = wxT("xml"); _longExt = _("KissCount xml files (*.xml)|*.xml"); _sax.startElement = XmlStartElement ; } XMLImportEngine::~XMLImportEngine() { } bool XMLImportEngine::HandleFile(const wxString& 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.mb_str()) >= 0; } catch (const char* s) { std::cout << "XMLImportEngine :: " << s << std::endl; res = -1; } return res >= 0; }