diff --git a/core/backends/loopback/loopbacklinkprovider.cpp b/core/backends/loopback/loopbacklinkprovider.cpp index 5442c82b5..88eb22dc2 100644 --- a/core/backends/loopback/loopbacklinkprovider.cpp +++ b/core/backends/loopback/loopbacklinkprovider.cpp @@ -25,7 +25,6 @@ LoopbackLinkProvider::LoopbackLinkProvider() : identityPackage(PACKAGE_TYPE_IDENTITY) { - loopbackDeviceLink = 0; NetworkPackage::createIdentityPackage(&identityPackage); } @@ -55,7 +54,6 @@ void LoopbackLinkProvider::onStop() { if (loopbackDeviceLink) { delete loopbackDeviceLink; - loopbackDeviceLink = 0; } } diff --git a/core/backends/loopback/loopbacklinkprovider.h b/core/backends/loopback/loopbacklinkprovider.h index 9dcb5296e..b4190d1e2 100644 --- a/core/backends/loopback/loopbacklinkprovider.h +++ b/core/backends/loopback/loopbacklinkprovider.h @@ -23,6 +23,7 @@ #include "../linkprovider.h" #include "loopbackdevicelink.h" +#include class LoopbackLinkProvider : public LinkProvider @@ -40,7 +41,7 @@ public: virtual void onNetworkChange(); private: - LoopbackDeviceLink* loopbackDeviceLink; + QPointer loopbackDeviceLink; NetworkPackage identityPackage; }; diff --git a/core/daemon.cpp b/core/daemon.cpp index 1aa5ad69d..ec971665e 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -44,6 +44,7 @@ struct DaemonPrivate //Every known device QMap mDevices; + bool discoveryMode = false; }; Daemon* Daemon::instance() @@ -80,8 +81,8 @@ Daemon::Daemon(QObject *parent, bool testMode) Q_FOREACH (LinkProvider* a, d->mLinkProviders) { connect(a, SIGNAL(onConnectionReceived(NetworkPackage,DeviceLink*)), this, SLOT(onNewDeviceLink(NetworkPackage,DeviceLink*))); + a->onStart(); } - setDiscoveryEnabled(true); //Register on DBus QDBusConnection::sessionBus().registerService("org.kde.kdeconnect"); @@ -92,11 +93,36 @@ Daemon::Daemon(QObject *parent, bool testMode) void Daemon::setDiscoveryEnabled(bool b) { - Q_FOREACH (LinkProvider* a, d->mLinkProviders) { - if (b) - a->onStart(); - else - a->onStop(); +// qDebug() << "setting discover..." << b; + if (b == d->discoveryMode) + return; + + d->discoveryMode = b; + if (b) { + forceOnNetworkChange(); + } else { + cleanDevices(); + } +} + +bool Daemon::isDiscoveryEnabled() const +{ + return d->discoveryMode; +} + +void Daemon::removeDevice(Device* device) +{ + d->mDevices.remove(device->id()); + device->deleteLater(); + Q_EMIT deviceRemoved(device->id()); +} + +void Daemon::cleanDevices() +{ + Q_FOREACH(Device* device, d->mDevices) { + if (!device->isPaired()) { + removeDevice(device); + } } } @@ -134,31 +160,32 @@ void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* Q_EMIT deviceVisibilityChanged(id, true); } } else { + Device* device = new Device(this, identityPackage, dl); //qCDebug(KDECONNECT_CORE) << "It is a new device"; - Device* device = new Device(this, identityPackage, dl); - connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged())); - connect(device, SIGNAL(pairingChanged(bool)), this, SLOT(onDeviceStatusChanged())); - d->mDevices[id] = device; + if (d->discoveryMode && !device->isPaired()) { + device->deleteLater(); + } else { + connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged())); + connect(device, SIGNAL(pairingChanged(bool)), this, SLOT(onDeviceStatusChanged())); + d->mDevices[id] = device; - Q_EMIT deviceAdded(id); + Q_EMIT deviceAdded(id); + } } } void Daemon::onDeviceStatusChanged() { Device* device = (Device*)sender(); - QString id = device->id(); qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "status changed. Reachable:" << device->isReachable() << ". Paired: " << device->isPaired(); if (!device->isReachable() && !device->isPaired()) { qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name(); - d->mDevices.remove(id); - device->deleteLater(); - Q_EMIT deviceRemoved(id); + removeDevice(device); } else { - Q_EMIT deviceVisibilityChanged(id, device->isReachable()); + Q_EMIT deviceVisibilityChanged(device->id(), device->isReachable()); } } diff --git a/core/daemon.h b/core/daemon.h index 4ef241581..91cfc4855 100644 --- a/core/daemon.h +++ b/core/daemon.h @@ -37,6 +37,7 @@ class KDECONNECTCORE_EXPORT Daemon { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon") + Q_PROPERTY(bool discoveryEnabled READ isDiscoveryEnabled WRITE setDiscoveryEnabled) public: explicit Daemon(QObject *parent, bool testMode = false); @@ -49,8 +50,9 @@ public: */ static Daemon* instance(); - //After calling this, signal deviceDiscovered will be triggered for each device - Q_SCRIPTABLE void setDiscoveryEnabled(bool b); + bool isDiscoveryEnabled() const; + void setDiscoveryEnabled(bool b); + QList devicesList() const; virtual void requestPairing(Device *d) = 0; @@ -78,6 +80,9 @@ private Q_SLOTS: void onDeviceStatusChanged(); private: + void removeDevice(Device* d); + void cleanDevices(); + QScopedPointer d; }; diff --git a/core/device.cpp b/core/device.cpp index dc938e909..1d02f686c 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -93,7 +93,7 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin Device::~Device() { - + qDeleteAll(m_deviceLinks); } bool Device::hasPlugin(const QString& name) const diff --git a/kcm/kcm.cpp b/kcm/kcm.cpp index d79b4a23b..f8997f86a 100644 --- a/kcm/kcm.cpp +++ b/kcm/kcm.cpp @@ -104,6 +104,7 @@ KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&) connect(kcmUi->renameShow_button,SIGNAL(clicked()), this, SLOT(renameShow())); + daemon->setDiscoveryEnabled(true); } void KdeConnectKcm::renameShow() @@ -133,6 +134,7 @@ void KdeConnectKcm::setRenameMode(bool b) { KdeConnectKcm::~KdeConnectKcm() { + daemon->setDiscoveryEnabled(false); delete kcmUi; }