From 1d8ea764b15b7183dcfeb506e8e155b5cb745697 Mon Sep 17 00:00:00 2001 From: Samoilenko Yuri Date: Tue, 28 Jan 2014 00:39:24 +0400 Subject: [PATCH] dealing with DBus from QML --- kded/daemon.cpp | 2 +- kded/plugins/sftp/sftpplugin.cpp | 11 +++- kded/plugins/sftp/sftpplugin.h | 2 +- .../kdeconnectdeclarativeplugin.cpp | 21 +++++++ .../kdeconnectdeclarativeplugin.h | 57 ++++++++++++++++++- .../package/contents/ui/DeviceDelegate.qml | 33 ++++++++++- 6 files changed, 119 insertions(+), 7 deletions(-) diff --git a/kded/daemon.cpp b/kded/daemon.cpp index c4ee42746..fd4d60352 100644 --- a/kded/daemon.cpp +++ b/kded/daemon.cpp @@ -94,7 +94,7 @@ Daemon::Daemon(QObject *parent) : QObject(parent) //Load backends (hardcoded by now, should be plugins in a future) mLinkProviders.insert(new LanLinkProvider()); - //mLinkProviders.insert(new LoopbackLinkProvider()); +// mLinkProviders.insert(new LoopbackLinkProvider()); //Read remebered paired devices const KConfigGroup& known = config->group("trusted_devices"); diff --git a/kded/plugins/sftp/sftpplugin.cpp b/kded/plugins/sftp/sftpplugin.cpp index ef7e356e7..efa5fea3b 100644 --- a/kded/plugins/sftp/sftpplugin.cpp +++ b/kded/plugins/sftp/sftpplugin.cpp @@ -113,16 +113,21 @@ void SftpPlugin::mount() connect(m_d->mounter, SIGNAL(failed(QString)), this, SLOT(onFailed(QString))); } +void SftpPlugin::umount() +{ + kDebug(kdeconnect_kded()) << "Device:" << device()->name(); + delete m_d->mounter.data(); +} + bool SftpPlugin::mountAndWait() { mount(); return m_d->mounter->wait(); } -void SftpPlugin::umount() +bool SftpPlugin::isMounted() { - kDebug(kdeconnect_kded()) << "Device:" << device()->name(); - delete m_d->mounter.data(); + return m_d->mounter; } void SftpPlugin::startBrowsing() diff --git a/kded/plugins/sftp/sftpplugin.h b/kded/plugins/sftp/sftpplugin.h index 57af4b060..abfc29991 100644 --- a/kded/plugins/sftp/sftpplugin.h +++ b/kded/plugins/sftp/sftpplugin.h @@ -54,9 +54,9 @@ public Q_SLOTS: Q_SCRIPTABLE void mount(); Q_SCRIPTABLE void umount(); Q_SCRIPTABLE bool mountAndWait(); + Q_SCRIPTABLE bool isMounted(); Q_SCRIPTABLE void startBrowsing(); - Q_SCRIPTABLE QString mountPoint(); private Q_SLOTS: diff --git a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp index bf3c54edb..b85f2cffc 100644 --- a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp +++ b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp @@ -21,15 +21,36 @@ #include "kdeconnectdeclarativeplugin.h" #include +#include +#include #include "libkdeconnect/devicesmodel.h" #include "libkdeconnect/notificationsmodel.h" #include "batteryinterface.h" +Q_EXPORT_PLUGIN2(kdeconnectdeclarativeplugin, KdeConnectDeclarativePlugin); + + + +QObject* createSftpInterface(QVariant deviceId) +{ + return new SyncSftpDbusInterface(deviceId.toString()); +} + void KdeConnectDeclarativePlugin::registerTypes(const char* uri) { Q_UNUSED(uri); qmlRegisterType("org.kde.kdeconnect", 1, 0, "DevicesModel"); qmlRegisterType("org.kde.kdeconnect", 1, 0, "NotificationsModel"); qmlRegisterType("org.kde.kdeconnect", 1, 0, "BatteryInterface"); + + //qmlRegisterUncreatableType("org.kde.kdeconnect", 1, 0, "SftpDbusInterface", "no create"); +} + +void KdeConnectDeclarativePlugin::initializeEngine(QDeclarativeEngine* engine, const char* uri) +{ + QDeclarativeExtensionPlugin::initializeEngine(engine, uri); + + engine->rootContext()->setContextProperty("SftpDbusInterfaceFactory" + , new ObjectFactory(engine, createSftpInterface)); } diff --git a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h index 900dd9d40..a6abf907b 100644 --- a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h +++ b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.h @@ -21,13 +21,68 @@ #ifndef ANALITZADECLARATIVEPLUGIN_H #define ANALITZADECLARATIVEPLUGIN_H +#include #include + +//FIXME HACK Force some slot to be synchronous +#include + +class SyncSftpDbusInterface : public SftpDbusInterface +{ + Q_OBJECT +public: + SyncSftpDbusInterface(const QString& id) : SftpDbusInterface(id) {} + ~SyncSftpDbusInterface(){} + + Q_INVOKABLE bool isMounted() { + return SftpDbusInterface::isMounted(); + } +}; + +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); + virtual void initializeEngine(QDeclarativeEngine *engine, const char *uri); }; -Q_EXPORT_PLUGIN2(kdeconnectdeclarativeplugin, KdeConnectDeclarativePlugin); + #endif // ANALITZADECLARATIVEPLUGIN_H diff --git a/plasmoid/package/contents/ui/DeviceDelegate.qml b/plasmoid/package/contents/ui/DeviceDelegate.qml index 9a29f96aa..3cb85e75a 100644 --- a/plasmoid/package/contents/ui/DeviceDelegate.qml +++ b/plasmoid/package/contents/ui/DeviceDelegate.qml @@ -27,6 +27,16 @@ PlasmaComponents.ListItem { id: root property string deviceId: model.deviceId + property variant sftp: null + + Component.onCompleted: { + sftp = SftpDbusInterfaceFactory.create(deviceId) + console.debug("hello") + //console.debug(sftp.isMounted()) + if (sftp.isMounted()) { + browse.state = "MOUNTED" + } + } Column { width: parent.width @@ -42,9 +52,30 @@ PlasmaComponents.ListItem PlasmaComponents.Button { id: browse text: "Browse" + state: "UNMOUNTED" onClicked: { - text = "Hello" + if (state == "UNMOUNTED") { + sftp.startBrowsing() + state = "MOUNTED" + console.debug(sftp.mountPoint()) + } + else { + sftp.umount() + state = "UNMOUNTED" + } } + + states: [ + State { + name: "UNMOUNTED" + PropertyChanges { target: browse; text: "Browse"} + }, + State { + name: "MOUNTED" + PropertyChanges { target: browse; text: "Unmount"} + } + ] + } height: browse.height