File indexing completed on 2024-02-25 17:31:02

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Red Hat Inc
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  *
0006  * SPDX-FileCopyrightText: 2016 Jan Grulich <jgrulich@redhat.com>
0007  */
0008 
0009 #include "desktopportal.h"
0010 #include "request_debug.h"
0011 
0012 #include <QDBusConnection>
0013 #include <QDBusMessage>
0014 #include <QDBusPendingCall>
0015 #include <QDBusPendingCallWatcher>
0016 #include <QDBusPendingReply>
0017 
0018 Request::Request(const QDBusObjectPath &handle, QObject *parent, const QString &portalName, const QVariant &data)
0019     : QDBusVirtualObject(parent)
0020     , m_data(data)
0021     , m_portalName(portalName)
0022 {
0023     auto sessionBus = QDBusConnection::sessionBus();
0024     if (sessionBus.registerVirtualObject(handle.path(), this, QDBusConnection::VirtualObjectRegisterOption::SubPath)) {
0025         connect(this, &Request::closeRequested, this, [this, handle]() {
0026             QDBusConnection::sessionBus().unregisterObject(handle.path());
0027             deleteLater();
0028         });
0029     } else {
0030         qCDebug(XdgRequestKdeRequest) << sessionBus.lastError().message();
0031         qCDebug(XdgRequestKdeRequest) << "Failed to register request object for" << handle.path();
0032         deleteLater();
0033     }
0034 }
0035 
0036 Request::~Request()
0037 {
0038 }
0039 
0040 bool Request::handleMessage(const QDBusMessage &message, const QDBusConnection &connection)
0041 {
0042     Q_UNUSED(connection);
0043 
0044     /* Check to make sure we're getting properties on our interface */
0045     if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
0046         return false;
0047     }
0048 
0049     qCDebug(XdgRequestKdeRequest) << message.interface();
0050     qCDebug(XdgRequestKdeRequest) << message.member();
0051     qCDebug(XdgRequestKdeRequest) << message.path();
0052 
0053     if (message.interface() == QLatin1String("org.freedesktop.impl.portal.Request")) {
0054         if (message.member() == QLatin1String("Close")) {
0055             if (m_portalName == QLatin1String("org.freedesktop.impl.portal.Inhibit")) {
0056                 QDBusMessage uninhibitMessage = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"),
0057                                                                                QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
0058                                                                                QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
0059                                                                                QStringLiteral("ReleaseInhibition"));
0060 
0061                 uninhibitMessage << m_data.toUInt();
0062 
0063                 QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(uninhibitMessage);
0064                 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
0065                 connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, message](QDBusPendingCallWatcher *watcher) {
0066                     QDBusPendingReply<> reply = *watcher;
0067                     QDBusMessage messageReply;
0068                     if (reply.isError()) {
0069                         qCDebug(XdgRequestKdeRequest) << "Uninhibit error: " << reply.error().message();
0070                         messageReply = message.createErrorReply(reply.error());
0071                     } else {
0072                         messageReply = message.createReply();
0073                         Q_EMIT closeRequested();
0074                     }
0075 
0076                     QDBusConnection::sessionBus().asyncCall(messageReply);
0077                 });
0078             } else {
0079                 Q_EMIT closeRequested();
0080                 QDBusMessage reply = message.createReply();
0081                 return connection.send(reply);
0082             }
0083         }
0084     }
0085 
0086     return true;
0087 }
0088 
0089 QString Request::introspect(const QString &path) const
0090 {
0091     QString nodes;
0092 
0093     if (path.startsWith(QLatin1String("/org/freedesktop/portal/desktop/request/"))) {
0094         nodes = QStringLiteral(
0095             "<interface name=\"org.freedesktop.impl.portal.Request\">"
0096             "    <method name=\"Close\">"
0097             "    </method>"
0098             "</interface>");
0099     }
0100 
0101     qCDebug(XdgRequestKdeRequest) << nodes;
0102 
0103     return nodes;
0104 }