Use acquire and release instead of a boolean property

This way we hope we won't end up without discovery if 2 instances need
discovery at the same time.

Reviewed by Albert Vaca
This commit is contained in:
Aleix Pol 2015-09-09 20:09:04 +02:00
parent 32daa4e51f
commit a9d2840664
4 changed files with 38 additions and 21 deletions

View file

@ -44,7 +44,7 @@ struct DaemonPrivate
//Every known device
QMap<QString, Device*> mDevices;
bool discoveryMode = false;
QSet<QString> 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<Device*> Daemon::devicesList() const
return d->mDevices.values();
}
bool Daemon::isDiscoveringDevices() const
{
return !d->mDiscoveryModeAcquisitions.isEmpty();
}
Daemon::~Daemon()
{
}

View file

@ -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<Device*> 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();

View file

@ -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();
}

View file

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