Code review

Fixes/adds some comments.
Proper use of some Qt API.

REVIEW: 113195
This commit is contained in:
Aleix Pol 2013-10-11 15:19:23 +02:00
parent 5119d17d6d
commit faa3daa585
15 changed files with 107 additions and 98 deletions

View file

@ -64,6 +64,10 @@ bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np
np.encrypt(key); np.encrypt(key);
int written = mSocket->write(np.serialize()); int written = mSocket->write(np.serialize());
//TODO: Actually detect if a package is received or not, now we keep TCP
//"ESTABLISHED" connections that look legit (return true when we use them),
//but that are actually broken
return (written != -1); return (written != -1);
} }
@ -76,6 +80,9 @@ bool LanDeviceLink::sendPackage(NetworkPackage& np)
} }
int written = mSocket->write(np.serialize()); int written = mSocket->write(np.serialize());
//TODO: Actually detect if a package is received or not, now we keep TCP
//"ESTABLISHED" connections that look legit (return true when we use them),
//but that are actually broken
return (written != -1); return (written != -1);
} }

View file

@ -20,36 +20,31 @@
#include "networkpackage.h" #include "networkpackage.h"
Device::Device(const QString& id) Device::Device(const QString& id)
: m_deviceId(id)
, m_pairStatus(Device::Paired)
, m_protocolVersion(NetworkPackage::ProtocolVersion) //We don't know it yet
{ {
m_deviceId = id;
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc"); KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
const KConfigGroup& data = config->group("trusted_devices").group(id); KConfigGroup data = config->group("trusted_devices").group(id);
m_deviceName = data.readEntry<QString>("deviceName", QString("unnamed")); m_deviceName = data.readEntry<QString>("deviceName", QString("unnamed"));
const QString& key = data.readEntry<QString>("publicKey",QString()); const QString& key = data.readEntry<QString>("publicKey",QString());
m_publicKey = QCA::RSAPublicKey::fromPEM(key); m_publicKey = QCA::RSAPublicKey::fromPEM(key);
m_pairStatus = Device::Paired;
m_protocolVersion = NetworkPackage::ProtocolVersion; //We don't know it yet
//Register in bus //Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
} }
Device::Device(const NetworkPackage& identityPackage, DeviceLink* dl) Device::Device(const NetworkPackage& identityPackage, DeviceLink* dl)
: m_deviceId(identityPackage.get<QString>("deviceId"))
, m_deviceName(identityPackage.get<QString>("deviceName"))
, m_pairStatus(Device::NotPaired)
, m_protocolVersion(identityPackage.get<int>("protocolVersion"))
{ {
m_deviceId = identityPackage.get<QString>("deviceId");
m_deviceName = identityPackage.get<QString>("deviceName");
m_protocolVersion = identityPackage.get<int>("protocolVersion");
addLink(identityPackage, dl); addLink(identityPackage, dl);
m_pairStatus = Device::NotPaired;
//Register in bus //Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
} }
@ -75,24 +70,23 @@ void Device::reloadPlugins()
if (isPaired() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices if (isPaired() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices
QString path = KStandardDirs().resourceDirs("config").first()+"kdeconnect/"; QString path = KGlobal::dirs()->findResource("config", "kdeconnect/"+id());
QMap<QString,QString> pluginStates = KSharedConfig::openConfig(path + id())->group("Plugins").entryMap(); KConfigGroup pluginStates = KSharedConfig::openConfig(path)->group("Plugins");
PluginLoader* loader = PluginLoader::instance(); PluginLoader* loader = PluginLoader::instance();
//Code borrowed from KWin //Code borrowed from KWin
foreach (const QString& pluginName, loader->getPluginList()) { foreach (const QString& pluginName, loader->getPluginList()) {
QString enabledKey = pluginName + QString::fromLatin1("Enabled");
const QString value = pluginStates.value(pluginName + QString::fromLatin1("Enabled"), QString()); bool isPluginEnabled = (pluginStates.hasKey(enabledKey) ? pluginStates.readEntry(enabledKey, false)
KPluginInfo info = loader->getPluginInfo(pluginName); : loader->getPluginInfo(pluginName).isPluginEnabledByDefault());
bool enabled = (value.isNull() ? info.isPluginEnabledByDefault() : QVariant(value).toBool());
if (enabled) { if (isPluginEnabled) {
KdeConnectPlugin* reusedPluginInstance = m_plugins.take(pluginName);
if (m_plugins.contains(pluginName)) { if (reusedPluginInstance) {
//Already loaded, reuse it //Already loaded, reuse it
newPluginMap[pluginName] = m_plugins[pluginName]; newPluginMap[pluginName] = reusedPluginInstance;
m_plugins.remove(pluginName);
} else { } else {
KdeConnectPlugin* plugin = loader->instantiatePluginForDevice(pluginName, this); KdeConnectPlugin* plugin = loader->instantiatePluginForDevice(pluginName, this);
@ -105,15 +99,13 @@ void Device::reloadPlugins()
} }
} }
//Erase all the plugins left in the original map (it means that we don't want //Erase all left plugins in the original map (meaning that we don't want
//them anymore, otherways they would have been moved to the newPluginMap) //them anymore, otherwise they would have been moved to the newPluginMap)
qDeleteAll(m_plugins); qDeleteAll(m_plugins);
m_plugins.clear();
m_plugins = newPluginMap; m_plugins = newPluginMap;
Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) { Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) {
plugin->connected(); plugin->connected();
} }
Q_EMIT pluginsChanged(); Q_EMIT pluginsChanged();
@ -122,17 +114,19 @@ void Device::reloadPlugins()
void Device::requestPair() void Device::requestPair()
{ {
if (m_pairStatus == Device::Paired) { switch(m_pairStatus) {
Q_EMIT pairingFailed(i18n("Already paired")); case Device::Paired:
return; Q_EMIT pairingFailed(i18n("Already paired"));
} return;
if (m_pairStatus == Device::Requested) { case Device::Requested:
Q_EMIT pairingFailed(i18n("Pairing already requested for this device")); Q_EMIT pairingFailed(i18n("Pairing already requested for this device"));
return; return;
} default:
if (!isReachable()) { if (!isReachable()) {
Q_EMIT pairingFailed(i18n("Device not reachable")); Q_EMIT pairingFailed(i18n("Device not reachable"));
return; return;
}
break;
} }
m_pairStatus = Device::Requested; m_pairStatus = Device::Requested;
@ -242,7 +236,7 @@ void Device::removeLink(DeviceLink* link)
//qDebug() << "RemoveLink" << m_deviceLinks.size() << "links remaining"; //qDebug() << "RemoveLink" << m_deviceLinks.size() << "links remaining";
if (m_deviceLinks.empty()) { if (m_deviceLinks.isEmpty()) {
reloadPlugins(); reloadPlugins();
Q_EMIT reachableStatusChanged(); Q_EMIT reachableStatusChanged();
} }
@ -252,17 +246,11 @@ bool Device::sendPackage(NetworkPackage& np)
{ {
if (np.type() != PACKAGE_TYPE_PAIR && isPaired()) { if (np.type() != PACKAGE_TYPE_PAIR && isPaired()) {
Q_FOREACH(DeviceLink* dl, m_deviceLinks) { Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
//TODO: Actually detect if a package is received or not, now we keep TCP
//"ESTABLISHED" connections that look legit (return true when we use them),
//but that are actually broken
if (dl->sendPackageEncrypted(m_publicKey, np)) return true; if (dl->sendPackageEncrypted(m_publicKey, np)) return true;
} }
} else { } else {
//Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data //Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data
Q_FOREACH(DeviceLink* dl, m_deviceLinks) { Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
//TODO: Actually detect if a package is received or not, now we keep TCP
//"ESTABLISHED" connections that look legit (return true when we use them),
//but that are actually broken
if (dl->sendPackage(np)) return true; if (dl->sendPackage(np)) return true;
} }
} }

View file

@ -49,10 +49,18 @@ class Device
}; };
public: public:
//Read device from KConfig, we already know it but we need to wait for a incoming devicelink to communicate /**
* Reads the @p device from KConfig
*
* We already know it but we need to wait for an incoming DeviceLink to communicate
*/
Device(const QString& id); Device(const QString& id);
//Device known via an incoming connection sent to us via a devicelink, we know everything but we don't trust it yet /**
* Device known via an incoming connection sent to us via a devicelink.
*
* We know everything but we don't trust it yet
*/
Device(const NetworkPackage& np, DeviceLink* dl); Device(const NetworkPackage& np, DeviceLink* dl);
virtual ~Device(); virtual ~Device();
@ -69,15 +77,17 @@ public:
Q_SCRIPTABLE bool pairRequested() const { return m_pairStatus==Device::Requested; } Q_SCRIPTABLE bool pairRequested() const { return m_pairStatus==Device::Requested; }
Q_SCRIPTABLE QStringList availableLinks() const; Q_SCRIPTABLE QStringList availableLinks() const;
Q_SCRIPTABLE bool isReachable() const { return !m_deviceLinks.empty(); } Q_SCRIPTABLE bool isReachable() const { return !m_deviceLinks.isEmpty(); }
Q_SCRIPTABLE QStringList loadedPlugins() const; Q_SCRIPTABLE QStringList loadedPlugins() const;
Q_SCRIPTABLE bool hasPlugin(const QString& name) const; Q_SCRIPTABLE bool hasPlugin(const QString& name) const;
//Send and receive
Q_SIGNALS: Q_SIGNALS:
///notifies about a @p np package that has just been received from the device
void receivedPackage(const NetworkPackage& np) const; void receivedPackage(const NetworkPackage& np) const;
public Q_SLOTS: public Q_SLOTS:
///sends a @p np package to the device
virtual bool sendPackage(NetworkPackage& np); virtual bool sendPackage(NetworkPackage& np);
//Dbus operations //Dbus operations
@ -103,7 +113,7 @@ Q_SIGNALS:
private: private:
//TODO: Replace device id by public key //TODO: Replace device id by public key
QString m_deviceId; const QString m_deviceId;
QString m_deviceName; QString m_deviceName;
QCA::PublicKey m_publicKey; QCA::PublicKey m_publicKey;
PairStatus m_pairStatus; PairStatus m_pairStatus;

View file

@ -21,16 +21,10 @@
#ifndef NETWORKPACKAGETYPES_H #ifndef NETWORKPACKAGETYPES_H
#define NETWORKPACKAGETYPES_H #define NETWORKPACKAGETYPES_H
#define PACKAGE_TYPE_IDENTITY QString("kdeconnect.identity") #define PACKAGE_TYPE_IDENTITY QLatin1String("kdeconnect.identity")
#define PACKAGE_TYPE_PAIR QString("kdeconnect.pair") #define PACKAGE_TYPE_PAIR QLatin1String("kdeconnect.pair")
#define PACKAGE_TYPE_ENCRYPTED QString("kdeconnect.encrypted") #define PACKAGE_TYPE_ENCRYPTED QLatin1String("kdeconnect.encrypted")
#define PACKAGE_TYPE_PING QString("kdeconnect.ping") #define PACKAGE_TYPE_TELEPHONY QLatin1String("kdeconnect.telephony")
#define PACKAGE_TYPE_NOTIFICATION QString("kdeconnect.notification") #define PACKAGE_TYPE_PING QLatin1String("kdeconnect.ping")
#define PACKAGE_TYPE_BATTERY QString("kdeconnect.battery")
#define PACKAGE_TYPE_TELEPHONY QString("kdeconnect.telephony")
#define PACKAGE_TYPE_CLIPBOARD QString("kdeconnect.clipboard")
#define PACKAGE_TYPE_MPRIS QString("kdeconnect.mpris")
#define PACKAGE_TYPE_SHARE QString("kdeconnect.share")
#endif // NETWORKPACKAGETYPES_H #endif // NETWORKPACKAGETYPES_H

View file

@ -69,6 +69,12 @@ bool BatteryPlugin::receivePackage(const NetworkPackage& np)
batteryDbusInterface->updateValues(isCharging, currentCharge); batteryDbusInterface->updateValues(isCharging, currentCharge);
//FIXME: Where's that 14 coming from?
//TODO: Decouple the protocol from Android
/*TODO: Look into the following android interfaces
android.intent.action.BATTERY_LOW
android.intent.action.BATTERY_OKAY
*/
if (currentCharge == 14 && !isCharging) { if (currentCharge == 14 && !isCharging) {
KNotification* notification = new KNotification("batteryLow"); KNotification* notification = new KNotification("batteryLow");
notification->setPixmap(KIcon("battery-040").pixmap(48, 48)); notification->setPixmap(KIcon("battery-040").pixmap(48, 48));

View file

@ -21,12 +21,10 @@
#ifndef BATTERYPLUGIN_H #ifndef BATTERYPLUGIN_H
#define BATTERYPLUGIN_H #define BATTERYPLUGIN_H
#include <QDBusAbstractAdaptor>
#include <KNotification>
#include "../kdeconnectplugin.h" #include "../kdeconnectplugin.h"
#define PACKAGE_TYPE_BATTERY QLatin1String("kdeconnect.battery")
class BatteryDbusInterface; class BatteryDbusInterface;
class BatteryPlugin class BatteryPlugin

View file

@ -29,15 +29,15 @@ K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_clipboard", "kdeconnect_cli
ClipboardPlugin::ClipboardPlugin(QObject *parent, const QVariantList &args) ClipboardPlugin::ClipboardPlugin(QObject *parent, const QVariantList &args)
: KdeConnectPlugin(parent, args) : KdeConnectPlugin(parent, args)
, clipboard(QApplication::clipboard())
, ignore_next_clipboard_change(false)
{ {
clipboard = QApplication::clipboard(); connect(clipboard, SIGNAL(changed(QClipboard::Mode)), this, SLOT(clipboardChanged(QClipboard::Mode)));
ignore_next_clipboard_change = false;
connect(clipboard,SIGNAL(changed(QClipboard::Mode)),this,SLOT(clipboardChanged(QClipboard::Mode)));
} }
void ClipboardPlugin::clipboardChanged(QClipboard::Mode mode) void ClipboardPlugin::clipboardChanged(QClipboard::Mode mode)
{ {
if (mode != QClipboard::QClipboard::Clipboard) return; if (mode != QClipboard::Clipboard) return;
if (ignore_next_clipboard_change) { if (ignore_next_clipboard_change) {
ignore_next_clipboard_change = false; ignore_next_clipboard_change = false;

View file

@ -28,6 +28,8 @@
#include "../../networkpackage.h" #include "../../networkpackage.h"
#include "../../device.h" #include "../../device.h"
#define PACKAGE_TYPE_CLIPBOARD QLatin1String("kdeconnect.clipboard")
class ClipboardPlugin class ClipboardPlugin
: public KdeConnectPlugin : public KdeConnectPlugin
{ {

View file

@ -41,9 +41,16 @@ public:
Device* device(); Device* device();
public Q_SLOTS: public Q_SLOTS:
//Returns true if it has handled the package in some way /**
//device.sendPackage can be used to send an answer back to the device * Returns true if it has handled the package in some way
* device.sendPackage can be used to send an answer back to the device
*/
virtual bool receivePackage(const NetworkPackage& np) = 0; virtual bool receivePackage(const NetworkPackage& np) = 0;
/**
* This method will be called when a device is connected to this computer.
* The plugin could be loaded already, but there is no guarantee we will be able to reach the device until this is called.
*/
virtual void connected() = 0; virtual void connected() = 0;
private: private:

View file

@ -26,6 +26,8 @@
#include "../kdeconnectplugin.h" #include "../kdeconnectplugin.h"
#define PACKAGE_TYPE_MPRIS QLatin1String("kdeconnect.mpris")
class MprisControlPlugin class MprisControlPlugin
: public KdeConnectPlugin : public KdeConnectPlugin
{ {

View file

@ -21,6 +21,7 @@
#include "notificationsdbusinterface.h" #include "notificationsdbusinterface.h"
#include "../../filetransferjob.h" #include "../../filetransferjob.h"
#include <notificationsplugin.h>
#include <QDebug> #include <QDebug>
#include <QDBusConnection> #include <QDBusConnection>
@ -67,7 +68,7 @@ void NotificationsDbusInterface::processPackage(const NetworkPackage& np)
//Do not show updates to existent notification nor answers to a initialization request //Do not show updates to existent notification nor answers to a initialization request
if (!mInternalIdToPublicId.contains(noti->internalId()) && !np.get<bool>("requestAnswer", false)) { if (!mInternalIdToPublicId.contains(noti->internalId()) && !np.get<bool>("requestAnswer", false)) {
KNotification* notification = new KNotification("notification"); KNotification* notification = new KNotification("notification", KNotification::CloseOnTimeout, this);
notification->setPixmap(KIcon("preferences-desktop-notification").pixmap(48, 48)); notification->setPixmap(KIcon("preferences-desktop-notification").pixmap(48, 48));
notification->setComponentData(KComponentData("kdeconnect", "kdeconnect")); notification->setComponentData(KComponentData("kdeconnect", "kdeconnect"));
notification->setTitle(mDevice->name()); notification->setTitle(mDevice->name());
@ -107,17 +108,14 @@ void NotificationsDbusInterface::removeNotification(const QString& internalId)
return; return;
} }
QString publicId = mInternalIdToPublicId[internalId]; QString publicId = mInternalIdToPublicId.take(internalId);
mInternalIdToPublicId.remove(internalId);
if (!mNotifications.contains(publicId)) { Notification* noti = mNotifications.take(publicId);
if (!noti) {
qDebug() << "Not found"; qDebug() << "Not found";
return; return;
} }
Notification* noti = mNotifications[publicId];
mNotifications.remove(publicId);
//Deleting the notification will unregister it automatically //Deleting the notification will unregister it automatically
//QDBusConnection::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId); //QDBusConnection::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId);
noti->deleteLater(); noti->deleteLater();

View file

@ -24,6 +24,7 @@
#include <knotification.h> #include <knotification.h>
#include "../kdeconnectplugin.h" #include "../kdeconnectplugin.h"
#define PACKAGE_TYPE_NOTIFICATION QLatin1String("kdeconnect.notification")
/* /*
* This class is just a proxy for NotificationsDbusInterface * This class is just a proxy for NotificationsDbusInterface

View file

@ -32,11 +32,8 @@ K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_pausemusic", "kdeconnect_pa
PauseMusicPlugin::PauseMusicPlugin(QObject* parent, const QVariantList& args) PauseMusicPlugin::PauseMusicPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args) : KdeConnectPlugin(parent, args)
, pauseWhen(PauseWhenRinging) //TODO: Be able to change this from plugin settings
{ {
//TODO: Be able to change this from plugin settings
pauseWhen = PauseWhenRinging;
} }
bool PauseMusicPlugin::receivePackage(const NetworkPackage& np) bool PauseMusicPlugin::receivePackage(const NetworkPackage& np)
@ -46,22 +43,19 @@ bool PauseMusicPlugin::receivePackage(const NetworkPackage& np)
return false; return false;
} }
switch(pauseWhen) {
if (pauseWhen == PauseWhenRinging) { case PauseWhenRinging:
if (np.get<QString>("event") != "ringing" && np.get<QString>("event") != "talking") {
if (np.get<QString>("event") != "ringing" && np.get<QString>("event") != "talking") { return true;
return true; }
} break;
case PauseWhenTalking:
} else if (pauseWhen == PauseWhenTalking) { if (np.get<QString>("event") != "talking") {
return true;
if (np.get<QString>("event") != "talking") { }
return true; break;
}
} }
bool pauseConditionFulfilled = !np.get<bool>("isCancel"); bool pauseConditionFulfilled = !np.get<bool>("isCancel");
if (pauseConditionFulfilled) { if (pauseConditionFulfilled) {

View file

@ -26,6 +26,8 @@
#include "../kdeconnectplugin.h" #include "../kdeconnectplugin.h"
#define PACKAGE_TYPE_SHARE QLatin1String("kdeconnect.share")
class ShareReceiverPlugin class ShareReceiverPlugin
: public KdeConnectPlugin : public KdeConnectPlugin
{ {

View file

@ -68,7 +68,7 @@ KNotification* TelephonyPlugin::createNotification(const NetworkPackage& np)
qDebug() << "Creating notification with type:" << type; qDebug() << "Creating notification with type:" << type;
KNotification* notification = new KNotification(type); //, KNotification::Persistent KNotification* notification = new KNotification(type, KNotification::CloseOnTimeout, this); //, KNotification::Persistent
notification->setPixmap(KIcon(icon).pixmap(48, 48)); notification->setPixmap(KIcon(icon).pixmap(48, 48));
notification->setComponentData(KComponentData("kdeconnect", "kdeconnect")); notification->setComponentData(KComponentData("kdeconnect", "kdeconnect"));
notification->setTitle(title); notification->setTitle(title);