diff --git a/kded/plugins/battery/batterydbusinterface.cpp b/kded/plugins/battery/batterydbusinterface.cpp index 4a7ee2917..6b760f2c3 100644 --- a/kded/plugins/battery/batterydbusinterface.cpp +++ b/kded/plugins/battery/batterydbusinterface.cpp @@ -37,7 +37,8 @@ void BatteryDbusInterface::updateValues(bool isCharging, int currentCharge) mIsCharging = isCharging; mCharge = currentCharge; - Q_EMIT chargingChange(); + Q_EMIT stateChanged(mIsCharging); + Q_EMIT chargeChanged(mCharge); } diff --git a/kded/plugins/battery/batterydbusinterface.h b/kded/plugins/battery/batterydbusinterface.h index 4de673174..d9f412e56 100644 --- a/kded/plugins/battery/batterydbusinterface.h +++ b/kded/plugins/battery/batterydbusinterface.h @@ -28,20 +28,19 @@ class BatteryDbusInterface { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.battery") - Q_PROPERTY( int charge READ charge NOTIFY chargingChange ) - Q_PROPERTY( bool isCharging READ isCharging NOTIFY chargingChange ) public: explicit BatteryDbusInterface(QObject *parent); virtual ~BatteryDbusInterface(); - int charge() { return mCharge; } - bool isCharging() { return mIsCharging; } + Q_SCRIPTABLE int charge() { return mCharge; } + Q_SCRIPTABLE bool isCharging() { return mIsCharging; } void updateValues(bool isCharging, int currentCharge); Q_SIGNALS: - Q_SCRIPTABLE void chargingChange(); + Q_SCRIPTABLE void stateChanged(bool charging); + Q_SCRIPTABLE void chargeChanged(int charge); private: int mCharge; diff --git a/plasmoid/declarativeplugin/CMakeLists.txt b/plasmoid/declarativeplugin/CMakeLists.txt index 3b7b5d649..4919d471c 100644 --- a/plasmoid/declarativeplugin/CMakeLists.txt +++ b/plasmoid/declarativeplugin/CMakeLists.txt @@ -4,7 +4,6 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR}) set(kdeconnectdeclarativeplugin_SRC - batteryinterface.cpp kdeconnectdeclarativeplugin.cpp responsewaiter.cpp ) diff --git a/plasmoid/declarativeplugin/batteryinterface.cpp b/plasmoid/declarativeplugin/batteryinterface.cpp deleted file mode 100644 index 4b8d0c9f3..000000000 --- a/plasmoid/declarativeplugin/batteryinterface.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright 2013 Albert Vaca - * - * 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 . - */ - -#include "batteryinterface.h" - -BatteryInterface::BatteryInterface(QObject* parent) - : QObject(parent) - , m_device(0) - , m_battery(0) -{ - -} - -void BatteryInterface::setDevice(const QString& deviceId) -{ - if (m_device) { - delete m_device; - } - m_device = new DeviceDbusInterface(deviceId, this); - connect(m_device, SIGNAL(pluginsChanged()), this, SLOT(devicePluginsChanged())); - devicePluginsChanged(); -} - -void BatteryInterface::devicePluginsChanged() -{ - if (m_device->hasPlugin("kdeconnect_battery")) { - m_battery = new DeviceBatteryDbusInterface(m_device->id(), this); - connect(m_battery, SIGNAL(chargingChange()), this, SIGNAL(infoChanged())); - Q_EMIT infoChanged(); - } else { - delete m_battery; - m_battery = 0; - } - - Q_EMIT availableChanged(); -} - -bool BatteryInterface::available() const -{ - return m_battery && m_battery->isValid(); -} - -QString BatteryInterface::displayString() const -{ - - if (!m_battery) { - return i18n("No info"); - } - - if (isCharging()) { - return i18n("Charging, %1%", chargePercent()); - } else { - return i18n("Discharging, %1%", chargePercent()); - } - -} diff --git a/plasmoid/declarativeplugin/batteryinterface.h b/plasmoid/declarativeplugin/batteryinterface.h deleted file mode 100644 index daf1c0d0c..000000000 --- a/plasmoid/declarativeplugin/batteryinterface.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2013 Albert Vaca - * - * 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 . - */ - -#ifndef BATTERYINTERFACE_H -#define BATTERYINTERFACE_H - -#include -#include -#include -#include "libkdeconnect/dbusinterfaces.h" - -//Wrapper to use it from QML -class BatteryInterface - : public QObject -{ - Q_OBJECT - - Q_PROPERTY(QString device READ device WRITE setDevice NOTIFY deviceChanged) - Q_PROPERTY(bool available READ available NOTIFY availableChanged) - Q_PROPERTY(QString displayString READ displayString NOTIFY infoChanged) - //Q_PROPERTY(bool displayString READ isCharging NOTIFY infoChanged) - //Q_PROPERTY(int chargePercent READ chargePercent NOTIFY infoChanged) - -public: - BatteryInterface(QObject* parent = 0); - void setDevice(const QString& deviceId); - QString device() const { return m_device->id(); } - - bool available() const; //True if the interface is accessible - - QString displayString() const; //Human readable string with the battery status - bool isCharging() const { return m_battery->isCharging(); } - int chargePercent() const { return m_battery->charge(); } - -Q_SIGNALS: - void availableChanged(); - void infoChanged(); - void deviceChanged(); - -private: - DeviceDbusInterface* m_device; - mutable DeviceBatteryDbusInterface* m_battery; - -public Q_SLOTS: - void devicePluginsChanged(); -}; - -#endif // BATTERYINTERFACE_H diff --git a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp index 7c63a5c13..9d8970b94 100644 --- a/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp +++ b/plasmoid/declarativeplugin/kdeconnectdeclarativeplugin.cpp @@ -35,12 +35,21 @@ Q_EXPORT_PLUGIN2(kdeconnectdeclarativeplugin, KdeConnectDeclarativePlugin); +QObject* createDeviceDbusInterface(QVariant deviceId) +{ + return new DeviceDbusInterface(deviceId.toString()); +} + +QObject* createDeviceBatteryDbusInterface(QVariant deviceId) +{ + return new DeviceBatteryDbusInterface(deviceId.toString()); +} + QObject* createSftpInterface(QVariant deviceId) { return new SftpDbusInterface(deviceId.toString()); } - QObject* createDBusResponse() { return new DBusAsyncResponse(); @@ -60,6 +69,12 @@ void KdeConnectDeclarativePlugin::registerTypes(const char* uri) void KdeConnectDeclarativePlugin::initializeEngine(QDeclarativeEngine* engine, const char* uri) { QDeclarativeExtensionPlugin::initializeEngine(engine, uri); + + engine->rootContext()->setContextProperty("DeviceDbusInterfaceFactory" + , new ObjectFactory(engine, createDeviceDbusInterface)); + + engine->rootContext()->setContextProperty("DeviceBatteryDbusInterfaceFactory" + , new ObjectFactory(engine, createDeviceBatteryDbusInterface)); engine->rootContext()->setContextProperty("SftpDbusInterfaceFactory" , new ObjectFactory(engine, createSftpInterface)); diff --git a/plasmoid/package/contents/ui/Battery.qml b/plasmoid/package/contents/ui/Battery.qml new file mode 100644 index 000000000..8ccf5588e --- /dev/null +++ b/plasmoid/package/contents/ui/Battery.qml @@ -0,0 +1,81 @@ +/** + * Copyright 2014 Samoilenko Yuri + * + * 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 QtQuick 1.1 +import org.kde.plasma.core 0.1 as PlasmaCore +import org.kde.plasma.components 0.1 as PlasmaComponents +import org.kde.kdeconnect 1.0 + +QtObject { + + id: root + + property string deviceId: "" + property bool available: false + property bool state: false + property int charge: -1 + property string displayString: (state) ? ("Charging, " + charge) : ("Discharging, " + charge) + + property variant device: DeviceDbusInterfaceFactory.create(deviceId) + property variant battery: null + + property variant nested1: DBusAsyncResponse { + id: startupCheck1 + autoDelete: false + onSuccess: state = result + } + + property variant nested2: DBusAsyncResponse { + id: startupCheck2 + autoDelete: false + onSuccess: charge = result + } + + onAvailableChanged: { + if (available) { + battery = DeviceBatteryDbusInterfaceFactory.create(deviceId) + + battery.stateChanged.connect(function(charging) {root.state = charging}) + battery.chargeChanged.connect(function(charge) {root.charge = charge}) + + startupCheck1.pendingCall = battery.isCharging() + startupCheck2.pendingCall = battery.charge() + } + else { + 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/DeviceDelegate.qml b/plasmoid/package/contents/ui/DeviceDelegate.qml index adbe07568..cfe6aca85 100644 --- a/plasmoid/package/contents/ui/DeviceDelegate.qml +++ b/plasmoid/package/contents/ui/DeviceDelegate.qml @@ -90,18 +90,20 @@ PlasmaComponents.ListItem //Battery PlasmaComponents.ListItem { - BatteryInterface { - id: batteryInterface - device: root.deviceId + + Battery { + id: battery + deviceId: root.deviceId } + sectionDelegate: true - visible: batteryInterface.available + visible: battery.available PlasmaComponents.Label { //font.bold: true text: i18n("Battery") } PlasmaComponents.Label { - text: batteryInterface.displayString + text: battery.displayString anchors.right: parent.right } }