File indexing completed on 2024-04-21 04:56:46

0001 /**
0002  * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef RESPONSE_WAITER_H
0008 #define RESPONSE_WAITER_H
0009 
0010 #include <QObject>
0011 #include <QTimer>
0012 #include <QVariant>
0013 
0014 class QDBusPendingCall;
0015 class QDBusPendingCallWatcher;
0016 
0017 class DBusResponseWaiter : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 public:
0022     static DBusResponseWaiter *instance();
0023 
0024     /// extract QDbusPendingCall from \p variant and blocks until completed
0025     Q_INVOKABLE QVariant waitForReply(QVariant variant) const;
0026 
0027     const QDBusPendingCall *extractPendingCall(QVariant &variant) const;
0028 
0029 private:
0030     DBusResponseWaiter();
0031 
0032     static DBusResponseWaiter *m_instance;
0033     QList<int> m_registered;
0034 };
0035 
0036 class DBusAsyncResponse : public QObject
0037 {
0038     Q_OBJECT
0039     Q_PROPERTY(bool autoDelete READ autodelete WRITE setAutodelete)
0040 
0041 public:
0042     explicit DBusAsyncResponse(QObject *parent = nullptr);
0043 
0044     Q_INVOKABLE void setPendingCall(QVariant e);
0045 
0046     void setAutodelete(bool b)
0047     {
0048         m_autodelete = b;
0049     };
0050     bool autodelete() const
0051     {
0052         return m_autodelete;
0053     }
0054 
0055 Q_SIGNALS:
0056     void success(const QVariant &result);
0057     void error(const QString &message);
0058 
0059 private Q_SLOTS:
0060     void onCallFinished(QDBusPendingCallWatcher *watcher);
0061     void onTimeout();
0062 
0063 private:
0064     QTimer m_timeout;
0065     bool m_autodelete;
0066 };
0067 
0068 #endif