a-la-karte/src/gamecenter/gamecenterkauthhelper.cpp

134 lines
4.8 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2026 A-La-Karte Contributors
#include <KAuth/ActionReply>
#include <KAuth/HelperSupport>
#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#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 QVariant unwrapDbusVariant(QVariant v)
{
if (v.canConvert<QDBusVariant>()) {
v = v.value<QDBusVariant>().variant();
}
return v;
}
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;
}
if (bus.interface() && !bus.interface()->isServiceRegistered(kPowerProfilesService)) {
ActionReply reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(QStringLiteral("power-profiles-daemon service not available"));
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;
}
{
const QDBusReply<QVariant> profilesReply = props.call(QStringLiteral("Get"), kPowerProfilesInterface, QStringLiteral("Profiles"));
if (profilesReply.isValid()) {
QVariant v = unwrapDbusVariant(profilesReply.value());
QVariantList list;
if (v.canConvert<QDBusArgument>()) {
list = qdbus_cast<QVariantList>(v.value<QDBusArgument>());
} else if (v.canConvert<QVariantList>()) {
list = v.toList();
}
if (!list.isEmpty()) {
QStringList available;
available.reserve(list.size());
for (const QVariant &item : list) {
QVariant mV = unwrapDbusVariant(item);
QVariantMap m;
if (mV.canConvert<QDBusArgument>()) {
m = qdbus_cast<QVariantMap>(mV.value<QDBusArgument>());
} else if (mV.canConvert<QVariantMap>()) {
m = mV.toMap();
}
const QString name = m.value(QStringLiteral("Profile")).toString();
if (!name.isEmpty()) {
available.push_back(name);
}
}
if (!available.isEmpty() && !available.contains(profile)) {
ActionReply reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(QStringLiteral("unsupported profile '%1'").arg(profile));
reply.addData(QStringLiteral("available"), 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"