Qt 6.x
The Qt SDK
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 &services, const QDBusConnection &c, QDBusServiceWatcher::WatchMode watchMode);
44
45 void addService(const QString &service);
46 void removeService(const QString &service);
47};
48
49void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner)
50{
52 emit q->serviceOwnerChanged(service, oldOwner, newOwner);
53 if (oldOwner.isEmpty())
54 emit q->serviceRegistered(service);
55 else if (newOwner.isEmpty())
56 emit q->serviceUnregistered(service);
57}
58
60 const QDBusConnection &c,
61 QDBusServiceWatcher::WatchMode wm)
62{
63 if (connection.isConnected()) {
64 // remove older rules
65 for (const QString &s : std::as_const(watchedServicesData.value()))
67 }
68
69 connection = c;
70 watchMode.setValueBypassingBindings(wm); // caller has to call notify()
71 watchedServicesData.setValueBypassingBindings(services); // caller has to call notify()
72
73 if (connection.isConnected()) {
74 // add new rules
75 for (const QString &s : std::as_const(watchedServicesData.value()))
77 }
78}
79
81{
83 if (d && d->shouldWatchService(service))
84 d->watchService(service, watchMode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
85}
86
88{
90 if (d && d->shouldWatchService(service))
91 d->unwatchService(service, watchMode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
92}
93
213 : QObject(*new QDBusServiceWatcherPrivate(QDBusConnection(QString()), WatchForOwnerChange), parent)
214{
215}
216
227{
228 d_func()->setConnection(QStringList() << service, connection, watchMode);
229}
230
236{
237}
238
245{
246 return d_func()->watchedServicesData;
247}
248
260{
262 d->watchedServicesData.removeBindingUnlessInWrapper();
263 if (services == d->watchedServicesData)
264 return;
265 d->setConnection(services, d->connection, d->watchMode);
266 d->watchedServicesData.notify();
267}
268
270{
272 return &d->watchedServicesData;
273}
274
283{
285 d->watchedServicesData.removeBindingUnlessInWrapper();
286 if (d->watchedServicesData.value().contains(newService))
287 return;
288 d->addService(newService);
289
290 auto templist = d->watchedServicesData.valueBypassingBindings();
291 templist << newService;
292 d->watchedServicesData.setValueBypassingBindings(templist);
293
294 d->watchedServicesData.notify();
295}
296
308{
310 d->watchedServicesData.removeBindingUnlessInWrapper();
311 d->removeService(service);
312 auto tempList = d->watchedServicesData.value();
313 bool result = tempList.removeOne(service);
314 if (result) {
315 d->watchedServicesData.setValueBypassingBindings(tempList);
316 d->watchedServicesData.notify();
317 return true;
318 } else {
319 // nothing changed
320 return false;
321 }
322}
323
324QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const
325{
326 return d_func()->watchMode;
327}
328
330{
331 return &d_func()->watchMode;
332}
333
335{
337 d->watchMode.removeBindingUnlessInWrapper();
338 if (mode == d->watchMode.value())
339 return;
340 d->setConnection(d->watchedServicesData, d->connection, mode);
341 d->watchMode.notify();
342}
343
350{
351 return d_func()->connection;
352}
353
367{
369 if (connection.name() == d->connection.name())
370 return;
371 d->setConnection(d->watchedServicesData, connection, d->watchMode);
372}
373
375
376#endif // QT_NO_DBUS
377
378#include "moc_qdbusservicewatcher.cpp"
std::vector< ObjCStrongReference< CBMutableService > > services
\inmodule QtCore
Definition qproperty.h:809
static QDBusConnectionPrivate * d(const QDBusConnection &q)
\inmodule QtDBus
QString name() const
void setWatchedServicesForwardToQ(const QStringList &list)
QDBusServiceWatcherPrivate(const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
void addService(const QString &service)
void setWatchModeForwardToQ(QDBusServiceWatcher::WatchMode mode)
Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QStringList, watchedServicesData, &QDBusServiceWatcherPrivate::setWatchedServicesForwardToQ) QDBusConnection connection
const QString const QString &void setConnection(const QStringList &services, const QDBusConnection &c, QDBusServiceWatcher::WatchMode watchMode)
Q_OBJECT_COMPAT_PROPERTY(QDBusServiceWatcherPrivate, QDBusServiceWatcher::WatchMode, watchMode, &QDBusServiceWatcherPrivate::setWatchModeForwardToQ) void _q_serviceOwnerChanged(const QString &
void removeService(const QString &service)
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:90
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
Combined button and popup list for selecting options.
DBusConnection * connection
#define SLOT(a)
Definition qobjectdefs.h:51
GLenum mode
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
#define emit
QList< int > list
[14]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent