File indexing completed on 2024-05-12 05:48:10

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0003 
0004 #pragma once
0005 
0006 #include <QDBusConnection>
0007 #include <QDBusContext>
0008 #include <QDBusMessage>
0009 #include <QDBusObjectPath>
0010 #include <QDebug>
0011 #include <QMetaMethod>
0012 
0013 class KJob;
0014 
0015 class BusObject : public QObject, protected QDBusContext
0016 {
0017     Q_OBJECT
0018 public:
0019     void setParent(QObject *parent) = delete;
0020 
0021 protected:
0022     BusObject(const QString &remoteService, const QDBusObjectPath &objectPath, QObject *parent = nullptr);
0023 
0024     template<typename PointerToMemberFunction, typename... Args>
0025     void sendSignal(PointerToMemberFunction signal, Args &&...args)
0026     {
0027         auto method = QMetaMethod::fromSignal(signal);
0028         Q_ASSERT(method.isValid());
0029         auto message = QDBusMessage::createTargetedSignal(m_remoteService,
0030                                                           m_objectPath.path(),
0031                                                           QLatin1String(metaObject()->classInfo(metaObject()->indexOfClassInfo("D-Bus Interface")).value()),
0032                                                           QLatin1String(method.name()));
0033         ((message << QVariant::fromValue(args)), ...);
0034         QDBusConnection::systemBus().send(message);
0035     }
0036 
0037     bool isAuthorized();
0038     void setParent(KJob *parent);
0039     void doKill();
0040 
0041 private:
0042     const QString m_remoteService;
0043     const QDBusObjectPath m_objectPath;
0044 
0045     KJob *m_job = nullptr;
0046 };