#include #include #include #include #include #include #include "responsewaiter.h" Q_DECLARE_METATYPE(QDBusPendingReply<>) Q_DECLARE_METATYPE(QDBusPendingReply) Q_DECLARE_METATYPE(QDBusPendingReply) Q_DECLARE_METATYPE(QDBusPendingReply) Q_DECLARE_METATYPE(QDBusPendingReply) //Q_DECLARE_METATYPE(DBusResponseWaiter::onComplete) //Q_DECLARE_METATYPE(DBusResponseWaiter::onError) DBusResponseWaiter::DBusResponseWaiter() : QObject() { m_registered << qRegisterMetaType >("QDBusPendingReply<>") << qRegisterMetaType >("QDBusPendingReply") << qRegisterMetaType >("QDBusPendingReply") << qRegisterMetaType >("QDBusPendingReply") << qRegisterMetaType >("QDBusPendingReply") ; //qRegisterMetaType("DBusResponseWaiter::onComplete"); //qRegisterMetaType("DBusResponseWaiter::onError"); } QVariant DBusResponseWaiter::waitForReply(QVariant variant) const { if (QDBusPendingCall* call = const_cast(extractPendingCall(variant))) { call->waitForFinished(); QDBusMessage reply = call->reply(); if (reply.arguments().count() > 0) { return reply.arguments().first(); } } return QVariant(); } void DBusResponse::setPendingCall(QVariant variant) { qDebug() << "spc1"; m_pendingCall = variant; qDebug() << "spc2"; if (QDBusPendingCall* call = const_cast(DBusResponseWaiter().extractPendingCall(m_pendingCall))) { qDebug() << "spc3"; QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(*call); connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(onCallFinished(QDBusPendingCallWatcher*))); connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), watcher, SLOT(deleteLater())); }; qDebug() << "spc4"; } void DBusResponse::onCallFinished(QDBusPendingCallWatcher* watcher) { QVariant variant = watcher->property("pengingCall"); qDebug() << "ocf 1"; if (QDBusPendingCall* call = const_cast(DBusResponseWaiter().extractPendingCall(m_pendingCall))) { qDebug() << "ocf 2"; if (call->isError()) { qDebug() << "ocf 3"; // onError failure = watcher->property("onError").value(); // failure(call->error().message()); } else { qDebug() << "ocf 4444"; // onComplete success = watcher->property("onComplete").value(); QDeclarativeExpression *expr = new QDeclarativeExpression(e_->rootContext(), this, "console.debug(this.onError)"); qDebug() << "ocf 555"; expr->evaluate(); // result = 400 qDebug() << "ocf 666"; QDBusMessage reply = call->reply(); if (reply.arguments().count() > 0) { // success(reply.arguments().first()); } else { // success(QVariant()); } } } } const QDBusPendingCall* DBusResponseWaiter::extractPendingCall(QVariant& variant) const { Q_FOREACH(int type, m_registered) { if (variant.canConvert(QVariant::Type(type))) { return reinterpret_cast(variant.constData()); } } return 0; }