forked from AdrianTM/mx-live-usb-maker
-
Notifications
You must be signed in to change notification settings - Fork 8
/
about.cpp
109 lines (95 loc) · 4.11 KB
/
about.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
/**********************************************************************
*
**********************************************************************
* Copyright (C) 2023-2024 MX Authors
*
* Authors: Adrian <[email protected]>
* MX Linux <http://mxlinux.org>
*
* This 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.
*
* This program 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 this package. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include "about.h"
#include <QApplication>
#include <QFileInfo>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
#include <QStandardPaths>
#include <QTextEdit>
#include <QVBoxLayout>
#include "common.h"
#include <unistd.h>
// Display document as normal user when run as root
void displayDoc(const QString &url, const QString &title)
{
bool isRunningAsRoot = (qEnvironmentVariable("HOME") == "root");
QString originalHome = isRunningAsRoot ? starting_home : QString();
if (isRunningAsRoot) {
qputenv("HOME", originalHome.toUtf8()); // Use original home for theming purposes
}
// Prefer mx-viewer, otherwise use xdg-open (use runuser to run that as logname user)
QString viewerExecutable = QStandardPaths::findExecutable("mx-viewer");
if (!viewerExecutable.isEmpty()) {
QProcess::startDetached(viewerExecutable, {url, title});
} else {
if (getuid() != 0) {
QProcess::startDetached("xdg-open", {url});
} else {
QProcess proc;
proc.start("logname", {}, QIODevice::ReadOnly);
proc.waitForFinished();
QString user = QString::fromUtf8(proc.readAllStandardOutput()).trimmed();
QProcess::startDetached("runuser", {"-u", user, "--", "xdg-open", url});
}
}
if (isRunningAsRoot) {
qputenv("HOME", "/root");
}
}
void displayAboutMsgBox(const QString &title, const QString &message, const QString &licenceUrl,
const QString &licenseTitle)
{
const int dialogWidth = 600;
const int dialogHeight = 500;
QMessageBox msgBox(QMessageBox::NoIcon, title, message);
auto *btnLicense = msgBox.addButton(QObject::tr("License"), QMessageBox::HelpRole);
auto *btnChangelog = msgBox.addButton(QObject::tr("Changelog"), QMessageBox::HelpRole);
auto *btnCancel = msgBox.addButton(QObject::tr("Cancel"), QMessageBox::NoRole);
btnCancel->setIcon(QIcon::fromTheme("window-close"));
msgBox.exec();
if (msgBox.clickedButton() == btnLicense) {
displayDoc(licenceUrl, licenseTitle);
} else if (msgBox.clickedButton() == btnChangelog) {
auto *changelogDialog = new QDialog;
changelogDialog->setWindowTitle(QObject::tr("Changelog"));
changelogDialog->resize(dialogWidth, dialogHeight);
auto *textEdit = new QTextEdit(changelogDialog);
textEdit->setReadOnly(true);
QProcess changelogProc;
changelogProc.start(
"zless",
{"/usr/share/doc/" + QFileInfo(QCoreApplication::applicationFilePath()).fileName() + "/changelog.gz"},
QIODevice::ReadOnly);
changelogProc.waitForFinished();
textEdit->setText(changelogProc.readAllStandardOutput());
auto *btnClose = new QPushButton(QObject::tr("&Close"), changelogDialog);
btnClose->setIcon(QIcon::fromTheme("window-close"));
QObject::connect(btnClose, &QPushButton::clicked, changelogDialog, &QDialog::close);
auto *layout = new QVBoxLayout(changelogDialog);
layout->addWidget(textEdit);
layout->addWidget(btnClose);
changelogDialog->setLayout(layout);
changelogDialog->exec();
}
}