Added battery reporting via dbus interface

This commit is contained in:
Albert Vaca 2013-07-26 16:21:19 +02:00
parent d48b8f145b
commit 43932a4300
20 changed files with 250 additions and 38 deletions

View file

@ -13,6 +13,8 @@ set(kded_kdeconnect_SRCS
packageinterfaces/notificationpackageinterface.cpp
packageinterfaces/pausemusicpackageinterface.cpp
packageinterfaces/clipboardpackageinterface.cpp
packageinterfaces/batterypackageinterface.cpp
packageinterfaces/devicebatteryinformation_p.cpp
networkpackage.cpp
daemon.cpp
@ -31,30 +33,34 @@ target_link_libraries(kded_kdeconnect
${QJSON_LIBRARY}
)
qt4_generate_dbus_interface(
macro (generate_and_install_dbus_interface project_main_target header_file output_xml_file) #OPTIONS qdbus_options
QT4_EXTRACT_OPTIONS(extra_files_ignore qdbus_options ${ARGN})
qt4_generate_dbus_interface(${header_file} ${output_xml_file} OPTIONS ${qdbus_options})
add_dependencies(${project_main_target} ${output_xml_file})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${output_xml_file} DESTINATION ${DBUS_INTERFACES_INSTALL_DIR})
endmacro (generate_and_install_dbus_interface)
generate_and_install_dbus_interface(
kded_kdeconnect
daemon.h
org.kde.kdeconnect.xml
OPTIONS -a
)
qt4_generate_dbus_interface(
generate_and_install_dbus_interface(
kded_kdeconnect
device.h
org.kde.kdeconnect.device.xml
OPTIONS -a
)
add_custom_target(
org.kde.kdeconnect.xml
SOURCES ${CMAKE_CURRENT_BINARY_DIR}/org.kde.kdeconnect.xml
generate_and_install_dbus_interface(
kded_kdeconnect
packageinterfaces/devicebatteryinformation_p.h
org.kde.kdeconnect.device.battery.xml
OPTIONS -a
)
add_custom_target(
org.kde.kdeconnect.device.xml
SOURCES ${CMAKE_CURRENT_BINARY_DIR}/org.kde.kdeconnect.device.xml
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.kde.kdeconnect.xml DESTINATION ${DBUS_INTERFACES_INSTALL_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.kde.kdeconnect.device.xml DESTINATION ${DBUS_INTERFACES_INSTALL_DIR})
install(TARGETS kded_kdeconnect DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES kdeconnect.desktop DESTINATION ${SERVICES_INSTALL_DIR}/kded)
install(FILES kdeconnect.notifyrc DESTINATION ${DATA_INSTALL_DIR}/kdeconnect)

View file

@ -26,6 +26,7 @@
#include "packageinterfaces/notificationpackageinterface.h"
#include "packageinterfaces/pausemusicpackageinterface.h"
#include "packageinterfaces/clipboardpackageinterface.h"
#include "packageinterfaces/batterypackageinterface.h"
#include "linkproviders/avahitcplinkprovider.h"
#include "linkproviders/loopbacklinkprovider.h"
@ -56,7 +57,6 @@ Daemon::Daemon(QObject *parent, const QList<QVariant>&)
//Debugging
qDebug() << "Starting KdeConnect daemon";
config->group("devices").group("paired").group("fake_unreachable").writeEntry("name","Fake device");
//TODO: Do not hardcode the load of the package interfaces
//use: https://techbase.kde.org/Development/Tutorials/Services/Plugins
@ -64,6 +64,7 @@ Daemon::Daemon(QObject *parent, const QList<QVariant>&)
mPackageInterfaces.push_back(new NotificationPackageInterface());
mPackageInterfaces.push_back(new PauseMusicPackageInterface());
mPackageInterfaces.push_back(new ClipboardPackageInterface());
mPackageInterfaces.push_back(new BatteryPackageInterface());
//TODO: Do not hardcode the load of the device locators
//use: https://techbase.kde.org/Development/Tutorials/Services/Plugins

View file

@ -49,7 +49,7 @@ class Daemon
: public KDEDModule
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "kdeconnect.daemon")
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
public:
Daemon(QObject *parent, const QList<QVariant>&);

View file

@ -3,7 +3,7 @@
#include <ksharedconfig.h>
#include "devicelinks/devicelink.h"
#include "linkproviders/linkprovider.h"
#include "packageinterfaces/devicebatteryinformation_p.h"
#include <KConfigGroup>
#include <QDebug>
@ -13,7 +13,10 @@ Device::Device(const QString& id, const QString& name)
m_deviceName = name;
m_paired = true;
m_knownIdentiy = true;
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/Devices/"+id, this, QDBusConnection::ExportScriptableContents);
//Register in bus
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/devices/"+id, this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
}
Device::Device(const QString& id, const QString& name, DeviceLink* link)
@ -22,7 +25,9 @@ Device::Device(const QString& id, const QString& name, DeviceLink* link)
m_deviceName = name;
m_paired = false;
m_knownIdentiy = true;
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/Devices/"+id, this, QDBusConnection::ExportScriptableContents);
//Register in bus
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/devices/"+id, this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
addLink(link);
}

View file

@ -32,7 +32,9 @@ class DeviceLink;
class Device : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "kdeconnect.device")
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device")
Q_PROPERTY(QString id READ id)
Q_PROPERTY(QString name READ name)
public:
@ -46,6 +48,9 @@ public:
//(not supported yet, do we need it or we can rely on the device presenging itself?)
//Device(const QString& id, DeviceLink* dl);
QString id() const{ return m_deviceId; }
QString name() const { return m_deviceName; }
//Add and remove links
void addLink(DeviceLink*);
void removeLink(DeviceLink*);
@ -56,10 +61,8 @@ Q_SIGNALS:
public Q_SLOTS:
bool sendPackage(const NetworkPackage& np);
//Public dbus interface
//Public dbus operations
public Q_SLOTS:
Q_SCRIPTABLE QString id() const{ return m_deviceId; }
Q_SCRIPTABLE QString name() const { return m_deviceName; }
Q_SCRIPTABLE QStringList availableLinks() const;
Q_SCRIPTABLE bool paired() const { return m_paired; }
Q_SCRIPTABLE bool reachable() const { return !m_deviceLinks.empty(); }

View file

@ -39,12 +39,16 @@ void TcpDeviceLink::dataReceived()
{
qDebug() << "TcpDeviceLink dataReceived";
QByteArray a = mSocket->readAll();
QByteArray data = mSocket->readAll();
QList<QByteArray> packages = data.split('\n');
Q_FOREACH(const QByteArray& package, packages) {
qDebug() << a;
if (package.length() < 3) continue;
NetworkPackage np;
NetworkPackage::unserialize(a,&np);
NetworkPackage np;
NetworkPackage::unserialize(package,&np);
emit receivedPackage(np);
emit receivedPackage(np);
}
}

View file

@ -20,6 +20,7 @@
#include "linkprovider.h"
LinkProvider::LinkProvider() {
LinkProvider::LinkProvider()
{
//gcc complains if we don't add something to compile on a class with virtual functions
}

View file

@ -23,7 +23,6 @@
#include <kconfiggroup.h>
#include <qbytearray.h>
#include <qdatastream.h>
#include <KDebug>
#include <QHostInfo>
#include <sstream>
#include <string>
@ -66,7 +65,8 @@ QByteArray NetworkPackage::serialize() const
void NetworkPackage::unserialize(QByteArray a, NetworkPackage* np)
{
kDebug() << a;
qDebug() << "Unserialize:" << a;
//Json -> QVariant
QJson::Parser parser;
bool ok;

View file

@ -24,6 +24,7 @@
#define PACKAGE_TYPE_IDENTITY QString("kdeconnect.identity")
#define PACKAGE_TYPE_PING QString("kdeconnect.ping")
#define PACKAGE_TYPE_NOTIFICATION QString("kdeconnect.notification")
#define PACKAGE_TYPE_BATTERY QString("kdeconnect.battery")
#define PACKAGE_TYPE_CALL QString("kdeconnect.call")
#define PACKAGE_TYPE_CLIPBOARD QString("kdeconnect.clipboard")

View file

@ -0,0 +1,57 @@
/**
* 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 "batterypackageinterface.h"
#include <QDebug>
#include <kicon.h>
BatteryPackageInterface::BatteryPackageInterface()
{
//TODO: Get initial state of all devices
}
bool BatteryPackageInterface::receivePackage(const Device& device, const NetworkPackage& np)
{
if (np.type() != PACKAGE_TYPE_BATTERY) return false;
QString id = device.id();
if (!devices.contains(id)) {
//TODO: Avoid ugly const_cast
DeviceBatteryInformation* deviceInfo = new DeviceBatteryInformation(const_cast<Device*>(&device));
devices[id] = deviceInfo;
qDebug() << "Added battery info to device" << id;
}
bool isCharging = np.get<bool>("isCharging");
devices[id]->setCharging(isCharging);
int currentCharge = np.get<int>("currentCharge");
devices[id]->setCharge(currentCharge);
return true;
}

View file

@ -0,0 +1,43 @@
/**
* 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 BATTERYPACKAGEINTERFACE_H
#define BATTERYPACKAGEINTERFACE_H
#include <knotification.h>
#include "packageinterface.h"
#include "devicebatteryinformation_p.h"
class BatteryPackageInterface
: public PackageInterface
{
public:
BatteryPackageInterface();
virtual bool receivePackage(const Device&, const NetworkPackage& np);
private:
QHash<QString,DeviceBatteryInformation*> devices;
};
#endif

View file

@ -24,7 +24,8 @@
#include <KDebug>
#include <QApplication>
ClipboardPackageInterface::ClipboardPackageInterface() {
ClipboardPackageInterface::ClipboardPackageInterface()
{
clipboard = QApplication::clipboard();
ignore_next_clipboard_change = false;
connect(clipboard,SIGNAL(changed(QClipboard::Mode)),this,SLOT(clipboardChanged(QClipboard::Mode)));

View file

@ -0,0 +1,27 @@
/**
* 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 "devicebatteryinformation_p.h"
DeviceBatteryInformation::DeviceBatteryInformation(QObject* parent)
: QDBusAbstractAdaptor(parent)
{
}

View file

@ -0,0 +1,52 @@
/**
* 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 DEVICEBATTERYINFORMATION_H
#define DEVICEBATTERYINFORMATION_H
#include <QObject>
#include <QDBusAbstractAdaptor>
class DeviceBatteryInformation
: public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.battery")
Q_PROPERTY( int charge READ charge )
Q_PROPERTY( bool isCharging READ isCharging NOTIFY chargingChange )
public:
DeviceBatteryInformation(QObject* parent = 0);
int charge() { return mCharge; }
void setCharge(int charge) { mCharge = charge; }
bool isCharging() { return mIsCharging; }
void setCharging(bool isCharging) { mIsCharging = isCharging; }
private:
bool mIsCharging;
int mCharge;
Q_SIGNALS:
Q_SCRIPTABLE void chargingChange(bool charging);
};
#endif //DEVICEBATTERYINFORMATION_H

View file

@ -23,7 +23,8 @@
#include <QDebug>
#include <kicon.h>
KNotification* NotificationPackageInterface::createNotification(const QString& deviceName, const NetworkPackage& np) {
KNotification* NotificationPackageInterface::createNotification(const QString& deviceName, const NetworkPackage& np)
{
QString npType = np.get<QString>("notificationType");
@ -71,7 +72,8 @@ KNotification* NotificationPackageInterface::createNotification(const QString& d
}
bool NotificationPackageInterface::receivePackage(const Device& device, const NetworkPackage& np) {
bool NotificationPackageInterface::receivePackage(const Device& device, const NetworkPackage& np)
{
if (np.type() != PACKAGE_TYPE_NOTIFICATION) return false;

View file

@ -20,6 +20,7 @@
#include "packageinterface.h"
PackageInterface::PackageInterface() {
PackageInterface::PackageInterface()
{
//gcc complains if we don't add something to compile on a class with virtual functions
}

View file

@ -23,7 +23,8 @@
#include <KDebug>
#include <kicon.h>
bool PingPackageInterface::receivePackage(const Device& device, const NetworkPackage& np) {
bool PingPackageInterface::receivePackage(const Device& device, const NetworkPackage& np)
{
if (np.type() != PACKAGE_TYPE_PING) return false;

View file

@ -31,6 +31,7 @@ target_link_libraries(kcm_kdeconnect
add_dependencies(kcm_kdeconnect
org.kde.kdeconnect.xml
org.kde.kdeconnect.device.xml
org.kde.kdeconnect.device.battery.xml
)
install(TARGETS kcm_kdeconnect DESTINATION ${PLUGIN_INSTALL_DIR})

View file

@ -21,13 +21,13 @@
#include "dbusinterfaces.h"
DaemonDbusInterface::DaemonDbusInterface(QObject* parent)
: KdeconnectDaemonInterface("org.kde.kdeconnect", "/modules/kdeconnect", QDBusConnection::sessionBus(), parent)
: OrgKdeKdeconnectDaemonInterface("org.kde.kdeconnect", "/modules/kdeconnect", QDBusConnection::sessionBus(), parent)
{
}
DeviceDbusInterface::DeviceDbusInterface(const QString& id, QObject* parent)
: KdeconnectDeviceInterface("org.kde.kdeconnect", "/modules/kdeconnect/Devices/"+id, QDBusConnection::sessionBus(), parent)
: OrgKdeKdeconnectDeviceInterface("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+id, QDBusConnection::sessionBus(), parent)
{
}

View file

@ -24,8 +24,12 @@
#ifndef DbusInterfaces_H_
#define DbusInterfaces_H_
/**
* Using these "proxy" classes just in case we need to rename the
* interface, so we can change the class name in a single place.
*/
class DaemonDbusInterface
: public KdeconnectDaemonInterface
: public OrgKdeKdeconnectDaemonInterface
{
Q_OBJECT
public:
@ -33,7 +37,9 @@ public:
};
class DeviceDbusInterface : public KdeconnectDeviceInterface{
class DeviceDbusInterface
: public OrgKdeKdeconnectDeviceInterface
{
Q_OBJECT
public:
DeviceDbusInterface(const QString& id, QObject* parent);