From dd24b61d355cad2cbd29c82fc30b2335b059a187 Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Mon, 26 Apr 2021 13:08:44 +0200 Subject: Port QDBusServiceWatcher::watchedServices to bindable properties Fixes: QTBUG-92993 Change-Id: I379c67c75bc536e387889de5303b01aef9399fcd Reviewed-by: Ivan Solovev Reviewed-by: Fabian Kosmale Reviewed-by: Sona Kurazyan --- src/dbus/qdbusservicewatcher.cpp | 58 ++++++++++++++++++++++++++++++++-------- src/dbus/qdbusservicewatcher.h | 4 ++- 2 files changed, 50 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/dbus/qdbusservicewatcher.cpp b/src/dbus/qdbusservicewatcher.cpp index bd2c4fdcc88..47cb4441c31 100644 --- a/src/dbus/qdbusservicewatcher.cpp +++ b/src/dbus/qdbusservicewatcher.cpp @@ -60,7 +60,13 @@ public: { } - QStringList servicesWatched; + void setWatchedServicesForwardToQ(const QStringList &list) + { + q_func()->setWatchedServices(list); + } + Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QStringList, watchedServicesData, + &QDBusServiceWatcherPrivate::setWatchedServicesForwardToQ) + QDBusConnection connection; void setWatchModeForwardToQ(QDBusServiceWatcher::WatchMode mode) { @@ -92,17 +98,17 @@ void QDBusServiceWatcherPrivate::setConnection(const QStringList &services, { if (connection.isConnected()) { // remove older rules - for (const QString &s : qAsConst(servicesWatched)) + for (const QString &s : qAsConst(watchedServicesData.value())) removeService(s); } connection = c; watchMode.setValueBypassingBindings(wm); // caller has to call notify() - servicesWatched = services; + watchedServicesData.setValueBypassingBindings(services); // caller has to call notify() if (connection.isConnected()) { // add new rules - for (const QString &s : qAsConst(servicesWatched)) + for (const QString &s : qAsConst(watchedServicesData.value())) addService(s); } } @@ -273,7 +279,7 @@ QDBusServiceWatcher::~QDBusServiceWatcher() */ QStringList QDBusServiceWatcher::watchedServices() const { - return d_func()->servicesWatched; + return d_func()->watchedServicesData; } /*! @@ -283,27 +289,45 @@ QStringList QDBusServiceWatcher::watchedServices() const watching services and adding new ones. This is an expensive operation and should be avoided, if possible. Instead, use addWatchedService() and removeWatchedService() if you can to manipulate entries in the list. + + Removes any existing binding of watchedServices. */ void QDBusServiceWatcher::setWatchedServices(const QStringList &services) { Q_D(QDBusServiceWatcher); - if (services == d->servicesWatched) + d->watchedServicesData.removeBindingUnlessInWrapper(); + if (services == d->watchedServicesData) return; d->setConnection(services, d->connection, d->watchMode); + d->watchedServicesData.notify(); +} + +QBindable QDBusServiceWatcher::bindableWatchedServices() +{ + Q_D(QDBusServiceWatcher); + return &d->watchedServicesData; } /*! Adds \a newService to the list of services to be watched by this object. This function is more efficient than setWatchedServices() and should be used whenever possible to add services. + + Removes any existing binding of watchedServices. */ void QDBusServiceWatcher::addWatchedService(const QString &newService) { Q_D(QDBusServiceWatcher); - if (d->servicesWatched.contains(newService)) + d->watchedServicesData.removeBindingUnlessInWrapper(); + if (d->watchedServicesData.value().contains(newService)) return; d->addService(newService); - d->servicesWatched << newService; + + auto templist = d->watchedServicesData.valueBypassingBindings(); + templist << newService; + d->watchedServicesData.setValueBypassingBindings(templist); + + d->watchedServicesData.notify(); } /*! @@ -312,13 +336,25 @@ void QDBusServiceWatcher::addWatchedService(const QString &newService) still be signals pending delivery about \a service. Those signals will still be emitted whenever the D-Bus messages are processed. + Removes any existing binding of watchedServices. + This function returns \c true if any services were removed. */ bool QDBusServiceWatcher::removeWatchedService(const QString &service) { Q_D(QDBusServiceWatcher); + d->watchedServicesData.removeBindingUnlessInWrapper(); d->removeService(service); - return d->servicesWatched.removeOne(service); + auto tempList = d->watchedServicesData.value(); + bool result = tempList.removeOne(service); + if (result) { + d->watchedServicesData.setValueBypassingBindings(tempList); + d->watchedServicesData.notify(); + return true; + } else { + // nothing changed + return false; + } } QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const @@ -337,7 +373,7 @@ void QDBusServiceWatcher::setWatchMode(WatchMode mode) d->watchMode.removeBindingUnlessInWrapper(); if (mode == d->watchMode.value()) return; - d->setConnection(d->servicesWatched, d->connection, mode); + d->setConnection(d->watchedServicesData, d->connection, mode); d->watchMode.notify(); } @@ -368,7 +404,7 @@ void QDBusServiceWatcher::setConnection(const QDBusConnection &connection) Q_D(QDBusServiceWatcher); if (connection.name() == d->connection.name()) return; - d->setConnection(d->servicesWatched, connection, d->watchMode); + d->setConnection(d->watchedServicesData, connection, d->watchMode); } QT_END_NAMESPACE diff --git a/src/dbus/qdbusservicewatcher.h b/src/dbus/qdbusservicewatcher.h index b9d0679c3a4..2d5720d63b0 100644 --- a/src/dbus/qdbusservicewatcher.h +++ b/src/dbus/qdbusservicewatcher.h @@ -53,7 +53,8 @@ class QDBusServiceWatcherPrivate; class Q_DBUS_EXPORT QDBusServiceWatcher: public QObject { Q_OBJECT - Q_PROPERTY(QStringList watchedServices READ watchedServices WRITE setWatchedServices) + Q_PROPERTY(QStringList watchedServices READ watchedServices WRITE setWatchedServices + BINDABLE bindableWatchedServices) Q_PROPERTY(WatchMode watchMode READ watchMode WRITE setWatchMode BINDABLE bindableWatchMode) public: enum WatchModeFlag { @@ -73,6 +74,7 @@ public: void setWatchedServices(const QStringList &services); void addWatchedService(const QString &newService); bool removeWatchedService(const QString &service); + QBindable bindableWatchedServices(); WatchMode watchMode() const; void setWatchMode(WatchMode mode); -- cgit v1.2.3