dealing with DBus from QML

This commit is contained in:
Samoilenko Yuri 2014-01-28 00:39:24 +04:00
parent a9ed55b644
commit 1d8ea764b1
6 changed files with 119 additions and 7 deletions

View file

@ -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");

View file

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

View file

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

View file

@ -21,15 +21,36 @@
#include "kdeconnectdeclarativeplugin.h"
#include <QtDeclarative/QDeclarativeItem>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>
#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<DevicesModel>("org.kde.kdeconnect", 1, 0, "DevicesModel");
qmlRegisterType<NotificationsModel>("org.kde.kdeconnect", 1, 0, "NotificationsModel");
qmlRegisterType<BatteryInterface>("org.kde.kdeconnect", 1, 0, "BatteryInterface");
//qmlRegisterUncreatableType<TestT>("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));
}

View file

@ -21,13 +21,68 @@
#ifndef ANALITZADECLARATIVEPLUGIN_H
#define ANALITZADECLARATIVEPLUGIN_H
#include <QVariant>
#include <QDeclarativeExtensionPlugin>
//FIXME HACK Force some slot to be synchronous
#include <libkdeconnect/dbusinterfaces.h>
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

View file

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