Commit 22718a0b authored by Samoilenko Yuri's avatar Samoilenko Yuri
Browse files

DeclarativePlugin::BatteryInterface removed

Due to bug occured in QML property access(when object derived from QDBusAbstract interface) C++
part of BatteryPlugin in plasmoid rewritten in pure QML.
BatteryPluginDBusInterface fixed to avoid this bug.

Bug occuring when QML try to access any property of any object derived from
QDBusAvstractInterface - QML engine crashes.
parent 499b8b00
......@@ -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);
}
......@@ -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;
......
......@@ -4,7 +4,6 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_BINARY_DIR})
set(kdeconnectdeclarativeplugin_SRC
batteryinterface.cpp
kdeconnectdeclarativeplugin.cpp
responsewaiter.cpp
)
......
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef BATTERYINTERFACE_H
#define BATTERYINTERFACE_H
#include <QObject>
#include <QString>
#include <KLocalizedString>
#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
......@@ -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));
......
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Samoilenko Yuri <kinnalru@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
......@@ -18,56 +18,64 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "batteryinterface.h"
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
BatteryInterface::BatteryInterface(QObject* parent)
: QObject(parent)
, m_device(0)
, m_battery(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
}
void BatteryInterface::setDevice(const QString& deviceId)
{
if (m_device) {
delete m_device;
property variant nested1: DBusAsyncResponse {
id: startupCheck1
autoDelete: false
onSuccess: state = result
}
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;
property variant nested2: DBusAsyncResponse {
id: startupCheck2
autoDelete: false
onSuccess: charge = result
}
Q_EMIT availableChanged();
}
bool BatteryInterface::available() const
{
return m_battery && m_battery->isValid();
}
QString BatteryInterface::displayString() const
{
if (!m_battery) {
return i18n("No info");
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
}
}
if (isCharging()) {
return i18n("Charging, %1%", chargePercent());
} else {
return i18n("Discharging, %1%", chargePercent());
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()
}
}
......@@ -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
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment