/* Copyright 2010-2012 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 #include #include "KissCount.hpp" #include std::vector * KissCount::_importEngines; std::vector * KissCount::_exportEngines; KissCount::KissCount(int argc, char** argv) : QApplication(argc, argv), _user(0) { QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8")); _wxUI = new wxUI(this, "KissCount"); _wxUI->showMaximized(); _wxUI->setDisabled(true); try { _db = new Database((argc == 2) ? argv[1] : 0, this); } catch (std::string s) { _wxUI->close(); throw ; } _wxUI->ChangeUser(); _wxUI->setDisabled(false); } KissCount::~KissCount() { delete _db; delete _wxUI; delete _importEngines; delete _exportEngines; if (_user) delete _user; } std::list KissCount::GetUsers() { return _db->GetUsers(); } bool KissCount::IsValidUser(const QString& user, const QString& password) { return _db->IsValidUser(user, password) ; } void KissCount::LoadUser(const QString& user) { if (_user) delete _user; _user = _db->LoadUser(user) ; if (_user) _wxUI->LoadUser(); } void KissCount::LoadYear(int year, bool force) { if (!force && _user->_operations[year] != 0) return; if (_user->_operations[year] != 0) { delete _user->_operations[year]; _user->_operations[year] = 0; } _db->LoadYear(_user, year); } User* KissCount::GetUser() { return _user; } double KissCount::GetAccountAmount(int id, int month, int year) { return _db->GetAccountAmount(id, month, year); } double KissCount::CalcAccountAmount(int id, int month, int year, bool* had_values) { return _db->CalcAccountAmount(id, month, year, had_values); } void KissCount::UpdateOperation(Operation& op, bool checkTransfert) { // Unlink if (checkTransfert) { op.transfert = 0; _user->LinkOrUnlinkOperation(op); } _db->UpdateOperation(_user, op, checkTransfert); // Link if (checkTransfert) _user->LinkOrUnlinkOperation(op); } int KissCount::AddOperation(Operation& op, bool checkTransfert) { int ret = _db->AddOperation(_user, op, checkTransfert); if (checkTransfert && op.transfert) _user->LinkOrUnlinkOperation(op); return ret; } void KissCount::DeleteOperation(Operation& op) { if (op.transfert) { op.transfert = 0; _user->LinkOrUnlinkOperation(op); } _db->DeleteOperation(_user, op); } void KissCount::DeleteOperations(int month, int year) { _db->DeleteOperations(_user, month, year); if (month != -1) (*_user->_operations[year]).erase(month); if (month == -1 || !_user->_operations[year]->size()) { delete _user->_operations[year]; _user->_operations.erase(year); } } double KissCount::MetaAmount(int id) { return _db->MetaAmount(id); } double KissCount::MetaPositiveAmount(int id) { return _db->MetaPositiveAmount(id); } void KissCount::SetAccountAmount(int accountId, int month, int year, double amount) { _db->SetAccountAmount(accountId, month, year, amount); } int KissCount::AddAccount(Account& ac) { QDate curDate = QDate::currentDate(); ac.id = _db->AddAccount(_user, ac); _user->AddAccount(ac); SetAccountAmount(ac.id, curDate.month(), curDate.year(), 0.0); return ac.id; } void KissCount::UpdateAccount(Account& ac) { _db->UpdateAccount(ac); _user->UpdateAccount(ac); // if (ac._default) // std::sort(_user->_accounts.begin(), _user->_accounts.end(), Account()); } void KissCount::DeleteAccount(Account& ac, int replacement) { std::map >* >::iterator it2; _db->DeleteAccount(_user, ac, replacement); _user->DeleteAccount(ac); for (it2= _user->_operations.begin(); it2 != _user->_operations.end(); it2++) LoadYear(it2->first, true); } void KissCount::AddSharedAccount(Account& ac, const QString& granted) { _db->AddSharedAccount(ac, granted); } void KissCount::RemoveSharedAccount(Account& ac, int granted) { _db->RemoveSharedAccount(ac, granted); } std::map KissCount::getSharedAccountOwners(int account) { return _db->getSharedAccountOwners(account); } QString KissCount::getSharedAccountOwner(int account) { return _db->getSharedAccountOwner(account); } int KissCount::AddCategory(Category& category) { category.id = _db->AddCategory(_user, category); _user->AddCategory(category); return category.id; } void KissCount::UpdateCategory(Category& category) { _db->UpdateCategory(category); _user->UpdateCategory(category); } void KissCount::DeleteCategory(Category& category, int replacement) { std::map >* >::iterator it; _db->DeleteCategory(_user, category, replacement); _user->DeleteCategory(category); for (it= _user->_operations.begin(); it != _user->_operations.end(); it++) LoadYear(it->first, true); } std::map > KissCount::GetAllOperations() { return _db->GetAllOperations(_user); } void KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearTo) { std::vector::iterator it, it2; std::map meta; Operation op; _db->GenerateMonth(_user, monthFrom, yearFrom, monthTo, yearTo); if (!_user->_operations[yearTo]) _user->_operations[yearTo] = new std::map >(); if (monthFrom != -1 && yearFrom != -1) { LoadYear(yearFrom, false); for(it = (*_user->_operations[yearFrom])[monthFrom].begin(); it != (*_user->_operations[yearFrom])[monthFrom].end(); it++) { if (!it->fix_cost) continue; op = *it; op.month = monthTo; op.year = yearTo; op.checked = false; op.id = AddOperation(op); op.childs.clear(); op._virtual = false; if (op.meta) meta[it->id] = op.id; (*_user->_operations[yearTo])[monthTo].push_back(op); } // Re Generate parents for(it = (*_user->_operations[yearTo])[monthTo].begin(); it != (*_user->_operations[yearTo])[monthTo].end(); it++) { if (it->parent) { it->parent = meta[it->parent]; UpdateOperation(*it); for(it2 = (*_user->_operations[yearTo])[monthTo].begin(); it2 != (*_user->_operations[yearTo])[monthTo].end(); it2++) if (it2->id == it->parent) { it2->childs.push_back(it->id); break; } } } } _wxUI->GenerateMonth(monthTo, yearTo); } void KissCount::ChangePassword(const QString& password) { _db->ChangePassword(_user, password); } bool KissCount::UserExists(const QString& name) { return _db->UserExists(name); } void KissCount::ChangeName(const QString& name) { _db->ChangeName(_user, name); _user->_name = name; } // To enable translation during xgettext QString default_cats[] = { _("Fix"), _("Groceries"), _("Hobbies"), _("Car"), _("Unexpected"), _("Other") }; void KissCount::NewUser(const QString& name) { QDate curDate = QDate::currentDate(); Account ac = { /*.id = */0, /*.name = */_("Account 1"), /*.number = */0, /*.shared = */false, /*.blocked = */false, /*._default = */true, /*.is_owner = */true}; Category cat ; _db->NewUser(name); if (_user) delete _user; _user = _db->LoadUser(name) ; AddAccount(ac); cat.parent = 0 ; cat.name = "Fix" ; cat.backcolor = view::OWN_YELLOW ; cat.forecolor = Qt::black; cat.fix_cost = true; AddCategory(cat); cat.parent = 0 ; cat.name = "Groceries" ; cat.backcolor = view::OWN_GREEN; cat.forecolor = Qt::black; cat.fix_cost = false; AddCategory(cat); cat.parent = 0 ; cat.name = "Hobbies" ; cat.backcolor = view::OWN_GREEN; cat.forecolor = Qt::black; cat.fix_cost = false; AddCategory(cat); cat.parent = 0 ; cat.name = "Car" ; cat.backcolor = view::OWN_GREEN; cat.forecolor = Qt::black; cat.fix_cost = false; AddCategory(cat); cat.parent = 0 ; cat.name = "Unexpected" ; cat.backcolor = view::OWN_GREEN; cat.forecolor = Qt::black; cat.fix_cost = false; AddCategory(cat); cat.parent = 0 ; cat.name = "Other" ; cat.backcolor = view::OWN_GREEN; cat.forecolor = Qt::black; cat.fix_cost = false; AddCategory(cat); SetOperationOrder("ASC"); _db->GenerateMonth(_user, -1, -1, (int)curDate.month(), curDate.year()); } void KissCount::KillMe() { _wxUI->KillMe(); _db->KillMe(_user); delete _user; _user = 0; _wxUI->ChangeUser(); } void KissCount::SetLanguage(QString language) { _user->_preferences["language"] = language; _db->UpdatePreference(_user, "language"); } /* ASC (default) or DESC */ void KissCount::SetOperationOrder(const QString& order) { _user->_preferences["operation_order"] = order; _db->UpdatePreference(_user, "operation_order"); } const QString& KissCount::GetOperationOrder() { return _user->_preferences["operation_order"] ; } std::vector* KissCount::Search(QString* description, QDate* dateFrom, QDate* dateTo, double* amountFrom, double* amountTo, std::vector categories, int types, std::vector accounts) { return _db->Search(_user, description, dateFrom, dateTo, amountFrom, amountTo, categories, types, accounts, true); } bool KissCount::SearchPreviousOperation(Operation* res, Operation& op, int month, int year, bool limitToType) { std::vector* operations; QDate* date ; //wxDateSpan threeMonths(0, 3); Not working :( std::vector v; int i; month -= 3; if (month < 0) { year -= 1; month += 12; } date = new QDate(year, month, 0); if (limitToType) operations = _db->Search(_user, &op.description, date, 0, 0, 0, v, op.fix_cost ? +Database::FIX_OP : +Database::NON_FIX_OP, v, false); else operations = _db->Search(_user, &op.description, date, 0, 0, 0, v, Database::ALL_OP, v, false); delete date; if (!operations->size()) { delete operations; return false; } for(i=operations->size()-1; i>=0; i--) if (!(*operations)[i].meta) { *res = (*operations)[i]; delete operations; return true; } delete operations; return false; } void KissCount::GetHistory(int month, int year, QStringList& list) { month -= 3; if (month < 0) { year -= 1; month += 12; } _db->GetHistory(month, year, list); } void KissCount::GetStats(int monthFrom, int yearFrom, int monthTo, int yearTo, std::map > >* accountAmounts, std::map* categories) { _db->GetStats(_user, monthFrom, yearFrom, monthTo, yearTo, accountAmounts, categories); } void KissCount::GetMonthStats(int month, int year, int nbDays, std::map >* operations, std::map* categories) { _db->GetMonthStats(_user, month, year, nbDays, operations, categories); } void KissCount::UpdateStats() { _wxUI->UpdateStats(); } std::map* KissCount::GetNotChecked(int month, int year) { return _db->GetNotChecked(_user, month, year); } std::map* KissCount::GetVirtualAmount(int month, int year) { return _db->GetVirtualAmount(_user, month, year); } QFont KissCount::ExtractFont(QString strFont) { long int pointSize, weight, family, style; QString faceName; if (!strFont.size()) { DEFAULT_FONT(f); return f; } QStringList list = strFont.split(";"); pointSize = list[0].toInt(); family = list[1].toInt(); style = list[2].toInt(); weight = list[3].toInt(); faceName = list[4]; return QFont(faceName, pointSize, weight); } QString KissCount::CompactFont(const QFont& font) { QString res ; res = res.sprintf("%d;%d;%d;%d;", font.pointSize(), 0, 0, font.weight()); res += font.family(); return res; } void KissCount::UnRegisterImportEngine(ImportEngine* engine) { std::vector* importEngines = KissCount::GetImportEngines(); std::vector::iterator it = std::find(importEngines->begin(), importEngines->end(), engine); if (it!=importEngines->end()) importEngines->erase(it); } void KissCount::RegisterImportEngine(ImportEngine* engine) { std::vector* importEngines = KissCount::GetImportEngines(); importEngines->push_back(engine); } QString KissCount::GetImportEngineExtensions() { QString res; std::vector::iterator it; int i; std::vector* importEngines = KissCount::GetImportEngines(); for(i=0; i<(int)importEngines->size()-1; i++) res = res + (*importEngines)[i]->GetFileExt() + ";;" ; if (importEngines->size()) res = res + (*importEngines)[i]->GetFileExt(); return res; } ImportEngine* KissCount::GetImportEngine(QString path) { std::vector::iterator it; std::vector* importEngines = KissCount::GetImportEngines(); for(it=importEngines->begin(); it!=importEngines->end(); it++) { if ((*it)->HandleFile(path, _user, _db, this)) return *it; } return 0; } void KissCount::UpdateImportPattern() { _db->UpdateImportPattern(_user); } void KissCount::UnRegisterExportEngine(ExportEngine* engine) { std::vector* exportEngines = KissCount::GetExportEngines(); std::vector::iterator it = std::find(exportEngines->begin(), exportEngines->end(), engine); if (it!=exportEngines->end()) exportEngines->erase(it); } void KissCount::RegisterExportEngine(ExportEngine* engine) { std::vector* exportEngines = KissCount::GetExportEngines(); exportEngines->push_back(engine); } QString KissCount::GetExportEngineExtensions() { QString res; std::vector::iterator it; int i; std::vector* exportEngines = KissCount::GetExportEngines(); for(i=0; i<(int)exportEngines->size()-1; i++) res = res + (*exportEngines)[i]->GetFileExt() + ";;" ; if (exportEngines->size()) res = res + (*exportEngines)[i]->GetFileExt(); return res; } ExportEngine* KissCount::GetExportEngine(QString path, QString filter) { std::vector::iterator it; std::vector* exportEngines = KissCount::GetExportEngines(); QString tmp; for(it=exportEngines->begin(); it!=exportEngines->end(); it++) { if (filter == (*it)->GetFileExt()) { tmp = path; if (!tmp.endsWith((*it)->GetShortExt())) tmp += (*it)->GetShortExt(); if ((*it)->HandleFile(tmp, _user, _db, this)) return *it; } } return 0; } std::vector* KissCount::GetImportEngines() { if (!_importEngines) _importEngines = new std::vector; return _importEngines; } std::vector* KissCount::GetExportEngines() { if (!_exportEngines) _exportEngines = new std::vector; return _exportEngines; } bool KissCount::ChangeDatabase(QString filename) { return _db->ChangeDatabase(filename); }