Add (XML) Export engine

Add Export Panel
Factorise SearchBanner
Bug fix :
 - (Import/Export engines initialization)
 - virtual field not set in Search method
 - when deleting on search results
 - Auto completion now does not require date
This commit is contained in:
2011-07-02 16:39:32 +02:00
parent 8c5fab48f2
commit 40ca2a0f25
9 changed files with 884 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "ExportEngine.h"
ExportEngine::ExportEngine()
{
}
ExportEngine::~ExportEngine()
{
}
bool ExportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
{
_path = path;
_user = user;
_db = db;
_kiss = kiss;
return path.EndsWith(_shortExt) || path.EndsWith(_shortExt.Upper());
}
bool ExportEngine::SaveFile(std::vector<Operation>* operations)
{
int i;
wxString account, category;
AccountAmount accountAmount;
if (!operations) return NULL;
_accounts.clear();
_categories.clear();
_accountAmounts.clear();
for(i=0; i<(int)operations->size(); i++)
{
account = (*operations)[i].account;
category = (*operations)[i].category;
accountAmount.account = account;
accountAmount.month = (*operations)[i].month;
accountAmount.year = (*operations)[i].year;
if (account.Length())
{
if (!_accounts.count(account))
_accounts[account]++;
if (!_accountAmounts.count(accountAmount))
_accountAmounts[accountAmount] = _kiss->GetAccountAmount(accountAmount.account, accountAmount.month, accountAmount.year);
}
if (category.Length() && !_categories.count(category))
_categories[category]++;
}
return true;
}
wxString ExportEngine::GetFileExt()
{
return wxGetTranslation(_longExt);
}

View File

@@ -0,0 +1,78 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef EXPORTENGINE_H
#define EXPORTENGINE_H
#include <model/model.h>
#include <controller/KissCount.h>
class KissCount;
class AccountAmount {
public:
wxString account;
int month;
int year;
bool operator()(const AccountAmount& x, const AccountAmount& y) const
{
long x1, y1;
if (x.account != y.account)
{
x.account.ToLong(&x1);
y.account.ToLong(&y1);
return x1 < y1;
}
return (x.year < y.year && x.month < y.month);
}
};
class ExportEngine {
public:
ExportEngine();
~ExportEngine();
// Get supported file extension. Example :
// "OFX files (*.ofx)|*.ofx"
virtual wxString GetFileExt();
// Handle the file
virtual bool HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)=0;
// Save operations (ExportEngin pre fill _accounts, _categories and _accountAmounts)
virtual bool SaveFile(std::vector<Operation>* operations)=0;
protected:
wxString _path;
Database* _db;
User* _user;
KissCount* _kiss;
wxString _shortExt;
wxString _longExt;
std::map<wxString, int> _accounts;
std::map<wxString, int> _categories;
std::map<AccountAmount, double, AccountAmount> _accountAmounts;
};
#endif

View File

@@ -0,0 +1,174 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "XMLExportEngine.h"
static XMLExportEngine xmlExportEngine;
XMLExportEngine::XMLExportEngine()
{
KissCount::RegisterExportEngine(this);
_shortExt = wxT("xml");
_longExt = _("XML files (*.xml)|*.xml");
}
XMLExportEngine::~XMLExportEngine()
{
}
bool XMLExportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
{
return ExportEngine::HandleFile(path, user, db, kiss);
}
bool XMLExportEngine::SaveAccounts()
{
Account account;
std::map<wxString, int>::iterator it;
for(it=_accounts.begin(); it!=_accounts.end(); it++)
{
account = _user->GetAccount(it->first);
xmlTextWriterStartElement(_writer, (const xmlChar*) "account");
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "id", (const xmlChar*) account.id.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "name", (const xmlChar*) account.name.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "number", (const xmlChar*) account.number.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "shared", (const xmlChar*) (account.shared ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "blocked", (const xmlChar*) (account.blocked ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "default", (const xmlChar*) (account._default ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "is_owner", (const xmlChar*) (account.is_owner ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "virtual", (const xmlChar*) (account._virtual ? "1" : "0"));
xmlTextWriterEndElement(_writer);
}
return true;
}
bool XMLExportEngine::SaveAccountAmounts()
{
std::map<AccountAmount, double, AccountAmount>::iterator it;
for(it=_accountAmounts.begin(); it!=_accountAmounts.end(); it++)
{
xmlTextWriterStartElement(_writer, (const xmlChar*) "account_amount");
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "account", (const xmlChar*) it->first.account.utf8_str().data());
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "month", "%d", it->first.month);
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "year", "%d", it->first.year);
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "amount", "%.2lf", it->second);
xmlTextWriterEndElement(_writer);
}
return true;
}
bool XMLExportEngine::SaveCategories()
{
Category category;
std::map<wxString, int>::iterator it;
int rgb;
for(it=_categories.begin(); it!=_categories.end(); it++)
{
category = _user->GetCategory(it->first);
xmlTextWriterStartElement(_writer, (const xmlChar*) "category");
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "id", (const xmlChar*) category.id.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "name", (const xmlChar*) category.name.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "font", (const xmlChar*) category.font.utf8_str().data());
rgb = category.backcolor.Blue();
rgb |= category.backcolor.Green() << 8;
rgb |= category.backcolor.Red() << 16;
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "backcolor", "%08X", rgb);
rgb = category.forecolor.Blue();
rgb |= category.forecolor.Green() << 8;
rgb |= category.forecolor.Red() << 16;
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "forecolor", "%08X", rgb);
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "fix_cost", (const xmlChar*) (category.fix_cost ? "1" : "0"));
xmlTextWriterEndElement(_writer);
}
return true;
}
bool XMLExportEngine::SaveOperations(std::vector<Operation>* operations)
{
std::vector<Operation>::iterator it;
for(it=operations->begin(); it!=operations->end(); it++)
{
xmlTextWriterStartElement(_writer, (const xmlChar*) "operation");
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "id", (const xmlChar*) it->id.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "parent", (const xmlChar*) it->parent.utf8_str().data());
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "day", "%d", it->day);
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "month", "%d", it->month);
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "year", "%d", it->year);
xmlTextWriterWriteFormatAttribute(_writer, (const xmlChar*) "amount", "%.2lf", it->amount);
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "description", (const xmlChar*) it->description.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "category", (const xmlChar*) it->category.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "fix_cost", (const xmlChar*) (it->fix_cost ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "account", (const xmlChar*) it->account.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "checked", (const xmlChar*) (it->checked ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "transfert", (const xmlChar*) it->transfert.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "formula", (const xmlChar*) it->formula.utf8_str().data());
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "meta", (const xmlChar*) (it->meta ? "1" : "0"));
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "virtual", (const xmlChar*) (it->_virtual ? "1" : "0"));
xmlTextWriterEndElement(_writer);
}
return true;
}
bool XMLExportEngine::SaveFile(std::vector<Operation>* operations)
{
int rc;
rc = ExportEngine::SaveFile(operations);
if (!rc) return false;
_writer = xmlNewTextWriterFilename(_path.mb_str(), 0);
if (!_writer)
{
std::cout << "Error can't open the file" << std::endl;
return false;
}
rc = xmlTextWriterStartDocument(_writer, NULL, "UTF-8", NULL);
if (rc < 0) return false;
xmlTextWriterStartElement(_writer, (const xmlChar*) "kisscount");
xmlTextWriterWriteAttribute(_writer, (const xmlChar*) "version", (const xmlChar*) "1");
SaveAccounts();
SaveAccountAmounts();
SaveCategories();
SaveOperations(operations);
xmlTextWriterEndElement(_writer);
xmlTextWriterEndDocument(_writer);
xmlFreeTextWriter(_writer);
return true;
}

View File

@@ -0,0 +1,47 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMLEXPORTENGINE_H
#define XMLEXPORTENGINE_H
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <model/model.h>
#include <controller/KissCount.h>
#include "ExportEngine.h"
class XMLExportEngine : public ExportEngine {
public:
XMLExportEngine();
~XMLExportEngine();
bool HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss);
bool SaveFile(std::vector<Operation>* operations);
private:
xmlTextWriterPtr _writer;
bool SaveAccounts();
bool SaveAccountAmounts();
bool SaveCategories();
bool SaveOperations(std::vector<Operation>* operations);
};
#endif