-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
qefientryview.cpp
572 lines (503 loc) · 19.3 KB
/
qefientryview.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "qefientryview.h"
#include "qefientrystaticlist.h"
#include <QDebug>
#include <QMessageBox>
#include <QProcess>
#include <QDialog>
#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include <QFileDialog>
#include <QSaveFile>
#include <QInputDialog>
#include <QSpinBox>
#include <QDialogButtonBox>
#include "qefientrydetailview.h"
#include "qefiloadoptioneditorview.h"
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
// Fix namsapce change of hex and dec
#define hex Qt::hex
#define dec Qt::dec
#endif
QEFIEntryView::QEFIEntryView(QWidget *parent)
: QWidget(parent)
{
// Add list view and buttons
m_topLevelLayout = new QBoxLayout(QBoxLayout::LeftToRight, this);
m_entries = new QListWidget(this);
m_entries->setContextMenuPolicy(Qt::DefaultContextMenu);
m_entryItems = QEFIEntryStaticList::instance()->entries();
m_order = QEFIEntryStaticList::instance()->order();
m_selectedItemIndex = -1;
for (int i = 0; i < m_order.size(); i++) {
if (m_entryItems.contains(m_order[i])) {
QEFIEntry &entry = m_entryItems[m_order[i]];
QString item = QStringLiteral("[%1] %2")
.arg(entry.id(), 4, 16, QLatin1Char('0'))
.arg(entry.name());
if (entry.devicePath().size() > 0) {
item.append('\n').append(entry.devicePath());
}
m_entries->addItem(item);
// Get the activation state
QListWidgetItem *currentItem = m_entries->item(i);
if (currentItem && !entry.isActive()) {
currentItem->setForeground(Qt::gray);
}
}
}
QObject::connect(m_entries, &QListWidget::currentRowChanged,
this, &QEFIEntryView::entryChanged);
m_topLevelLayout->addWidget(m_entries, 3);
m_buttonLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
m_topLevelLayout->addLayout(m_buttonLayout, 1);
m_moveUpEntryButton = new QPushButton(tr("Move up"), this);
m_moveDownEntryButton = new QPushButton(tr("Move down"), this);
m_setCurrentButton = new QPushButton(tr("Make default"), this);
m_rebootTargetButton = new QPushButton(tr("Set reboot"), this);
m_rebootTargetButton->setDisabled(true);
m_buttonLayout->addWidget(m_moveUpEntryButton);
m_buttonLayout->addWidget(m_moveDownEntryButton);
m_buttonLayout->addWidget(m_setCurrentButton);
m_buttonLayout->addWidget(m_rebootTargetButton);
QObject::connect(m_moveUpEntryButton, &QPushButton::clicked,
this, &QEFIEntryView::moveUpClicked);
QObject::connect(m_moveDownEntryButton, &QPushButton::clicked,
this, &QEFIEntryView::moveDownClicked);
QObject::connect(m_setCurrentButton, &QPushButton::clicked,
this, &QEFIEntryView::setCurrentClicked);
QObject::connect(m_rebootTargetButton, &QPushButton::clicked,
this, &QEFIEntryView::rebootClicked);
m_buttonLayout->addStretch(1);
m_bootTimeoutLabel = new QLabel(tr("Timeout: %1 second(s)").arg(
QEFIEntryStaticList::instance()->timeout()), this);
m_addButton = new QPushButton(tr("Add"), this);
m_importButton = new QPushButton(tr("Import"), this);
m_saveButton = new QPushButton(tr("Save"), this);
m_resetButton = new QPushButton(tr("Reset"), this);
m_buttonLayout->addWidget(m_bootTimeoutLabel);
m_buttonLayout->addWidget(m_addButton);
m_buttonLayout->addWidget(m_importButton);
m_buttonLayout->addWidget(m_saveButton);
m_buttonLayout->addWidget(m_resetButton);
QObject::connect(m_addButton, &QPushButton::clicked,
this, &QEFIEntryView::addClicked);
QObject::connect(m_importButton, &QPushButton::clicked,
this, &QEFIEntryView::importClicked);
QObject::connect(m_saveButton, &QPushButton::clicked,
this, &QEFIEntryView::saveClicked);
QObject::connect(m_resetButton, &QPushButton::clicked,
this, &QEFIEntryView::resetFromStaticListClicked);
updateButtonState();
this->setLayout(m_topLevelLayout);
}
QEFIEntryView::~QEFIEntryView()
{
if (m_topLevelLayout != nullptr) {
m_topLevelLayout->deleteLater();
m_topLevelLayout = nullptr;
}
if (m_entries != nullptr) m_entries->deleteLater();
m_entries = nullptr;
// Seems that we have no ownership on it
if (m_buttonLayout != nullptr) m_buttonLayout->deleteLater();
if (m_moveUpEntryButton != nullptr) m_moveUpEntryButton->deleteLater();
if (m_moveDownEntryButton != nullptr) m_moveDownEntryButton->deleteLater();
if (m_setCurrentButton != nullptr) m_setCurrentButton->deleteLater();
if (m_saveButton != nullptr) m_saveButton->deleteLater();
if (m_resetButton != nullptr) m_resetButton->deleteLater();
if (m_rebootTargetButton != nullptr) m_rebootTargetButton->deleteLater();
if (m_bootTimeoutLabel != nullptr) m_bootTimeoutLabel->deleteLater();
if (m_importButton != nullptr) m_importButton->deleteLater();
if (m_addButton != nullptr) m_addButton->deleteLater();
}
void QEFIEntryView::entryChanged(int currentRow)
{
qDebug() << "[EFIEntryView] Clicked: " << currentRow;
m_selectedItemIndex = currentRow;
updateButtonState();
}
void QEFIEntryView::resetClicked(bool checked)
{
m_entries->clear();
for (const auto &i: std::as_const(m_order)) {
if (m_entryItems.contains(i)) {
QEFIEntry &entry = m_entryItems[i];
QString item = QStringLiteral("[%1] %2")
.arg(entry.id(), 4, 16, QLatin1Char('0'))
.arg(entry.name());
if (entry.devicePath().size() > 0) {
item.append('\n').append(entry.devicePath());
}
m_entries->addItem(item);
}
}
updateButtonState();
}
void QEFIEntryView::saveClicked(bool checked)
{
qDebug() << "[EFIEntryView] Save button is clicked";
// TODO: Retrieve from m_order, serialize and save them
// TODO: Check if there is any changes
QEFIEntryStaticList::instance()->setBootOrder(m_order);
}
void QEFIEntryView::setCurrentClicked(bool checked)
{
// Set BootCurrent
if (m_selectedItemIndex > 0) {
#if QT_VERSION < QT_VERSION_CHECK(5,13,0)
m_order.swap(m_selectedItemIndex, 0);
#else
m_order.swapItemsAt(m_selectedItemIndex, 0);
#endif
resetClicked(checked);
}
updateButtonState();
}
void QEFIEntryView::moveUpClicked(bool checked)
{
// Move the current up
if (m_selectedItemIndex > 0) {
#if QT_VERSION < QT_VERSION_CHECK(5,13,0)
m_order.swap(m_selectedItemIndex, m_selectedItemIndex - 1);
#else
m_order.swapItemsAt(m_selectedItemIndex, m_selectedItemIndex - 1);
#endif
resetClicked(checked);
}
updateButtonState();
}
void QEFIEntryView::moveDownClicked(bool checked)
{
// Move the current down
if (m_selectedItemIndex < m_order.size() - 1) {
#if QT_VERSION < QT_VERSION_CHECK(5,13,0)
m_order.swap(m_selectedItemIndex, m_selectedItemIndex + 1);
#else
m_order.swapItemsAt(m_selectedItemIndex, m_selectedItemIndex + 1);
#endif
resetClicked(checked);
}
updateButtonState();
}
void QEFIEntryView::updateButtonState()
{
if (m_selectedItemIndex < m_order.size() && m_selectedItemIndex >= 0) {
m_moveUpEntryButton->setDisabled(false);
m_moveDownEntryButton->setDisabled(false);
m_setCurrentButton->setDisabled(false);
m_rebootTargetButton->setDisabled(false);
m_saveButton->setDisabled(false);
m_resetButton->setDisabled(false);
if (0 == m_selectedItemIndex) {
m_moveUpEntryButton->setDisabled(true);
}
if (m_order.size() - 1 == m_selectedItemIndex) {
m_moveDownEntryButton->setDisabled(true);
}
} else {
m_moveUpEntryButton->setDisabled(true);
m_moveDownEntryButton->setDisabled(true);
m_setCurrentButton->setDisabled(true);
m_rebootTargetButton->setDisabled(true);
m_saveButton->setDisabled(false);
m_resetButton->setDisabled(false);
}
}
void QEFIEntryView::resetFromStaticListClicked(bool checked)
{
m_entryItems = QEFIEntryStaticList::instance()->entries();
m_order = QEFIEntryStaticList::instance()->order();
m_selectedItemIndex = -1;
resetClicked(checked);
}
void QEFIEntryView::rebootClicked(bool checked)
{
Q_UNUSED(checked);
if (m_selectedItemIndex >= 0 || m_selectedItemIndex < m_order.size()) {
qDebug() << "[EFIRebootView] Set " << m_order[m_selectedItemIndex] << " "
<< m_entryItems[m_order[m_selectedItemIndex]].name() << " as reboot target";
// Set BootNext
QEFIEntryStaticList::instance()->setBootNext(m_order[m_selectedItemIndex]);
int ret = QMessageBox::warning(this, tr("Reboot to ") +
m_entryItems[m_order[m_selectedItemIndex]].name(),
tr("Do you want to reboot now?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (ret == QMessageBox::Yes) {
// Reboot now
qDebug() << "[EFIRebootView] Reboot now";
QProcess process;
#ifdef Q_OS_WIN
process.startDetached("shutdown", {"/r", "/t", "0"});
#else
process.startDetached("reboot", {});
#endif
} else {
// Do nothing
qDebug() << "[EFIRebootView] Reboot later";
}
return;
}
}
void QEFIEntryView::visibilityClicked(bool checked)
{
Q_UNUSED(checked);
if (m_selectedItemIndex >= 0 || m_selectedItemIndex < m_order.size()) {
qDebug() << "[EFIRebootView] Set " << m_order[m_selectedItemIndex] << " "
<< m_entryItems[m_order[m_selectedItemIndex]].name() << " visibility";
// Set Attribute
QEFIEntry &entry = m_entryItems[m_order[m_selectedItemIndex]];
bool visibility = entry.loadOption()->isVisible();
if (QEFIEntryStaticList::instance()->setBootVisibility(
m_order[m_selectedItemIndex], !visibility)) {
// Already set visibility successful
// Set the load option visibility
QEFIEntryStaticList::instance()->entries()[m_order[m_selectedItemIndex]].
setActive(!visibility);
// Set the activation state
QListWidgetItem *currentItem = m_entries->item(m_selectedItemIndex);
if (currentItem != nullptr) {
currentItem->setForeground(visibility ? Qt::gray : Qt::black);
}
}
}
}
class DetailDialog : public QDialog
{
QEFIEntryDetailView *m_view;
QBoxLayout *m_topLevelLayout;
public:
DetailDialog(QEFIEntry &entry, QWidget *parent = nullptr)
: QDialog(parent)
{
m_view = new QEFIEntryDetailView(entry, this);
setWindowTitle(entry.name());
m_topLevelLayout = new QBoxLayout(QBoxLayout::LeftToRight, this);
m_topLevelLayout->addWidget(m_view);
}
~DetailDialog()
{
if (m_topLevelLayout) m_topLevelLayout->deleteLater();
if (m_view) m_view->deleteLater();
}
};
void QEFIEntryView::detailClicked(bool checked)
{
Q_UNUSED(checked);
if (m_selectedItemIndex >= 0 || m_selectedItemIndex < m_order.size()) {
DetailDialog dialog(m_entryItems[m_order[m_selectedItemIndex]], this);
dialog.exec();
}
}
void QEFIEntryView::exportClicked(bool checked)
{
Q_UNUSED(checked);
if (m_selectedItemIndex >= 0 || m_selectedItemIndex < m_order.size()) {
QByteArray data = QEFIEntryStaticList::instance()->
getRawData(m_order[m_selectedItemIndex]);
if (data.size() == 0) {
// Show a warning and stop
QMessageBox::warning(this, tr("Export failed"),
tr("Data to export is empty."));
return;
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QString exportFilename = QFileDialog::getSaveFileName(this, tr("Export Boot%1")
.arg(m_order[m_selectedItemIndex], 4, 16, QLatin1Char('0')));
QSaveFile exportFile(exportFilename);
exportFile.open(QIODevice::WriteOnly);
exportFile.write(data);
exportFile.commit();
#else
QFileDialog::saveFileContent(data,
QStringLiteral("Boot%1.bin").arg(m_order[m_selectedItemIndex], 4, 16, QLatin1Char('0')));
#endif
}
}
void QEFIEntryView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
if (m_selectedItemIndex >= 0 || m_selectedItemIndex < m_order.size()) {
QListWidgetItem *currentItem = m_entries->itemAt(event->pos());
if (currentItem != nullptr) {
menu.addSection(QStringLiteral("Boot%1").arg(m_order[m_selectedItemIndex],
4, 16, QLatin1Char('0')));
QEFIEntry &entry = m_entryItems[m_order[m_selectedItemIndex]];
connect(menu.addAction(entry.isActive() ?
tr("Disable") : tr("Enable")),
&QAction::triggered, this, &QEFIEntryView::visibilityClicked);
connect(menu.addAction(tr("Delete")), &QAction::triggered,
this, &QEFIEntryView::deleteClicked);
connect(menu.addAction(tr("Export")), &QAction::triggered,
this, &QEFIEntryView::exportClicked);
connect(menu.addAction(tr("Property")), &QAction::triggered,
this, &QEFIEntryView::detailClicked);
menu.addSeparator();
}
}
connect(menu.addAction(tr("Add")), &QAction::triggered,
this, &QEFIEntryView::addClicked);
connect(menu.addAction(tr("Import")), &QAction::triggered,
this, &QEFIEntryView::importClicked);
menu.exec(event->globalPos());
}
void QEFIEntryView::deleteClicked(bool checked)
{
Q_UNUSED(checked);
if (m_selectedItemIndex >= 0 || m_selectedItemIndex < m_order.size()) {
m_order.removeAt(m_selectedItemIndex);
resetClicked(checked);
}
}
void QEFIEntryView::importClicked(bool checked)
{
Q_UNUSED(checked);
QString filename = QFileDialog::getOpenFileName(this, "Open File");
if (filename.isNull()) return;
QFile fileToImport(filename);
if (!fileToImport.exists()) return;
fileToImport.open(QIODevice::ReadOnly);
QByteArray data = fileToImport.readAll();
// Parse using a QEFILoadOption
QEFILoadOption loadOption(data);
if (!loadOption.isValidated()) {
// Show a warning and stop
QMessageBox::warning(this, tr("Import failed"),
tr("Data are invalidated."));
return;
}
// Choose an ID
QInputDialog dialog(this);
dialog.setWindowTitle(tr("Choose an ID"));
dialog.setLabelText(tr("Hex value from 0 to FFFF"));
dialog.setIntRange(0, 0xFFFF);
dialog.setIntValue(0x1000);
dialog.setIntStep(1);
QSpinBox *spinbox = dialog.findChild<QSpinBox*>();
if (spinbox) spinbox->setDisplayIntegerBase(16);
if (dialog.exec() != QDialog::Accepted) {
// Show a warning and stop
QMessageBox::warning(this, tr("Import failed"),
tr("The action is cancelled."));
return;
}
bool validatedBootID = false;
quint16 bootID = spinbox->text().toInt(&validatedBootID, 16);
if (!validatedBootID) {
// Show a warning and stop
QMessageBox::warning(this, tr("Import failed"),
tr("The chosen ID is invalidated."));
return;
}
bool orderFound = m_order.contains(bootID);
if (orderFound) {
// Override: Show a confirmation
if (QMessageBox::question(this, tr("Import"),
tr("Do you want to override Boot%1?").arg(bootID, 4, 16, QLatin1Char('0'))) ==
QMessageBox::No) {
// Show a warning and stop
QMessageBox::warning(this, tr("Import failed"),
tr("The action is cancelled."));
return;
}
}
if (!QEFIEntryStaticList::instance()->updateBootEntry(bootID, data)) {
// Show a warning and stop
QMessageBox::warning(this, tr("Import failed"),
tr("Data might be invalidated."));
return;
}
// Check and update the list
if (!orderFound) m_order.append(bootID);
m_entryItems = QEFIEntryStaticList::instance()->entries();
m_selectedItemIndex = -1;
resetClicked(false);
}
class BootEntryEditorDialog : public QDialog
{
QEFILoadOptionEditorView *m_view;
QBoxLayout *m_topLevelLayout;
QDialogButtonBox *m_buttonBox;
quint16 m_bootID;
QByteArray m_loadOptionData;
public:
BootEntryEditorDialog(QWidget *parent = nullptr)
: QDialog(parent)
{
m_view = new QEFILoadOptionEditorView(nullptr, this);
setWindowTitle(tr("Add EFI Boot Entry"));
m_topLevelLayout = new QBoxLayout(QBoxLayout::Down, this);
m_topLevelLayout->addWidget(m_view);
m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok |
QDialogButtonBox::Cancel);
m_topLevelLayout->addWidget(m_buttonBox);
connect(m_buttonBox, &QDialogButtonBox::accepted,
this, &BootEntryEditorDialog::onAccept);
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
quint16 bootID() const { return m_bootID; }
QByteArray loadOptionData() const { return m_loadOptionData; }
~BootEntryEditorDialog()
{
if (m_topLevelLayout) m_topLevelLayout->deleteLater();
if (m_view) m_view->deleteLater();
if (m_buttonBox) m_buttonBox->deleteLater();
}
public slots:
void onAccept() {
// Init the class from the view
if (m_view != nullptr) {
m_bootID = m_view->getBootEntryID();
// FIXME: This method cannot be called the second time
m_loadOptionData = m_view->generateLoadOption();
qDebug() << "Will add Boot Entry " << m_bootID << m_loadOptionData;
}
accept();
}
};
void QEFIEntryView::addClicked(bool checked)
{
Q_UNUSED(checked);
BootEntryEditorDialog dialog(this);
if (dialog.exec() == QDialog::Rejected) return;
// Get Load Option and add it
quint16 bootID = dialog.bootID();
QByteArray data = dialog.loadOptionData();
if (data.size() == 0) {
// Error
QMessageBox::warning(this, tr("Add failed"),
tr("Data might be invalidated."));
}
// Parse using a QEFILoadOption
QEFILoadOption loadOption(data);
if (!loadOption.isValidated()) {
// Show a warning and stop
QMessageBox::warning(this, tr("Add failed"),
tr("Data are invalidated."));
return;
}
bool orderFound = m_order.contains(bootID);
if (orderFound) {
// Override: Show a confirmation
if (QMessageBox::question(this, tr("Add"),
tr("Do you want to override Boot%1?").arg(bootID, 4, 16, QLatin1Char('0'))) ==
QMessageBox::No) {
// Show a warning and stop
QMessageBox::warning(this, tr("Add failed"),
tr("The action is cancelled."));
return;
}
}
if (!QEFIEntryStaticList::instance()->updateBootEntry(bootID, data)) {
// Show a warning and stop
QMessageBox::warning(this, tr("Add failed"),
tr("Data might be invalidated."));
return;
}
// Check and update the list
if (!orderFound) m_order.append(bootID);
m_entryItems = QEFIEntryStaticList::instance()->entries();
m_selectedItemIndex = -1;
resetClicked(false);
}