Port of QDBusServiceWatcher::watchMode to new property system

Port watchMode in QDBusServiceWatcher to the new property
system. This is the easiest part.

Task-number: QTBUG-85520
Change-Id: I588212af205e77765862b8fecdbdcbf871717142
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Andreas Buhr 2021-01-04 16:28:45 +01:00
parent 673d9c34f4
commit 97a8727f0e
3 changed files with 63 additions and 6 deletions

View File

@ -43,6 +43,7 @@
#include <QStringList>
#include <private/qproperty_p.h>
#include <private/qobject_p.h>
#include <private/qdbusconnection_p.h>
@ -61,7 +62,12 @@ public:
QStringList servicesWatched;
QDBusConnection connection;
QDBusServiceWatcher::WatchMode watchMode;
void setWatchModeForwardToQ(QDBusServiceWatcher::WatchMode mode)
{
q_func()->setWatchMode(mode);
}
Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QDBusServiceWatcher::WatchMode, watchMode,
&QDBusServiceWatcherPrivate::setWatchModeForwardToQ)
void _q_serviceOwnerChanged(const QString &, const QString &, const QString &);
void setConnection(const QStringList &services, const QDBusConnection &c, QDBusServiceWatcher::WatchMode watchMode);
@ -80,7 +86,9 @@ void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(const QString &service,
emit q->serviceUnregistered(service);
}
void QDBusServiceWatcherPrivate::setConnection(const QStringList &s, const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
void QDBusServiceWatcherPrivate::setConnection(const QStringList &services,
const QDBusConnection &c,
QDBusServiceWatcher::WatchMode wm)
{
if (connection.isConnected()) {
// remove older rules
@ -89,8 +97,8 @@ void QDBusServiceWatcherPrivate::setConnection(const QStringList &s, const QDBus
}
connection = c;
watchMode = wm;
servicesWatched = s;
watchMode.setValueBypassingBindings(wm); // caller has to call notify()
servicesWatched = services;
if (connection.isConnected()) {
// add new rules
@ -318,12 +326,19 @@ QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const
return d_func()->watchMode;
}
QBindable<QDBusServiceWatcher::WatchMode> QDBusServiceWatcher::bindableWatchMode()
{
return &d_func()->watchMode;
}
void QDBusServiceWatcher::setWatchMode(WatchMode mode)
{
Q_D(QDBusServiceWatcher);
if (mode == d->watchMode)
d->watchMode.removeBindingUnlessInWrapper();
if (mode == d->watchMode.value())
return;
d->setConnection(d->servicesWatched, d->connection, mode);
d->watchMode.notify();
}
/*!

View File

@ -54,7 +54,7 @@ class Q_DBUS_EXPORT QDBusServiceWatcher: public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList watchedServices READ watchedServices WRITE setWatchedServices)
Q_PROPERTY(WatchMode watchMode READ watchMode WRITE setWatchMode)
Q_PROPERTY(WatchMode watchMode READ watchMode WRITE setWatchMode BINDABLE bindableWatchMode)
public:
enum WatchModeFlag {
WatchForRegistration = 0x01,
@ -76,6 +76,7 @@ public:
WatchMode watchMode() const;
void setWatchMode(WatchMode mode);
QBindable<WatchMode> bindableWatchMode();
QDBusConnection connection() const;
void setConnection(const QDBusConnection &connection);

View File

@ -54,6 +54,7 @@ private slots:
void disconnectedConnection();
void setConnection_data();
void setConnection();
void bindings();
private:
QString generateServiceName();
@ -428,5 +429,45 @@ void tst_QDBusServiceWatcher::setConnection()
QCOMPARE(spyU.at(0).at(0).toString(), watchedName);
}
void tst_QDBusServiceWatcher::bindings()
{
QString serviceName("normal");
QDBusConnection con("");
QVERIFY(!con.isConnected());
QDBusServiceWatcher watcher(serviceName, con, QDBusServiceWatcher::WatchForRegistration);
QProperty<QDBusServiceWatcher::WatchMode> follower;
int notificationCounter = 0;
auto connection = follower.subscribe([&]() { notificationCounter++; });
QCOMPARE(notificationCounter, 1);
follower.setBinding([&]() { return watcher.watchMode(); });
QCOMPARE(follower.value(), QDBusServiceWatcher::WatchForRegistration);
QCOMPARE(notificationCounter, 2);
watcher.setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
QCOMPARE(follower.value(), QDBusServiceWatcher::WatchForUnregistration);
QCOMPARE(notificationCounter, 3);
QProperty<QDBusServiceWatcher::WatchMode> leader(QDBusServiceWatcher::WatchForRegistration);
watcher.bindableWatchMode().setBinding([&]() { return leader.value(); });
QCOMPARE(follower.value(), QDBusServiceWatcher::WatchForRegistration);
QCOMPARE(notificationCounter, 4);
leader = QDBusServiceWatcher::WatchForUnregistration;
QCOMPARE(follower.value(), QDBusServiceWatcher::WatchForUnregistration);
QCOMPARE(notificationCounter, 5);
// check that setting a value breaks the binding
watcher.setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
QCOMPARE(notificationCounter, 5);
leader = QDBusServiceWatcher::WatchForRegistration;
QCOMPARE(follower.value(), QDBusServiceWatcher::WatchForUnregistration);
// check that setting the same value again does not trigger notification
watcher.setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
QCOMPARE(notificationCounter, 5);
}
QTEST_MAIN(tst_QDBusServiceWatcher)
#include "tst_qdbusservicewatcher.moc"