File indexing completed on 2024-04-28 16:55:45

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "globalshortcuts.h"
0008 #include "notificationinhibition.h"
0009 #include "quickdialog.h"
0010 #include "session.h"
0011 #include "utils.h"
0012 #include "waylandintegration.h"
0013 
0014 #include <KGlobalAccel>
0015 #include <QAction>
0016 #include <QDBusMetaType>
0017 #include <QDataStream>
0018 #include <QDesktopServices>
0019 #include <QLoggingCategory>
0020 #include <QUrl>
0021 
0022 Q_LOGGING_CATEGORY(XdgDesktopPortalKdeGlobalShortcuts, "xdp-kde-GlobalShortcuts")
0023 
0024 GlobalShortcutsPortal::GlobalShortcutsPortal(QObject *parent)
0025     : QDBusAbstractAdaptor(parent)
0026 {
0027     qDBusRegisterMetaType<Shortcut>();
0028     qDBusRegisterMetaType<Shortcuts>();
0029 
0030 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0031     Q_ASSERT(QLatin1String(QDBusMetaType::typeToSignature(qMetaTypeId<Shortcuts>())) == QLatin1String("a(sa{sv})"));
0032 #else
0033     Q_ASSERT(QLatin1String(QDBusMetaType::typeToSignature(QMetaType(qMetaTypeId<Shortcuts>()))) == QLatin1String("a(sa{sv})"));
0034 #endif
0035 }
0036 
0037 GlobalShortcutsPortal::~GlobalShortcutsPortal() = default;
0038 
0039 uint GlobalShortcutsPortal::version() const
0040 {
0041     return 1;
0042 }
0043 
0044 uint GlobalShortcutsPortal::CreateSession(const QDBusObjectPath &handle,
0045                                           const QDBusObjectPath &session_handle,
0046                                           const QString &app_id,
0047                                           const QVariantMap &options,
0048                                           QVariantMap &results)
0049 {
0050     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "CreateSession called with parameters:";
0051     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    handle: " << handle.path();
0052     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    session_handle: " << session_handle.path();
0053     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    app_id: " << app_id;
0054     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    options: " << options;
0055 
0056     auto session = qobject_cast<GlobalShortcutsSession *>(Session::createSession(this, Session::GlobalShortcuts, app_id, session_handle.path()));
0057     if (!session) {
0058         return 2;
0059     }
0060 
0061     session->restoreActions(options["shortcuts"]);
0062     connect(session, &GlobalShortcutsSession::shortcutsChanged, this, [this, session, session_handle] {
0063         Q_EMIT ShortcutsChanged(session_handle, session->shortcutDescriptions());
0064     });
0065 
0066     connect(session, &GlobalShortcutsSession::shortcutActivated, this, [this, session](const QString &shortcutName, qlonglong timestamp) {
0067         Q_EMIT Activated(QDBusObjectPath(session->handle()), shortcutName, timestamp);
0068     });
0069     connect(session, &GlobalShortcutsSession::shortcutDeactivated, this, [this, session](const QString &shortcutName, qlonglong timestamp) {
0070         Q_EMIT Deactivated(QDBusObjectPath(session->handle()), shortcutName, timestamp);
0071     });
0072 
0073     results = {
0074         {"shortcuts", session->shortcutDescriptionsVariant()},
0075     };
0076     return 0;
0077 }
0078 
0079 uint GlobalShortcutsPortal::ListShortcuts(const QDBusObjectPath &handle, const QDBusObjectPath &session_handle, QVariantMap &results)
0080 {
0081     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "ListShortcuts called with parameters:";
0082     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    handle: " << handle.path();
0083     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    session_handle: " << session_handle.path();
0084 
0085     auto session = qobject_cast<GlobalShortcutsSession *>(Session::getSession(session_handle.path()));
0086     if (!session) {
0087         return 2;
0088     }
0089     results = {
0090         {"shortcuts", session->shortcutDescriptionsVariant()},
0091     };
0092     return 0;
0093 }
0094 
0095 uint GlobalShortcutsPortal::BindShortcuts(const QDBusObjectPath &handle,
0096                                           const QDBusObjectPath &session_handle,
0097                                           const Shortcuts &shortcuts,
0098                                           const QString &parent_window,
0099                                           const QVariantMap &options,
0100                                           QVariantMap &results)
0101 {
0102     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "BindShortcuts called with parameters:";
0103     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    handle: " << handle.path();
0104     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    session_handle: " << session_handle.path();
0105     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    shortcuts: " << shortcuts;
0106     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    parent_window: " << parent_window;
0107     qCDebug(XdgDesktopPortalKdeGlobalShortcuts) << "    options: " << options;
0108 
0109     auto session = qobject_cast<GlobalShortcutsSession *>(Session::getSession(session_handle.path()));
0110     if (!session) {
0111         return 2;
0112     }
0113     QDesktopServices::openUrl(QUrl(QStringLiteral("systemsettings://kcm_keys/") + session->componentName()));
0114 
0115     results = {
0116         {"shortcuts", session->shortcutDescriptionsVariant()},
0117     };
0118     return 0;
0119 }