Minor formatting tidying up.
[qwerkisync] / Windows / RestoreCrashBackupWindow.cpp
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
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.
8  *
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.
13  *
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/>.
17  */
18
19 #include "RestoreCrashBackupWindow.h"
20
21 #include "EventLogBackupManager.h"
22
23 #include "RestoreAccountStateWindow.h"
24
25 #include <QtGui>
26
27 #include <stdexcept>
28
29 using namespace Windows;
30
31 RestoreCrashBackupWindow::RestoreCrashBackupWindow(Settings& settings, QWidget *parent) :
32         BaseWindow(settings, parent)
33 {
34         if(LockedBackupsPresent())
35                 CreateContents();
36         else
37                 Advance();
38 }
39
40 void RestoreCrashBackupWindow::CreateContents()
41 {
42         switch(CurrentSettings().AppMode())
43         {
44                 case Settings::APPMODE_GUI:
45                 {
46                         QVBoxLayout *layout = new QVBoxLayout();
47                         {
48                                 layout->setMargin(40);
49
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);
54
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);
59
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);
64
65                                 QHBoxLayout *layoutButtons = new QHBoxLayout();
66                                 {
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);
71
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);
76                                 }
77                                 layout->addLayout(layoutButtons);
78
79                                 centralWidget()->setLayout(layout);
80                         }
81
82                         break;
83                 }
84
85                 case Settings::APPMODE_CONSOLE:
86                 {
87                         // Process args.
88
89                         Advance();
90                         break;
91                 }
92         }
93 }
94
95 void RestoreCrashBackupWindow::RestoreBackup()
96 {
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))
103         {
104                 return;
105         }
106
107         try
108         {
109                 for(int i = 0; i < lockedBackupPaths.count(); ++i)
110                 {
111                         // Only restore the most recent (the list returned from CurrentBackups is ordered by name desc)
112                         if(i == 0)
113                                 backupManager.RestoreBackup(lockedBackupPaths.value(i).absoluteFilePath());
114
115                         backupManager.UnlockBackup(lockedBackupPaths.value(i).absoluteFilePath());
116                 }
117
118                 Advance();
119         }
120         catch(const std::exception &exception)
121         {
122                 QMessageBox::critical(this,
123                         tr("Error"),
124                         QString(tr("A serious error has occurred whilst unlocking backups:\n%1\nPlease try again.")).arg(exception.what()));
125         }
126 }
127
128 void RestoreCrashBackupWindow::UnlockBackup()
129 {
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))
136         {
137                 return;
138         }
139
140         try
141         {
142                 foreach(QFileInfo backupPath, lockedBackupPaths)
143                         backupManager.UnlockBackup(backupPath.absoluteFilePath());
144
145                 Advance();
146         }
147         catch(const std::exception &exception)
148         {
149                 QMessageBox::critical(this,
150                         tr("Error"),
151                         QString(tr("A serious error has occurred whilst unlocking backups:\n%1\nPlease try again.")).arg(exception.what()));
152         }
153 }
154
155 void RestoreCrashBackupWindow::Advance()
156 {
157         QWidget *next = new RestoreAccountStateWindow(CurrentSettings());
158         next->show();
159         close();
160 }
161
162 bool RestoreCrashBackupWindow::LockedBackupsPresent()
163 {
164         EventLogBackupManager backupManager(CurrentSettings());
165         return backupManager.CurrentBackups(true).count() > 0;
166 }