diff --git a/core/daemon.cpp b/core/daemon.cpp index 81084c299..9af4819dd 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -44,7 +44,7 @@ struct DaemonPrivate //Every known device QMap mDevices; - bool discoveryMode = false; + QSet mDiscoveryModeAcquisitions; }; Daemon* Daemon::instance() @@ -91,23 +91,26 @@ Daemon::Daemon(QObject *parent, bool testMode) qCDebug(KDECONNECT_CORE) << "KdeConnect daemon started"; } -void Daemon::setDiscoveryEnabled(bool b) +void Daemon::acquireDiscoveryMode(const QString &key) { -// qDebug() << "setting discover..." << b; - if (b == d->discoveryMode) - return; + bool oldState = d->mDiscoveryModeAcquisitions.isEmpty(); - d->discoveryMode = b; - if (b) { + d->mDiscoveryModeAcquisitions.insert(key); + + if (oldState != d->mDiscoveryModeAcquisitions.isEmpty()) { forceOnNetworkChange(); - } else { - cleanDevices(); } } -bool Daemon::isDiscoveryEnabled() const +void Daemon::releaseDiscoveryMode(const QString &key) { - return d->discoveryMode; + bool oldState = d->mDiscoveryModeAcquisitions.isEmpty(); + + d->mDiscoveryModeAcquisitions.remove(key); + + if (oldState != d->mDiscoveryModeAcquisitions.isEmpty()) { + cleanDevices(); + } } void Daemon::removeDevice(Device* device) @@ -165,7 +168,7 @@ void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* //we discard the connections that we created but it's not paired. //we keep the remotely initiated ones, since the remotes require them - if (!isDiscoveryEnabled() && !device->isPaired() && dl->connectionSource() == DeviceLink::ConnectionStarted::Locally) { + if (!isDiscoveringDevices() && !device->isPaired() && dl->connectionSource() == DeviceLink::ConnectionStarted::Locally) { device->deleteLater(); } else { connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged())); @@ -218,8 +221,12 @@ QList Daemon::devicesList() const return d->mDevices.values(); } +bool Daemon::isDiscoveringDevices() const +{ + return !d->mDiscoveryModeAcquisitions.isEmpty(); +} + Daemon::~Daemon() { } - diff --git a/core/daemon.h b/core/daemon.h index 91cfc4855..dca3e07d6 100644 --- a/core/daemon.h +++ b/core/daemon.h @@ -37,7 +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) + Q_PROPERTY(bool isDiscoveringDevices READ isDiscoveringDevices) public: explicit Daemon(QObject *parent, bool testMode = false); @@ -50,9 +50,6 @@ public: */ static Daemon* instance(); - bool isDiscoveryEnabled() const; - void setDiscoveryEnabled(bool b); - QList devicesList() const; virtual void requestPairing(Device *d) = 0; @@ -60,6 +57,8 @@ public: virtual QNetworkAccessManager* networkAccessManager(); public Q_SLOTS: + Q_SCRIPTABLE void acquireDiscoveryMode(const QString &id); + Q_SCRIPTABLE void releaseDiscoveryMode(const QString &id); Q_SCRIPTABLE void forceOnNetworkChange(); @@ -80,6 +79,7 @@ private Q_SLOTS: void onDeviceStatusChanged(); private: + bool isDiscoveringDevices() const; void removeDevice(Device* d); void cleanDevices(); diff --git a/interfaces/devicesmodel.cpp b/interfaces/devicesmodel.cpp index 9b30ce817..7b0fdbb77 100644 --- a/interfaces/devicesmodel.cpp +++ b/interfaces/devicesmodel.cpp @@ -34,6 +34,10 @@ Q_LOGGING_CATEGORY(KDECONNECT_INTERFACES, "kdeconnect.interfaces"); +static QString createId() { return QCoreApplication::instance()->applicationName()+QString::number(QCoreApplication::applicationPid()); } + +Q_GLOBAL_STATIC_WITH_ARGS(QString, s_keyId, (createId())); + DevicesModel::DevicesModel(QObject *parent) : QAbstractListModel(parent) , m_dbusInterface(new DaemonDbusInterface(this)) @@ -59,7 +63,8 @@ DevicesModel::DevicesModel(QObject *parent) connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &DevicesModel::refreshDeviceList); connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &DevicesModel::clearDevices); - refreshDeviceList(); + //refresh the view, acquireDiscoveryMode if necessary + setDisplayFilter(NoFilter); } QHash< int, QByteArray > DevicesModel::roleNames() const @@ -74,6 +79,7 @@ QHash< int, QByteArray > DevicesModel::roleNames() const DevicesModel::~DevicesModel() { + m_dbusInterface->releaseDiscoveryMode(*s_keyId); } int DevicesModel::rowForDevice(const QString& id) const @@ -147,6 +153,13 @@ int DevicesModel::displayFilter() const void DevicesModel::setDisplayFilter(int flags) { m_displayFilter = (StatusFilterFlag)flags; + + const bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable); + if (onlyReachable) + m_dbusInterface->releaseDiscoveryMode(*s_keyId); + else + m_dbusInterface->acquireDiscoveryMode(*s_keyId); + refreshDeviceList(); } diff --git a/kcm/kcm.cpp b/kcm/kcm.cpp index 82fcab4df..a2ac58089 100644 --- a/kcm/kcm.cpp +++ b/kcm/kcm.cpp @@ -103,8 +103,6 @@ KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&) this, SLOT(renameDone())); connect(kcmUi->renameShow_button,SIGNAL(clicked()), this, SLOT(renameShow())); - - daemon->setDiscoveryEnabled(true); } void KdeConnectKcm::renameShow() @@ -134,7 +132,6 @@ void KdeConnectKcm::setRenameMode(bool b) { KdeConnectKcm::~KdeConnectKcm() { - daemon->setDiscoveryEnabled(false); delete kcmUi; }