2 * Copyright (C) 2011, Jamie Thompson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include "RestoreCrashBackupWindow.h"
21 #include "EventLogBackupManager.h"
23 #include "RestoreAccountStateWindow.h"
29 using namespace Windows;
31 RestoreCrashBackupWindow::RestoreCrashBackupWindow(Settings& settings, QWidget *parent) :
32 BaseWindow(settings, parent)
34 if(LockedBackupsPresent())
40 void RestoreCrashBackupWindow::CreateContents()
42 switch(CurrentSettings().AppMode())
44 case Settings::APPMODE_GUI:
46 QVBoxLayout *layout = new QVBoxLayout();
48 layout->setMargin(40);
50 QLabel *lblNotice1 = new QLabel(tr("A backup has been found that suggests this program crashed during an import."));
51 lblNotice1->setAlignment(Qt::AlignCenter);
52 lblNotice1->setWordWrap(true);
53 layout->addWidget(lblNotice1, 1);
55 QLabel *lblNotice2 = new QLabel(tr("It is strongly advised that you restore this backup so the system is operating from a known valid state."));
56 lblNotice2->setWordWrap(true);
57 lblNotice2->setAlignment(Qt::AlignCenter);
58 layout->addWidget(lblNotice2, 1);
60 QLabel *lblNotice3 = new QLabel(tr("However, any events that have occurred since the backup was made will be lost."));
61 lblNotice3->setWordWrap(true);
62 lblNotice3->setAlignment(Qt::AlignCenter);
63 layout->addWidget(lblNotice3, 1);
65 QHBoxLayout *layoutButtons = new QHBoxLayout();
67 QPushButton* btnRestoreBackup = new QPushButton(tr("Restore backup"));
68 QObject::connect(btnRestoreBackup, SIGNAL(clicked()), this, SLOT(RestoreBackup()));
69 layoutButtons->addWidget(btnRestoreBackup);
70 layoutButtons->setAlignment(btnRestoreBackup, Qt::AlignHCenter);
72 QPushButton* btnUnlockBackup = new QPushButton(tr("Unlock backup"));
73 QObject::connect(btnUnlockBackup, SIGNAL(clicked()), this, SLOT(UnlockBackup()));
74 layoutButtons->addWidget(btnUnlockBackup);
75 layoutButtons->setAlignment(btnUnlockBackup, Qt::AlignHCenter);
77 layout->addLayout(layoutButtons);
79 centralWidget()->setLayout(layout);
85 case Settings::APPMODE_CONSOLE:
95 void RestoreCrashBackupWindow::RestoreBackup()
97 EventLogBackupManager backupManager(CurrentSettings());
98 QFileInfoList lockedBackupPaths(backupManager.CurrentBackups(true));
99 if(lockedBackupPaths.count() > 1 && QMessageBox::Yes != QMessageBox::question(this,
100 tr("Warning! Multiple locked backups found!"),
101 tr("This should never be possible, and we're very sorry for the problem. Only the most recent backup will be restored and all other locked backups will be unlocked. Is this OK?"),
102 QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes))
109 for(int i = 0; i < lockedBackupPaths.count(); ++i)
111 // Only restore the most recent (the list returned from CurrentBackups is ordered by name desc)
113 backupManager.RestoreBackup(lockedBackupPaths.value(i).absoluteFilePath());
115 backupManager.UnlockBackup(lockedBackupPaths.value(i).absoluteFilePath());
120 catch(const std::exception &exception)
122 QMessageBox::critical(this,
124 QString(tr("A serious error has occurred whilst unlocking backups:\n%1\nPlease try again.")).arg(exception.what()));
128 void RestoreCrashBackupWindow::UnlockBackup()
130 EventLogBackupManager backupManager(CurrentSettings());
131 QFileInfoList lockedBackupPaths(backupManager.CurrentBackups(true));
132 if(lockedBackupPaths.count() > 1 && QMessageBox::Yes != QMessageBox::question(this,
133 tr("Warning! Multiple locked backups found!"),
134 tr("This should never be possible, and we're very sorry for the problem. All locked backups must be unlocked before you continue. Is this OK?"),
135 QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes))
142 foreach(QFileInfo backupPath, lockedBackupPaths)
143 backupManager.UnlockBackup(backupPath.absoluteFilePath());
147 catch(const std::exception &exception)
149 QMessageBox::critical(this,
151 QString(tr("A serious error has occurred whilst unlocking backups:\n%1\nPlease try again.")).arg(exception.what()));
155 void RestoreCrashBackupWindow::Advance()
157 QWidget *next = new RestoreAccountStateWindow(CurrentSettings());
162 bool RestoreCrashBackupWindow::LockedBackupsPresent()
164 EventLogBackupManager backupManager(CurrentSettings());
165 return backupManager.CurrentBackups(true).count() > 0;