button works

This commit is contained in:
Samoilenko Yuri 2014-01-31 14:06:21 +04:00
parent 881450146f
commit 776543409d
4 changed files with 60 additions and 51 deletions

View file

@ -43,7 +43,7 @@ QObject* createSftpInterface(QVariant deviceId)
QObject* createDBusResponse()
{
return new DBusResponse();
return new DBusAsyncResponse();
}
void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
@ -54,7 +54,7 @@ void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
qmlRegisterType<NotificationsModel>("org.kde.kdeconnect", 1, 0, "NotificationsModel");
qmlRegisterType<BatteryInterface>("org.kde.kdeconnect", 1, 0, "BatteryInterface");
qmlRegisterType<DBusResponse>("org.kde.kdeconnect", 1, 0, "DBusResponse");
qmlRegisterType<DBusAsyncResponse>("org.kde.kdeconnect", 1, 0, "DBusResponse");
}
void KdeConnectDeclarativePlugin::initializeEngine(QDeclarativeEngine* engine, const char* uri)
@ -68,6 +68,6 @@ void KdeConnectDeclarativePlugin::initializeEngine(QDeclarativeEngine* engine, c
engine->rootContext()->setContextProperty("DBusResponseFactory"
, new ObjectFactory(engine, createDBusResponse));
engine->rootContext()->setContextProperty("ResponseWaiter"
, new DBusResponseWaiter());
engine->rootContext()->setContextProperty("DBusResponseWaiter"
, DBusResponseWaiter::instance());
}

View file

@ -15,6 +15,17 @@ Q_DECLARE_METATYPE(QDBusPendingReply<bool>)
Q_DECLARE_METATYPE(QDBusPendingReply<int>)
Q_DECLARE_METATYPE(QDBusPendingReply<QString>)
DBusResponseWaiter* DBusResponseWaiter::m_instance = 0;
DBusResponseWaiter* DBusResponseWaiter::instance()
{
if (!m_instance)
{
m_instance = new DBusResponseWaiter();
}
return m_instance;
}
DBusResponseWaiter::DBusResponseWaiter()
: QObject()
{
@ -42,9 +53,9 @@ QVariant DBusResponseWaiter::waitForReply(QVariant variant) const
return QVariant();
}
void DBusResponse::setPendingCall(QVariant variant)
void DBusAsyncResponse::setPendingCall(QVariant variant)
{
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(DBusResponseWaiter().extractPendingCall(variant)))
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(DBusResponseWaiter::instance()->extractPendingCall(variant)))
{
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(*call);
watcher->setProperty("pengingCall", variant);
@ -54,11 +65,11 @@ void DBusResponse::setPendingCall(QVariant variant)
}
void DBusResponse::onCallFinished(QDBusPendingCallWatcher* watcher)
void DBusAsyncResponse::onCallFinished(QDBusPendingCallWatcher* watcher)
{
QVariant variant = watcher->property("pengingCall");
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(DBusResponseWaiter().extractPendingCall(variant)))
if (QDBusPendingCall* call = const_cast<QDBusPendingCall*>(DBusResponseWaiter::instance()->extractPendingCall(variant)))
{
if (call->isError())
{

View file

@ -11,15 +11,36 @@
class QDBusPendingCall;
class QDBusPendingCallWatcher;
class DBusResponse : public QObject
class DBusResponseWaiter : public QObject
{
Q_OBJECT
public:
static DBusResponseWaiter* instance();
///extract QDbusPendingCall from \p variant and blocks untill completed
Q_INVOKABLE QVariant waitForReply(QVariant variant) const;
const QDBusPendingCall* extractPendingCall(QVariant& variant) const;
private:
DBusResponseWaiter();
static DBusResponseWaiter* m_instance;
QList<int> m_registered;
};
class DBusAsyncResponse : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariant pendingCall WRITE setPendingCall)
public:
DBusResponse(QObject* parent = 0) : QObject(parent) {}
virtual ~DBusResponse() {};
DBusAsyncResponse(QObject* parent = 0) : QObject(parent) {}
virtual ~DBusAsyncResponse() {};
void setPendingCall(QVariant e);
@ -31,22 +52,5 @@ private Q_SLOTS:
void onCallFinished(QDBusPendingCallWatcher* watcher);
};
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,13 +31,23 @@ PlasmaComponents.ListItem
Component.onCompleted: {
sftp.mounted.connect( function() {
browse.state = "MOUNTED"
})
sftp.unmounted.connect( function() {
console.log(222)
browse.state = "UNMOUNTED"
})
var response = DBusResponseFactory.create()
response.success.connect( function(result) {
if (result) {
state = "MOUNTED"
browse.state = "MOUNTED"
}
else {
state = "UNMOUNTED"
browse.state = "UNMOUNTED"
}
})
@ -62,47 +72,31 @@ PlasmaComponents.ListItem
PlasmaComponents.Button {
id: browse
text: "Browse"
checkable: true
state: "UNMOUNTED"
onClicked: {
if (state == "UNMOUNTED") {
state = "MOUNTING"
var response = DBusResponseFactory.create()
response.success.connect( function(result){
if (result) {
state = "MOUNTED"
}
else {
state = "UNMOUNTED"
}
})
response.error.connect( function(message) {
console.error("Error:" + message)
state = "UNMOUNTED"
})
response.pendingCall = sftp.startBrowsing()
sftp.startBrowsing()
}
else {
sftp.umount()
state = "UNMOUNTED"
}
}
states: [
State {
name: "UNMOUNTED"
PropertyChanges { target: browse; text: "Browse"}
PropertyChanges { target: browse; checked: false; text: "Browse"}
},
State {
name: "MOUNTING"
PropertyChanges { target: browse; text: "Mounting..."}
PropertyChanges { target: browse; checked: true; text: "Mounting..."}
},
State {
name: "MOUNTED"
PropertyChanges { target: browse; text: "Unmount"}
PropertyChanges { target: browse; checked: false; text: "Unmount"}
}
]