/*
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 "SnapshotsDialog.hpp"
#include
#include
#include
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 a snapshot"));
QPushButton* buttonDelete = new QPushButton(_("Delete a 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;
std::list users;
std::list::iterator it;
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);
users = _kiss->GetUsers();
for(it=users.begin(); it!=users.end(); it++)
{
if (*it == user)
{
_kiss->LoadUser(user);
QMessageBox::information(0, "KissCount", _("Welcome back to ") + filename);
break;
}
}
close();
if (it == users.end())
{
_wxUI->KillMe(); // Clear all panels
QMessageBox::information(0, "KissCount", _("Welcome back to ") + filename);
_wxUI->ChangeUser();
}
}
void SnapshotsDialog::OnOK()
{
close();
}