From e9071c2b862294ff63080a812af79810de4f7fb6 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Thu, 12 Feb 2026 14:51:20 +0100 Subject: [PATCH] gamecenter: add KAuth helper for power profiles --- CMakeLists.txt | 2 + src/gamecenter/CMakeLists.txt | 28 +++++++ src/gamecenter/gamecenterkauthhelper.cpp | 79 +++++++++++++++++++ .../org.kde.alakarte.gamecenter.helper.policy | 17 ++++ 4 files changed, 126 insertions(+) create mode 100644 src/gamecenter/gamecenterkauthhelper.cpp create mode 100644 src/gamecenter/polkit/org.kde.alakarte.gamecenter.helper.policy diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cb11b8..aefe0cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,8 @@ find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS WindowSystem ) +find_package(KF6Auth ${KF_MIN_VERSION} QUIET) + find_package(KF6KirigamiAddons 1.0.0 REQUIRED) qt_policy(SET QTP0001 NEW) diff --git a/src/gamecenter/CMakeLists.txt b/src/gamecenter/CMakeLists.txt index c95eb66..760273b 100644 --- a/src/gamecenter/CMakeLists.txt +++ b/src/gamecenter/CMakeLists.txt @@ -19,6 +19,34 @@ target_link_libraries(alakarte_gamecenter PRIVATE KF6::DBusAddons ) +if (KF6Auth_FOUND) + target_compile_definitions(alakarte_gamecenter PRIVATE + ALAKARTE_HAVE_KAUTH + ) + target_link_libraries(alakarte_gamecenter PRIVATE + KF6::AuthCore + ) + + add_executable(alakarte_gamecenter_helper + gamecenterkauthhelper.cpp + ) + target_link_libraries(alakarte_gamecenter_helper PRIVATE + Qt6::Core + Qt6::DBus + KF6::AuthCore + ) + + install(TARGETS alakarte_gamecenter_helper + DESTINATION ${KAUTH_HELPER_INSTALL_DIR} + ) + + install(FILES polkit/org.kde.alakarte.gamecenter.helper.policy + DESTINATION ${KAUTH_POLICY_FILES_INSTALL_DIR} + ) + + KAUTH_INSTALL_HELPER_FILES(alakarte_gamecenter_helper org.kde.alakarte.gamecenter.helper root) +endif() + set_target_properties(alakarte_gamecenter PROPERTIES OUTPUT_NAME "alakarte-gamecenter" ) diff --git a/src/gamecenter/gamecenterkauthhelper.cpp b/src/gamecenter/gamecenterkauthhelper.cpp new file mode 100644 index 0000000..4403704 --- /dev/null +++ b/src/gamecenter/gamecenterkauthhelper.cpp @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2026 A-La-Karte Contributors + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace KAuth; + +static const QString kPowerProfilesService = QStringLiteral("net.hadess.PowerProfiles"); +static const QString kPowerProfilesPath = QStringLiteral("/net/hadess/PowerProfiles"); +static const QString kPowerProfilesInterface = QStringLiteral("net.hadess.PowerProfiles"); + +class GameCenterHelper : public QObject +{ + Q_OBJECT + +public Q_SLOTS: + ActionReply setpowerprofile(const QVariantMap &args); +}; + +static QString unwrapStringArg(const QVariantMap &args, const QString &key) +{ + QVariant v = args.value(key); + if (v.canConvert()) { + v = v.value().variant(); + } + return v.toString(); +} + +ActionReply GameCenterHelper::setpowerprofile(const QVariantMap &args) +{ + const QString profile = unwrapStringArg(args, QStringLiteral("profile")); + if (profile.isEmpty()) { + ActionReply reply = ActionReply::HelperErrorReply(); + reply.setErrorDescription(QStringLiteral("missing profile")); + return reply; + } + + QDBusConnection bus = QDBusConnection::systemBus(); + if (!bus.isConnected()) { + ActionReply reply = ActionReply::HelperErrorReply(); + reply.setErrorDescription(QStringLiteral("system bus not connected")); + return reply; + } + + QDBusInterface props(kPowerProfilesService, kPowerProfilesPath, QStringLiteral("org.freedesktop.DBus.Properties"), bus); + if (!props.isValid()) { + ActionReply reply = ActionReply::HelperErrorReply(); + reply.setErrorDescription(QStringLiteral("power-profiles-daemon D-Bus interface not available")); + return reply; + } + + QDBusMessage msg = + QDBusMessage::createMethodCall(kPowerProfilesService, kPowerProfilesPath, QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("Set")); + msg.setArguments({kPowerProfilesInterface, QStringLiteral("ActiveProfile"), QVariant::fromValue(QDBusVariant(profile))}); + + const QDBusMessage replyMsg = bus.call(msg, QDBus::Block, 5000); + if (replyMsg.type() == QDBusMessage::ErrorMessage) { + ActionReply reply = ActionReply::HelperErrorReply(); + reply.setErrorDescription(replyMsg.errorName() + QStringLiteral(": ") + replyMsg.errorMessage()); + return reply; + } + + return ActionReply::SuccessReply(); +} + +KAUTH_HELPER_MAIN("org.kde.alakarte.gamecenter.helper", GameCenterHelper) + +#include "gamecenterkauthhelper.moc" diff --git a/src/gamecenter/polkit/org.kde.alakarte.gamecenter.helper.policy b/src/gamecenter/polkit/org.kde.alakarte.gamecenter.helper.policy new file mode 100644 index 0000000..b1102a9 --- /dev/null +++ b/src/gamecenter/polkit/org.kde.alakarte.gamecenter.helper.policy @@ -0,0 +1,17 @@ + + + + A-La-Karte + https://invent.kde.org/marcoa/a-la-karte + org.kde.alakarte + + Set system power profile + Authentication is required to set the system power profile. + + no + auth_admin_keep + + +