KissCount/controller/KissCount.cpp

146 lines
3.0 KiB
C++

#include "KissCount.h"
KissCount::KissCount() : _user(NULL)
{
_wxUI = new wxUI(this, _("KissCount"), wxPoint(50, 50), wxSize(1024, 768));
_wxUI->Show(true);
_wxUI->Centre();
_wxUI->Disable();
try
{
_db = new Database();
}
catch (std::string s)
{
_wxUI->Close(true);
throw s;
}
_wxUI->ChangeUser();
_wxUI->Enable();
}
KissCount::~KissCount()
{
delete _db;
delete _wxUI;
if (_user) delete _user;
}
std::list<wxString> KissCount::GetUsers()
{
return _db->GetUsers();
}
bool KissCount::IsValidUser(wxString user, wxString password)
{
return _db->IsValidUser(user, password) ;
}
void KissCount::LoadUser(wxString 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] != NULL) return;
if (_user->_operations[year] != NULL)
{
delete _user->_operations[year];
}
_db->LoadYear(_user, year);
}
User* KissCount::GetUser()
{
return _user;
}
double KissCount::GetAccountAmount(wxString id, int month, int year)
{
return _db->GetAccountAmount(id, month, year);
}
void KissCount::UpdateOperation(struct operation op)
{
_db->UpdateOperation(op);
}
wxString KissCount::AddOperation(struct operation op)
{
return _db->AddOperation(_user, op);
}
void KissCount::DeleteOperation(struct operation op)
{
_db->DeleteOperation(op);
}
void KissCount::SetAccountAmount(int month, int year, wxString accountId, double amount)
{
_db->SetAccountAmount(month, year, accountId, amount);
}
void KissCount::AddAccount(struct Account ac)
{
ac.id = _db->AddAccount(_user, ac);
_user->_accounts.push_back(ac);
}
void KissCount::UpdateAccount(struct Account ac)
{
std::vector<Account>::iterator it;
int i;
_db->UpdateAccount(ac);
for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++)
if (it->id == ac.id)
_user->_accounts[i] = ac;
}
void KissCount::DeleteAccount(struct Account ac)
{
std::vector<Account>::iterator it;
int i;
_db->DeleteAccount(ac);
for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++)
if (it->id == ac.id)
_user->_accounts.erase(_user->_accounts.begin()+i);
}
void KissCount::AddCategory(wxString name, wxColour colour)
{
wxString color;
color = _("#") ;
color += wxString::Format(_("%02X"), colour.Red());
color += wxString::Format(_("%02X"), colour.Green());
color += wxString::Format(_("%02X"), colour.Blue());
_db->AddCategory(_user, name, color);
}
void KissCount::UpdateCategory(wxString oldName, wxString name, wxColour colour)
{
wxString color;
color = _("#") ;
color += wxString::Format(_("%02X"), colour.Red());
color += wxString::Format(_("%02X"), colour.Green());
color += wxString::Format(_("%02X"), colour.Blue());
_db->UpdateCategory(_user, oldName, name, color);
}
void KissCount::DeleteCategory(wxString name)
{
_db->DeleteCategory(_user, name);
}