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
|
/* SPDX-License-Identifier: GPL-2.0-only */
#include <QFileDialog>
#include <QHeaderView>
#include <QLabel>
#include <QMessageBox>
#include <QShortcut>
#include <QtGui>
#include "AboutDialog.h"
#include "Configuration.h"
#include "MainWindow.h"
#include "NvramToolCli.h"
#include "ToggleSwitch.h"
#include "ui_MainWindow.h"
static auto s_errorWindowTitle = MainWindow::tr("Error Occured");
static auto s_nvramErrorMessage = MainWindow::tr("Nvramtool was not able to access cmos settings. Look at documentation for possible causes of errors.");
QString makeNvramErrorMessage(const QString& error){
if(!error.trimmed().isEmpty()){
return QString(MainWindow::tr("%1<br><br>Error message:<br><tt>%2</tt>")).arg(s_nvramErrorMessage,
Qt::convertFromPlainText(error));
}
return s_nvramErrorMessage;
}
namespace YAML {
template <>
struct convert<QString>{
static Node encode(const QString& rhs) { return Node(rhs.toUtf8().data()); }
static bool decode(const Node& node, QString& rhs) {
if (!node.IsScalar())
return false;
rhs = QString::fromStdString(node.Scalar());
return true;
}
};
}
static auto s_metadataErrorMessage = MainWindow::tr("Can't load categories metadata file. Check your installation.");
static constexpr char s_sudoProg[] = "/usr/bin/pkexec";
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionAbout, &QAction::triggered, this, [](){
AboutDialog().exec();
});
#if MOCK
this->setWindowTitle("coreboot configurator "+tr("[MOCKED DATA]"));
#else
this->setWindowTitle("coreboot configurator");
#endif
this->setWindowIcon(QIcon::fromTheme("coreboot_configurator"));
QFile catFile(":/config/categories.yaml");
if(!catFile.open(QFile::ReadOnly)){
QMessageBox::critical(this, s_errorWindowTitle, s_metadataErrorMessage);
this->close();
return;
}
m_categories = YAML::Load(catFile.readAll());
if(m_categories.IsNull() || !m_categories.IsDefined()){
QMessageBox::critical(this, s_errorWindowTitle, s_metadataErrorMessage);
this->close();
return;
}
QShortcut* returnAction = new QShortcut(QKeySequence("Ctrl+Return"), this);
connect(returnAction, &QShortcut::activated, this, &MainWindow::on_saveButton_clicked);
generateUi();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::pullSettings()
{
QString error;
m_parameters = NvramToolCli::readParameters(&error);
if(m_parameters.isEmpty()){
QMessageBox::critical(this, s_errorWindowTitle, makeNvramErrorMessage(error));
/* we need delayed close as initialization error happened before event loop start so we can't stop application properly */
QTimer::singleShot(0, this, &MainWindow::close);
}
}
void MainWindow::pushSettings()
{
QString error;
if(!NvramToolCli::writeParameters(m_parameters, &error)){
QMessageBox::critical(this, s_errorWindowTitle, makeNvramErrorMessage(error));
}
}
QComboBox* MainWindow::createComboBox(const QString& key) {
auto box = new QComboBox(this);
auto opts = NvramToolCli::readOptions(key);
box->addItems(opts);
box->setCurrentText(m_parameters[key]);
connect(ui->advancedModeCheckBox, &QCheckBox::clicked, this, [box](bool clicked){
box->setEditable(clicked);
});
connect(this, &MainWindow::updateValue, this, [box, this, key](const QString& name){
if(key!=name || m_parameters[name]==box->currentText()){
return;
}
box->setCurrentText(m_parameters[name]);
});
connect(box, &QComboBox::currentTextChanged, this, [key, this](const QString& value){
if(value==m_parameters[key]){
return;
}
m_parameters[key] = value;
emit updateValue(key);
});
return box;
}
QString boolToString(bool value){
return value?QStringLiteral("Enable"):QStringLiteral("Disable");
}
bool stringToBool(const QString& str){
return str==QStringLiteral("Enable");
}
QCheckBox* MainWindow::createCheckBox(const QString& key) {
auto box = new ToggleSwitch(this);
box->setChecked(stringToBool(m_parameters[key]));
connect(this, &MainWindow::updateValue, this, [box, this, key](const QString& name){
if(key!=name || m_parameters[name]==boolToString(box->isChecked())){
return;
}
auto newValue = stringToBool(m_parameters[name]);
box->setChecked(newValue);
});
connect(box, &QCheckBox::clicked, this, [key, this](bool checked){
auto value = boolToString(checked);
if(value==m_parameters[key]){
return;
}
m_parameters[key] = value;
emit updateValue(key);
});
return box;
}
QTableWidget *MainWindow::createRawTable()
{
/* Create Raw values table */
auto table = new QTableWidget(m_parameters.size(), 2);
table->setHorizontalHeaderLabels({tr("Key"), tr("Value")});
table->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
table->verticalHeader()->hide();
table->setSelectionBehavior(QTableWidget::SelectRows);
connect(table, &QTableWidget::cellChanged, this, [table, this](int row, int column){
if(column != 1 || row >= table->rowCount() || row < 0 ){
/* Weird state when changed cell is not a value cell */
return;
}
auto keyItem = table->item(row, 0);
auto valueItem = table->item(row, 1);
if(keyItem == nullptr || valueItem == nullptr){
/* Invalid cells */
return;
}
if(valueItem->text()==m_parameters[keyItem->text()]){
return;
}
m_parameters[keyItem->text()] = valueItem->text();
emit updateValue(keyItem->text());
});
auto it = m_parameters.begin();
for(int i = 0; i<m_parameters.size(); i++, ++it){
auto item = new QTableWidgetItem(it.key());
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
table->setItem(i,0,item);
item = new QTableWidgetItem(it.value());
connect(this, &MainWindow::updateValue, this, [item, it, this](const QString& name){
if(it.key()!=name || m_parameters[name]==item->text()){
return;
}
item->setText(m_parameters[name]);
});
table->setItem(i,1,item);
}
return table;
}
void MainWindow::generateUi()
{
pullSettings();
if(!m_categories.IsMap()){
return;
}
for(const auto& category : m_categories){
if(!category.second.IsMap()){
continue;
}
auto name = category.second["displayName"].as<QString>();
auto layout = new QVBoxLayout;
auto tabPage = new QWidget(this);
tabPage->setLayout(layout);
ui->centralTabWidget->addTab(tabPage, name);
for(const auto& value : category.second){
if(!value.second.IsMap() || !m_parameters.contains(value.first.as<QString>())){
continue;
}
auto displayName = value.second["displayName"];
if(!displayName.IsDefined()){
continue;
}
auto type = value.second["type"];
if(!type.IsDefined()){
continue;
}
auto controlLayout = new QHBoxLayout();
auto help = value.second["help"];
if(help.IsDefined()){
auto labelWithTooltip = new QWidget;
labelWithTooltip->setToolTip(help.as<QString>());
labelWithTooltip->setCursor({Qt::WhatsThisCursor});
labelWithTooltip->setLayout(new QHBoxLayout);
auto helpButton = new QLabel();
helpButton->setPixmap(QIcon::fromTheme("help-hint").pixmap(16,16));
{
auto layout = qobject_cast<QHBoxLayout*>(labelWithTooltip->layout());
layout->addWidget(new QLabel(displayName.as<QString>()));
layout->addWidget(helpButton,1);
}
controlLayout->addWidget(labelWithTooltip, 0);
} else {
controlLayout->addWidget(new QLabel(displayName.as<QString>()), 0);
}
controlLayout->addStretch(1);
QWidget* res = nullptr;
if(type.as<QString>() == QStringLiteral("bool")){
res = createCheckBox(value.first.as<QString>());
} else if (type.as<QString>() == QStringLiteral("enum")){
res = createComboBox(value.first.as<QString>());
} else {
controlLayout->deleteLater();
continue;
}
res->setObjectName(value.first.as<QString>());
controlLayout->addWidget(res, 0);
layout->addLayout(controlLayout);
}
}
auto table = createRawTable();
connect(ui->advancedModeCheckBox, &QCheckBox::clicked, this, [table,this](bool clicked){
if(clicked && ui->centralTabWidget->widget(ui->centralTabWidget->count()-1) != table){
ui->centralTabWidget->addTab(table, tr("Raw"));
} else if(!clicked && ui->centralTabWidget->widget(ui->centralTabWidget->count()-1) == table) {
ui->centralTabWidget->removeTab(ui->centralTabWidget->count()-1);
}
});
}
void MainWindow::askForReboot()
{
QMessageBox rebootDialog(QMessageBox::Question,
tr("Reboot"),
tr("Changes are saved. Do you want to reboot to apply changes?"));
auto nowButton = rebootDialog.addButton(tr("Reboot now"), QMessageBox::AcceptRole);
rebootDialog.addButton(tr("Reboot later"), QMessageBox::RejectRole);
rebootDialog.exec();
if(rebootDialog.clickedButton()==nowButton){
QProcess::startDetached(s_sudoProg, {"/usr/bin/systemctl", "reboot"});
this->close();
}
}
void MainWindow::readSettings(const QString &fileName)
{
if(fileName.isEmpty()){
return;
}
auto configValues = Configuration::fromFile(fileName);
for(auto it = configValues.begin(); it != configValues.end(); ++it){
if(!m_parameters.contains(it.key())){
continue;
}
m_parameters[it.key()]=it.value();
emit updateValue(it.key());
}
}
void MainWindow::writeSettings(const QString &fileName)
{
if(fileName.isEmpty()){
return;
}
if(!Configuration::toFile(fileName, m_parameters)){
QMessageBox::critical(this, tr("Error Occured"), tr("Can't open file to write"));
this->close();
}
}
void MainWindow::on_actionSave_triggered()
{
auto filename = QFileDialog::getSaveFileName(this,
tr("Select File To Save"),
QDir::homePath(),
tr("Coreboot Configuration Files")+"(*.cfg)");
writeSettings(filename);
}
void MainWindow::on_actionLoad_triggered()
{
auto filename = QFileDialog::getOpenFileName(this,
tr("Select File To Load"),
QDir::homePath(),
tr("Coreboot Configuration Files")+"(*.cfg)");
readSettings(filename);
}
void MainWindow::on_saveButton_clicked()
{
ui->centralwidget->setEnabled(false);
ui->menubar->setEnabled(false);
pushSettings();
askForReboot();
ui->centralwidget->setEnabled(true);
ui->menubar->setEnabled(true);
}
|