forked from AdrianTM/mx-tools
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathabout.cpp
More file actions
131 lines (116 loc) · 4.63 KB
/
about.cpp
File metadata and controls
131 lines (116 loc) · 4.63 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
/**********************************************************************
*
**********************************************************************
* Copyright (C) 2023-2024 MX Authors
*
* Authors: Adrian <adrian@mxlinux.org>
* 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 <QDebug>
#include <QDialog>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
#include <QTextBrowser>
#include <QTextEdit>
#include <QUrl>
#include <QVBoxLayout>
namespace
{
void setupDocDialog(QDialog &dialog, QTextBrowser *browser, const QString &title, bool largeWindow)
{
dialog.setWindowTitle(title);
if (largeWindow) {
dialog.setWindowFlags(Qt::Window);
dialog.resize(1000, 800);
} else {
dialog.resize(700, 600);
}
browser->setOpenExternalLinks(true);
auto *btnClose = new QPushButton(QObject::tr("&Close"), &dialog);
btnClose->setIcon(QIcon::fromTheme("window-close"));
QObject::connect(btnClose, &QPushButton::clicked, &dialog, &QDialog::close);
auto *layout = new QVBoxLayout(&dialog);
layout->addWidget(browser);
layout->addWidget(btnClose);
}
void showHtmlDoc(const QString &url, const QString &title, bool largeWindow)
{
QDialog dialog;
auto *browser = new QTextBrowser(&dialog);
setupDocDialog(dialog, browser, title, largeWindow);
const QUrl sourceUrl = QUrl::fromUserInput(url);
const QString localPath = sourceUrl.isLocalFile() ? sourceUrl.toLocalFile() : url;
if (sourceUrl.isLocalFile() ? QFileInfo::exists(localPath) : QFileInfo::exists(url)) {
browser->setSource(sourceUrl.isLocalFile() ? sourceUrl : QUrl::fromLocalFile(url));
} else {
browser->setText(QObject::tr("Could not load %1").arg(url));
qDebug() << "Could not load HTML document" << url;
}
dialog.exec();
}
} // namespace
void displayDoc(const QString &url, const QString &title, bool largeWindow)
{
showHtmlDoc(url, title, largeWindow);
}
void displayHelpDoc(const QString &path, const QString &title)
{
showHtmlDoc(path, title, true);
}
void displayAboutMsgBox(const QString &title, const QString &message, const QString &licence_url,
const QString &license_title)
{
const auto width = 600;
const auto height = 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(licence_url, license_title);
} else if (msgBox.clickedButton() == btnChangelog) {
QDialog changelog;
changelog.setWindowTitle(QObject::tr("Changelog"));
changelog.resize(width, height);
auto *text = new QTextEdit(&changelog);
text->setReadOnly(true);
const QString changelogPath =
QStringLiteral("/usr/share/doc/") + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
+ QStringLiteral("/changelog.gz");
QProcess proc;
proc.start("zcat", {changelogPath}, QIODevice::ReadOnly);
if (!proc.waitForFinished()) {
text->setText(QObject::tr("Unable to load changelog from %1").arg(changelogPath));
} else {
text->setText(proc.readAllStandardOutput());
}
QPushButton btnClose(QObject::tr("&Close"), &changelog);
btnClose.setIcon(QIcon::fromTheme("window-close"));
QObject::connect(&btnClose, &QPushButton::clicked, &changelog, &QDialog::close);
auto *layout = new QVBoxLayout(&changelog);
layout->addWidget(text);
layout->addWidget(&btnClose);
changelog.setLayout(layout);
changelog.exec();
}
}