File indexing completed on 2024-04-28 05:36:51

0001 /*
0002  * SPDX-FileCopyrightText: 2017 Red Hat Inc
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  *
0006  * SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
0007  */
0008 
0009 #include "inhibit.h"
0010 #include "inhibit_debug.h"
0011 
0012 #include <QDBusConnection>
0013 #include <QDBusMessage>
0014 #include <QDBusPendingCall>
0015 #include <QDBusPendingCallWatcher>
0016 #include <QDBusPendingReply>
0017 
0018 InhibitPortal::InhibitPortal(QObject *parent)
0019     : QDBusAbstractAdaptor(parent)
0020 {
0021 }
0022 
0023 void InhibitPortal::Inhibit(const QDBusObjectPath &handle, const QString &app_id, const QString &window, uint flags, const QVariantMap &options)
0024 {
0025     qCDebug(XdgDesktopPortalKdeInhibit) << "Inhibit called with parameters:";
0026     qCDebug(XdgDesktopPortalKdeInhibit) << "    handle: " << handle.path();
0027     qCDebug(XdgDesktopPortalKdeInhibit) << "    app_id: " << app_id;
0028     qCDebug(XdgDesktopPortalKdeInhibit) << "    window: " << window;
0029     qCDebug(XdgDesktopPortalKdeInhibit) << "    flags: " << flags;
0030     qCDebug(XdgDesktopPortalKdeInhibit) << "    options: " << options;
0031 
0032     QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"),
0033                                                           QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
0034                                                           QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
0035                                                           QStringLiteral("AddInhibition"));
0036     //         interrupt session (1)
0037     message << (uint)1 << app_id << options.value(QStringLiteral("reason")).toString();
0038 
0039     QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message);
0040     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
0041     connect(watcher, &QDBusPendingCallWatcher::finished, [handle, this](QDBusPendingCallWatcher *watcher) {
0042         QDBusPendingReply<uint> reply = *watcher;
0043         if (reply.isError()) {
0044             qCDebug(XdgDesktopPortalKdeInhibit) << "Inhibition error: " << reply.error().message();
0045         } else {
0046             QDBusConnection sessionBus = QDBusConnection::sessionBus();
0047             new Request(handle, this, QStringLiteral("org.freedesktop.impl.portal.Inhibit"), QVariant(reply.value()));
0048         }
0049     });
0050 }