Add Snapshot feature

Add ChangeDatabase() method
Add GetDatabaseHome() and GetDatabaseFile()
This commit is contained in:
2012-03-20 20:57:45 +01:00
parent 391dc12304
commit e273303dcd
10 changed files with 303 additions and 8 deletions

View File

@@ -26,6 +26,7 @@
#include "AccountPanel.hpp"
#include "grid/FloatDelegate.hpp"
#include "GenerateDialog.hpp"
#include "SnapshotsDialog.hpp"
enum {ACCOUNT_NUMBER, ACCOUNT_NAME, ACCOUNT_INIT, ACCOUNT_CUR, ACCOUNT_FINAL, NUMBER_COLS_ACCOUNTS};
enum {CUR_CREDIT, CUR_DEBIT, TOTAL_CREDIT, TOTAL_DEBIT, BALANCE, STATS_ROW, CATS_STATS, NON_FIX};
@@ -167,10 +168,12 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, pare
QPushButton* buttonGroup = new QPushButton(_("Group"));
QPushButton* buttonUnGroup = new QPushButton(_("UnGroup"));
QPushButton* buttonUpdateNextMonths = new QPushButton(_("Update next months"));
QPushButton* buttonSnapshots = new QPushButton(_("Snapshots"));
connect(buttonGroup, SIGNAL(clicked()), this, SLOT(OnGroup()));
connect(buttonUnGroup, SIGNAL(clicked()), this, SLOT(OnUnGroup()));
connect(buttonUpdateNextMonths, SIGNAL(clicked()), this, SLOT(OnUpdateNextMonths()));
connect(buttonSnapshots, SIGNAL(clicked()), this, SLOT(OnSnapshots()));
vbox3->addWidget(_tree);
vbox3->addWidget(buttonUpdateNextMonths);
@@ -178,6 +181,8 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, pare
vbox3->addWidget(buttonGroup);
vbox3->addWidget(buttonUnGroup);
vbox3->addStretch(1);
vbox3->addWidget(buttonSnapshots);
vbox3->addStretch(1);
vbox3->addWidget(groupBox);
hbox->addLayout(vbox3);
@@ -1202,3 +1207,9 @@ end:
delete[] deltas;
delete[] cur_amounts;
}
void AccountPanel::OnSnapshots()
{
SnapshotsDialog u(_kiss, _wxUI);
u.exec();
}

View File

@@ -67,6 +67,7 @@ private slots:
void OnGroup();
void OnUnGroup();
void OnUpdateNextMonths();
void OnSnapshots();
private:
QTreeWidget *_tree;

View File

@@ -0,0 +1,191 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "SnapshotsDialog.hpp"
#include <QMessageBox>
#include <QDir>
#include <model/Database.hpp>
SnapshotsDialog::SnapshotsDialog(KissCount* kiss, wxUI *parent) : QDialog(0, Qt::Dialog), _kiss(kiss), _wxUI(parent)
{
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
QVBoxLayout *vbox2 = new QVBoxLayout;
setWindowTitle(_("Snapshots"));
setModal(true);
setLayout(vbox);
_snapshots = new QListWidget(this);
hbox->addWidget(_snapshots);
QPushButton* buttonCreate = new QPushButton(_("Create snapshot"));
QPushButton* buttonDelete = new QPushButton(_("Delete snapshot"));
QPushButton* buttonBackTo = new QPushButton(_("Back to this snapshot"));
connect(buttonCreate, SIGNAL(clicked()), this, SLOT(OnCreate()));
connect(buttonDelete, SIGNAL(clicked()), this, SLOT(OnDelete()));
connect(buttonBackTo, SIGNAL(clicked()), this, SLOT(OnBackTo()));
vbox2->addWidget(buttonCreate);
vbox2->addWidget(buttonDelete);
vbox2->addWidget(buttonBackTo);
hbox->addLayout(vbox2);
QPushButton* ok = new QPushButton(_("OK"));
connect(ok, SIGNAL(clicked()), this, SLOT(OnOK()));
vbox->addLayout(hbox);
vbox->addWidget(ok);
LoadFiles();
}
void SnapshotsDialog::LoadFiles()
{
QDir dir(Database::GetDatabaseHome());
QStringList res;
QStringList::const_iterator it;
res << Database::GetDatabaseFile() + ".bak.????.??.??*";
dir.setNameFilters(res);
dir.setFilter(QDir::Files);
dir.setSorting(QDir::Name);
res.clear();
res = dir.entryList();
_snapshots->clear();
for(it=res.begin(); it!=res.end(); it++)
{
QString item = it->right(it->size()-Database::GetDatabaseFile().size()-5);
new QListWidgetItem(item, _snapshots);
}
}
void SnapshotsDialog::OnCreate()
{
QDir dir(Database::GetDatabaseHome());
QString bak, bak2, v ;
QDate curDate = QDate::currentDate();
int i;
bak = Database::GetDatabaseFile() + ".bak.";
bak += v.sprintf("%d.%02d.%02d", curDate.year(), curDate.month(), curDate.day());
if (dir.exists(bak))
{
bak2 = bak;
for(i=2; dir.exists(bak2); i++)
{
bak2 = bak + "_" + QString::number(i);
}
bak = bak2;
}
QFile file(Database::GetDatabaseHome() + Database::GetDatabaseFile());
if (file.copy(Database::GetDatabaseHome() + bak))
{
new QListWidgetItem(bak.right(bak.size()-Database::GetDatabaseFile().size()-5), _snapshots);
QMessageBox::information(0, "KissCount", bak + _(" successfully created"));
}
else
{
QMessageBox::critical(0, _("Error"), _("Unable to create ") + Database::GetDatabaseHome() + bak);
}
}
void SnapshotsDialog::OnDelete()
{
QString filename ;
QListWidgetItem * item = _snapshots->currentItem();
if (!item) return;
filename = Database::GetDatabaseFile() + ".bak." + item->text();
QFile file(Database::GetDatabaseHome() + filename);
if (!file.exists())
{
QMessageBox::critical(0, _("Error"), filename + _(" does not exist"));
return;
}
if (QMessageBox::question(0, "KissCount", _("Are you sure want to delete ") + filename + " ?", QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
return;
if (!file.remove())
{
QMessageBox::critical(0, _("Error"), _("Unable to remove ") + filename);
return;
}
QMessageBox::information(0, "KissCount", filename + _(" successfully removed"));
delete _snapshots->takeItem(_snapshots->currentRow());
}
void SnapshotsDialog::OnBackTo()
{
QString filename, defaultFilename ;
QListWidgetItem * item = _snapshots->currentItem();
QString user;
if (!item) return;
filename = Database::GetDatabaseFile() + ".bak." + item->text();
defaultFilename = Database::GetDatabaseFile();
QFile file(Database::GetDatabaseHome() + filename);
QFile defaultFile(Database::GetDatabaseHome() + defaultFilename);
if (!file.exists())
{
QMessageBox::critical(0, _("Error"), filename + _(" does not exist"));
return;
}
if (QMessageBox::question(0, "KissCount", _("Are you sure want to come back to ") + filename + " ?", QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
return;
if (!defaultFile.remove())
{
QMessageBox::critical(0, _("Error"), _("Unable to remove ") + defaultFilename);
return;
}
if (file.copy(Database::GetDatabaseHome() + defaultFilename))
user = _kiss->GetUser()->_name;
_kiss->ChangeDatabase(Database::GetDatabaseHome() + defaultFilename);
_kiss->LoadUser(user);
QMessageBox::information(0, "KissCount", _("Welcome back to ") + filename);
}
void SnapshotsDialog::OnOK()
{
close();
}

View File

@@ -0,0 +1,49 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef SNAPSHOTSDIALOG_H
#define SNAPSHOTSDIALOG_H
#include <QDialog>
#include <QListWidget>
#include "wxUI.hpp"
class SnapshotsDialog : public QDialog
{
Q_OBJECT;
public:
SnapshotsDialog(KissCount* kiss, wxUI *parent);
private slots:
void OnOK();
void OnCreate();
void OnDelete();
void OnBackTo();
private:
KissCount* _kiss;
wxUI* _wxUI;
QListWidget* _snapshots;
// QLineEdit* _oldSnapshots, *_newSnapshots, *_confirmSnapshots;
void LoadFiles();
};
#endif