-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
534 lines (486 loc) · 19.9 KB
/
mainwindow.cpp
File metadata and controls
534 lines (486 loc) · 19.9 KB
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
/**********************************************************************
* Copyright (C) 2014 MX Authors
*
* Authors: Adrian
* MX Linux <http://mxlinux.org>
*
* This file is part of MX Tools.
*
* MX Tools 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 Tools 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 Tools. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QResizeEvent>
#include <QScreen>
#include <QTextEdit>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "flatbutton.h"
#include "version.h"
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::MainWindow)
{
qDebug().noquote() << qApp->applicationName() << "version:" << VERSION;
ui->setupUi(this);
setConnections();
setWindowFlags(Qt::Window); // for the close, min and max buttons
// detect if tools are displayed in the menu (check for only one since all are set at the same time)
if (system("grep -q \"NoDisplay=true\" /home/$USER/.local/share/applications/mx-user.desktop >/dev/null 2>&1") == 0)
ui->checkHide->setChecked(true);
QString search_folder = "/usr/share/applications";
live_list = listDesktopFiles("MX-Live", search_folder);
maintenance_list = listDesktopFiles("MX-Maintenance", search_folder);
setup_list = listDesktopFiles("MX-Setup", search_folder);
software_list = listDesktopFiles("MX-Software", search_folder);
utilities_list = listDesktopFiles("MX-Utilities", search_folder);
QVector<QStringList *> lists {
&live_list,
&maintenance_list,
&setup_list,
&software_list,
&utilities_list };
QString test = getCmdOut("df -T / |tail -n1 |awk '{print $2}'");
// remove mx-remastercc and live-kernel-updater from list if not running Live
bool live = (test == "aufs" || test == "overlay");
if (!live) {
const QStringList live_list_copy = live_list;
for (const QString &item : live_list_copy)
if (item.contains("mx-remastercc.desktop") || item.contains("live-kernel-updater.desktop"))
live_list.removeOne(item);
}
for (auto &list : lists)
removeEnvExclusive(*list, live);
// remove item from list if it is only meant for XFCE
if (qgetenv("XDG_CURRENT_DESKTOP") != "XFCE")
for (auto &list : lists)
removeXfceOnly(*list);
// remove item from list if it is only meant for FLUXBOX
if (qgetenv("XDG_SESSION_DESKTOP") != "fluxbox")
for (auto &list : lists)
removeFLUXBOXonly(*list);
category_map.insert("MX-Live", live_list);
category_map.insert("MX-Maintenance", maintenance_list);
category_map.insert("MX-Setup", setup_list);
category_map.insert("MX-Software", software_list);
category_map.insert("MX-Utilities", utilities_list);
readInfo(category_map);
addButtons(info_map);
ui->textSearch->setFocus();
this->adjustSize();
QSize size = this->size();
restoreGeometry(settings.value("geometry").toByteArray());
if (this->isMaximized()) { // if started maximized give option to resize to normal window size
this->resize(size);
QRect screenGeometry = qApp->primaryScreen()->geometry();
int x = (screenGeometry.width() - this->width()) / 2;
int y = (screenGeometry.height() - this->height()) / 2;
this->move(x, y);
}
icon_size = settings.value("icon_size", icon_size).toInt();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setConnections()
{
connect(ui->pushAbout, &QPushButton::clicked, this, &MainWindow::pushAbout_clicked);
connect(ui->pushHelp, &QPushButton::clicked, this, &MainWindow::pushHelp_clicked);
connect(ui->checkHide, &QCheckBox::clicked, this, &MainWindow::checkHide_clicked);
connect(ui->textSearch, &QLineEdit::textChanged, this, &MainWindow::textSearch_textChanged);
}
QString MainWindow::getCmdOut(const QString &cmd)
{
proc = new QProcess(this);
proc->start("/bin/bash", {"-c", cmd});
proc->setReadChannel(QProcess::StandardOutput);
proc->setProcessChannelMode(QProcess::MergedChannels);
proc->waitForFinished(-1);
auto result = proc->readAllStandardOutput().trimmed();
delete proc;
return result;
}
// List .desktop files that contain a specific string
QStringList MainWindow::listDesktopFiles(const QString &search_string, const QString &location)
{
QStringList listDesktop;
QString cmd = QString("grep -Elr %1 %2 | sort").arg(search_string, location);
QString out = getCmdOut(cmd);
if (!out.isEmpty())
listDesktop = out.split("\n");
return listDesktop;
}
// Load info (name, comment, exec, icon_name, category, terminal) to the info_map
void MainWindow::readInfo(const QMultiMap<QString, QStringList> &category_map)
{
QString name;
QString comment;
QString exec;
QString icon_name;
QString terminal_switch;
QStringList list;
QLocale locale;
QString lang = locale.name().split('_').first();
QString lang_region = locale.name();
QMultiMap<QString, QStringList> map;
QRegularExpression re;
re.setPatternOptions(QRegularExpression::MultilineOption);
QMapIterator<QString, QStringList> it(category_map);
QString category;
while (it.hasNext()) {
category = it.next().key();
list = category_map.value(category);
for (const QString &file_name : qAsConst(list)) {
QFile file(file_name);
if (!file.open(QFile::Text | QFile::ReadOnly))
continue;
QString text = file.readAll();
file.close();
name.clear();
comment.clear();
if (lang != QLatin1String("en")) {
re.setPattern(QLatin1String("^Name\\[") + lang_region + QLatin1String("\\]=(.*)$"));
name = re.match(text).captured(1);
if (name.isEmpty()) { // check lang
re.setPattern(QLatin1String("^Name\\[") + lang + QLatin1String("\\]=(.*)$"));
name = re.match(text).captured(1);
}
re.setPattern(QLatin1String("^Comment\\[") + lang_region + QLatin1String("\\]=(.*)$"));
comment = re.match(text).captured(1);
if (comment.isEmpty()) { // check lang
re.setPattern(QLatin1String("^Comment\\[") + lang + QLatin1String("\\]=(.*)$"));
comment = re.match(text).captured(1);
}
}
if (lang_region == QLatin1String("pt_BR")) { // not using Portuguese [pt] for Brazilian Portuguese [pt_BR]
re.setPattern(QLatin1String("^Name\\[") + lang_region + QLatin1String("\\]=(.*)$"));
name = re.match(text).captured(1);
re.setPattern(QLatin1String("^Comment\\[") + lang_region + QLatin1String("\\]=(.*)$"));
comment = re.match(text).captured(1);
}
if (name.isEmpty()) { // backup if Name is not translated
re.setPattern(QLatin1String("^Name=(.*)$"));
name = re.match(text).captured(1);
name = name.remove(QRegularExpression(QLatin1String("^MX "))).replace(QLatin1Char('&'), QLatin1String("&&"));
}
if (comment.isEmpty()) { // backup if Comment is not translated
re.setPattern(QLatin1String("^Comment=(.*)$"));
comment = re.match(text).captured(1);
}
re.setPattern(QLatin1String("^Exec=(.*)$"));
exec = re.match(text).captured(1);
re.setPattern(QLatin1String("^Icon=(.*)$"));
icon_name = re.match(text).captured(1);
re.setPattern(QLatin1String("^Terminal=(.*)$"));
terminal_switch = re.match(text).captured(1);
QStringList info;
map.insert(file_name, info << name << comment << icon_name << exec << category << terminal_switch);
}
info_map.insert(category, map);
map.clear();
}
}
// read the info_map and add the buttons to the UI
void MainWindow::addButtons(const QMultiMap<QString, QMultiMap<QString, QStringList> > &info_map)
{
int col = 0;
int row = 0;
const int max = this->width() / 200;
max_elements = 0;
QMapIterator<QString, QMultiMap<QString, QStringList>> it(info_map);
QString category;
while (it.hasNext()) {
category = it.next().key();
if (info_map.value(category).keys().count() > max_elements)
max_elements = info_map.value(category).keys().count();
}
QString name;
QString comment;
QString exec;
QString icon_name;
QString terminal_switch;
it.toFront();
while (it.hasNext()) {
category = it.next().key();
if (!info_map.value(category).isEmpty()) {
// add empty row and delimiter except for the first row
if (row != 0) {
++row;
auto *line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
ui->gridLayout_btn->addWidget(line, row, 0, 1, -1);
}
auto *label = new QLabel();
QFont font;
font.setBold(true);
font.setUnderline(true);
label->setFont(font);
QString label_txt = category;
label_txt.remove(QRegularExpression("^MX-"));
label->setText(label_txt);
++row;
ui->gridLayout_btn->addWidget(label, row, 0);
++row;
col = 0;
QMapIterator<QString, QStringList> it(info_map.value(category));
QString file_name;
while (it.hasNext()) {
file_name = it.next().key();
if (col >= col_count)
col_count = col + 1;
QStringList file_info = info_map.value(category).value(file_name);
name = file_info.at(Info::Name);
comment = file_info.at(Info::Comment);
icon_name = file_info.at(Info::IconName);
exec = file_info.at(Info::Exec);
terminal_switch = file_info.at(Info::Terminal);
btn = new FlatButton(name);
btn->setToolTip(comment);
btn->setAutoDefault(false);
btn->setIcon(findIcon(icon_name));
btn->setIconSize(icon_size, icon_size);
ui->gridLayout_btn->addWidget(btn, row, col);
//ui->gridLayout_btn->setRowStretch(row, 0);
++col;
if (col >= max) {
col = 0;
++row;
}
QString cmd = "x-terminal-emulator -e ";
if (terminal_switch == "true")
btn->setObjectName(cmd + exec); // add the command to be executed to the object name
else
btn->setObjectName(exec); // add the command to be executed to the object name
QObject::connect(btn, &FlatButton::clicked, this, &MainWindow::btn_clicked);
}
}
}
ui->gridLayout_btn->setRowStretch(row + 2, 1);
}
QIcon MainWindow::findIcon(QString icon_name)
{
if (icon_name.isEmpty())
return QIcon();
if (QFileInfo::exists("/" + icon_name))
return QIcon(icon_name);
QString search_term = icon_name;
if (!icon_name.endsWith(".png") && !icon_name.endsWith(".svg") && !icon_name.endsWith(".xpm"))
search_term = icon_name + ".*";
icon_name.remove(QRegularExpression(R"(\.png$|\.svg$|\.xpm$)"));
// return the icon from the theme if it exists
if (QIcon::hasThemeIcon(icon_name))
return QIcon::fromTheme(icon_name);
// Try to find in most obvious places
QStringList search_paths { QDir::homePath() + "/.local/share/icons/",
"/usr/share/pixmaps/",
"/usr/local/share/icons/",
"/usr/share/icons/hicolor/48x48/apps/" };
for (const QString &path : search_paths) {
if (!QFileInfo::exists(path)) {
search_paths.removeOne(path);
continue;
}
for (const QString &ext : {".png", ".svg", ".xpm"} ) {
QString file = path + icon_name + ext;
if (QFileInfo::exists(file))
return QIcon(file);
}
}
// Search recursive
search_paths.append("/usr/share/icons/hicolor/48x48/");
search_paths.append("/usr/share/icons/hicolor/");
search_paths.append("/usr/share/icons/");
QString out = getCmdOut("find " + search_paths.join(" ") + " -iname \"" + search_term
+ "\" -print -quit 2>/dev/null");
return (!out.isEmpty()) ? QIcon(out) : QIcon();
}
void MainWindow::btn_clicked()
{
this->hide();
system(sender()->objectName().toUtf8());
this->show();
}
void MainWindow::closeEvent(QCloseEvent *)
{
settings.setValue("geometry", saveGeometry());
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
if (event->oldSize().width() == event->size().width())
return;
int new_count = this->width() / 200;
if (this->width() / 200 != col_count) {
if (new_count > max_elements && col_count == max_elements)
return;
col_count = 0;
if (ui->textSearch->text().isEmpty()) {
QLayoutItem *child = nullptr;
while ((child = ui->gridLayout_btn->takeAt(0)) != nullptr) {
delete child->widget();
delete child;
}
addButtons(info_map);
} else {
textSearch_textChanged(ui->textSearch->text());
}
}
}
// hide icons in menu checkbox
void MainWindow::checkHide_clicked(bool checked) {
for (const QStringList &list : qAsConst(category_map))
for (const QString &file_name : qAsConst(list))
hideShowIcon(file_name, checked);
system("sh -c 'which xfce4-panel >/dev/null 2>/dev/null && xfce4-panel --restart'");
}
// hide or show icon for .desktop file
void MainWindow::hideShowIcon(const QString &file_name, bool hide)
{
QFileInfo file(file_name);
QString file_name_home = QDir::homePath() + "/.local/share/applications/" + file.fileName();
if (!hide) {
QFile::remove(file_name_home);
} else {
QFile::copy(file_name, file_name_home);
QString cmd = "sed -i -r -e '/^(NoDisplay|Hidden)=/d' ";
cmd += "-e '/Exec/aNoDisplay=true' " ;
cmd += file_name_home;
system(cmd.toUtf8());
}
}
void MainWindow::pushAbout_clicked()
{
this->hide();
QMessageBox msgBox(QMessageBox::NoIcon,
tr("About MX Tools"), "<p align=\"center\"><b><h2>" +
tr("MX Tools") + "</h2></b></p><p align=\"center\">" + tr("Version: ") +
VERSION + "</p><p align=\"center\"><h3>" + tr("Configuration Tools for MX Linux") +
R"(</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>");
QPushButton *btnLicense = msgBox.addButton(tr("License"), QMessageBox::HelpRole);
QPushButton *btnChangelog = msgBox.addButton(tr("Changelog"), QMessageBox::HelpRole);
QPushButton *btnCancel = msgBox.addButton(tr("Cancel"), QMessageBox::NoRole);
btnCancel->setIcon(QIcon::fromTheme("window-close"));
msgBox.exec();
if (msgBox.clickedButton() == btnLicense) {
system("mx-viewer file:///usr/share/doc/mx-tools/license.html 'MX Tools License'");
} else if (msgBox.clickedButton() == btnChangelog) {
auto *changelog = new QDialog(this);
changelog->resize(600, 500);
auto *text = new QTextEdit;
text->setReadOnly(true);
text->setText(getCmdOut("zless /usr/share/doc/" +
QFileInfo(QCoreApplication::applicationFilePath()).fileName() + "/changelog.gz"));
auto *btnClose = new QPushButton(tr("&Close"));
btnClose->setIcon(QIcon::fromTheme("window-close"));
connect(btnClose, &QPushButton::clicked, changelog, &QDialog::close);
auto *layout = new QVBoxLayout;
layout->addWidget(text);
layout->addWidget(btnClose);
changelog->setLayout(layout);
changelog->exec();
}
this->show();
}
void MainWindow::pushHelp_clicked()
{
QString cmd;
if (QFile::exists("/usr/bin/mx-manual"))
cmd = QString("mx-manual");
else
cmd = QString("mx-viewer file:///usr/local/share/doc/mxum.html#toc-Subsection-3.2");
system(cmd.toUtf8());
}
void MainWindow::textSearch_textChanged(const QString &arg1)
{
// Remove all items from the layout
QLayoutItem *child = nullptr;
while ((child = ui->gridLayout_btn->takeAt(0)) != nullptr) {
delete child->widget();
delete child;
}
QMultiMap<QString, QMultiMap<QString, QStringList> > new_map;
QMultiMap<QString, QStringList> map;
// Create a new_map with items that match the search argument
QMapIterator<QString, QMultiMap<QString, QStringList>> it(info_map);
QString category;
while(it.hasNext()) {
category = it.next().key();
QMultiMap<QString, QStringList> file_info = info_map.value(category);
for (const QString &file_name : category_map.value(category)) {
//qDebug() << file_name;
QString name = file_info.value(file_name)[0];
QString comment = file_info.value(file_name)[1];
QString category = file_info.value(file_name)[4];
if (name.contains(arg1, Qt::CaseInsensitive) || comment.contains(arg1, Qt::CaseInsensitive)
|| category.contains(arg1, Qt::CaseInsensitive)) {
map.insert(file_name, info_map.value(category).value(file_name));
}
}
if (!map.isEmpty()) {
new_map.insert(category, map);
map.clear();
}
}
if (!new_map.isEmpty()) {
arg1.isEmpty() ? addButtons(info_map) : addButtons(new_map);
}
}
// Remove Xfce-only apps from the list
void MainWindow::removeXfceOnly(QStringList &list)
{
const QStringList list_copy = list;
for (const QString &file_name : list_copy) {
QFile file(file_name);
if (!file.open(QFile::Text | QFile::ReadOnly))
continue;
QString text = file.readAll();
file.close();
if (text.contains(QLatin1String("OnlyShowIn=XFCE")))
list.removeOne(file_name);
}
}
// Remove FLUXBOX-only apps from the list
void MainWindow::removeFLUXBOXonly(QStringList &list)
{
const QStringList list_copy = list;
for (const QString &file_name : list_copy) {
QFile file(file_name);
if (!file.open(QFile::Text | QFile::ReadOnly))
continue;
QString text = file.readAll();
file.close();
if (text.contains(QLatin1String("OnlyShowIn=FLUXBOX")))
list.removeOne(file_name);
}
}
// When running live remove programs meant only for installed environments and the other way round
void MainWindow::removeEnvExclusive(QStringList &list, bool live)
{
const QString term = live ? "MX-OnlyInstalled" : "MX-OnlyLive";
const QStringList list_copy = list;
for (const QString &file_name : list_copy) {
QFile file(file_name);
if (!file.open(QFile::Text | QFile::ReadOnly))
continue;
QString text = file.readAll();
file.close();
if (text.contains(term))
list.removeOne(file_name);
}
}