File indexing completed on 2024-05-19 16:34:50

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include <QObject>
0012 #include <QString>
0013 #include <QVariant>
0014 
0015 namespace KWin
0016 {
0017 
0018 /**
0019  * @brief Qml export for providing a wrapper for sending a message over the DBus
0020  * session bus.
0021  *
0022  * Allows to setup the connection arguments just like in QDBusMessage and supports
0023  * adding arguments to the call. To invoke the message use the slot @ref call.
0024  *
0025  * If the call succeeds the signal @ref finished is emitted, if the call fails
0026  * the signal @ref failed is emitted.
0027  *
0028  * Note: the DBusCall always uses the session bus and performs an async call.
0029  *
0030  * Example on how to use in Qml:
0031  * @code
0032  * DBusCall {
0033  *     id: dbus
0034  *     service: "org.kde.KWin"
0035  *     path: "/KWin"
0036  *     method: "nextDesktop"
0037  *     Component.onCompleted: dbus.call()
0038  * }
0039  * @endcode
0040  *
0041  * Example with arguments:
0042  * @code
0043  * DBusCall {
0044  *     id: dbus
0045  *     service: "org.kde.KWin"
0046  *     path: "/KWin"
0047  *     method: "setCurrentDesktop"
0048  *     arguments: [1]
0049  *     Component.onCompleted: dbus.call()
0050  * }
0051  * @endcode
0052  *
0053  * Example with a callback:
0054  * @code
0055  * DBusCall {
0056  *      id: dbus
0057  *      service: "org.kde.KWin"
0058  *      path: "/KWin"
0059  *      method: "currentDesktop"
0060  *      onFinished: console.log(returnValue[0])
0061  *  }
0062  * @endcode
0063  */
0064 class DBusCall : public QObject
0065 {
0066     Q_OBJECT
0067     Q_PROPERTY(QString service READ service WRITE setService NOTIFY serviceChanged)
0068     Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
0069     Q_PROPERTY(QString dbusInterface READ interface WRITE setInterface NOTIFY interfaceChanged)
0070     Q_PROPERTY(QString method READ method WRITE setMethod NOTIFY methodChanged)
0071     Q_PROPERTY(QVariantList arguments READ arguments WRITE setArguments NOTIFY argumentsChanged)
0072 public:
0073     explicit DBusCall(QObject *parent = nullptr);
0074     ~DBusCall() override;
0075 
0076     const QString &service() const;
0077     const QString &path() const;
0078     const QString &interface() const;
0079     const QString &method() const;
0080     const QVariantList &arguments() const;
0081 
0082 public Q_SLOTS:
0083     void call();
0084 
0085     void setService(const QString &service);
0086     void setPath(const QString &path);
0087     void setInterface(const QString &interface);
0088     void setMethod(const QString &method);
0089     void setArguments(const QVariantList &arguments);
0090 
0091 Q_SIGNALS:
0092     void finished(QVariantList returnValue);
0093     void failed();
0094 
0095     void serviceChanged();
0096     void pathChanged();
0097     void interfaceChanged();
0098     void methodChanged();
0099     void argumentsChanged();
0100 
0101 private:
0102     QString m_service;
0103     QString m_path;
0104     QString m_interface;
0105     QString m_method;
0106     QVariantList m_arguments;
0107 };
0108 
0109 #define GENERIC_WRAPPER(type, name, upperName)      \
0110     inline type DBusCall::name() const              \
0111     {                                               \
0112         return m_##name;                            \
0113     }                                               \
0114     inline void DBusCall::set##upperName(type name) \
0115     {                                               \
0116         if (m_##name == name) {                     \
0117             return;                                 \
0118         }                                           \
0119         m_##name = name;                            \
0120         Q_EMIT name##Changed();                     \
0121     }
0122 #define WRAPPER(name, upperName) \
0123     GENERIC_WRAPPER(const QString &, name, upperName)
0124 
0125 WRAPPER(interface, Interface)
0126 WRAPPER(method, Method)
0127 WRAPPER(path, Path)
0128 WRAPPER(service, Service)
0129 
0130 GENERIC_WRAPPER(const QVariantList &, arguments, Arguments)
0131 #undef WRAPPER
0132 #undef GENERIC_WRAPPER
0133 
0134 } // KWin