Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdbusservicewatcher.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "qdbusconnection.h"
6#include "qdbusutil_p.h"
7
8#include <QStringList>
9
10#include <private/qproperty_p.h>
11#include <private/qobject_p.h>
12#include <private/qdbusconnection_p.h>
13
14#ifndef QT_NO_DBUS
15
17
19{
20 Q_DECLARE_PUBLIC(QDBusServiceWatcher)
21public:
22 QDBusServiceWatcherPrivate(const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
23 : connection(c), watchMode(wm)
24 {
25 }
26
28 {
29 q_func()->setWatchedServices(list);
30 }
33
35 void setWatchModeForwardToQ(QDBusServiceWatcher::WatchMode mode)
36 {
37 q_func()->setWatchMode(mode);
38 }
39 Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QDBusServiceWatcher::WatchMode, watchMode,
41
42 void _q_serviceOwnerChanged(const QString &, const QString &, const QString &);
43 void setConnection(const QStringList &newServices, const QDBusConnection &newConnection,
44 QDBusServiceWatcher::WatchMode newMode);
45
46 void addService(const QString &service, QDBusServiceWatcher::WatchMode mode);
47 void removeService(const QString &service, QDBusServiceWatcher::WatchMode mode);
48};
49
50void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner)
51{
53 emit q->serviceOwnerChanged(service, oldOwner, newOwner);
54 if (oldOwner.isEmpty())
55 emit q->serviceRegistered(service);
56 else if (newOwner.isEmpty())
57 emit q->serviceUnregistered(service);
58}
59
61 const QDBusConnection &newConnection,
62 QDBusServiceWatcher::WatchMode newMode)
63{
64 const QStringList oldServices = watchedServicesData.valueBypassingBindings();
65 const QDBusServiceWatcher::WatchMode oldMode = watchMode.valueBypassingBindings();
66 if (connection.isConnected()) {
67 // remove older rules
68 for (const QString &s : oldServices)
69 removeService(s, oldMode);
70 }
71
72 connection = newConnection;
73 watchMode.setValueBypassingBindings(newMode); // caller has to call notify()
74 watchedServicesData.setValueBypassingBindings(newServices); // caller has to call notify()
75
76 if (connection.isConnected()) {
77 // add new rules
78 for (const QString &s : newServices)
79 addService(s, newMode);
80 }
81}
82
84 QDBusServiceWatcher::WatchMode mode)
85{
87 if (d && d->shouldWatchService(service))
88 d->watchService(service, mode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
89}
90
92 QDBusServiceWatcher::WatchMode mode)
93{
95 if (d && d->shouldWatchService(service))
96 d->unwatchService(service, mode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
97}
98
221
230QDBusServiceWatcher::QDBusServiceWatcher(const QString &service, const QDBusConnection &connection, WatchMode watchMode, QObject *parent)
231 : QObject(*new QDBusServiceWatcherPrivate(connection, watchMode), parent)
232{
233 d_func()->setConnection(QStringList() << service, connection, watchMode);
234}
235
243
250{
251 return d_func()->watchedServicesData;
252}
253
265{
267 d->watchedServicesData.removeBindingUnlessInWrapper();
268 if (services == d->watchedServicesData.valueBypassingBindings())
269 return;
270 // trigger watchMode re-evaluation, but only once for the setter
271 d->setConnection(services, d->connection, d->watchMode);
272 d->watchedServicesData.notify();
273}
274
276{
278 return &d->watchedServicesData;
279}
280
289{
291 d->watchedServicesData.removeBindingUnlessInWrapper();
292 auto services = d->watchedServicesData.valueBypassingBindings();
293 if (services.contains(newService))
294 return;
295 // re-evaluate watch mode
296 d->addService(newService, d->watchMode);
297
298 services << newService;
299 d->watchedServicesData.setValueBypassingBindings(services);
300
301 d->watchedServicesData.notify();
302}
303
315{
317 d->watchedServicesData.removeBindingUnlessInWrapper();
318 auto tempList = d->watchedServicesData.valueBypassingBindings();
319 const bool result = tempList.removeOne(service);
320 if (!result)
321 return false; // nothing changed
322
323 // re-evaluate watch mode
324 d->removeService(service, d->watchMode);
325 d->watchedServicesData.setValueBypassingBindings(tempList);
326 d->watchedServicesData.notify();
327 return true;
328}
329
330QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const
331{
332 return d_func()->watchMode;
333}
334
335QBindable<QDBusServiceWatcher::WatchMode> QDBusServiceWatcher::bindableWatchMode()
336{
337 return &d_func()->watchMode;
338}
339
341{
343 d->watchMode.removeBindingUnlessInWrapper();
344 if (mode == d->watchMode.valueBypassingBindings())
345 return;
346 // trigger watchedServicesData re-evaluation, but only once for the setter
347 d->setConnection(d->watchedServicesData, d->connection, mode);
348 d->watchMode.notify();
349}
350
357{
358 return d_func()->connection;
359}
360
374{
376 if (connection.name() == d->connection.name())
377 return;
378 d->setConnection(d->watchedServicesData, connection, d->watchMode);
379}
380
382
383#endif // QT_NO_DBUS
384
385#include "moc_qdbusservicewatcher.cpp"
std::vector< ObjCStrongReference< CBMutableService > > services
static QDBusConnectionPrivate * d(const QDBusConnection &q)
\inmodule QtDBus
QString name() const
const QString const QString &void setConnection(const QStringList &newServices, const QDBusConnection &newConnection, QDBusServiceWatcher::WatchMode newMode)
void setWatchedServicesForwardToQ(const QStringList &list)
QDBusServiceWatcherPrivate(const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
void setWatchModeForwardToQ(QDBusServiceWatcher::WatchMode mode)
Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QStringList, watchedServicesData, &QDBusServiceWatcherPrivate::setWatchedServicesForwardToQ) QDBusConnection connection
Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QDBusServiceWatcher::WatchMode, watchMode, &QDBusServiceWatcherPrivate::setWatchModeForwardToQ) void _q_serviceOwnerChanged(const QString &
void removeService(const QString &service, QDBusServiceWatcher::WatchMode mode)
void addService(const QString &service, QDBusServiceWatcher::WatchMode mode)
The QDBusServiceWatcher class allows the user to watch for a bus service change.
bool removeWatchedService(const QString &service)
Removes the service from the list of services being watched by this object.
QStringList watchedServices
the list of services watched.
~QDBusServiceWatcher()
Destroys the QDBusServiceWatcher object and releases any resources associated with it.
void setConnection(const QDBusConnection &connection)
Sets the D-Bus connection that this object is attached to be connection.
QBindable< QStringList > bindableWatchedServices()
QDBusConnection connection() const
Returns the QDBusConnection that this object is attached to.
WatchMode watchMode
the current watch mode for this QDBusServiceWatcher object.
QDBusServiceWatcher(QObject *parent=nullptr)
Creates a QDBusServiceWatcher object.
void addWatchedService(const QString &newService)
Adds newService to the list of services to be watched by this object.
void setWatchMode(WatchMode mode)
QBindable< WatchMode > bindableWatchMode()
void setWatchedServices(const QStringList &services)
Sets the list of D-Bus services being watched to be services.
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection * connection
#define SLOT(a)
Definition qobjectdefs.h:52
GLenum mode
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
#define emit
QList< int > list
[14]