[OpenDocString] kdeconnect-kde (cpp)
responsewaiter.cpp
DBusResponseWaiter *DBusResponseWaiter::instance()
{
    if (!m_instance) {
        m_instance = new DBusResponseWaiter();
    }
    return m_instance;
}
This returns the singleton DBusResponseWaiter object. It makes it lazy - creating it if it doesn't exist.
DBusResponseWaiter::DBusResponseWaiter()
    : QObject()
{
    m_registered << qRegisterMetaType>("QDBusPendingReply<>")
                 << qRegisterMetaType>("QDBusPendingReply")
                 << qRegisterMetaType>("QDBusPendingReply") << qRegisterMetaType>("QDBusPendingReply")
                 << qRegisterMetaType>("QDBusPendingReply");
}
This constructor builds a DBus response waiter object. It registers the meta types for all pending reply objects.
QVariant DBusResponseWaiter::waitForReply(QVariant variant) const
{
    if (QDBusPendingCall *call = const_cast(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();
}
This function waits for a reply from a QDBusPendingCall object. It returns the reply argument if it exists.
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);
}
This constructor builds a DBusAsyncResponse object and sets up a single shot timeout. It sets the autodelete flag to true, sets the interval to 15000 seconds and connects the signal handler to the onTimeout signal.
void DBusAsyncResponse::setPendingCall(QVariant variant)
{
    if (QDBusPendingCall *call = const_cast(DBusResponseWaiter::instance()->extractPendingCall(variant))) {
        QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(*call);
        watcher->setProperty("pengingCallVariant", 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();
    }
}
This sets a variant of a QDBusPendingCall object and starts a signaling it for its completion. It also connects the signal to the finished signal, and the timeout timer is started.
void DBusAsyncResponse::onCallFinished(QDBusPendingCallWatcher *watcher)
{
    m_timeout.stop();
    QVariant variant = watcher->property("pengingCallVariant");

    if (QDBusPendingCall *call = const_cast(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();
    }
}
This removes the penging call from the list of pending calls, and emits a QMessage if it is successful.
void DBusAsyncResponse::onTimeout()
{
    Q_EMIT error(QStringLiteral("timeout when waiting dbus response!"));
}
This code is called when the dbus response times out.
const QDBusPendingCall *DBusResponseWaiter::extractPendingCall(QVariant &variant) const
{
    for (int type : qAsConst(m_registered)) {
        if (variant.canConvert(QVariant::Type(type))) {
            return reinterpret_cast(variant.constData());
        }
    }

    return nullptr;
}
Returns a QDBusPendingCall object, if it can handle the given variant, and returns it. If the variant cannot handle the type, it returns nullptr.