-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
464 lines (408 loc) · 15.3 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**********************************************************************
* mainwindow.cpp
**********************************************************************
* Copyright (C) 2015 MX Authors
*
* Authors: Adrian
* MX Linux <http://mxlinux.org>
*
* This file is part of MX Snapshot.
*
* MX Snapshot 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.
*
* MX Snapshot 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 MX Snapshot. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <QDebug>
#include <QFileDialog>
#include <QKeyEvent>
#include <QScrollBar>
#include <QTextStream>
#include <QTime>
#include "about.h"
#include "mainwindow.h"
#include "settings.h"
#include "work.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent, const QCommandLineParser &arg_parser) :
QDialog(parent),
Settings(arg_parser),
ui(new Ui::MainWindow),
work(this)
{
ui->setupUi(this);
monthly = arg_parser.isSet("month");
setConnections();
setup();
loadSettings();
listFreeSpace();
setExclusions();
setOtherOptions();
if (monthly) {
ui->buttonNext->click();
ui->buttonNext->click();
} else {
listUsedSpace();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
// load settings or use the default value
void MainWindow::loadSettings()
{
qDebug() << "+++" << __PRETTY_FUNCTION__ << "+++";
ui->labelSnapshotDir->setText(snapshot_dir);
if (snapshot_name.isEmpty())
ui->lineEditName->setText(getFilename());
else
ui->lineEditName->setText(snapshot_name);
}
void MainWindow::setOtherOptions()
{
ui->cbCompression->setCurrentIndex(ui->cbCompression->findText(compression));
ui->checksums->setChecked(make_chksum);
ui->radioRespin->setChecked(reset_accounts);
}
void MainWindow::setConnections()
{
connect(qApp, &QApplication::aboutToQuit, [this] { cleanUp(); });
connect(&timer, &QTimer::timeout, this, &MainWindow::progress);
connect(shell, &Cmd::started, this, &MainWindow::procStart);
connect(shell, &Cmd::finished, this, &MainWindow::procDone);
connect(shell, &Cmd::outputAvailable, [](const QString &out) { qDebug().noquote() << out; });
connect(shell, &Cmd::errorAvailable, [](const QString &out) { qWarning().noquote() << out; });
connect(&work, &Work::message, this, &MainWindow::processMsg);
connect(&work, &Work::messageBox, this, &MainWindow::processMsgBox);
}
void MainWindow::setExclusions()
{
ui->excludeDesktop->setChecked(exclusions.testFlag(Exclude::Desktop));
ui->excludeDocuments->setChecked(exclusions.testFlag(Exclude::Documents));
ui->excludeDownloads->setChecked(exclusions.testFlag(Exclude::Downloads));
ui->excludeMusic->setChecked(exclusions.testFlag(Exclude::Music));
ui->excludeNetworks->setChecked(exclusions.testFlag(Exclude::Networks));
ui->excludePictures->setChecked(exclusions.testFlag(Exclude::Pictures));
ui->excludeVideos->setChecked(exclusions.testFlag(Exclude::Videos));
}
// setup/refresh versious items first time program runs
void MainWindow::setup()
{
qDebug() << "+++" << __PRETTY_FUNCTION__ << "+++";
setWindowFlags(Qt::Window); // for the close, min and max buttons
QFont font("monospace");
font.setStyleHint(QFont::Monospace);
ui->outputBox->setFont(font);
this->setWindowTitle(tr("MX Snapshot"));
ui->buttonBack->setHidden(true);
ui->stackedWidget->setCurrentIndex(0);
ui->buttonCancel->setEnabled(true);
ui->buttonNext->setEnabled(true);
this->show();
}
// List used space
void MainWindow::listUsedSpace()
{
this->show();
ui->buttonNext->setDisabled(true);
ui->buttonCancel->setDisabled(true);
ui->buttonSelectSnapshot->setDisabled(true);
QString out = getUsedSpace();
ui->buttonNext->setEnabled(true);
ui->buttonCancel->setEnabled(true);
ui->buttonSelectSnapshot->setEnabled(true);
ui->labelUsedSpace->setText(out);
}
// List free space on drives
void MainWindow::listFreeSpace()
{
qDebug() << "+++" << __PRETTY_FUNCTION__ << "+++";
QString path = snapshot_dir;
path.remove(QRegularExpression("/snapshot$"));
QString free_space = getFreeSpaceStrings(path);
ui->labelFreeSpace->clear();
ui->labelFreeSpace->setText("- " + tr("Free space on %1, where snapshot folder is placed: ").arg(path) + free_space + "\n");
ui->labelDiskSpaceHelp->setText(tr("The free space should be sufficient to hold the compressed data from / and /home\n\n"
" If necessary, you can create more available space\n"
" by removing previous snapshots and saved copies:\n"
" %1 snapshots are taking up %2 of disk space.\n").arg(QString::number(getSnapshotCount())).arg(getSnapshotSize()));
}
// Installs package
bool MainWindow::installPackage(const QString &package)
{
this->setWindowTitle(tr("Installing ") + package);
ui->outputLabel->setText(tr("Installing ") + package);
ui->outputBox->clear();
ui->buttonNext->setDisabled(true);
ui->buttonBack->setDisabled(true);
ui->stackedWidget->setCurrentWidget(ui->outputPage);
displayOutput();
if (!work.installPackage(package)) {
disableOutput();
return false;
}
disableOutput();
return true;
}
// clean up changes before exit
[[ noreturn ]] void MainWindow::cleanUp()
{
ui->stackedWidget->setCurrentWidget(ui->outputPage);
work.cleanUp();
}
void MainWindow::procStart()
{
timer.start(500);
setCursor(QCursor(Qt::BusyCursor));
}
void MainWindow::processMsgBox(BoxType box_type, const QString &title, const QString &msg)
{
qDebug().noquote() << title << msg;
switch (box_type)
{
case BoxType::warning: QMessageBox::warning(this, title, msg); break;
case BoxType::critical: QMessageBox::critical(this, title, msg); break;
case BoxType::question: QMessageBox::question(this, title, msg); break;
case BoxType::information: QMessageBox::information(this, title, msg); break;
}
}
void MainWindow::processMsg(const QString &msg)
{
qDebug().noquote() << msg;
ui->outputLabel->setText(msg);
}
void MainWindow::procDone()
{
timer.stop();
ui->progressBar->setValue(ui->progressBar->maximum());
setCursor(QCursor(Qt::ArrowCursor));
}
void MainWindow::displayOutput()
{
connect(shell, &Cmd::outputAvailable, this, &MainWindow::outputAvailable);
connect(shell, &Cmd::errorAvailable, this, &MainWindow::outputAvailable);
}
void MainWindow::disableOutput()
{
disconnect(shell, &Cmd::outputAvailable, this, &MainWindow::outputAvailable);
disconnect(shell, &Cmd::errorAvailable, this, &MainWindow::outputAvailable);
}
// update output box
void MainWindow::outputAvailable(const QString &output)
{
if (output.contains("\r")) {
ui->outputBox->moveCursor(QTextCursor::Up, QTextCursor::KeepAnchor);
ui->outputBox->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
}
ui->outputBox->insertPlainText(output);
ui->outputBox->verticalScrollBar()->setValue(ui->outputBox->verticalScrollBar()->maximum());
}
void MainWindow::progress()
{
ui->progressBar->setValue((ui->progressBar->value() + 1) % 100);
// in live environment and first page, blink text while calculating used disk space
if (live && (ui->stackedWidget->currentIndex() == 0)) {
if (ui->progressBar->value() % 4 == 0 )
ui->labelUsedSpace->setText("\n " + tr("Please wait."));
else
ui->labelUsedSpace->setText("\n " + tr("Please wait. Calculating used disk space..."));
}
}
// Next button clicked
void MainWindow::on_buttonNext_clicked()
{
QString file_name = ui->lineEditName->text();
if (!file_name.endsWith(".iso")) file_name += ".iso";
// on first page
if (ui->stackedWidget->currentIndex() == 0) {
this->setWindowTitle(tr("Settings"));
ui->stackedWidget->setCurrentWidget(ui->settingsPage);
ui->buttonBack->setHidden(false);
ui->buttonBack->setEnabled(true);
selectKernel();
ui->label_1->setText(tr("Snapshot will use the following settings:*"));
ui->label_2->setText("\n" + tr("- Snapshot directory:") + " " + snapshot_dir + "\n" +
"- " + tr("Snapshot name:") + " " + file_name + "\n" +
tr("- Kernel to be used:") + " " + kernel + "\n");
// on settings page
} else if (ui->stackedWidget->currentWidget() == ui->settingsPage) {
if (QMessageBox::Cancel == QMessageBox::question(this, tr("Final chance"),
tr("Snapshot now has all the information it needs to create an ISO from your running system.") + "\n\n" +
tr("It will take some time to finish, depending on the size of the installed system and the capacity of your computer.") + "\n\n" +
tr("OK to start?"), QMessageBox::Ok | QMessageBox::Cancel)) {
return;
}
work.started = true;
work.e_timer.start();
if (!checkSnapshotDir()) {
QMessageBox::critical(this, tr("Error"), tr("Could not create working directory. ") + snapshot_dir);
cleanUp();
}
if (!checkTempDir()) {
QMessageBox::critical(this, tr("Error"), tr("Could not create temporary directory. ") + snapshot_dir);
cleanUp();
}
if (not monthly) work.checkEnoughSpace();
otherExclusions();
ui->buttonNext->setEnabled(false);
ui->buttonBack->setEnabled(false);
ui->stackedWidget->setCurrentWidget(ui->outputPage);
this->setWindowTitle(tr("Output"));
ui->outputBox->clear();
work.copyNewIso();
ui->outputLabel->setText("");
if (!work.mkDir(file_name)) return;
work.savePackageList(file_name);
if (edit_boot_menu) {
if (QMessageBox::Yes == QMessageBox::question(this, tr("Edit Boot Menu"),
tr("The program will now pause to allow you to edit any files in the work directory. Select Yes to edit the boot menu or select No to bypass this step and continue creating the snapshot."),
QMessageBox::Yes | QMessageBox::No)) {
this->hide();
QString cmd = getEditor() + " \"" + work_dir + "/iso-template/boot/isolinux/isolinux.cfg\"";
shell->run(cmd);
this->show();
}
}
work.setupEnv();
displayOutput();
work.createIso(file_name);
ui->buttonCancel->setText(tr("Close"));
} else {
qApp->quit();
}
}
void MainWindow::on_buttonBack_clicked()
{
this->setWindowTitle(tr("MX Snapshot"));
ui->stackedWidget->setCurrentIndex(0);
ui->buttonNext->setEnabled(true);
ui->buttonBack->setHidden(true);
ui->outputBox->clear();
}
void MainWindow::on_buttonEditExclude_clicked()
{
this->hide();
shell->run(getEditor() + " " + snapshot_excludes.fileName());
this->show();
}
void MainWindow::on_excludeDocuments_toggled(bool checked)
{
excludeDocuments(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_excludeDownloads_toggled(bool checked)
{
excludeDownloads(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_excludePictures_toggled(bool checked)
{
excludePictures(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_excludeMusic_toggled(bool checked)
{
excludeMusic(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_excludeVideos_toggled(bool checked)
{
excludeVideos(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_excludeDesktop_toggled(bool checked)
{
excludeDesktop(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_radioRespin_toggled(bool checked)
{
reset_accounts = checked;
if (checked and not ui->excludeAll->isChecked())
ui->excludeAll->click();
}
void MainWindow::on_radioPersonal_clicked(bool checked)
{
reset_accounts = !checked;
if (checked and ui->excludeAll->isChecked())
ui->excludeAll->click();
}
// About button clicked
void MainWindow::on_buttonAbout_clicked()
{
this->hide();
displayAboutMsgBox(tr("About %1").arg(this->windowTitle()), "<p align=\"center\"><b><h2>" + this->windowTitle() +"</h2></b></p><p align=\"center\">" +
tr("Version: ") + qApp->applicationVersion() + "</p><p align=\"center\"><h3>" +
tr("Program for creating a live-CD from the running system for MX Linux") +
"</h3></p><p align=\"center\"><a href=\"http://mxlinux.org\">http://mxlinux.org</a><br /></p><p align=\"center\">" +
tr("Copyright (c) MX Linux") + "<br /><br /></p>",
"/usr/share/doc/mx-snapshot/license.html", tr("%1 License").arg(this->windowTitle()), true);
this->show();
}
// Help button clicked
void MainWindow::on_buttonHelp_clicked()
{
QLocale locale;
QString lang = locale.bcp47Name();
QString url = "/usr/share/doc/mx-snapshot/mx-snapshot.html";
if (lang.startsWith("fr"))
url = "https://mxlinux.org/french-wiki/help-files-fr/help-mx-instantane";
displayDoc(url, tr("%1 Help").arg(this->windowTitle()), true);
}
// Select snapshot directory
void MainWindow::on_buttonSelectSnapshot_clicked()
{
QFileDialog dialog;
QString selected = dialog.getExistingDirectory(this, tr("Select Snapshot Directory"), QString(), QFileDialog::ShowDirsOnly);
if (!selected.isEmpty()) {
snapshot_dir = selected + "/snapshot";
ui->labelSnapshotDir->setText(snapshot_dir);
listFreeSpace();
}
}
// process keystrokes
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
closeApp();
}
// close application
void MainWindow::closeApp() {
// ask for confirmation when on outputPage and not done
if (ui->stackedWidget->currentWidget() == ui->outputPage and not work.done) {
if (QMessageBox::Yes != QMessageBox::question(this, tr("Confirmation"), tr("Are you sure you want to quit the application?"),
QMessageBox::Yes | QMessageBox::No))
return;
}
cleanUp();
}
void MainWindow::on_buttonCancel_clicked()
{
closeApp();
}
void MainWindow::on_cbCompression_currentIndexChanged(const QString &arg1)
{
QSettings settings(config_file.fileName(), QSettings::IniFormat);
settings.setValue("compression", arg1);
compression = arg1;
}
void MainWindow::on_excludeNetworks_toggled(bool checked)
{
excludeNetworks(checked);
if (!checked) ui->excludeAll->setChecked(false);
}
void MainWindow::on_checksums_toggled(bool checked)
{
QSettings settings(config_file.fileName(), QSettings::IniFormat);
settings.setValue("make_md5sum", checked ? "yes" : "no");
make_chksum = checked;
}