Changeset View
Changeset View
Standalone View
Standalone View
kcmkwin/supportinformation/main.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2019 Kai Uwe Broulik <kde@broulik.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 3 of | ||||
7 | * the License or any later version accepted by the membership of | ||||
8 | * KDE e.V. (or its successor approved by the membership of KDE | ||||
9 | * e.V.), which shall act as a proxy defined in Section 14 of | ||||
10 | * version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "main.h" | ||||
22 | | ||||
23 | #include <QDBusConnection> | ||||
24 | #include <QDBusPendingCallWatcher> | ||||
25 | #include <QClipboard> | ||||
26 | #include <QDialogButtonBox> | ||||
27 | #include <QFileDialog> | ||||
28 | #include <QFontDatabase> | ||||
29 | #include <QGuiApplication> | ||||
30 | #include <QSaveFile> | ||||
31 | | ||||
32 | #include <KAboutData> | ||||
33 | #include <KLocalizedString> | ||||
34 | #include <KMessageWidget> | ||||
35 | #include <KPluginFactory> | ||||
36 | | ||||
37 | #include "ui_main.h" | ||||
38 | | ||||
39 | #include "kwin_interface.h" | ||||
40 | | ||||
41 | K_PLUGIN_FACTORY(KWinSupportInformationFactory, registerPlugin<KWin::KWinSupportInformation>();) | ||||
42 | | ||||
43 | using namespace KWin; | ||||
44 | | ||||
45 | KWinSupportInformation::KWinSupportInformation(QWidget *parent, const QVariantList &args) | ||||
46 | : KCModule(KAboutData::pluginData(QStringLiteral("kcm_kwinsupportinformation")), parent, args) | ||||
47 | , m_ui(new Ui::KWinSupportInformationForm) | ||||
48 | { | ||||
49 | m_ui->setupUi(this); | ||||
50 | m_ui->textBrowser->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); | ||||
51 | m_ui->textBrowser->setEnabled(false); | ||||
52 | m_ui->messageWidget->hide(); | ||||
53 | | ||||
54 | auto *kwinInterface = new org::kde::KWin(QStringLiteral("org.kde.KWin"), | ||||
55 | QStringLiteral("/KWin"), | ||||
56 | QDBusConnection::sessionBus(), | ||||
57 | this); | ||||
58 | | ||||
59 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(kwinInterface->supportInformation(), this); | ||||
60 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, watcher]() { | ||||
61 | QDBusPendingReply<QString> reply = *watcher; | ||||
62 | if (reply.isError()) { | ||||
63 | showMessage(KMessageWidget::Error, i18n("Failed to query support information from KWin: %1", reply.error().message())); | ||||
64 | } else { | ||||
65 | m_ui->textBrowser->setPlainText(reply.value()); | ||||
66 | m_ui->textBrowser->setEnabled(true); | ||||
67 | } | ||||
68 | watcher->deleteLater(); | ||||
69 | }); | ||||
70 | | ||||
71 | connect(m_ui->copyButton, &QPushButton::clicked, this, [this] { | ||||
72 | QGuiApplication::clipboard()->setText(m_ui->textBrowser->toPlainText()); | ||||
73 | m_ui->messageWidget->setMessageType(KMessageWidget::Positive); | ||||
74 | m_ui->messageWidget->setText(i18n("Support information was copied to clipboard.")); | ||||
75 | m_ui->messageWidget->animatedShow(); | ||||
76 | }); | ||||
77 | connect(m_ui->saveButton, &QPushButton::clicked, this, [this] { | ||||
78 | QFileDialog *dialog = new QFileDialog(this); | ||||
79 | dialog->setWindowTitle(i18n("Save Support Information")); | ||||
80 | dialog->setAcceptMode(QFileDialog::AcceptSave); | ||||
81 | dialog->setMimeTypeFilters({QStringLiteral("text/plain")}); | ||||
82 | dialog->setSupportedSchemes({QStringLiteral("file")}); | ||||
83 | dialog->setConfirmOverwrite(true); | ||||
84 | if (!dialog->exec()) { | ||||
85 | return; | ||||
86 | } | ||||
87 | | ||||
88 | const QUrl url = dialog->selectedUrls().value(0); | ||||
89 | if (!url.isValid()) { | ||||
90 | return; | ||||
91 | } | ||||
92 | | ||||
93 | if (!saveToFile(url.toLocalFile())) { | ||||
94 | showMessage(KMessageWidget::Error, i18n("Failed to save support information to this file.")); | ||||
95 | return; | ||||
96 | } | ||||
97 | }); | ||||
98 | } | ||||
99 | | ||||
100 | KWinSupportInformation::~KWinSupportInformation() = default; | ||||
101 | | ||||
102 | void KWinSupportInformation::showMessage(KMessageWidget::MessageType type, const QString &text) | ||||
103 | { | ||||
104 | m_ui->messageWidget->setMessageType(type); | ||||
105 | m_ui->messageWidget->setText(text); | ||||
106 | m_ui->messageWidget->animatedShow(); | ||||
107 | } | ||||
108 | | ||||
109 | bool KWinSupportInformation::saveToFile(const QString &path) | ||||
110 | { | ||||
111 | QSaveFile file(path); | ||||
112 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||||
113 | return false; | ||||
114 | } | ||||
115 | | ||||
116 | file.write(m_ui->textBrowser->toPlainText().toUtf8()); | ||||
117 | | ||||
118 | if (!file.commit()) { | ||||
119 | return false; | ||||
120 | } | ||||
121 | | ||||
122 | return true; | ||||
123 | } | ||||
124 | | ||||
125 | #include "main.moc" |