1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtDBus module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdbusservicewatcher.h"
#include "qdbusconnection.h"
#include "qdbusutil_p.h"
#include <QStringList>
#include <private/qproperty_p.h>
#include <private/qobject_p.h>
#include <private/qdbusconnection_p.h>
#ifndef QT_NO_DBUS
QT_BEGIN_NAMESPACE
class QDBusServiceWatcherPrivate: public QObjectPrivate
{
Q_DECLARE_PUBLIC(QDBusServiceWatcher)
public:
QDBusServiceWatcherPrivate(const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
: connection(c), watchMode(wm)
{
}
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)
{
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);
void addService(const QString &service);
void removeService(const QString &service);
};
void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner)
{
Q_Q(QDBusServiceWatcher);
emit q->serviceOwnerChanged(service, oldOwner, newOwner);
if (oldOwner.isEmpty())
emit q->serviceRegistered(service);
else if (newOwner.isEmpty())
emit q->serviceUnregistered(service);
}
void QDBusServiceWatcherPrivate::setConnection(const QStringList &services,
const QDBusConnection &c,
QDBusServiceWatcher::WatchMode wm)
{
if (connection.isConnected()) {
// remove older rules
for (const QString &s : qAsConst(watchedServicesData.value()))
removeService(s);
}
connection = c;
watchMode.setValueBypassingBindings(wm); // caller has to call notify()
watchedServicesData.setValueBypassingBindings(services); // caller has to call notify()
if (connection.isConnected()) {
// add new rules
for (const QString &s : qAsConst(watchedServicesData.value()))
addService(s);
}
}
void QDBusServiceWatcherPrivate::addService(const QString &service)
{
QDBusConnectionPrivate *d = QDBusConnectionPrivate::d(connection);
if (d && d->shouldWatchService(service))
d->watchService(service, watchMode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
}
void QDBusServiceWatcherPrivate::removeService(const QString &service)
{
QDBusConnectionPrivate *d = QDBusConnectionPrivate::d(connection);
if (d && d->shouldWatchService(service))
d->unwatchService(service, watchMode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
}
/*!
\class QDBusServiceWatcher
\since 4.6
\inmodule QtDBus
\brief The QDBusServiceWatcher class allows the user to watch for a bus service change.
A QDBusServiceWatcher object can be used to notify the application about
an ownership change of a service name on the bus. It has three watch
modes:
\list
\li Watching for service registration only.
\li Watching for service unregistration only.
\li Watching for any kind of service ownership change (the default mode).
\endlist
Besides being created or deleted, services may change owners without a
unregister/register operation happening. So the serviceRegistered()
and serviceUnregistered() signals may not be emitted if that
happens.
This class is more efficient than using the
QDBusConnectionInterface::serviceOwnerChanged() signal because it allows
one to receive only the signals for which the class is interested in.
Ending a service name with the character '*' will match all service names
within the specified namespace.
For example "com.example.backend1*" will match
\list
\li com.example.backend1
\li com.example.backend1.foo
\li com.example.backend1.foo.bar
\endlist
Substrings in the same domain will not be matched, i.e "com.example.backend12".
\sa QDBusConnection
*/
/*!
\enum QDBusServiceWatcher::WatchModeFlag
QDBusServiceWatcher supports three different watch modes, which are configured by this flag:
\value WatchForRegistration watch for service registration only, ignoring
any signals related to other service ownership change.
\value WatchForUnregistration watch for service unregistration only,
ignoring any signals related to other service ownership change.
\value WatchForOwnerChange watch for any kind of service ownership
change.
*/
/*!
\property QDBusServiceWatcher::watchMode
\brief the current watch mode for this QDBusServiceWatcher object.
The default value for this property is
QDBusServiceWatcher::WatchForOwnershipChange.
*/
/*!
\property QDBusServiceWatcher::watchedServices
\brief the list of services watched.
\note Modifying this list with setServicesWatched() is an expensive
operation. If you can, prefer to change it by way of addWatchedService()
and removeWatchedService().
*/
/*!
\fn void QDBusServiceWatcher::serviceRegistered(const QString &serviceName)
This signal is emitted whenever this object detects that the service \a
serviceName became available on the bus.
\sa serviceUnregistered(), serviceOwnerChanged()
*/
/*!
\fn void QDBusServiceWatcher::serviceUnregistered(const QString &serviceName)
This signal is emitted whenever this object detects that the service \a
serviceName was unregistered from the bus and is no longer available.
\sa serviceRegistered(), serviceOwnerChanged()
*/
/*!
\fn void QDBusServiceWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
This signal is emitted whenever this object detects that there was a
service ownership change relating to the \a serviceName service. The \a
oldOwner parameter contains the old owner name and \a newOwner is the new
owner. Both \a oldOwner and \a newOwner are unique connection names.
Note that this signal is also emitted whenever the \a serviceName service
was registered or unregistered. If it was registered, \a oldOwner will
contain an empty string, whereas if it was unregistered, \a newOwner will
contain an empty string.
If you need only to find out if the service is registered or unregistered
only, without being notified that the ownership changed, consider using
the specific modes for those operations. This class is more efficient if
you use the more specific modes.
\sa serviceRegistered(), serviceUnregistered()
*/
/*!
Creates a QDBusServiceWatcher object. Note that until you set a
connection with setConnection(), this object will not emit any signals.
The \a parent parameter is passed to QObject to set the parent of this
object.
*/
QDBusServiceWatcher::QDBusServiceWatcher(QObject *parent)
: QObject(*new QDBusServiceWatcherPrivate(QDBusConnection(QString()), WatchForOwnerChange), parent)
{
}
/*!
Creates a QDBusServiceWatcher object and attaches it to the \a connection
connection. Also, this function immediately starts watching for \a
watchMode changes to service \a service.
The \a parent parameter is passed to QObject to set the parent of this
object.
*/
QDBusServiceWatcher::QDBusServiceWatcher(const QString &service, const QDBusConnection &connection, WatchMode watchMode, QObject *parent)
: QObject(*new QDBusServiceWatcherPrivate(connection, watchMode), parent)
{
d_func()->setConnection(QStringList() << service, connection, watchMode);
}
/*!
Destroys the QDBusServiceWatcher object and releases any resources
associated with it.
*/
QDBusServiceWatcher::~QDBusServiceWatcher()
{
}
/*!
Returns the list of D-Bus services that are being watched.
\sa setWatchedServices()
*/
QStringList QDBusServiceWatcher::watchedServices() const
{
return d_func()->watchedServicesData;
}
/*!
Sets the list of D-Bus services being watched to be \a services.
Note that setting the entire list means removing all previous rules for
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);
d->watchedServicesData.removeBindingUnlessInWrapper();
if (services == d->watchedServicesData)
return;
d->setConnection(services, d->connection, d->watchMode);
d->watchedServicesData.notify();
}
QBindable<QStringList> 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);
d->watchedServicesData.removeBindingUnlessInWrapper();
if (d->watchedServicesData.value().contains(newService))
return;
d->addService(newService);
auto templist = d->watchedServicesData.valueBypassingBindings();
templist << newService;
d->watchedServicesData.setValueBypassingBindings(templist);
d->watchedServicesData.notify();
}
/*!
Removes the \a service from the list of services being watched by this
object. Note that D-Bus notifications are asynchronous, so there may
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);
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
{
return d_func()->watchMode;
}
QBindable<QDBusServiceWatcher::WatchMode> QDBusServiceWatcher::bindableWatchMode()
{
return &d_func()->watchMode;
}
void QDBusServiceWatcher::setWatchMode(WatchMode mode)
{
Q_D(QDBusServiceWatcher);
d->watchMode.removeBindingUnlessInWrapper();
if (mode == d->watchMode.value())
return;
d->setConnection(d->watchedServicesData, d->connection, mode);
d->watchMode.notify();
}
/*!
Returns the QDBusConnection that this object is attached to.
\sa setConnection()
*/
QDBusConnection QDBusServiceWatcher::connection() const
{
return d_func()->connection;
}
/*!
Sets the D-Bus connection that this object is attached to be \a
connection. All services watched will be transferred to this connection.
Note that QDBusConnection objects are reference counted:
QDBusServiceWatcher will keep a reference for this connection while it
exists. The connection is not closed until the reference count drops to
zero, so this will ensure that any notifications are received while this
QDBusServiceWatcher object exists.
\sa connection()
*/
void QDBusServiceWatcher::setConnection(const QDBusConnection &connection)
{
Q_D(QDBusServiceWatcher);
if (connection.name() == d->connection.name())
return;
d->setConnection(d->watchedServicesData, connection, d->watchMode);
}
QT_END_NAMESPACE
#endif // QT_NO_DBUS
#include "moc_qdbusservicewatcher.cpp"
|