File 0003-kcms-memory-use-KAuth-dmidecode-helper.patch of Package kinfocenter6
From 375abcd571b991aa1ea8436fad6c7a4d30f62fa1 Mon Sep 17 00:00:00 2001
From: Kristen McWilliam <kmcwilliampublic@gmail.com>
Date: Mon, 14 Oct 2024 15:45:02 -0400
Subject: [PATCH 3/3] kcms/memory: use KAuth dmidecode helper
Refactor to use a KAuth helper to run dmidecode as
root, rather than using the CommandOutputContext.
This means the KCM no longer requires the user to
enter their password to view memory information,
and won't show an obtuse error like before if the
user cancelled the password dialog.
---
kcms/helpers/dmidecode-helper/helper.cpp | 26 ++++++++++
kcms/helpers/dmidecode-helper/helper.h | 5 ++
.../org.kde.kinfocenter.dmidecode.actions | 7 +++
kcms/memory/CMakeLists.txt | 9 +++-
kcms/memory/main.cpp | 48 ++++++++++++++++---
kcms/memory/ui/main.qml | 25 ++++++++--
6 files changed, 108 insertions(+), 12 deletions(-)
diff --git a/kcms/helpers/dmidecode-helper/helper.cpp b/kcms/helpers/dmidecode-helper/helper.cpp
index e9d068dc..11921934 100644
--- a/kcms/helpers/dmidecode-helper/helper.cpp
+++ b/kcms/helpers/dmidecode-helper/helper.cpp
@@ -26,6 +26,24 @@ DMIDecodeHelper::DMIDecodeHelper(QObject *parent)
m_dmidecodePath = QStandardPaths::findExecutable("dmidecode");
}
+KAuth::ActionReply DMIDecodeHelper::memoryinformation(const QVariantMap &args)
+{
+ Q_UNUSED(args);
+
+ KAuth::ActionReply reply;
+ auto result = executeDmidecode({QStringLiteral("--type"), QStringLiteral("17")});
+
+ if (result.failed()) {
+ qWarning() << "DMIDecodeHelper: Unable to get memory information";
+ return KAuth::ActionReply::HelperErrorReply();
+ }
+
+ const QString output = result.data().value("result").toString();
+ reply.addData("memory", output);
+
+ return reply;
+}
+
KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
{
Q_UNUSED(args);
@@ -45,6 +63,8 @@ KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
for (const auto &key : keys) {
auto result = executeDmidecode({QStringLiteral("--string"), key});
if (result.failed()) {
+ qWarning() << "DMIDecodeHelper: Unable to get system information for " << key;
+ // We don't want to fail the entire action if we can't get a single piece of information.
continue;
}
@@ -71,6 +91,12 @@ KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
reply.addData(key, output);
}
+ if (reply.data().isEmpty()) {
+ qWarning() << "DMIDecodeHelper: Unable to get system information";
+ // If we didn't get any data, we should fail the action.
+ return KAuth::ActionReply::HelperErrorReply();
+ }
+
return reply;
}
diff --git a/kcms/helpers/dmidecode-helper/helper.h b/kcms/helpers/dmidecode-helper/helper.h
index c1b588e8..805d5fa6 100644
--- a/kcms/helpers/dmidecode-helper/helper.h
+++ b/kcms/helpers/dmidecode-helper/helper.h
@@ -14,6 +14,11 @@ public:
explicit DMIDecodeHelper(QObject *parent = nullptr);
public Q_SLOTS:
+ /**
+ * Retrieves memory (RAM) information from dmidecode.
+ */
+ KAuth::ActionReply memoryinformation(const QVariantMap &args);
+
KAuth::ActionReply systeminformation(const QVariantMap &args);
private:
diff --git a/kcms/helpers/dmidecode-helper/org.kde.kinfocenter.dmidecode.actions b/kcms/helpers/dmidecode-helper/org.kde.kinfocenter.dmidecode.actions
index 66d3aa99..9f0c76f3 100644
--- a/kcms/helpers/dmidecode-helper/org.kde.kinfocenter.dmidecode.actions
+++ b/kcms/helpers/dmidecode-helper/org.kde.kinfocenter.dmidecode.actions
@@ -1,5 +1,7 @@
# SPDX-License-Identifier: CC0-1.0
# SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
+# SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
+
[Domain]
Icon=computer
URL=https://www.kde.org
@@ -47,6 +49,11 @@ Name[x-test]=xxDesktop Management Interfacexx
Name[zh_CN]=桌面管理界面 (DMI)
Name[zh_TW]=桌面管理介面
+[org.kde.kinfocenter.dmidecode.memoryinformation]
+Policy=yes
+Name=Memory Information
+Description=Read memory information from system's Desktop Management Interface (DMI)
+
[org.kde.kinfocenter.dmidecode.systeminformation]
Policy=yes
PolicyInactive=yes
diff --git a/kcms/memory/CMakeLists.txt b/kcms/memory/CMakeLists.txt
index 49219ac4..59c93b69 100644
--- a/kcms/memory/CMakeLists.txt
+++ b/kcms/memory/CMakeLists.txt
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: BSD-3-Clause
# SPDX-FileCopyrightText: 2024 Thomas Duckworth <tduck973564@gmail.com>
+# SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
find_package(dmidecode)
set_package_properties(dmidecode PROPERTIES TYPE RUNTIME)
@@ -7,4 +8,10 @@ set_package_properties(dmidecode PROPERTIES TYPE RUNTIME)
add_definitions(-DTRANSLATION_DOMAIN=\"kcm_memory\")
kinfocenter_add_kcm(kcm_memory main.cpp)
-target_link_libraries(kcm_memory PRIVATE KF6::CoreAddons KF6::KCMUtilsQuick KF6::I18n KInfoCenterInternal)
+target_link_libraries(kcm_memory PRIVATE
+ KF6::AuthCore
+ KF6::CoreAddons
+ KF6::KCMUtilsQuick
+ KF6::I18n
+ KInfoCenterInternal
+)
diff --git a/kcms/memory/main.cpp b/kcms/memory/main.cpp
index d2190140..7aab18ce 100644
--- a/kcms/memory/main.cpp
+++ b/kcms/memory/main.cpp
@@ -1,30 +1,64 @@
/*
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
SPDX-FileCopyrightText: 2024 Thomas Duckworth <tduck973564@gmail.com>
+ SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
*/
+#include <KAuth/Action>
+#include <KAuth/ExecuteJob>
+#include <KLocalizedString>
#include <KPluginFactory>
#include <KQuickConfigModule>
-#include "CommandOutputContext.h"
-
class KCMMemory : public KQuickConfigModule
{
Q_OBJECT
- Q_PROPERTY(CommandOutputContext *infoOutputContext READ outputContext CONSTANT FINAL)
+ Q_PROPERTY(QString memoryInformation READ memoryInformation NOTIFY changed)
public:
explicit KCMMemory(QObject *parent, const KPluginMetaData &data)
: KQuickConfigModule(parent, data)
{
- m_outputContext = new CommandOutputContext(QStringLiteral("pkexec"), {"dmidecode", "--type", "17"}, parent);
+ loadData();
}
- CommandOutputContext *outputContext() const
+
+ QString memoryInformation() const
{
- return m_outputContext;
+ return m_memoryInformation;
}
private:
- CommandOutputContext *m_outputContext;
+ Q_SIGNAL void changed();
+
+ /**
+ * The memory information that will be displayed in the KCM.
+ */
+ QString m_memoryInformation;
+
+ /**
+ * Load the memory information from the dmidecode helper.
+ */
+ void loadData()
+ {
+ KAuth::Action action(QStringLiteral("org.kde.kinfocenter.dmidecode.memoryinformation"));
+ action.setHelperId(QStringLiteral("org.kde.kinfocenter.dmidecode"));
+ KAuth::ExecuteJob *job = action.execute();
+
+ connect(job, &KJob::result, this, [this, job]() {
+ if (job->error()) {
+ qWarning() << "Failed to retrieve memory information: " << job->errorString();
+ return;
+ } else {
+ const auto reply = job->data();
+ if (reply.contains("memory")) {
+ m_memoryInformation = reply["memory"].toString();
+ }
+ }
+
+ Q_EMIT changed();
+ });
+
+ job->start();
+ }
};
K_PLUGIN_CLASS_WITH_JSON(KCMMemory, "kcm_memory.json")
diff --git a/kcms/memory/ui/main.qml b/kcms/memory/ui/main.qml
index 0300d619..42fdcbc7 100644
--- a/kcms/memory/ui/main.qml
+++ b/kcms/memory/ui/main.qml
@@ -1,14 +1,31 @@
/*
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
SPDX-FileCopyrightText: 2024 Thomas Duckworth <tduck973564@gmail.com>
+ SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
*/
-import QtQuick 2.5
+import QtQuick 2.15
+import org.kde.kirigami 2.20 as Kirigami
import org.kde.kcmutils as KCM
-import org.kde.kinfocenter.private 1.0 as KInfoCenter
+KCM.SimpleKCM {
+ id: kcm_memory
+ Kirigami.Theme.colorSet: Kirigami.Theme.View
+ Kirigami.Theme.inherit: false
-KInfoCenter.CommandOutputKCM {
- output: kcm.infoOutputContext
+ Kirigami.PlaceholderMessage {
+ anchors.centerIn: parent
+ width: parent.width - (Kirigami.Units.largeSpacing * 8)
+ visible: kcm.memoryInformation === ""
+ icon.name: "data-warning"
+ text: i18ndc("kinfocenter", "@info the KCM has no data to display", "No data available")
+ }
+
+ Kirigami.SelectableLabel {
+ id: text
+ text: kcm.memoryInformation
+ font.family: "monospace"
+ visible: kcm.memoryInformation !== ""
+ }
}
--
2.47.0