Work on import engines : remove libxml dependency in favor of QtXml
This commit is contained in:
@@ -18,39 +18,93 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <QXmlInputSource>
|
||||
|
||||
#include "XMLImportEngine.hpp"
|
||||
|
||||
static XMLImportEngine xmlImportEngine;
|
||||
|
||||
void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs)
|
||||
XMLImportEngine::XMLImportEngine()
|
||||
{
|
||||
int i, id;
|
||||
KissCount::RegisterImportEngine(this);
|
||||
|
||||
_shortExt = ".xml";
|
||||
_longExt = _("KissCount xml files (*.xml)");
|
||||
|
||||
_sax.setContentHandler(this);
|
||||
}
|
||||
|
||||
XMLImportEngine::~XMLImportEngine()
|
||||
{
|
||||
}
|
||||
|
||||
bool XMLImportEngine::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;
|
||||
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
std::cout << "Error can't open the file " << path.toStdString() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
res = _sax.parse(&file);
|
||||
}
|
||||
catch (const char* s)
|
||||
{
|
||||
std::cout << "XMLImportEngine :: " << s << std::endl;
|
||||
res = false;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool XMLImportEngine::startElement (const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& attrs)
|
||||
{
|
||||
if (qName == "kisscount")
|
||||
{
|
||||
if (attrs.value("version") != "1")
|
||||
throw "Unsupported version !";
|
||||
}
|
||||
|
||||
else if (qName == "account")
|
||||
LoadAccount(attrs);
|
||||
|
||||
else if (qName == "account_amount")
|
||||
LoadAccountAmount(attrs);
|
||||
|
||||
else if (qName == "category")
|
||||
LoadCategory(attrs);
|
||||
|
||||
else if (qName == "operation")
|
||||
LoadOperation(attrs);
|
||||
else
|
||||
std::cout << "Unknown element : '" << qName.toStdString() << "'" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void XMLImportEngine::LoadAccount(const QXmlAttributes& attrs)
|
||||
{
|
||||
int 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");
|
||||
|
||||
else if (!strcmp(attrs[i], "hidden"))
|
||||
ac.hidden = (QString(attrs[i+1]) == "1");
|
||||
}
|
||||
ac.name = attrs.value("name");
|
||||
ac.id = id = attrs.value("id").toInt();
|
||||
ac.number = account_number = attrs.value("number");
|
||||
ac.blocked = (attrs.value("blocked") == "1");
|
||||
ac._virtual = (attrs.value("virtual") == "1");
|
||||
ac.hidden = (attrs.value("hidden") == "1");
|
||||
|
||||
UNESCAPE_CHARS(ac.name);
|
||||
UNESCAPE_CHARS(ac.number);
|
||||
@@ -62,7 +116,7 @@ void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs)
|
||||
if (account_number.size())
|
||||
{
|
||||
try {
|
||||
_this->_accounts[ac.id] = _this->_user->GetAccountIdFromAccountNumber(account_number);
|
||||
_accounts[ac.id] = _user->GetAccountIdFromAccountNumber(account_number);
|
||||
return;
|
||||
}
|
||||
catch (User::AccountNotFound)
|
||||
@@ -70,224 +124,94 @@ void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs)
|
||||
}
|
||||
}
|
||||
|
||||
_this->_accounts[id] = --unknownAccount;
|
||||
_this->_unresolvedAccounts.push_back(ac);
|
||||
_accounts[id] = --unknownAccount;
|
||||
_unresolvedAccounts.push_back(ac);
|
||||
}
|
||||
|
||||
void XMLImportEngine::LoadAccountAmount(XMLImportEngine* _this, const char** attrs)
|
||||
void XMLImportEngine::LoadAccountAmount(const QXmlAttributes& 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();
|
||||
accountAmount.account = attrs.value("account").toInt();
|
||||
accountAmount.month = attrs.value("month").toInt();
|
||||
accountAmount.year = attrs.value("year").toInt();
|
||||
amount = attrs.value("amount").toDouble();
|
||||
|
||||
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;
|
||||
_accountAmounts[accountAmount] = amount;
|
||||
}
|
||||
|
||||
void XMLImportEngine::LoadCategory(XMLImportEngine* _this, const char** attrs)
|
||||
void XMLImportEngine::LoadCategory(const QXmlAttributes& attrs)
|
||||
{
|
||||
QString name;
|
||||
int i, id;
|
||||
int id;
|
||||
long rgb;
|
||||
Category cat;
|
||||
static int unknownCategory = 0;
|
||||
|
||||
for (i=0; attrs[i]; i+=2)
|
||||
cat.fix_cost = false;
|
||||
|
||||
cat.name = name = attrs.value("name");
|
||||
cat.id = id = attrs.value("id").toInt();
|
||||
cat.parent = attrs.value("parent").toInt();
|
||||
cat.font = attrs.value("font");
|
||||
|
||||
if (attrs.value("backcolor") != "")
|
||||
{
|
||||
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], "parent"))
|
||||
cat.parent = 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");
|
||||
rgb = attrs.value("backcolor").toInt(0, 16);
|
||||
cat.backcolor = QColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
|
||||
}
|
||||
else
|
||||
cat.backcolor = view::OWN_GREEN;
|
||||
|
||||
if (attrs.value("forecolor") != "")
|
||||
{
|
||||
rgb = attrs.value("forecolor").toInt(0, 16);
|
||||
cat.forecolor = QColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
|
||||
}
|
||||
else
|
||||
cat.forecolor = Qt::black;
|
||||
|
||||
cat.fix_cost = (attrs.value("fix_cost") == "1");
|
||||
|
||||
UNESCAPE_CHARS(cat.name);
|
||||
|
||||
int catId = _this->_user->GetCategoryId(name);
|
||||
int catId = _user->GetCategoryId(name);
|
||||
|
||||
if (catId)
|
||||
{
|
||||
_this->_categories[id] = catId;
|
||||
_categories[id] = catId;
|
||||
return;
|
||||
}
|
||||
|
||||
_this->_categories[id] = --unknownCategory;
|
||||
_this->_unresolvedCategories.push_back(cat);
|
||||
_categories[id] = --unknownCategory;
|
||||
_unresolvedCategories.push_back(cat);
|
||||
}
|
||||
|
||||
void XMLImportEngine::LoadOperation(XMLImportEngine* _this, const char** attrs)
|
||||
void XMLImportEngine::LoadOperation(const QXmlAttributes& 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");
|
||||
}
|
||||
op.id = attrs.value("id").toInt();
|
||||
op.parent = attrs.value("parent").toInt();
|
||||
op.day = attrs.value("day").toInt();
|
||||
op.month = attrs.value("month").toInt();
|
||||
op.year = attrs.value("year").toInt();
|
||||
op.amount = attrs.value("amount").toDouble();
|
||||
op.description = attrs.value("description");
|
||||
op.category = attrs.value("category").toInt();
|
||||
op.category = (attrs.value("fix_cost") == "1");
|
||||
op.account = attrs.value("account").toInt();
|
||||
op.checked = (attrs.value("checked") == "1");
|
||||
op.transfert = attrs.value("transfert").toInt();
|
||||
op.formula = attrs.value("formula");
|
||||
op.meta = (attrs.value("meta") == "1");
|
||||
op._virtual = (attrs.value("virtual") == "1");
|
||||
|
||||
UNESCAPE_CHARS(op.description);
|
||||
|
||||
_this->_operations.push_back(op);
|
||||
_this->_descriptions[op.id] = op.description;
|
||||
_operations.push_back(op);
|
||||
_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)");
|
||||
|
||||
_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;
|
||||
MatchPattern(op.description, op);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user