diff --git a/daemon/plugins/battery/CMakeLists.txt b/daemon/plugins/battery/CMakeLists.txt index b4583cfe2..e20f13bc4 100644 --- a/daemon/plugins/battery/CMakeLists.txt +++ b/daemon/plugins/battery/CMakeLists.txt @@ -1,14 +1,30 @@ +find_package(KDE4 REQUIRED) +include (KDE4Defaults) +include_directories(${KDE4_INCLUDES}) + +set(kdeconnect_battery_SRCS + batteryplugin.cpp + batterydbusinterface.cpp + ../kdeconnectplugin.cpp +) + +kde4_add_plugin(kdeconnect_battery ${kdeconnect_battery_SRCS}) + +target_link_libraries(kdeconnect_battery + ${KDE4_KDECORE_LIBS} + ${KDE4_KDEUI_LIBS} + ${QT_QTNETWORK_LIBRARY} + qjson +) + +install(TARGETS kdeconnect_battery DESTINATION ${PLUGIN_INSTALL_DIR} ) +install(FILES kdeconnect_battery.desktop DESTINATION ${SERVICES_INSTALL_DIR} ) + +include(../../../cmakemacros.txt) generate_and_install_dbus_interface( - kded_kdeconnect - packageinterfaces/devicebatteryinformation_p.h + kdeconnect_battery + batterydbusinterface.h org.kde.kdeconnect.device.battery.xml OPTIONS -a ) - -generate_and_install_dbus_interface( - kded_kdeconnect - packageinterfaces/batterypackageinterface.h - org.kde.kdeconnect.battery.xml - OPTIONS -a -) diff --git a/daemon/plugins/battery/batterydbusinterface.cpp b/daemon/plugins/battery/batterydbusinterface.cpp new file mode 100644 index 000000000..d44a99839 --- /dev/null +++ b/daemon/plugins/battery/batterydbusinterface.cpp @@ -0,0 +1,39 @@ +/** + * 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 "batterydbusinterface.h" + +#include + +BatteryDbusInterface::BatteryDbusInterface(QObject *parent) + : QDBusAbstractAdaptor(parent) +{ + +} + +void BatteryDbusInterface::updateValues(bool isCharging, int currentCharge) +{ + mIsCharging = isCharging; + mCharge = currentCharge; + + Q_EMIT chargingChange(); +} + + diff --git a/daemon/plugins/battery/batterydbusinterface.h b/daemon/plugins/battery/batterydbusinterface.h new file mode 100644 index 000000000..457cbb09e --- /dev/null +++ b/daemon/plugins/battery/batterydbusinterface.h @@ -0,0 +1,51 @@ +/** + * 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 BATTERYDBUSINTERFACE_H +#define BATTERYDBUSINTERFACE_H + +#include + +class BatteryDbusInterface + : public QDBusAbstractAdaptor +{ + 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); + + int charge() { return mCharge; } + bool isCharging() { return mIsCharging; } + + void updateValues(bool isCharging, int currentCharge); + +Q_SIGNALS: + Q_SCRIPTABLE void chargingChange(); + +private: + bool mIsCharging; + int mCharge; + +}; + +#endif diff --git a/daemon/plugins/battery/batteryplugin.cpp b/daemon/plugins/battery/batteryplugin.cpp index 047836462..5de2fd1ef 100644 --- a/daemon/plugins/battery/batteryplugin.cpp +++ b/daemon/plugins/battery/batteryplugin.cpp @@ -18,67 +18,56 @@ * along with this program. If not, see . */ -#include "batterypackageinterface.h" +#include "batteryplugin.h" #include -#include +#include +#include +#include "batterydbusinterface.h" -BatteryPackageInterface::BatteryPackageInterface(QObject* parent) - : PackageInterface(parent) +K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< BatteryPlugin >(); ) +K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_battery", "kdeconnect_battery") ) + +BatteryPlugin::BatteryPlugin(QObject *parent, const QVariantList &args) + : KdeConnectPlugin(parent, args) { - //TODO: Get initial state of all devices + batteryDbusInterface = new BatteryDbusInterface(parent); - QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/plugins/battery", this, QDBusConnection::ExportScriptableContents); - //The solid backend watches for this service to become available, because it is not possible to watch for a path - QDBusConnection::sessionBus().registerService("org.kde.kdeconnect.battery"); - - qDebug() << "BatteryPackageInterface registered in dbus"; + NetworkPackage np(PACKAGE_TYPE_BATTERY); + np.set("request",true); + device()->sendPackage(np); } -bool BatteryPackageInterface::receivePackage(const Device& device, const NetworkPackage& np) +BatteryPlugin::~BatteryPlugin() { + batteryDbusInterface->deleteLater(); +} + +bool BatteryPlugin::receivePackage(const NetworkPackage& np) +{ + if (np.type() != PACKAGE_TYPE_BATTERY) return false; - QString id = device.id(); + bool isCharging = np.get("isCharging"); + int currentCharge = np.get("currentCharge"); - if (!mDevices.contains(id)) { + if (batteryDbusInterface->isCharging() != currentCharge || batteryDbusInterface->isCharging() != isCharging) { - //TODO: Avoid ugly const_cast - DeviceBatteryInformation* deviceInfo = new DeviceBatteryInformation(const_cast(&device)); + batteryDbusInterface->updateValues(isCharging, currentCharge); - mDevices[id] = deviceInfo; - - emit batteryDeviceAdded(device.id()); - connect(&device, SIGNAL(reachableStatusChanged()), this, SLOT(deviceReachableStatusChange())); - - qDebug() << "Added battery info to device" << id; + if (currentCharge == 14 && !isCharging) { + KNotification* notification = new KNotification("battery100"); + notification->setPixmap(KIcon("battery-040").pixmap(48, 48)); + notification->setComponentData(KComponentData("kdeconnect", "kdeconnect")); + notification->setTitle(device()->name() + ": low battery"); + notification->setText("Battery at 14%"); + } } - bool isCharging = np.get("isCharging"); - mDevices[id]->setCharging(isCharging); - - int currentCharge = np.get("currentCharge"); - mDevices[id]->setCharge(currentCharge); - return true; } -QStringList BatteryPackageInterface::getBatteryReportingDevices() -{ - return mDevices.keys(); -} - -void BatteryPackageInterface::deviceReachableStatusChange() -{ - Device* device = static_cast(sender()); - if (!device->reachable()) { - mDevices.remove(device->id()); - emit batteryDeviceLost(device->id()); - disconnect(device, SIGNAL(reachableStatusChanged()), this, SLOT(deviceReachableStatusChange())); - } -} - diff --git a/daemon/plugins/battery/batteryplugin.h b/daemon/plugins/battery/batteryplugin.h index 4d2fa8e91..ba7daae3d 100644 --- a/daemon/plugins/battery/batteryplugin.h +++ b/daemon/plugins/battery/batteryplugin.h @@ -18,38 +18,31 @@ * along with this program. If not, see . */ -#ifndef BATTERYPACKAGEINTERFACE_H -#define BATTERYPACKAGEINTERFACE_H +#ifndef BATTERYPLUGIN_H +#define BATTERYPLUGIN_H -#include - -#include "packageinterface.h" #include -#include "devicebatteryinformation_p.h" +#include -class BatteryPackageInterface - : public PackageInterface +#include "../kdeconnectplugin.h" + +class BatteryDbusInterface; + +class BatteryPlugin + : public KdeConnectPlugin { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.battery") public: - BatteryPackageInterface(QObject* parent); - virtual bool receivePackage(const Device&, const NetworkPackage& np); - -private: - QHash mDevices; + explicit BatteryPlugin(QObject *parent, const QVariantList &args); + virtual ~BatteryPlugin(); public Q_SLOTS: - Q_SCRIPTABLE QStringList getBatteryReportingDevices(); + virtual bool receivePackage(const NetworkPackage& np); -Q_SIGNALS: - Q_SCRIPTABLE void batteryDeviceAdded(const QString& id); - Q_SCRIPTABLE void batteryDeviceLost(const QString& id); - -public slots: - void deviceReachableStatusChange(); +private: + BatteryDbusInterface* batteryDbusInterface; }; #endif diff --git a/daemon/plugins/battery/kdeconnect_battery.desktop b/daemon/plugins/battery/kdeconnect_battery.desktop new file mode 100644 index 000000000..ef0fbcb41 --- /dev/null +++ b/daemon/plugins/battery/kdeconnect_battery.desktop @@ -0,0 +1,15 @@ +[Desktop Entry] +Encoding=UTF-8 +Type=Service +ServiceTypes=KdeConnect/Plugin +X-KDE-Library=kdeconnect_battery +X-KDE-PluginInfo-Author=Albert Vaca +X-KDE-PluginInfo-Email=albertvaka@gmail.com +X-KDE-PluginInfo-Name=kdeconnect_battery +X-KDE-PluginInfo-Version=0.1 +X-KDE-PluginInfo-Website=http://albertvaka.wordpress.com +X-KDE-PluginInfo-License=GPL +X-KDE-PluginInfo-EnabledByDefault=true +Icon=battery-100 +Name=Battery display +Comment=Show your phone battery next to your computer battery