Skip to content

Instantly share code, notes, and snippets.

@albertvaka
Created August 16, 2013 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertvaka/6250781 to your computer and use it in GitHub Desktop.
Save albertvaka/6250781 to your computer and use it in GitHub Desktop.
Solid backend to read battery info from KDE Connect
diff --git a/solid/solid/CMakeLists.txt b/solid/solid/CMakeLists.txt
index a7f1f07..c2099dc 100644
--- a/solid/solid/CMakeLists.txt
+++ b/solid/solid/CMakeLists.txt
@@ -287,6 +287,15 @@ if(NOT WIN32 AND NOT APPLE)
backends/upower/upowergenericinterface.cpp
)
+ message(STATUS "Building Solid PhonePower backend." )
+ set(solid_LIB_SRCS ${solid_LIB_SRCS}
+ backends/phonepower/phonepowermanager.cpp
+ backends/phonepower/phonepowerdevice.cpp
+ backends/phonepower/phonepowerbattery.cpp
+ backends/phonepower/phonepowerdeviceinterface.cpp
+ backends/phonepower/kdeconnectinterface.cpp
+ )
+
# FIXME: this should work on more Unix systems
if (CMAKE_SYSTEM_NAME MATCHES Linux)
diff --git a/solid/solid/backends/phonepower/kdeconnectinterface.cpp b/solid/solid/backends/phonepower/kdeconnectinterface.cpp
new file mode 100644
index 0000000..8b7f519
--- /dev/null
+++ b/solid/solid/backends/phonepower/kdeconnectinterface.cpp
@@ -0,0 +1,28 @@
+/*
+ * This file was generated by qdbusxml2cpp version 0.7
+ * Command line was: qdbusxml2cpp -m -p daemoninterface /home/vaka/kde4/build/kdeconnect-kded/daemon/org.kde.kdeconnect.daemon.xml
+ *
+ * qdbusxml2cpp is Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+ *
+ * This is an auto-generated file.
+ * This file may have been hand-edited. Look for HAND-EDIT comments
+ * before re-generating it.
+ */
+
+#include "kdeconnectinterface.h"
+
+/*
+ * Implementation of interface class OrgKdeKdeconnectDaemonInterface
+ */
+
+OrgKdeKdeconnectDaemonInterface::OrgKdeKdeconnectDaemonInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent)
+ : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent)
+{
+}
+
+OrgKdeKdeconnectDaemonInterface::~OrgKdeKdeconnectDaemonInterface()
+{
+}
+
+
+#include "kdeconnectinterface.moc"
diff --git a/solid/solid/backends/phonepower/kdeconnectinterface.h b/solid/solid/backends/phonepower/kdeconnectinterface.h
new file mode 100644
index 0000000..e813c4c
--- /dev/null
+++ b/solid/solid/backends/phonepower/kdeconnectinterface.h
@@ -0,0 +1,77 @@
+/*
+ * This file was generated by qdbusxml2cpp version 0.7
+ * Command line was: qdbusxml2cpp -m -p daemoninterface /home/vaka/kde4/build/kdeconnect-kded/daemon/org.kde.kdeconnect.daemon.xml
+ *
+ * qdbusxml2cpp is Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+ *
+ * This is an auto-generated file.
+ * Do not edit! All changes made to it will be lost.
+ */
+
+#ifndef DAEMONINTERFACE_H_1376408992
+#define DAEMONINTERFACE_H_1376408992
+
+#include <QtCore/QObject>
+#include <QtCore/QByteArray>
+#include <QtCore/QList>
+#include <QtCore/QMap>
+#include <QtCore/QString>
+#include <QtCore/QStringList>
+#include <QtCore/QVariant>
+#include <QtDBus/QtDBus>
+
+/*
+ * Proxy class for interface org.kde.kdeconnect.daemon
+ */
+class OrgKdeKdeconnectDaemonInterface: public QDBusAbstractInterface
+{
+ Q_OBJECT
+public:
+ static inline const char *staticInterfaceName()
+ { return "org.kde.kdeconnect.daemon"; }
+
+public:
+ OrgKdeKdeconnectDaemonInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = 0);
+
+ ~OrgKdeKdeconnectDaemonInterface();
+
+public Q_SLOTS: // METHODS
+ inline QDBusPendingReply<QStringList> devices()
+ {
+ QList<QVariant> argumentList;
+ return asyncCallWithArgumentList(QLatin1String("devices"), argumentList);
+ }
+
+ inline QDBusPendingReply<> forceOnNetworkChange()
+ {
+ QList<QVariant> argumentList;
+ return asyncCallWithArgumentList(QLatin1String("forceOnNetworkChange"), argumentList);
+ }
+
+ inline QDBusPendingReply<> setDiscoveryEnabled(bool b)
+ {
+ QList<QVariant> argumentList;
+ argumentList << QVariant::fromValue(b);
+ return asyncCallWithArgumentList(QLatin1String("setDiscoveryEnabled"), argumentList);
+ }
+
+ inline QDBusPendingReply<QStringList> visibleDevices()
+ {
+ QList<QVariant> argumentList;
+ return asyncCallWithArgumentList(QLatin1String("visibleDevices"), argumentList);
+ }
+
+Q_SIGNALS: // SIGNALS
+ void deviceAdded(const QString &id);
+ void deviceRemoved(const QString &id);
+ void deviceVisibilityChanged(const QString &id, bool isVisible);
+};
+
+namespace org {
+ namespace kde {
+ namespace kdeconnect {
+ typedef ::OrgKdeKdeconnectDaemonInterface daemon;
+ }
+ }
+}
+#endif
diff --git a/solid/solid/backends/phonepower/phonepowerbattery.cpp b/solid/solid/backends/phonepower/phonepowerbattery.cpp
new file mode 100644
index 0000000..c5c4d87
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowerbattery.cpp
@@ -0,0 +1,98 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "phonepowerbattery.h"
+
+#include <QDebug>
+
+using namespace Solid::Backends::PhonePower;
+
+Battery::Battery(PhonePowerDevice *device)
+ : DeviceInterface(device)
+ , m_chargePercent(0)
+ , m_chargeState(Solid::Battery::NoCharge)
+ , mDevice(device)
+{
+ connect(device, SIGNAL(changed()), this, SLOT(slotChanged()));
+}
+
+Battery::~Battery()
+{
+}
+
+bool Battery::isPlugged() const
+{
+ return true;
+}
+
+Solid::Battery::BatteryType Battery::type() const
+{
+ return Solid::Battery::PhoneBattery;
+}
+
+int Battery::chargePercent() const
+{
+ m_chargePercent = mDevice->chargePercent();
+ return m_chargePercent;
+}
+
+int Battery::capacity() const
+{
+ return 100;
+}
+
+bool Battery::isRechargeable() const
+{
+ return true;
+}
+
+bool Battery::isPowerSupply() const
+{
+ return false;
+}
+
+Solid::Battery::ChargeState Battery::chargeState() const
+{
+ m_chargeState = mDevice->chargeState();
+ return m_chargeState;
+}
+
+void Battery::slotChanged()
+{
+
+ if (!mDevice) {
+ return;
+ }
+
+ if (m_chargePercent != mDevice->chargePercent()) {
+ m_chargePercent = mDevice->chargePercent();
+ qDebug() << "Percentage has changed: " << m_chargePercent << mDevice->udi();
+ Q_EMIT chargePercentChanged(m_chargePercent, mDevice->udi());
+ }
+
+ if (m_chargeState != mDevice->chargeState()) {
+ m_chargeState = mDevice->chargeState();
+ qDebug() << "State has changed: " << m_chargeState << mDevice->udi();
+ Q_EMIT chargeStateChanged(m_chargeState, mDevice->udi());
+ }
+
+}
+
+#include "backends/phonepower/phonepowerbattery.moc"
diff --git a/solid/solid/backends/phonepower/phonepowerbattery.h b/solid/solid/backends/phonepower/phonepowerbattery.h
new file mode 100644
index 0000000..c279df5
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowerbattery.h
@@ -0,0 +1,73 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef SOLID_BACKENDS_PHONEPOWER_BATTERY_H
+#define SOLID_BACKENDS_PHONEPOWER_BATTERY_H
+
+#include <solid/ifaces/battery.h>
+#include "phonepowerdevice.h"
+#include "phonepowerdeviceinterface.h"
+
+namespace Solid
+{
+namespace Backends
+{
+namespace PhonePower
+{
+class Battery : public DeviceInterface, virtual public Solid::Ifaces::Battery
+{
+ Q_OBJECT
+ Q_INTERFACES(Solid::Ifaces::Battery)
+
+public:
+ Battery(PhonePowerDevice *device);
+ virtual ~Battery();
+
+ virtual bool isPlugged() const;
+ virtual Solid::Battery::BatteryType type() const;
+
+ virtual int chargePercent() const;
+ virtual int capacity() const;
+
+ virtual bool isRechargeable() const;
+ virtual bool isPowerSupply() const;
+
+ virtual Solid::Battery::ChargeState chargeState() const;
+
+Q_SIGNALS:
+ void chargePercentChanged(int value, const QString &udi);
+ void capacityChanged(int value, const QString &udi);
+ void chargeStateChanged(int newState, const QString &udi);
+ void plugStateChanged(bool newState, const QString &udi);
+ void powerSupplyStateChanged(bool newState, const QString &udi);
+
+private Q_SLOTS:
+ void slotChanged();
+
+private:
+ mutable int m_chargePercent;
+ mutable Solid::Battery::ChargeState m_chargeState;
+ PhonePowerDevice *mDevice;
+};
+}
+}
+}
+
+#endif // SOLID_BACKENDS_PHONEPOWER_BATTERY_H
diff --git a/solid/solid/backends/phonepower/phonepowerdevice.cpp b/solid/solid/backends/phonepower/phonepowerdevice.cpp
new file mode 100644
index 0000000..8a15168
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowerdevice.cpp
@@ -0,0 +1,141 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "phonepowerdevice.h"
+#include "phonepowerdeviceinterface.h"
+#include "phonepowerbattery.h"
+
+#include <solid/genericinterface.h>
+#include <solid/device.h>
+
+#include <QtCore/QStringList>
+#include <QtCore/QDebug>
+#include <QtDBus/QDBusPendingReply>
+
+using namespace Solid::Backends::PhonePower;
+
+PhonePowerDevice::PhonePowerDevice(const QString &udi, const QString &id)
+ : Solid::Ifaces::Device()
+ , m_device("org.kde.kdeconnect",
+ "/modules/kdeconnect/devices/"+id,
+ "org.kde.kdeconnect.device",
+ QDBusConnection::sessionBus())
+ , m_battery("org.kde.kdeconnect",
+ "/modules/kdeconnect/devices/"+id,
+ "org.kde.kdeconnect.device.battery",
+ QDBusConnection::sessionBus())
+ , m_udi(udi)
+{
+ qDebug() << "/modules/kdeconnect/devices/"+id;
+
+ if (m_battery.isValid())
+ connect(&m_battery, SIGNAL(chargingChange()), this, SLOT(slotChanged()));
+}
+
+PhonePowerDevice::~PhonePowerDevice()
+{
+}
+
+QObject* PhonePowerDevice::createDeviceInterface(const Solid::DeviceInterface::Type& type)
+{
+ if (!queryDeviceInterface(type)) {
+ return 0;
+ }
+
+ DeviceInterface *iface = 0;
+ switch (type)
+ {
+ case Solid::DeviceInterface::Battery:
+ iface = new Battery(this);
+ break;
+ default:
+ break;
+ }
+ return iface;
+}
+
+bool PhonePowerDevice::queryDeviceInterface(const Solid::DeviceInterface::Type& type) const
+{
+ switch(type) {
+ case Solid::DeviceInterface::Battery:
+ return true;
+ default:
+ return false;
+ }
+}
+
+QStringList PhonePowerDevice::emblems() const
+{
+ return QStringList();
+}
+
+QString PhonePowerDevice::description() const
+{
+ return QObject::tr("%1 Battery", "%1 is battery technology").arg(batteryTechnology());
+}
+
+QString PhonePowerDevice::batteryTechnology() const
+{
+ return QLatin1String("Android");
+}
+
+QString PhonePowerDevice::icon() const
+{
+ return "pda";
+}
+
+QString PhonePowerDevice::product() const
+{
+ return m_device.property("name").toString();
+}
+
+QString PhonePowerDevice::vendor() const
+{
+ return QLatin1String("Android");
+}
+
+QString PhonePowerDevice::udi() const
+{
+ return m_udi;
+}
+
+QString PhonePowerDevice::parentUdi() const
+{
+ return QLatin1String("/org/kde/kdeconnect");
+}
+
+int PhonePowerDevice::chargePercent()
+{
+ return m_battery.property("charge").toInt();
+}
+
+Solid::Battery::ChargeState PhonePowerDevice::chargeState()
+{
+ bool charging = m_battery.property("isCharging").toBool();
+ if (charging) return Solid::Battery::Charging;
+ else return Solid::Battery::NoCharge;
+ //TODO: return Solid::Battery::Discharging;
+}
+
+void PhonePowerDevice::slotChanged()
+{
+ qDebug() << "charge information changed" << m_udi;
+ emit changed();
+}
\ No newline at end of file
diff --git a/solid/solid/backends/phonepower/phonepowerdevice.h b/solid/solid/backends/phonepower/phonepowerdevice.h
new file mode 100644
index 0000000..96e352e
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowerdevice.h
@@ -0,0 +1,76 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PHONEPOWERDEVICE_H
+#define PHONEPOWERDEVICE_H
+
+#include <ifaces/device.h>
+#include <solid/deviceinterface.h>
+#include <battery.h>
+
+#include <QtDBus/QDBusInterface>
+#include <QtCore/QSet>
+
+namespace Solid
+{
+namespace Backends
+{
+namespace PhonePower
+{
+
+class PhonePowerDevice : public Solid::Ifaces::Device
+{
+ Q_OBJECT
+public:
+ PhonePowerDevice(const QString &udi, const QString &di);
+ virtual ~PhonePowerDevice();
+
+ virtual QObject* createDeviceInterface(const Solid::DeviceInterface::Type& type);
+ virtual bool queryDeviceInterface(const Solid::DeviceInterface::Type& type) const;
+ virtual QString description() const;
+ virtual QStringList emblems() const;
+ virtual QString icon() const;
+ virtual QString product() const;
+ virtual QString vendor() const;
+ virtual QString udi() const;
+ virtual QString parentUdi() const;
+ Solid::Battery::ChargeState chargeState();
+ int chargePercent();
+
+Q_SIGNALS:
+ void changed();
+
+private Q_SLOTS:
+ void slotChanged();
+
+private:
+ QString batteryTechnology() const;
+ mutable QDBusInterface m_device;
+ mutable QDBusInterface m_battery;
+ QString m_udi;
+
+};
+
+}
+}
+}
+
+#endif // PHONEPOWERDEVICE_H
diff --git a/solid/solid/backends/phonepower/phonepowerdeviceinterface.cpp b/solid/solid/backends/phonepower/phonepowerdeviceinterface.cpp
new file mode 100644
index 0000000..23ce11c
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowerdeviceinterface.cpp
@@ -0,0 +1,33 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "phonepowerdeviceinterface.h"
+
+using namespace Solid::Backends::PhonePower;
+
+DeviceInterface::DeviceInterface(PhonePowerDevice *device)
+ : QObject(device), m_device(device)
+{
+
+}
+
+DeviceInterface::~DeviceInterface()
+{
+}
diff --git a/solid/solid/backends/phonepower/phonepowerdeviceinterface.h b/solid/solid/backends/phonepower/phonepowerdeviceinterface.h
new file mode 100644
index 0000000..85bac01
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowerdeviceinterface.h
@@ -0,0 +1,56 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PHONEPOWERDEVICEINTERFACE_H
+#define PHONEPOWERDEVICEINTERFACE_H
+
+#include <ifaces/deviceinterface.h>
+#include "phonepowerdevice.h"
+
+#include <QtCore/QObject>
+#include <QtCore/QStringList>
+#include <QtCore/QWeakPointer>
+
+namespace Solid
+{
+namespace Backends
+{
+namespace PhonePower
+{
+
+class DeviceInterface : public QObject, virtual public Solid::Ifaces::DeviceInterface
+{
+ Q_OBJECT
+ Q_INTERFACES(Solid::Ifaces::DeviceInterface)
+public:
+ DeviceInterface(PhonePowerDevice *device);
+ virtual ~DeviceInterface();
+
+protected:
+ QWeakPointer<PhonePowerDevice> m_device;
+
+};
+
+}
+}
+}
+
+#endif // PHONEPOWERDEVICEINTERFACE_H
diff --git a/solid/solid/backends/phonepower/phonepowermanager.cpp b/solid/solid/backends/phonepower/phonepowermanager.cpp
new file mode 100644
index 0000000..6a51450
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowermanager.cpp
@@ -0,0 +1,236 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "phonepowermanager.h"
+#include "phonepowerdevice.h"
+
+#include <QtDBus/QDBusReply>
+#include <QtCore/QDebug>
+#include <QtDBus/QDBusMetaType>
+#include <QtDBus/QDBusConnectionInterface>
+#include <QDBusServiceWatcher>
+
+#include "../shared/rootdevice.h"
+
+using namespace Solid::Backends::PhonePower;
+using namespace Solid::Backends::Shared;
+
+PhonePowerManager::PhonePowerManager(QObject *parent)
+ : Solid::Ifaces::DeviceManager(parent)
+ , m_manager("org.kde.kdeconnect",
+ "/modules/kdeconnect",
+ QDBusConnection::sessionBus())
+ , m_ready(false)
+{
+ m_supportedInterfaces << Solid::DeviceInterface::Battery;
+
+
+ QDBusServiceWatcher* watcher = new QDBusServiceWatcher("org.kde.kdeconnect", QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
+ connect(watcher, SIGNAL(serviceRegistered(QString)), this, SLOT(serviceReady()));
+ connect(watcher, SIGNAL(serviceUnregistered(QString)), this, SLOT(serviceUnregistered()));
+
+ QDBusPendingCall msg = m_manager.asyncCall("visibleDevices");
+ msg.waitForFinished();
+ if (msg.isValid()) {
+ serviceReady();
+ }
+
+}
+
+void PhonePowerManager::serviceReady()
+{
+
+ QDBusPendingReply<QStringList> pendingDeviceList = m_manager.visibleDevices();
+ pendingDeviceList.waitForFinished();
+ if (!pendingDeviceList.isValid() || pendingDeviceList.isError()) {
+ return;
+ }
+ QStringList deviceList = pendingDeviceList.value();
+
+ Q_FOREACH(const QString& device, deviceList) {
+ slotDeviceAdded(device);
+ }
+
+ connect(&m_manager, SIGNAL(deviceVisibilityChanged(QString, bool)),
+ this, SLOT(deviceVisibilityChanged(QString,bool)));
+
+ m_ready = true;
+}
+
+void PhonePowerManager::serviceUnregistered()
+{
+ m_ready = false;
+ Q_FOREACH(const QString& device, m_devices) {
+ slotDeviceRemoved(device);
+ }
+ m_devices.clear();
+
+ disconnect(&m_manager, SIGNAL(deviceVisibilityChanged(QString, bool)),
+ this, SLOT(deviceVisibilityChanged(QString,bool)));
+
+}
+
+void PhonePowerManager::deviceVisibilityChanged(const QString& id, bool visible)
+{
+ if (visible)
+ slotDeviceAdded(id);
+ else
+ slotDeviceRemoved(id);
+}
+
+PhonePowerManager::~PhonePowerManager()
+{
+
+}
+
+QObject* PhonePowerManager::createDevice(const QString& udi)
+{
+ if (!m_ready) return 0;
+
+ QString connectId = udi.mid(udiPrefix().count() + 1);
+ qDebug() << "createDevice" << udi << connectId;
+ if (udi == udiPrefix()) {
+
+ RootDevice *root = new RootDevice(udiPrefix());
+
+ root->setProduct(tr("Power Management"));
+ root->setDescription(tr("Batteries and other sources of power"));
+ root->setIcon("preferences-system-power-management");
+
+ return root;
+
+ } else if (m_devices.contains(connectId)) {
+ return new PhonePowerDevice(udi, connectId);
+ } else {
+ return 0;
+ }
+}
+
+QStringList PhonePowerManager::devicesFromQuery(const QString& parentUdi, Solid::DeviceInterface::Type type)
+{
+ if (!m_ready) {
+ return QStringList();
+ }
+
+ if (type != Solid::DeviceInterface::Battery)
+ {
+ return QStringList();
+ }
+
+ if (!parentUdi.isEmpty() && parentUdi != udiPrefix())
+ {
+ return QStringList();
+ }
+
+ return allDevices();
+
+}
+
+QStringList PhonePowerManager::allDevices()
+{
+ if (!m_ready) {
+ return QStringList();
+ }
+
+ QStringList udiList;
+ Q_FOREACH(const QString& device, m_devices) {
+ udiList.append(udiPrefix()+"/"+device);
+ }
+
+ return udiList;
+}
+
+QSet< Solid::DeviceInterface::Type > PhonePowerManager::supportedInterfaces() const
+{
+ return m_supportedInterfaces;
+}
+
+QString PhonePowerManager::udiPrefix() const
+{
+ return QLatin1String("/org/kde/kdeconnect");
+}
+
+void PhonePowerManager::slotDeviceAdded(const QString &id)
+{
+ qDebug() << "slotDeviceAdded" << id;
+ if (m_devices.contains(id)) {
+ return;
+ }
+
+ //Test if device has battery support!
+ QDBusInterface* device = new QDBusInterface("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+id, "org.kde.kdeconnect.device", QDBusConnection::sessionBus(), this);
+ QDBusPendingReply<bool> reply = device->asyncCallWithArgumentList("hasPlugin",QVariantList() << QVariant("kdeconnect_battery"));
+ reply.waitForFinished();
+ if (!reply.isValid()) {
+ return;
+ }
+
+ bool hasBattery = reply.value();
+
+ qDebug() << "HasBattery:" << hasBattery;
+
+ if (hasBattery) {
+ m_devices.insert(id);
+ Q_EMIT deviceAdded(udiPrefix()+"/"+id);
+ device->deleteLater();
+ } else {
+ connect(device, SIGNAL(pluginsChanged()),
+ this, SLOT(pluginsChanged()));
+ }
+
+}
+
+void PhonePowerManager::pluginsChanged()
+{
+ QDBusInterface* device = (QDBusInterface*)sender();
+
+ disconnect(device, SIGNAL(pluginsChanged()),
+ this, SLOT(pluginsChanged()));
+
+ QVariant id = device->property("id");
+ if (id.isValid()) {
+ qDebug() << "PluginsChanged" << id.toString();
+
+ QDBusInterface* device = new QDBusInterface("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+id.toString(), "org.kde.kdeconnect.device", QDBusConnection::sessionBus(), this);
+ QDBusPendingReply<bool> reply = device->asyncCallWithArgumentList("hasPlugin",QVariantList() << QVariant("kdeconnect_battery"));
+ reply.waitForFinished();
+ if (!reply.isValid() || !reply.value()) {
+ slotDeviceRemoved(id.toString());
+ } else {
+ slotDeviceAdded(id.toString());
+ }
+
+ //device->deleteLater();
+ }
+
+}
+
+void PhonePowerManager::slotDeviceRemoved(const QString &id)
+{
+ qDebug() << "slotDeviceRemoved" << id;
+ if (!m_devices.contains(id)) {
+ return;
+ }
+
+ m_devices.remove(id);
+ Q_EMIT deviceRemoved(id);
+}
+
+#include "backends/phonepower/phonepowermanager.moc"
diff --git a/solid/solid/backends/phonepower/phonepowermanager.h b/solid/solid/backends/phonepower/phonepowermanager.h
new file mode 100644
index 0000000..2722899
--- /dev/null
+++ b/solid/solid/backends/phonepower/phonepowermanager.h
@@ -0,0 +1,73 @@
+/*
+ Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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 6 of version 3 of the license.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PHONEPOWERMANAGER_H
+#define PHONEPOWERMANAGER_H
+
+#include "solid/ifaces/devicemanager.h"
+
+#include <QtDBus/QDBusInterface>
+#include <QtCore/QSet>
+
+#include "kdeconnectinterface.h"
+
+namespace Solid
+{
+namespace Backends
+{
+namespace PhonePower
+{
+
+class PhonePowerDevice;
+
+class PhonePowerManager : public Solid::Ifaces::DeviceManager
+{
+ Q_OBJECT
+
+public:
+ PhonePowerManager(QObject *parent);
+ virtual ~PhonePowerManager();
+ virtual QObject* createDevice(const QString& udi);
+ virtual QStringList devicesFromQuery(const QString& parentUdi, Solid::DeviceInterface::Type type);
+ virtual QStringList allDevices();
+ virtual QSet< Solid::DeviceInterface::Type > supportedInterfaces() const;
+ virtual QString udiPrefix() const;
+
+private Q_SLOTS:
+ void serviceReady();
+ void serviceUnregistered();
+ void deviceVisibilityChanged(const QString& id ,bool visible);
+ void slotDeviceAdded(const QString &opath);
+ void slotDeviceRemoved(const QString &opath);
+ void pluginsChanged();
+
+private:
+ QSet<Solid::DeviceInterface::Type> m_supportedInterfaces;
+ QSet<QString> m_devices;
+ OrgKdeKdeconnectDaemonInterface m_manager;
+ bool m_ready;
+
+};
+
+}
+}
+}
+#endif // PHONEPOWERMANAGER_H
diff --git a/solid/solid/managerbase.cpp b/solid/solid/managerbase.cpp
index 6649972..96d66ac 100644
--- a/solid/solid/managerbase.cpp
+++ b/solid/solid/managerbase.cpp
@@ -47,6 +47,7 @@
#endif
#include "backends/fstab/fstabmanager.h"
+#include "backends/phonepower/phonepowermanager.h"
#elif defined (Q_WS_WIN) && !defined(_WIN32_WCE)
#include "backends/win/windevicemanager.h"
@@ -99,6 +100,7 @@ void Solid::ManagerBasePrivate::loadBackends()
<< new Solid::Backends::UPower::UPowerManager(0)
<< new Solid::Backends::Fstab::FstabManager(0);
}
+ m_backends << new Solid::Backends::PhonePower::PhonePowerManager(0);
# endif
# if defined (HUPNP_FOUND)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment