From 42b927bcab99c33bcf245796f897789107017823 Mon Sep 17 00:00:00 2001 From: Samoilenko Yuri Date: Wed, 29 Jan 2014 02:22:59 +0400 Subject: [PATCH] hard experiments.. --- plasmoid/declarativeplugin/CMakeLists.txt | 8 ++ .../kdeconnectdeclarativeplugin.cpp | 59 ++------- .../kdeconnectdeclarativeplugin.h | 50 ------- plasmoid/declarativeplugin/objectfactory.h | 46 +++++++ plasmoid/declarativeplugin/responsewaiter.cpp | 123 ++++++++++++++++++ plasmoid/declarativeplugin/responsewaiter.h | 70 ++++++++++ .../package/contents/ui/DeviceDelegate.qml | 23 +++- 7 files changed, 277 insertions(+), 102 deletions(-) create mode 100644 plasmoid/declarativeplugin/objectfactory.h create mode 100644 plasmoid/declarativeplugin/responsewaiter.cpp create mode 100644 plasmoid/declarativeplugin/responsewaiter.h diff --git a/plasmoid/declarativeplugin/CMakeLists.txt b/plasmoid/declarativeplugin/CMakeLists.txt index 9e5f10ab1..3b7b5d649 100644 --- a/plasmoid/declarativeplugin/CMakeLists.txt +++ b/plasmoid/declarativeplugin/CMakeLists.txt @@ -2,11 +2,19 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) + set(kdeconnectdeclarativeplugin_SRC batteryinterface.cpp kdeconnectdeclarativeplugin.cpp + responsewaiter.cpp ) +set(kdeconnectdeclarativeplugin_MOC + objectfactory.h +) + +qt4_wrap_cpp(kdeconnectdeclarativeplugin_SRC ${kdeconnectdeclarativeplugin_MOC}) + kde4_add_library(kdeconnectdeclarativeplugin SHARED ${kdeconnectdeclarativeplugin_SRC}) add_dependencies(kdeconnectdeclarativeplugin libkdeconnect) target_link_libraries(kdeconnectdeclarativeplugin diff --git a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp index 03916503a..4038c9ece 100644 --- a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp +++ b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp @@ -26,83 +26,50 @@ #include #include +#include "objectfactory.h" +#include "responsewaiter.h" + #include "libkdeconnect/devicesmodel.h" #include "libkdeconnect/notificationsmodel.h" #include "batteryinterface.h" Q_EXPORT_PLUGIN2(kdeconnectdeclarativeplugin, KdeConnectDeclarativePlugin); -// Q_DECLARE_METATYPE(QDBusPendingCall) - -Q_DECLARE_METATYPE(QDBusPendingReply<>) -Q_DECLARE_METATYPE(QDBusPendingReply) -Q_DECLARE_METATYPE(QDBusPendingReply) -Q_DECLARE_METATYPE(QDBusPendingReply) - QObject* createSftpInterface(QVariant deviceId) { return new SftpDbusInterface(deviceId.toString()); } -const QDBusPendingCall* extractPendingCall(QVariant& variant) -{ - if (variant.canConvert >()) - {} - else if (variant.canConvert >()) - {} - else if (variant.canConvert >()) - {} - else if (variant.canConvert >()) - {} - else - { - return 0; - } - - return reinterpret_cast(variant.constData()); -} +QDeclarativeEngine* engine_; -QVariant DBusResponseWaiter::waitForReply(QVariant variant) const +QObject* createDBusResponse() { - if (QDBusPendingCall* call = const_cast(extractPendingCall(variant))) - { - call->waitForFinished(); - QDBusMessage reply = call->reply(); - - if (reply.arguments().count() > 0) - { - qDebug() < >("QDBusPendingReply<>"); - qRegisterMetaType >("QDBusPendingReply"); - qRegisterMetaType >("QDBusPendingReply"); - qRegisterMetaType >("QDBusPendingReply"); - qmlRegisterType("org.kde.kdeconnect", 1, 0, "DevicesModel"); qmlRegisterType("org.kde.kdeconnect", 1, 0, "NotificationsModel"); qmlRegisterType("org.kde.kdeconnect", 1, 0, "BatteryInterface"); + + qmlRegisterType("org.kde.kdeconnect", 1, 0, "DBusResponse"); } void KdeConnectDeclarativePlugin::initializeEngine(QDeclarativeEngine* engine, const char* uri) { + engine_ = engine; QDeclarativeExtensionPlugin::initializeEngine(engine, uri); engine->rootContext()->setContextProperty("SftpDbusInterfaceFactory" , new ObjectFactory(engine, createSftpInterface)); + + engine->rootContext()->setContextProperty("DBusResponseFactory" + , new ObjectFactory(engine, createDBusResponse)); + engine->rootContext()->setContextProperty("ResponseWaiter" , new DBusResponseWaiter()); } diff --git a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h index 3589fe78a..6324b7aba 100644 --- a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h +++ b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h @@ -21,58 +21,8 @@ #ifndef ANALITZADECLARATIVEPLUGIN_H #define ANALITZADECLARATIVEPLUGIN_H -#include #include -class DBusResponseWaiter : public QObject -{ - Q_OBJECT -public: - DBusResponseWaiter() {} - - virtual ~DBusResponseWaiter(){}; - - Q_INVOKABLE QVariant waitForReply(QVariant variant) const; -}; - - -class ObjectFactory : public QObject -{ - Q_OBJECT - - typedef QObject* (*Func0)(); - typedef QObject* (*Func1)(QVariant); - typedef QObject* (*Func2)(QVariant, QVariant); - -public: - ObjectFactory(QObject* parent, Func0 f0) : QObject(parent), m_f0(f0), m_f1(0), m_f2(0) {} - ObjectFactory(QObject* parent, Func1 f1) : QObject(parent), m_f0(0), m_f1(f1), m_f2(0) {} - ObjectFactory(QObject* parent, Func2 f2) : QObject(parent), m_f0(0), m_f1(0), m_f2(f2) {} - - virtual ~ObjectFactory() {}; - - - Q_INVOKABLE QObject* create() { - if (m_f0) return m_f0(); return 0; - } - - Q_INVOKABLE QObject* create(QVariant arg1) { - if (m_f1) return m_f1(arg1); - return 0; - } - - Q_INVOKABLE QObject* create(QVariant arg1, QVariant arg2) { - if (m_f2) return m_f2(arg1, arg2); - return 0; - } - -private: - Func0 m_f0; - Func1 m_f1; - Func2 m_f2; -}; - - class KdeConnectDeclarativePlugin : public QDeclarativeExtensionPlugin { virtual void registerTypes(const char* uri); diff --git a/plasmoid/declarativeplugin/objectfactory.h b/plasmoid/declarativeplugin/objectfactory.h new file mode 100644 index 000000000..a57420393 --- /dev/null +++ b/plasmoid/declarativeplugin/objectfactory.h @@ -0,0 +1,46 @@ +#ifndef QOBJECT_FACTORY_H +#define QOBJECT_FACTORY_H + +#include +#include + +//Wraps a factory function with QObject class to be exposed to qml context as named factory + +class ObjectFactory : public QObject +{ + Q_OBJECT + + typedef QObject* (*Func0)(); + typedef QObject* (*Func1)(QVariant); + typedef QObject* (*Func2)(QVariant, QVariant); + +public: + ObjectFactory(QObject* parent, Func0 f0) : QObject(parent), m_f0(f0), m_f1(0), m_f2(0) {} + ObjectFactory(QObject* parent, Func1 f1) : QObject(parent), m_f0(0), m_f1(f1), m_f2(0) {} + ObjectFactory(QObject* parent, Func2 f2) : QObject(parent), m_f0(0), m_f1(0), m_f2(f2) {} + + virtual ~ObjectFactory() {}; + + + Q_INVOKABLE QObject* create() { + if (m_f0) return m_f0(); return 0; + } + + Q_INVOKABLE QObject* create(QVariant arg1) { + if (m_f1) return m_f1(arg1); + return 0; + } + + Q_INVOKABLE QObject* create(QVariant arg1, QVariant arg2) { + if (m_f2) return m_f2(arg1, arg2); + return 0; + } + +private: + Func0 m_f0; + Func1 m_f1; + Func2 m_f2; +}; + + +#endif diff --git a/plasmoid/declarativeplugin/responsewaiter.cpp b/plasmoid/declarativeplugin/responsewaiter.cpp new file mode 100644 index 000000000..e86d41b7c --- /dev/null +++ b/plasmoid/declarativeplugin/responsewaiter.cpp @@ -0,0 +1,123 @@ + +#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; +} + + + diff --git a/plasmoid/declarativeplugin/responsewaiter.h b/plasmoid/declarativeplugin/responsewaiter.h new file mode 100644 index 000000000..0a0af804e --- /dev/null +++ b/plasmoid/declarativeplugin/responsewaiter.h @@ -0,0 +1,70 @@ + +#ifndef RESPONSE_WAITER_H +#define RESPONSE_WAITER_H + +#include +#include +#include + +#include + +class QDBusPendingCall; +class QDBusPendingCallWatcher; + +class DBusResponse : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QVariant pendingCall READ pendingCall WRITE setPendingCall NOTIFY pendingCallChanged) + Q_PROPERTY(QVariant onError READ onError WRITE setOnError NOTIFY onErrorChanged) + Q_PROPERTY(QVariant onSuccess READ onSuccess WRITE setOnSuccess NOTIFY onSuccessChanged) + +public: + DBusResponse(QDeclarativeEngine* e = 0) : QObject(e) , e_(e) {qDebug() << "C";}; + virtual ~DBusResponse() {}; + + void setPendingCall(QVariant e); + QVariant pendingCall() {return m_pendingCall;} + + + void setOnError(QVariant e) {m_onError = e;} + QVariant onError() {return m_onError;} + + void setOnSuccess(QVariant e) {m_onSuccess = e;} + QVariant onSuccess() {return m_onSuccess;} + +Q_SIGNALS: + void onSuccessChanged(); + void onErrorChanged(); + void pendingCallChanged(); + +private Q_SLOTS: + void onCallFinished(QDBusPendingCallWatcher* watcher); + +private: + QVariant m_pendingCall; + QVariant m_onError; + QVariant m_onSuccess; + + QDeclarativeEngine* e_; +}; + +class DBusResponseWaiter : public QObject +{ + Q_OBJECT + +public: + + DBusResponseWaiter(); + + virtual ~DBusResponseWaiter(){}; + + ///extract QDbusPendingCall from \p variant and blocks untill completed + Q_INVOKABLE QVariant waitForReply(QVariant variant) const; + + const QDBusPendingCall* extractPendingCall(QVariant& variant) const; + + QList m_registered; +}; + +#endif diff --git a/plasmoid/package/contents/ui/DeviceDelegate.qml b/plasmoid/package/contents/ui/DeviceDelegate.qml index 35f39c0a7..e3b28b2be 100644 --- a/plasmoid/package/contents/ui/DeviceDelegate.qml +++ b/plasmoid/package/contents/ui/DeviceDelegate.qml @@ -30,12 +30,23 @@ PlasmaComponents.ListItem property variant sftp: null Component.onCompleted: { - sftp = SftpDbusInterfaceFactory.create(deviceId) - if (ResponseWaiter.waitForReply(sftp.isMounted())) { - browse.state = "MOUNTED" - } - - console.debug(ResponseWaiter.waitForReply(sftp.mountPoint())) + sftp = SftpDbusInterfaceFactory.create(deviceId) + + var response = DBusResponseFactory.create() + response.onSuccess = function(v) {console.log("gggggggggggggggggg", v)} + console.log("o3") + response.onError = function(v) {console.log("eeeeee")} + console.log("o4") + response.pendingCall = sftp.isMounted() + console.log("o5") + +// rr.pendingCall = 1; +// onCompleted: { +// console.debug("GGGGGGGGGGGGGGGGG") +// } +// } +// +// console.debug(ResponseWaiter.waitForReply(sftp.mountPoint())) }