2013-01-17 20:22:42 +01:00
|
|
|
/*
|
|
|
|
Copyright 2010-2013 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 <QTextStream>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "CSVExportEngine.hpp"
|
|
|
|
|
|
|
|
static CSVExportEngine csvExportEngine;
|
|
|
|
|
|
|
|
CSVExportEngine::CSVExportEngine()
|
|
|
|
{
|
|
|
|
KissCount::RegisterExportEngine(this);
|
|
|
|
|
|
|
|
_shortExt = ".csv";
|
|
|
|
_longExt = _("CSV files (*.csv)");
|
|
|
|
}
|
|
|
|
|
|
|
|
CSVExportEngine::~CSVExportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVExportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
|
|
|
|
{
|
|
|
|
return ExportEngine::HandleFile(path, user, db, kiss);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVExportEngine::SaveAccounts()
|
|
|
|
{
|
|
|
|
Account account;
|
|
|
|
std::map<int, int>::iterator it;
|
|
|
|
|
|
|
|
*_writer << "Accounts" << endl << endl;
|
|
|
|
*_writer << "id;name;number;blocked;virtual;hidden" << endl;
|
|
|
|
|
|
|
|
for(it=_accounts.begin(); it!=_accounts.end(); it++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
account = _user->GetAccount(it->first);
|
|
|
|
}
|
|
|
|
catch(User::AccountNotFound)
|
|
|
|
{
|
|
|
|
account.id = it->first;
|
|
|
|
account.name = "Unknown";
|
|
|
|
account.blocked = false;
|
|
|
|
account._virtual = false;
|
|
|
|
account.hidden = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESCAPE_CHARS(account.name);
|
|
|
|
ESCAPE_CHARS(account.number);
|
|
|
|
|
|
|
|
*_writer << QString::number(account.id) << ";";
|
|
|
|
*_writer << "\"" << account.name << "\"" << ";";
|
|
|
|
*_writer << "\"" << account.number << "\"" << ";";
|
|
|
|
// *_writer << (account.shared ? "1" : "0") << ";";
|
|
|
|
*_writer << (account.blocked ? "1" : "0") << ";";
|
|
|
|
// *_writer << (account._default ? "1" : "0") << ";" ;
|
|
|
|
// *_writer << (account.is_owner ? "1" : "0") << ";";
|
|
|
|
*_writer << (account._virtual ? "1" : "0") << ";";
|
|
|
|
*_writer << (account.hidden ? "1" : "0");
|
|
|
|
*_writer << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_writer << endl << endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVExportEngine::SaveAccountAmounts()
|
|
|
|
{
|
|
|
|
std::map<AccountAmount, int, AccountAmount>::iterator it;
|
|
|
|
QString v;
|
|
|
|
|
|
|
|
*_writer << "Account Amounts" << endl << endl;
|
|
|
|
*_writer << "id;month;year;amount" << endl;
|
|
|
|
|
|
|
|
for(it=_accountAmounts.begin(); it!=_accountAmounts.end(); it++)
|
|
|
|
{
|
|
|
|
*_writer << QString::number(it->first.account) << ";";
|
|
|
|
*_writer << QString::number(it->first.month+1) << ";";
|
|
|
|
*_writer << QString::number(it->first.year) << ";";
|
|
|
|
*_writer << v.sprintf("%d", it->second);
|
|
|
|
*_writer << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_writer << endl << endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVExportEngine::SaveCategories()
|
|
|
|
{
|
|
|
|
Category category;
|
|
|
|
std::map<int, int>::iterator it;
|
|
|
|
int rgb;
|
|
|
|
QString v;
|
|
|
|
|
|
|
|
*_writer << "Categories" << endl << endl;
|
|
|
|
*_writer << "id;parent;name;font;backcolor;forecolor;fix_cost" << endl;
|
|
|
|
|
|
|
|
for(it=_categories.begin(); it!=_categories.end(); it++)
|
|
|
|
{
|
|
|
|
category = _user->GetCategory(it->first);
|
|
|
|
|
|
|
|
ESCAPE_CHARS(category.name);
|
|
|
|
|
|
|
|
*_writer << QString::number(category.id) << ";";
|
|
|
|
*_writer << QString::number(category.parent) << ";";
|
|
|
|
*_writer << "\"" << category.name << "\"" << ";";
|
|
|
|
*_writer << "\"" << category.font << "\"" << ";";
|
|
|
|
rgb = category.backcolor.blue();
|
|
|
|
rgb |= category.backcolor.green() << 8;
|
|
|
|
rgb |= category.backcolor.red() << 16;
|
|
|
|
*_writer << v.sprintf("0x%08X", rgb) << ";";
|
|
|
|
rgb = category.forecolor.blue();
|
|
|
|
rgb |= category.forecolor.green() << 8;
|
|
|
|
rgb |= category.forecolor.red() << 16;
|
|
|
|
*_writer << v.sprintf("0x%08X", rgb) << ";";
|
|
|
|
*_writer << (category.fix_cost ? "1" : "0");
|
|
|
|
*_writer << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_writer << endl << endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-10 11:54:28 +01:00
|
|
|
bool CSVExportEngine::SaveTags()
|
|
|
|
{
|
|
|
|
Tag tag;
|
|
|
|
std::map<int, int>::iterator it;
|
|
|
|
QString v;
|
|
|
|
|
|
|
|
*_writer << "Tags" << endl << endl;
|
|
|
|
*_writer << "id;name" << endl;
|
|
|
|
|
|
|
|
for(it=_tags.begin(); it!=_tags.end(); it++)
|
|
|
|
{
|
|
|
|
tag = _user->GetTag(it->first);
|
|
|
|
|
|
|
|
ESCAPE_CHARS(tag.name);
|
|
|
|
|
|
|
|
*_writer << QString::number(tag.id) << ";";
|
|
|
|
*_writer << "\"" << tag.name << "\"" << ";";
|
|
|
|
*_writer << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_writer << endl << endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-17 20:22:42 +01:00
|
|
|
bool CSVExportEngine::SaveOperations(std::vector<Operation>* operations)
|
|
|
|
{
|
|
|
|
std::vector<Operation>::iterator it;
|
|
|
|
QString v;
|
|
|
|
|
|
|
|
*_writer << "Operations" << endl << endl;
|
2014-11-10 11:54:28 +01:00
|
|
|
*_writer << "id;parent;day;month;year;amount;description;category;fix_cost;account;checked;transfert;formula;meta;virtual;tag" << endl;
|
2013-01-17 20:22:42 +01:00
|
|
|
|
|
|
|
for(it=operations->begin(); it!=operations->end(); it++)
|
|
|
|
{
|
|
|
|
ESCAPE_CHARS(it->description);
|
|
|
|
|
|
|
|
*_writer << QString::number(it->id) << ";" ;
|
|
|
|
*_writer << QString::number(it->parent) << ";";
|
|
|
|
*_writer << QString::number(it->day+1) << ";";
|
|
|
|
*_writer << QString::number(it->month+1) << ";";
|
|
|
|
*_writer << QString::number(it->year) << ";";
|
|
|
|
*_writer << v.sprintf("%d", it->amount) << ";";
|
|
|
|
*_writer << "\"" << it->description << "\"" << ";";
|
|
|
|
*_writer << QString::number(it->category) << ";";
|
|
|
|
*_writer << (it->fix_cost ? "1" : "0") << ";";
|
|
|
|
*_writer << QString::number(it->account) << ";";
|
|
|
|
*_writer << (it->checked ? "1" : "0") << ";";
|
|
|
|
*_writer << QString::number(it->transfert) << ";";
|
|
|
|
*_writer << it->formula << ";";
|
|
|
|
*_writer << (it->meta ? "1" : "0") << ";";
|
|
|
|
*_writer << (it->_virtual ? "1" : "0");
|
2014-11-10 11:54:28 +01:00
|
|
|
*_writer << QString::number(it->tag) << ";";
|
2013-01-17 20:22:42 +01:00
|
|
|
*_writer << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_writer << endl << endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSVExportEngine::SaveFile(std::vector<Operation>* operations)
|
|
|
|
{
|
|
|
|
QFile file(_path);
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ExportEngine::SaveFile(operations);
|
|
|
|
|
|
|
|
if (!rc) return false;
|
|
|
|
|
|
|
|
if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
|
|
|
|
{
|
|
|
|
std::cout << "Error can't open the file " << _path.toStdString() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_writer = new QTextStream(&file);
|
|
|
|
|
|
|
|
_writer->setCodec("UTF-8");
|
|
|
|
|
|
|
|
SaveAccounts();
|
|
|
|
SaveAccountAmounts();
|
|
|
|
SaveCategories();
|
2014-11-10 11:54:28 +01:00
|
|
|
SaveTags();
|
2013-01-17 20:22:42 +01:00
|
|
|
SaveOperations(operations);
|
|
|
|
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
delete _writer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|