hard experiments..

This commit is contained in:
Samoilenko Yuri 2014-01-29 02:22:59 +04:00
parent 55c1221223
commit 42b927bcab
7 changed files with 277 additions and 102 deletions

View file

@ -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

View file

@ -26,83 +26,50 @@
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#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<QVariant>)
Q_DECLARE_METATYPE(QDBusPendingReply<bool>)
Q_DECLARE_METATYPE(QDBusPendingReply<QString>)
QObject* createSftpInterface(QVariant deviceId)
{
return new SftpDbusInterface(deviceId.toString());
}
const QDBusPendingCall* extractPendingCall(QVariant& variant)
{
if (variant.canConvert<QDBusPendingReply<> >())
{}
else if (variant.canConvert<QDBusPendingReply<QVariant> >())
{}
else if (variant.canConvert<QDBusPendingReply<bool> >())
{}
else if (variant.canConvert<QDBusPendingReply<QString> >())
{}
else
{
return 0;
}
QDeclarativeEngine* engine_;
return reinterpret_cast<const QDBusPendingCall*>(variant.constData());
}
QVariant DBusResponseWaiter::waitForReply(QVariant variant) const
QObject* createDBusResponse()
{
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(extractPendingCall(variant)))
{
call->waitForFinished();
QDBusMessage reply = call->reply();
if (reply.arguments().count() > 0)
{
qDebug() <<reply.arguments().first();
return reply.arguments().first();
}
else
{
return QVariant();
}
}
return QVariant();
return new DBusResponse(engine_);
}
void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
{
Q_UNUSED(uri);
qRegisterMetaType<QDBusPendingReply<> >("QDBusPendingReply<>");
qRegisterMetaType<QDBusPendingReply<QVariant> >("QDBusPendingReply<QVariant>");
qRegisterMetaType<QDBusPendingReply<bool> >("QDBusPendingReply<bool>");
qRegisterMetaType<QDBusPendingReply<QString> >("QDBusPendingReply<QString>");
qmlRegisterType<DevicesModel>("org.kde.kdeconnect", 1, 0, "DevicesModel");
qmlRegisterType<NotificationsModel>("org.kde.kdeconnect", 1, 0, "NotificationsModel");
qmlRegisterType<BatteryInterface>("org.kde.kdeconnect", 1, 0, "BatteryInterface");
qmlRegisterType<DBusResponse>("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());
}

View file

@ -21,58 +21,8 @@
#ifndef ANALITZADECLARATIVEPLUGIN_H
#define ANALITZADECLARATIVEPLUGIN_H
#include <QVariant>
#include <QDeclarativeExtensionPlugin>
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);

View file

@ -0,0 +1,46 @@
#ifndef QOBJECT_FACTORY_H
#define QOBJECT_FACTORY_H
#include <QObject>
#include <QVariant>
//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

View file

@ -0,0 +1,123 @@
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDebug>
#include <QCoreApplication>
#include <qdeclarativeexpression.h>
#include <QDeclarativeEngine>
#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>)
//Q_DECLARE_METATYPE(DBusResponseWaiter::onComplete)
//Q_DECLARE_METATYPE(DBusResponseWaiter::onError)
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<DBusResponseWaiter::onComplete>("DBusResponseWaiter::onComplete");
//qRegisterMetaType<DBusResponseWaiter::onError>("DBusResponseWaiter::onError");
}
QVariant DBusResponseWaiter::waitForReply(QVariant variant) const
{
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(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<QDBusPendingCall*>(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<QDBusPendingCall*>(DBusResponseWaiter().extractPendingCall(m_pendingCall)))
{
qDebug() << "ocf 2";
if (call->isError())
{
qDebug() << "ocf 3";
// onError failure = watcher->property("onError").value<onError>();
// failure(call->error().message());
}
else
{
qDebug() << "ocf 4444";
// onComplete success = watcher->property("onComplete").value<onComplete>();
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<const QDBusPendingCall*>(variant.constData());
}
}
return 0;
}

View file

@ -0,0 +1,70 @@
#ifndef RESPONSE_WAITER_H
#define RESPONSE_WAITER_H
#include <QObject>
#include <QVariant>
#include <QDebug>
#include <QDeclarativeEngine>
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<int> m_registered;
};
#endif

View file

@ -31,11 +31,22 @@ PlasmaComponents.ListItem
Component.onCompleted: {
sftp = SftpDbusInterfaceFactory.create(deviceId)
if (ResponseWaiter.waitForReply(sftp.isMounted())) {
browse.state = "MOUNTED"
}
console.debug(ResponseWaiter.waitForReply(sftp.mountPoint()))
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()))
}