File indexing completed on 2024-11-17 05:15:20

0001 /*
0002  * Copyright 2022 Devin Lin <devin@kde.org>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "xdgportal.h"
0007 
0008 #include <QDBusConnection>
0009 #include <QDBusMessage>
0010 #include <QDBusObjectPath>
0011 #include <QDBusPendingCall>
0012 #include <QDBusPendingCallWatcher>
0013 #include <QDBusPendingReply>
0014 #include <QDebug>
0015 
0016 #include <KLocalizedString>
0017 
0018 void XDGPortal::requestBackground()
0019 {
0020     qDebug() << "Requesting portal to run in the background and autostart...";
0021 
0022     // create our initial request
0023     QDBusMessage autostartMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"),
0024                                                                QStringLiteral("/org/freedesktop/portal/desktop"),
0025                                                                QStringLiteral("org.freedesktop.portal.Background"),
0026                                                                QStringLiteral("RequestBackground"));
0027 
0028     // build arguments
0029     QList<QVariant> msgArgs;
0030     msgArgs << QString(); // parent_window - we leave blank since this is a daemon
0031 
0032     QStringList cmdLine = {{QStringLiteral("kclockd")}};
0033     QMap<QString, QVariant> msgOpts = {{QStringLiteral("handle_token"), m_handleToken},
0034                                        {QStringLiteral("reason"), i18n("Allow the clock process to be in the background and launched on startup.")},
0035                                        {QStringLiteral("autostart"), true},
0036                                        {QStringLiteral("dbus-activatable"), true},
0037                                        {QStringLiteral("commandline"), cmdLine}};
0038     msgArgs << msgOpts;
0039 
0040     autostartMsg.setArguments(msgArgs);
0041 
0042     QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(autostartMsg);
0043     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
0044 
0045     // listen to response
0046     connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) {
0047         QDBusPendingReply<QDBusObjectPath> reply = *watcher;
0048 
0049         if (reply.isError()) {
0050             qWarning() << "Could not get reply from org.freedesktop.portal.Background:" << reply.error().message();
0051         } else {
0052             // get response object from org.freedesktop.portal.Request
0053             QDBusConnection::sessionBus().connect(QString(),
0054                                                   reply.value().path(),
0055                                                   QLatin1String("org.freedesktop.portal.Request"),
0056                                                   QLatin1String("Response"),
0057                                                   this,
0058                                                   SLOT(requestBackgroundResponse(uint, QVariantMap)));
0059         }
0060     });
0061 }
0062 
0063 void XDGPortal::requestBackgroundResponse(uint response, const QVariantMap &results)
0064 {
0065     if (!response) {
0066         qDebug() << "Request for autostart with org.freedesktop.portal.Background results:";
0067         for (auto key : results.keys()) {
0068             qDebug() << "  " << key << ":" << results[key].toString();
0069         }
0070 
0071         // stop interaction
0072         QDBusMessage stopMsg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Request"),
0073                                                               QStringLiteral("/org/freedesktop/portal/Request"),
0074                                                               QStringLiteral("org.freedesktop.portal.Request"),
0075                                                               QStringLiteral("Close"));
0076 
0077         QDBusConnection::sessionBus().call(stopMsg);
0078     } else {
0079         qWarning() << "Failed to receive response from org.freedesktop.portal.Request.";
0080     }
0081 }