From 8d7dad3604cdda2f9ffaf234bf896d1bd9e53a86 Mon Sep 17 00:00:00 2001 From: David Kahles Date: Wed, 6 Apr 2016 02:55:50 +0200 Subject: [PATCH] Introduce QML component to check for plugin availability Remove those checks from three different places and put them into a reusable component, which can be used for different plugins. REVIEW: 127583 --- plasmoid/package/contents/ui/Battery.qml | 27 ++++------- plasmoid/package/contents/ui/FindMyPhone.qml | 23 ++++------ .../package/contents/ui/PluginChecker.qml | 45 +++++++++++++++++++ plasmoid/package/contents/ui/Sftp.qml | 23 ++++------ 4 files changed, 69 insertions(+), 49 deletions(-) create mode 100644 plasmoid/package/contents/ui/PluginChecker.qml diff --git a/plasmoid/package/contents/ui/Battery.qml b/plasmoid/package/contents/ui/Battery.qml index 4538437b9..02bf7cf91 100644 --- a/plasmoid/package/contents/ui/Battery.qml +++ b/plasmoid/package/contents/ui/Battery.qml @@ -27,9 +27,14 @@ QtObject { id: root - property string deviceId: "" - property variant device: DeviceDbusInterfaceFactory.create(deviceId) - property bool available: false + property alias deviceId: checker.deviceId + readonly property alias device: checker.device + readonly property alias available: checker.available + + readonly property PluginChecker pluginChecker: PluginChecker { + id: checker + pluginName: "battery" + } property bool charging: false property int charge: -1 @@ -48,7 +53,6 @@ QtObject { onSuccess: root.charge = result } - // Note: magically called by qml onAvailableChanged: { if (available) { battery = DeviceBatteryDbusInterfaceFactory.create(deviceId) @@ -62,19 +66,4 @@ QtObject { battery = null } } - - function pluginsChanged() { - var result = DBusResponseWaiter.waitForReply(device.hasPlugin("kdeconnect_battery")) - - if (result && result != "error") { - available = true - } else { - available = false - } - } - - Component.onCompleted: { - device.pluginsChanged.connect(pluginsChanged) - device.pluginsChanged() - } } diff --git a/plasmoid/package/contents/ui/FindMyPhone.qml b/plasmoid/package/contents/ui/FindMyPhone.qml index 8a666b7bc..3a23fde76 100644 --- a/plasmoid/package/contents/ui/FindMyPhone.qml +++ b/plasmoid/package/contents/ui/FindMyPhone.qml @@ -27,9 +27,14 @@ QtObject { id: root - property string deviceId: "" - property variant device: DeviceDbusInterfaceFactory.create(deviceId) - property bool available: false + property alias deviceId: checker.deviceId + readonly property alias device: checker.device + readonly property alias available: checker.available + + readonly property PluginChecker pluginChecker: PluginChecker { + id: checker + pluginName: "findmyphone" + } property variant findMyPhone: null @@ -39,12 +44,6 @@ QtObject { } } - Component.onCompleted: { - device.pluginsChanged.connect(pluginsChanged) - pluginsChanged() - } - - // Note: magically called by qml onAvailableChanged: { if (available) { findMyPhone = FindMyPhoneDbusInterfaceFactory.create(deviceId) @@ -52,10 +51,4 @@ QtObject { findMyPhone = null } } - - function pluginsChanged() { - var result = DBusResponseWaiter.waitForReply(device.hasPlugin("kdeconnect_findmyphone")) - available = (result && result != "error"); - } - } diff --git a/plasmoid/package/contents/ui/PluginChecker.qml b/plasmoid/package/contents/ui/PluginChecker.qml new file mode 100644 index 000000000..adfc72767 --- /dev/null +++ b/plasmoid/package/contents/ui/PluginChecker.qml @@ -0,0 +1,45 @@ +/** + * Copyright 2014 Samoilenko Yuri + * Copyright 2016 David Kahles + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import QtQml 2.2 +import org.kde.kdeconnect 1.0 + +QtObject { + + id: root + + property string deviceId: "" + property string pluginName: "" + readonly property variant device: DeviceDbusInterfaceFactory.create(deviceId) + property bool available: false + + property Connections connection: Connections { + target: device + onPluginsChanged: pluginsChanged() + } + + Component.onCompleted: pluginsChanged() + + function pluginsChanged() { + var result = DBusResponseWaiter.waitForReply(device.hasPlugin("kdeconnect_" + pluginName)) + available = (result && result != "error"); + } +} diff --git a/plasmoid/package/contents/ui/Sftp.qml b/plasmoid/package/contents/ui/Sftp.qml index 9ef4f4176..fd817e567 100644 --- a/plasmoid/package/contents/ui/Sftp.qml +++ b/plasmoid/package/contents/ui/Sftp.qml @@ -27,9 +27,14 @@ QtObject { id: root - property string deviceId: "" - property variant device: DeviceDbusInterfaceFactory.create(deviceId) - property bool available: false + property alias deviceId: checker.deviceId + readonly property alias device: checker.device + readonly property alias available: checker.available + + readonly property PluginChecker pluginChecker: PluginChecker { + id: checker + pluginName: "sftp" + } property variant sftp: null @@ -38,12 +43,6 @@ QtObject { sftp.startBrowsing(); } - Component.onCompleted: { - device.pluginsChanged.connect(pluginsChanged) - pluginsChanged() - } - - // Note: magically called by qml onAvailableChanged: { if (available) { sftp = SftpDbusInterfaceFactory.create(deviceId) @@ -51,10 +50,4 @@ QtObject { sftp = null } } - - function pluginsChanged() { - var result = DBusResponseWaiter.waitForReply(device.hasPlugin("kdeconnect_sftp")) - available = (result && result != "error"); - } - }