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:
parent
673d9c34f4
commit
97a8727f0e
@ -43,6 +43,7 @@
|
|||||||
|
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
|
#include <private/qproperty_p.h>
|
||||||
#include <private/qobject_p.h>
|
#include <private/qobject_p.h>
|
||||||
#include <private/qdbusconnection_p.h>
|
#include <private/qdbusconnection_p.h>
|
||||||
|
|
||||||
@ -61,7 +62,12 @@ public:
|
|||||||
|
|
||||||
QStringList servicesWatched;
|
QStringList servicesWatched;
|
||||||
QDBusConnection connection;
|
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 _q_serviceOwnerChanged(const QString &, const QString &, const QString &);
|
||||||
void setConnection(const QStringList &services, const QDBusConnection &c, QDBusServiceWatcher::WatchMode watchMode);
|
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);
|
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()) {
|
if (connection.isConnected()) {
|
||||||
// remove older rules
|
// remove older rules
|
||||||
@ -89,8 +97,8 @@ void QDBusServiceWatcherPrivate::setConnection(const QStringList &s, const QDBus
|
|||||||
}
|
}
|
||||||
|
|
||||||
connection = c;
|
connection = c;
|
||||||
watchMode = wm;
|
watchMode.setValueBypassingBindings(wm); // caller has to call notify()
|
||||||
servicesWatched = s;
|
servicesWatched = services;
|
||||||
|
|
||||||
if (connection.isConnected()) {
|
if (connection.isConnected()) {
|
||||||
// add new rules
|
// add new rules
|
||||||
@ -318,12 +326,19 @@ QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const
|
|||||||
return d_func()->watchMode;
|
return d_func()->watchMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QBindable<QDBusServiceWatcher::WatchMode> QDBusServiceWatcher::bindableWatchMode()
|
||||||
|
{
|
||||||
|
return &d_func()->watchMode;
|
||||||
|
}
|
||||||
|
|
||||||
void QDBusServiceWatcher::setWatchMode(WatchMode mode)
|
void QDBusServiceWatcher::setWatchMode(WatchMode mode)
|
||||||
{
|
{
|
||||||
Q_D(QDBusServiceWatcher);
|
Q_D(QDBusServiceWatcher);
|
||||||
if (mode == d->watchMode)
|
d->watchMode.removeBindingUnlessInWrapper();
|
||||||
|
if (mode == d->watchMode.value())
|
||||||
return;
|
return;
|
||||||
d->setConnection(d->servicesWatched, d->connection, mode);
|
d->setConnection(d->servicesWatched, d->connection, mode);
|
||||||
|
d->watchMode.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -54,7 +54,7 @@ class Q_DBUS_EXPORT QDBusServiceWatcher: public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(QStringList watchedServices READ watchedServices WRITE setWatchedServices)
|
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:
|
public:
|
||||||
enum WatchModeFlag {
|
enum WatchModeFlag {
|
||||||
WatchForRegistration = 0x01,
|
WatchForRegistration = 0x01,
|
||||||
@ -76,6 +76,7 @@ public:
|
|||||||
|
|
||||||
WatchMode watchMode() const;
|
WatchMode watchMode() const;
|
||||||
void setWatchMode(WatchMode mode);
|
void setWatchMode(WatchMode mode);
|
||||||
|
QBindable<WatchMode> bindableWatchMode();
|
||||||
|
|
||||||
QDBusConnection connection() const;
|
QDBusConnection connection() const;
|
||||||
void setConnection(const QDBusConnection &connection);
|
void setConnection(const QDBusConnection &connection);
|
||||||
|
@ -54,6 +54,7 @@ private slots:
|
|||||||
void disconnectedConnection();
|
void disconnectedConnection();
|
||||||
void setConnection_data();
|
void setConnection_data();
|
||||||
void setConnection();
|
void setConnection();
|
||||||
|
void bindings();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString generateServiceName();
|
QString generateServiceName();
|
||||||
@ -428,5 +429,45 @@ void tst_QDBusServiceWatcher::setConnection()
|
|||||||
QCOMPARE(spyU.at(0).at(0).toString(), watchedName);
|
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)
|
QTEST_MAIN(tst_QDBusServiceWatcher)
|
||||||
#include "tst_qdbusservicewatcher.moc"
|
#include "tst_qdbusservicewatcher.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user