mirror of
https://invent.kde.org/marcoa/a-la-karte.git
synced 2026-03-26 17:03:08 +00:00
gamecenter: add KAuth helper for power profiles
This commit is contained in:
parent
9dbff6fdb8
commit
e9071c2b86
4 changed files with 126 additions and 0 deletions
|
|
@ -104,6 +104,8 @@ find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS
|
||||||
WindowSystem
|
WindowSystem
|
||||||
)
|
)
|
||||||
|
|
||||||
|
find_package(KF6Auth ${KF_MIN_VERSION} QUIET)
|
||||||
|
|
||||||
find_package(KF6KirigamiAddons 1.0.0 REQUIRED)
|
find_package(KF6KirigamiAddons 1.0.0 REQUIRED)
|
||||||
|
|
||||||
qt_policy(SET QTP0001 NEW)
|
qt_policy(SET QTP0001 NEW)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,34 @@ target_link_libraries(alakarte_gamecenter PRIVATE
|
||||||
KF6::DBusAddons
|
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
|
set_target_properties(alakarte_gamecenter PROPERTIES
|
||||||
OUTPUT_NAME "alakarte-gamecenter"
|
OUTPUT_NAME "alakarte-gamecenter"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
79
src/gamecenter/gamecenterkauthhelper.cpp
Normal file
79
src/gamecenter/gamecenterkauthhelper.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
// SPDX-FileCopyrightText: 2026 A-La-Karte Contributors
|
||||||
|
|
||||||
|
#include <KAuth/ActionReply>
|
||||||
|
#include <KAuth/HelperSupport>
|
||||||
|
|
||||||
|
#include <QDBusConnection>
|
||||||
|
#include <QDBusError>
|
||||||
|
#include <QDBusInterface>
|
||||||
|
#include <QDBusMessage>
|
||||||
|
#include <QDBusReply>
|
||||||
|
#include <QDBusVariant>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QVariant>
|
||||||
|
#include <QVariantMap>
|
||||||
|
|
||||||
|
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<QDBusVariant>()) {
|
||||||
|
v = v.value<QDBusVariant>().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"
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!DOCTYPE policyconfig PUBLIC
|
||||||
|
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
|
||||||
|
"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
|
||||||
|
<policyconfig>
|
||||||
|
<vendor>A-La-Karte</vendor>
|
||||||
|
<vendor_url>https://invent.kde.org/marcoa/a-la-karte</vendor_url>
|
||||||
|
<icon_name>org.kde.alakarte</icon_name>
|
||||||
|
<action id="org.kde.alakarte.gamecenter.helper.setpowerprofile">
|
||||||
|
<description>Set system power profile</description>
|
||||||
|
<message>Authentication is required to set the system power profile.</message>
|
||||||
|
<defaults>
|
||||||
|
<allow_inactive>no</allow_inactive>
|
||||||
|
<allow_active>auth_admin_keep</allow_active>
|
||||||
|
</defaults>
|
||||||
|
</action>
|
||||||
|
</policyconfig>
|
||||||
Loading…
Reference in a new issue