kdeconnect-kde/plasmoid/declarativeplugin/responsewaiter.cpp

133 lines
3.6 KiB
C++
Raw Normal View History

2014-01-28 22:22:59 +00:00
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDebug>
#include <QCoreApplication>
#include "responsewaiter.h"
Q_DECLARE_METATYPE(QDBusPendingReply<>)
Q_DECLARE_METATYPE(QDBusPendingReply<QVariant>)
Q_DECLARE_METATYPE(QDBusPendingReply<bool>)
Q_DECLARE_METATYPE(QDBusPendingReply<int>)
Q_DECLARE_METATYPE(QDBusPendingReply<QString>)
2014-01-31 10:06:21 +00:00
DBusResponseWaiter* DBusResponseWaiter::m_instance = 0;
DBusResponseWaiter* DBusResponseWaiter::instance()
{
if (!m_instance)
{
m_instance = new DBusResponseWaiter();
}
return m_instance;
}
2014-01-28 22:22:59 +00:00
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>")
;
}
QVariant DBusResponseWaiter::waitForReply(QVariant variant) const
{
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(extractPendingCall(variant)))
{
call->waitForFinished();
2014-02-05 20:36:51 +00:00
if (call->isError())
{
return QVariant("error");
}
2014-01-28 22:22:59 +00:00
QDBusMessage reply = call->reply();
if (reply.arguments().count() > 0)
{
return reply.arguments().first();
}
}
return QVariant();
}
2014-02-06 15:25:22 +00:00
DBusAsyncResponse::DBusAsyncResponse(QObject* parent)
: QObject(parent)
, m_autodelete(false)
{
m_timeout.setSingleShot(true);
m_timeout.setInterval(15000);
connect(&m_timeout, SIGNAL(timeout()), this, SLOT(onTimeout()));
}
2014-01-31 10:06:21 +00:00
void DBusAsyncResponse::setPendingCall(QVariant variant)
2014-01-28 22:22:59 +00:00
{
2014-01-31 10:06:21 +00:00
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(DBusResponseWaiter::instance()->extractPendingCall(variant)))
2014-01-28 22:22:59 +00:00
{
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(*call);
2014-02-06 15:25:22 +00:00
watcher->setProperty("pengingCallVariant", variant);
2014-01-28 22:22:59 +00:00
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(onCallFinished(QDBusPendingCallWatcher*)));
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), watcher, SLOT(deleteLater()));
2014-02-06 15:25:22 +00:00
connect(&m_timeout, SIGNAL(timeout()), watcher, SLOT(deleteLater()));
m_timeout.start();
2014-01-28 22:22:59 +00:00
};
}
2014-01-31 10:06:21 +00:00
void DBusAsyncResponse::onCallFinished(QDBusPendingCallWatcher* watcher)
2014-01-28 22:22:59 +00:00
{
2014-02-06 15:25:22 +00:00
m_timeout.stop();
QVariant variant = watcher->property("pengingCallVariant");
2014-01-28 22:22:59 +00:00
2014-01-31 10:06:21 +00:00
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(DBusResponseWaiter::instance()->extractPendingCall(variant)))
2014-01-28 22:22:59 +00:00
{
if (call->isError())
{
2014-01-30 16:25:22 +00:00
Q_EMIT error(call->error().message());
2014-01-28 22:22:59 +00:00
}
else
{
QDBusMessage reply = call->reply();
if (reply.arguments().count() > 0)
{
2014-01-30 16:25:22 +00:00
Q_EMIT success(reply.arguments().first());
2014-01-28 22:22:59 +00:00
}
else
{
2014-01-30 16:25:22 +00:00
Q_EMIT success(QVariant());
2014-01-28 22:22:59 +00:00
}
}
}
2014-02-05 20:36:51 +00:00
if (m_autodelete)
{
deleteLater();
}
2014-01-28 22:22:59 +00:00
}
2014-02-06 15:25:22 +00:00
void DBusAsyncResponse::onTimeout()
{
Q_EMIT error("timeout when waiting dbus response!");
}
2014-01-28 22:22:59 +00:00
const QDBusPendingCall* DBusResponseWaiter::extractPendingCall(QVariant& variant) const
{
Q_FOREACH(int type, m_registered)
{
if (variant.canConvert(QVariant::Type(type)))
{
return reinterpret_cast<const QDBusPendingCall*>(variant.constData());
}
}
return 0;
}