forked from MX-Linux/mx-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
488 lines (448 loc) · 19.8 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**********************************************************************
* 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 "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QResizeEvent>
#include <QScreen>
#include <QTextEdit>
#include "about.h"
#include "flatbutton.h"
#include "version.h"
MainWindow::MainWindow(QWidget *parent)
: QDialog(parent)
, ui(new Ui::MainWindow)
{
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);
const QString search_folder = QStringLiteral("/usr/share/applications");
live_list = listDesktopFiles(QStringLiteral("MX-Live"), search_folder);
maintenance_list = listDesktopFiles(QStringLiteral("MX-Maintenance"), search_folder);
setup_list = listDesktopFiles(QStringLiteral("MX-Setup"), search_folder);
software_list = listDesktopFiles(QStringLiteral("MX-Software"), search_folder);
utilities_list = listDesktopFiles(QStringLiteral("MX-Utilities"), search_folder);
QVector<QStringList *> lists {&live_list, &maintenance_list, &setup_list, &software_list, &utilities_list};
const QString partitionType = getCmdOut(QStringLiteral("df -T / |tail -n1 |awk '{print $2}'"));
// remove mx-remastercc and live-kernel-updater from list if not running Live
bool live = (partitionType == QLatin1String("aufs") || partitionType == QLatin1String("overlay"));
if (!live) {
const QStringList live_list_copy = live_list;
for (const QString &item : live_list_copy)
if (item.contains(QLatin1String("mx-remastercc.desktop"))
|| item.contains(QLatin1String("live-kernel-updater.desktop")))
live_list.removeOne(item);
}
QStringList termsToRemove;
termsToRemove << (live ? QStringLiteral("MX-OnlyInstalled") : QStringLiteral("MX-OnlyLive"));
// Since we are loading only MX apps we control this works OK, however we need to keep in mind that some
// app .desktop files have something like "OnlyShownIn=Blah;Xfce;KDE;Blah" so this login would fail in that case
QString desktop = qgetenv("XDG_CURRENT_DESKTOP");
if (desktop != "XFCE")
termsToRemove << QStringLiteral("OnlyShowIn=XFCE");
if (desktop != "Fluxbox")
termsToRemove << QStringLiteral("OnlyShowIn=FLUXBOX");
if (desktop != "KDE")
termsToRemove << QStringLiteral("OnlyShowIn=KDE");
for (auto &list : lists)
removeEnvExclusive(list, termsToRemove);
category_map.insert(QStringLiteral("MX-Live"), live_list);
category_map.insert(QStringLiteral("MX-Maintenance"), maintenance_list);
category_map.insert(QStringLiteral("MX-Setup"), setup_list);
category_map.insert(QStringLiteral("MX-Software"), software_list);
category_map.insert(QStringLiteral("MX-Utilities"), utilities_list);
readInfo(category_map);
addButtons(info_map);
ui->textSearch->setFocus();
// this->adjustSize();
QSize size = this->size();
restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray());
if (this->isMaximized()) { // if started maximized give option to resize to normal window size
this->resize(size);
QRect screenGeometry = QApplication::primaryScreen()->geometry();
int x = (screenGeometry.width() - this->width()) / 2;
int y = (screenGeometry.height() - this->height()) / 2;
this->move(x, y);
}
icon_size = settings.value(QStringLiteral("icon_size"), icon_size).toInt();
}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::setConnections()
{
connect(ui->checkHide, &QCheckBox::clicked, this, &MainWindow::checkHide_clicked);
connect(ui->pushAbout, &QPushButton::clicked, this, &MainWindow::pushAbout_clicked);
connect(ui->pushCancel, &QPushButton::clicked, this, &MainWindow::close);
connect(ui->pushHelp, &QPushButton::clicked, this, &MainWindow::pushHelp_clicked);
connect(ui->textSearch, &QLineEdit::textChanged, this, &MainWindow::textSearch_textChanged);
}
QString MainWindow::getCmdOut(const QString &cmd)
{
proc = new QProcess(this);
proc->start(QStringLiteral("/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 = QStringLiteral("grep -Elr %1 %2 | sort").arg(search_string, location);
QString out = getCmdOut(cmd);
if (!out.isEmpty())
listDesktop = out.split(QStringLiteral("\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(QStringLiteral("^Name\\[") + lang_region + QStringLiteral("\\]=(.*)$"));
name = re.match(text).captured(1).trimmed();
if (name.isEmpty()) { // check lang
re.setPattern(QStringLiteral("^Name\\[") + lang + QStringLiteral("\\]=(.*)$"));
name = re.match(text).captured(1).trimmed();
}
re.setPattern(QStringLiteral("^Comment\\[") + lang_region + QStringLiteral("\\]=(.*)$"));
comment = re.match(text).captured(1).trimmed();
if (comment.isEmpty()) { // check lang
re.setPattern(QStringLiteral("^Comment\\[") + lang + QStringLiteral("\\]=(.*)$"));
comment = re.match(text).captured(1).trimmed();
}
}
if (lang_region == QLatin1String("pt_BR")) { // not using Portuguese [pt] for Brazilian Portuguese [pt_BR]
re.setPattern(QStringLiteral("^Name\\[") + lang_region + QStringLiteral("\\]=(.*)$"));
name = re.match(text).captured(1).trimmed();
re.setPattern(QStringLiteral("^Comment\\[") + lang_region + QStringLiteral("\\]=(.*)$"));
comment = re.match(text).captured(1).trimmed();
}
if (name.isEmpty()) { // backup if Name is not translated
re.setPattern(QStringLiteral("^Name=(.*)$"));
name = re.match(text).captured(1).trimmed();
name = name.remove(QRegularExpression(QStringLiteral("^MX ")))
.replace(QLatin1Char('&'), QLatin1String("&&"));
}
if (comment.isEmpty()) { // backup if Comment is not translated
re.setPattern(QStringLiteral("^Comment=(.*)$"));
comment = re.match(text).captured(1).trimmed();
}
re.setPattern(QStringLiteral("^Exec=(.*)$"));
exec = re.match(text).captured(1).trimmed();
fixExecItem(&exec);
re.setPattern(QStringLiteral("^Icon=(.*)$"));
icon_name = re.match(text).captured(1).trimmed();
re.setPattern(QStringLiteral("^Terminal=(.*)$"));
terminal_switch = re.match(text).captured(1).trimmed();
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;
int max = 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;
// get max button size
QMapIterator<QString, QStringList> itsize(info_map.value(category));
int max_button_width = 20; // set a min != 0 to avoid div/0 in case of error
while (itsize.hasNext()) {
QString file_name = itsize.next().key();
QStringList file_info = info_map.value(category).value(file_name);
name = file_info.at(Info::Name);
max_button_width = qMax(name.size() * QApplication::font().pointSize() + icon_size, max_button_width);
}
max = this->width() / max_button_width;
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(QStringLiteral("^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 = QStringLiteral("x-terminal-emulator -e ");
if (terminal_switch == QLatin1String("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 {};
if (QFileInfo::exists("/" + icon_name))
return QIcon(icon_name);
QString search_term = icon_name;
if (!icon_name.endsWith(QLatin1String(".png")) && !icon_name.endsWith(QLatin1String(".svg"))
&& !icon_name.endsWith(QLatin1String(".xpm")))
search_term = icon_name + ".*";
icon_name.remove(QRegularExpression(QStringLiteral(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(QStringLiteral("/usr/share/icons/hicolor/48x48/"));
search_paths.append(QStringLiteral("/usr/share/icons/hicolor/"));
search_paths.append(QStringLiteral("/usr/share/icons/"));
QString out = getCmdOut("find " + search_paths.join(QStringLiteral(" ")) + " -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 * /*unused*/) { settings.setValue(QStringLiteral("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 'pgrep xfce4-panel >/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 = QStringLiteral("sed -i -r -e '/^(NoDisplay|Hidden)=/d' ");
cmd += QLatin1String("-e '/Exec/aNoDisplay=true' ");
cmd += file_name_home;
system(cmd.toUtf8());
}
}
void MainWindow::pushAbout_clicked()
{
this->hide();
displayAboutMsgBox(
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")
+ "</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>",
QStringLiteral("/usr/share/doc/mx-tools/license.html"), tr("%1 License").arg(this->windowTitle()));
this->show();
}
void MainWindow::pushHelp_clicked()
{
if (QFile::exists(QStringLiteral("/usr/bin/mx-manual")))
QProcess::startDetached(QStringLiteral("mx-manual"), {});
else // for MX19?
QProcess::startDetached(QStringLiteral("xdg-open"),
{"file:///usr/local/share/doc/mxum.html#toc-Subsection-3.2"});
}
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).at(0);
QString comment = file_info.value(file_name).at(1);
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);
}
}
// Strip %f, %F, %U, etc. if exec expects a file name since it's called without an argument from this launcher.
void MainWindow::fixExecItem(QString *item) { item->remove(QRegularExpression(QStringLiteral(R"( %[a-zA-Z])"))); }
// When running live remove programs meant only for installed environments and the other way round
// Remove XfceOnly and FluxboxOnly when not running in that environment
void MainWindow::removeEnvExclusive(QStringList *list, const QStringList &termsToRemove)
{
for (auto it = list->begin(); it != list->end();) {
QFile file(*it);
if (file.open(QFile::Text | QFile::ReadOnly)) {
QString text = file.readAll();
file.close();
if (std::none_of(termsToRemove.cbegin(), termsToRemove.cend(),
[&text](const QString &term) { return text.contains(term); }))
++it;
else
it = list->erase(it);
} else {
qWarning() << "Could not open file:" << *it;
++it;
}
}
}