gamecenter: harden KAuth helper power profile setting

This commit is contained in:
Marco Allegretti 2026-02-14 14:08:45 +01:00
parent f3b130008f
commit d56b91dbd0

View file

@ -4,7 +4,9 @@
#include <KAuth/ActionReply>
#include <KAuth/HelperSupport>
#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusError>
#include <QDBusInterface>
#include <QDBusMessage>
@ -28,6 +30,14 @@ 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);
@ -53,6 +63,12 @@ ActionReply GameCenterHelper::setpowerprofile(const QVariantMap &args)
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();
@ -60,6 +76,44 @@ ActionReply GameCenterHelper::setpowerprofile(const QVariantMap &args)
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))});