Migrated battery plugin to new format

Added notification when battery is low (untested!)
Added intial battery status request
This commit is contained in:
Albert Vaca 2013-08-14 00:03:46 +02:00
parent 16a18c3f30
commit d868211229
6 changed files with 175 additions and 72 deletions

View file

@ -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
)

View file

@ -0,0 +1,39 @@
/**
* 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/>.
*/
#include "batterydbusinterface.h"
#include <QDebug>
BatteryDbusInterface::BatteryDbusInterface(QObject *parent)
: QDBusAbstractAdaptor(parent)
{
}
void BatteryDbusInterface::updateValues(bool isCharging, int currentCharge)
{
mIsCharging = isCharging;
mCharge = currentCharge;
Q_EMIT chargingChange();
}

View file

@ -0,0 +1,51 @@
/**
* 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 BATTERYDBUSINTERFACE_H
#define BATTERYDBUSINTERFACE_H
#include <QDBusAbstractAdaptor>
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

View file

@ -18,67 +18,56 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "batterypackageinterface.h"
#include "batteryplugin.h"
#include <QDebug>
#include <kicon.h>
#include <KNotification>
#include <KIcon>
#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<bool>("isCharging");
int currentCharge = np.get<int>("currentCharge");
if (!mDevices.contains(id)) {
if (batteryDbusInterface->isCharging() != currentCharge || batteryDbusInterface->isCharging() != isCharging) {
//TODO: Avoid ugly const_cast
DeviceBatteryInformation* deviceInfo = new DeviceBatteryInformation(const_cast<Device*>(&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<bool>("isCharging");
mDevices[id]->setCharging(isCharging);
int currentCharge = np.get<int>("currentCharge");
mDevices[id]->setCharge(currentCharge);
return true;
}
QStringList BatteryPackageInterface::getBatteryReportingDevices()
{
return mDevices.keys();
}
void BatteryPackageInterface::deviceReachableStatusChange()
{
Device* device = static_cast<Device*>(sender());
if (!device->reachable()) {
mDevices.remove(device->id());
emit batteryDeviceLost(device->id());
disconnect(device, SIGNAL(reachableStatusChanged()), this, SLOT(deviceReachableStatusChange()));
}
}

View file

@ -18,38 +18,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BATTERYPACKAGEINTERFACE_H
#define BATTERYPACKAGEINTERFACE_H
#ifndef BATTERYPLUGIN_H
#define BATTERYPLUGIN_H
#include <knotification.h>
#include "packageinterface.h"
#include <QDBusAbstractAdaptor>
#include "devicebatteryinformation_p.h"
#include <KNotification>
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<QString,DeviceBatteryInformation*> 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

View file

@ -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