File: responsewaiter.cpp

package info (click to toggle)
kdeconnect 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: cpp: 22,922; xml: 520; python: 92; sh: 25; makefile: 5
file content (121 lines) | stat: -rw-r--r-- 3,988 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#include "responsewaiter.h"

#include <QCoreApplication>
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDebug>

Q_DECLARE_METATYPE(QDBusPendingReply<>)
Q_DECLARE_METATYPE(QDBusPendingReply<QVariant>)
Q_DECLARE_METATYPE(QDBusPendingReply<bool>)
Q_DECLARE_METATYPE(QDBusPendingReply<int>)
Q_DECLARE_METATYPE(QDBusPendingReply<QString>)

DBusResponseWaiter *DBusResponseWaiter::m_instance = nullptr;

DBusResponseWaiter *DBusResponseWaiter::instance()
{
    if (!m_instance) {
        m_instance = new DBusResponseWaiter();
    }
    return m_instance;
}

DBusResponseWaiter::DBusResponseWaiter()
    : QObject()
{
    m_registered << qRegisterMetaType<QDBusPendingReply<>>("QDBusPendingReply<>")
                 << qRegisterMetaType<QDBusPendingReply<QVariant>>("QDBusPendingReply<QVariant>")
                 << qRegisterMetaType<QDBusPendingReply<bool>>("QDBusPendingReply<bool>") << qRegisterMetaType<QDBusPendingReply<int>>("QDBusPendingReply<int>")
                 << qRegisterMetaType<QDBusPendingReply<QString>>("QDBusPendingReply<QString>")
                 << qRegisterMetaType<QDBusPendingReply<QStringList>>("QDBusPendingReply<QStringList>");
}

QVariant DBusResponseWaiter::waitForReply(QVariant variant) const
{
    if (QDBusPendingCall *call = const_cast<QDBusPendingCall *>(extractPendingCall(variant))) {
        call->waitForFinished();

        if (call->isError()) {
            qWarning() << "error:" << call->error();
            return QVariant(QStringLiteral("error"));
        }

        QDBusMessage reply = call->reply();

        if (reply.arguments().count() > 0) {
            return reply.arguments().at(0);
        }
    }
    return QVariant();
}

DBusAsyncResponse::DBusAsyncResponse(QObject *parent)
    : QObject(parent)
    , m_autodelete(false)
{
    m_timeout.setSingleShot(true);
    m_timeout.setInterval(15000);
    connect(&m_timeout, &QTimer::timeout, this, &DBusAsyncResponse::onTimeout);
}

void DBusAsyncResponse::setPendingCall(QVariant variant)
{
    if (QDBusPendingCall *call = const_cast<QDBusPendingCall *>(DBusResponseWaiter::instance()->extractPendingCall(variant))) {
        QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(*call);
        watcher->setProperty("pendingCallVariant", variant);
        connect(watcher, &QDBusPendingCallWatcher::finished, this, &DBusAsyncResponse::onCallFinished);
        connect(watcher, &QDBusPendingCallWatcher::finished, watcher, &QObject::deleteLater);
        connect(&m_timeout, &QTimer::timeout, watcher, &QObject::deleteLater);
        m_timeout.start();
    } else {
        qWarning() << "error: extractPendingCall didn't work";
    }
}

void DBusAsyncResponse::onCallFinished(QDBusPendingCallWatcher *watcher)
{
    m_timeout.stop();
    QVariant variant = watcher->property("pendingCallVariant");

    if (QDBusPendingCall *call = const_cast<QDBusPendingCall *>(DBusResponseWaiter::instance()->extractPendingCall(variant))) {
        if (call->isError()) {
            Q_EMIT error(call->error().message());
        } else {
            QDBusMessage reply = call->reply();

            if (reply.arguments().count() > 0) {
                Q_EMIT success(reply.arguments().at(0));
            } else {
                Q_EMIT success(QVariant());
            }
        }
    }
    if (m_autodelete) {
        deleteLater();
    }
}

void DBusAsyncResponse::onTimeout()
{
    Q_EMIT error(QStringLiteral("timeout when waiting dbus response!"));
}

const QDBusPendingCall *DBusResponseWaiter::extractPendingCall(QVariant &variant) const
{
    for (int type : std::as_const(m_registered)) {
        if (variant.typeId() == type) {
            return reinterpret_cast<const QDBusPendingCall *>(variant.constData());
        }
    }

    return nullptr;
}

#include "moc_responsewaiter.cpp"