Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/dbus/qdbusservicewatcher.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtDBus module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qdbusservicewatcher.h" | - | ||||||||||||
35 | #include "qdbusconnection.h" | - | ||||||||||||
36 | #include "qdbusutil_p.h" | - | ||||||||||||
37 | - | |||||||||||||
38 | #include <QStringList> | - | ||||||||||||
39 | - | |||||||||||||
40 | #include <private/qobject_p.h> | - | ||||||||||||
41 | #include <private/qdbusconnection_p.h> | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifndef QT_NO_DBUS | - | ||||||||||||
44 | - | |||||||||||||
45 | QT_BEGIN_NAMESPACE | - | ||||||||||||
46 | - | |||||||||||||
47 | class QDBusServiceWatcherPrivate: public QObjectPrivate | - | ||||||||||||
48 | { | - | ||||||||||||
49 | Q_DECLARE_PUBLIC(QDBusServiceWatcher) | - | ||||||||||||
50 | public: | - | ||||||||||||
51 | QDBusServiceWatcherPrivate(const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm) | - | ||||||||||||
52 | : connection(c), watchMode(wm) | - | ||||||||||||
53 | { | - | ||||||||||||
54 | } executed 814 times by 155 tests: end of block Executed by:
| 814 | ||||||||||||
55 | - | |||||||||||||
56 | QStringList servicesWatched; | - | ||||||||||||
57 | QDBusConnection connection; | - | ||||||||||||
58 | QDBusServiceWatcher::WatchMode watchMode; | - | ||||||||||||
59 | - | |||||||||||||
60 | void _q_serviceOwnerChanged(const QString &, const QString &, const QString &); | - | ||||||||||||
61 | void setConnection(const QStringList &services, const QDBusConnection &c, QDBusServiceWatcher::WatchMode watchMode); | - | ||||||||||||
62 | - | |||||||||||||
63 | void addService(const QString &service); | - | ||||||||||||
64 | void removeService(const QString &service); | - | ||||||||||||
65 | }; | - | ||||||||||||
66 | - | |||||||||||||
67 | void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner) | - | ||||||||||||
68 | { | - | ||||||||||||
69 | Q_Q(QDBusServiceWatcher); | - | ||||||||||||
70 | emit q->serviceOwnerChanged(service, oldOwner, newOwner); | - | ||||||||||||
71 | if (oldOwner.isEmpty())
| 5-8 | ||||||||||||
72 | emit q->serviceRegistered(service); executed 8 times by 3 tests: q->serviceRegistered(service); Executed by:
| 8 | ||||||||||||
73 | else if (newOwner.isEmpty())
| 0-5 | ||||||||||||
74 | emit q->serviceUnregistered(service); executed 5 times by 1 test: q->serviceUnregistered(service); Executed by:
| 5 | ||||||||||||
75 | } executed 13 times by 3 tests: end of block Executed by:
| 13 | ||||||||||||
76 | - | |||||||||||||
77 | void QDBusServiceWatcherPrivate::setConnection(const QStringList &s, const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm) | - | ||||||||||||
78 | { | - | ||||||||||||
79 | if (connection.isConnected()) {
| 6-816 | ||||||||||||
80 | // remove older rules | - | ||||||||||||
81 | foreach (const QString &s, servicesWatched) | - | ||||||||||||
82 | removeService(s); executed 4 times by 1 test: removeService(s); Executed by:
| 4 | ||||||||||||
83 | } executed 816 times by 155 tests: end of block Executed by:
| 816 | ||||||||||||
84 | - | |||||||||||||
85 | connection = c; | - | ||||||||||||
86 | watchMode = wm; | - | ||||||||||||
87 | servicesWatched = s; | - | ||||||||||||
88 | - | |||||||||||||
89 | if (connection.isConnected()) {
| 5-817 | ||||||||||||
90 | // add new rules | - | ||||||||||||
91 | foreach (const QString &s, servicesWatched) | - | ||||||||||||
92 | addService(s); executed 817 times by 155 tests: addService(s); Executed by:
| 817 | ||||||||||||
93 | } executed 817 times by 155 tests: end of block Executed by:
| 817 | ||||||||||||
94 | } executed 822 times by 155 tests: end of block Executed by:
| 822 | ||||||||||||
95 | - | |||||||||||||
96 | void QDBusServiceWatcherPrivate::addService(const QString &service) | - | ||||||||||||
97 | { | - | ||||||||||||
98 | QDBusConnectionPrivate *d = QDBusConnectionPrivate::d(connection); | - | ||||||||||||
99 | if (d && d->shouldWatchService(service))
| 0-817 | ||||||||||||
100 | d->watchService(service, watchMode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString))); executed 817 times by 155 tests: d->watchService(service, watchMode, q_func(), qFlagLocation("1""_q_serviceOwnerChanged(QString,QString,QString)" "\0" __FILE__ ":" "100")); Executed by:
| 817 | ||||||||||||
101 | } executed 819 times by 155 tests: end of block Executed by:
| 819 | ||||||||||||
102 | - | |||||||||||||
103 | void QDBusServiceWatcherPrivate::removeService(const QString &service) | - | ||||||||||||
104 | { | - | ||||||||||||
105 | QDBusConnectionPrivate *d = QDBusConnectionPrivate::d(connection); | - | ||||||||||||
106 | if (d && d->shouldWatchService(service))
| 0-4 | ||||||||||||
107 | d->unwatchService(service, watchMode, q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString))); executed 4 times by 1 test: d->unwatchService(service, watchMode, q_func(), qFlagLocation("1""_q_serviceOwnerChanged(QString,QString,QString)" "\0" __FILE__ ":" "107")); Executed by:
| 4 | ||||||||||||
108 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
109 | - | |||||||||||||
110 | /*! | - | ||||||||||||
111 | \class QDBusServiceWatcher | - | ||||||||||||
112 | \since 4.6 | - | ||||||||||||
113 | \inmodule QtDBus | - | ||||||||||||
114 | - | |||||||||||||
115 | \brief The QDBusServiceWatcher class allows the user to watch for a bus service change. | - | ||||||||||||
116 | - | |||||||||||||
117 | A QDBusServiceWatcher object can be used to notify the application about | - | ||||||||||||
118 | an ownership change of a service name on the bus. It has three watch | - | ||||||||||||
119 | modes: | - | ||||||||||||
120 | - | |||||||||||||
121 | \list | - | ||||||||||||
122 | \li Watching for service registration only. | - | ||||||||||||
123 | \li Watching for service unregistration only. | - | ||||||||||||
124 | \li Watching for any kind of service ownership change (the default mode). | - | ||||||||||||
125 | \endlist | - | ||||||||||||
126 | - | |||||||||||||
127 | Besides being created or deleted, services may change owners without a | - | ||||||||||||
128 | unregister/register operation happening. So the serviceRegistered() | - | ||||||||||||
129 | and serviceUnregistered() signals may not be emitted if that | - | ||||||||||||
130 | happens. | - | ||||||||||||
131 | - | |||||||||||||
132 | This class is more efficient than using the | - | ||||||||||||
133 | QDBusConnectionInterface::serviceOwnerChanged() signal because it allows | - | ||||||||||||
134 | one to receive only the signals for which the class is interested in. | - | ||||||||||||
135 | - | |||||||||||||
136 | \sa QDBusConnection | - | ||||||||||||
137 | */ | - | ||||||||||||
138 | - | |||||||||||||
139 | /*! | - | ||||||||||||
140 | \enum QDBusServiceWatcher::WatchModeFlag | - | ||||||||||||
141 | - | |||||||||||||
142 | QDBusServiceWatcher supports three different watch modes, which are configured by this flag: | - | ||||||||||||
143 | - | |||||||||||||
144 | \value WatchForRegistration watch for service registration only, ignoring | - | ||||||||||||
145 | any signals related to other service ownership change. | - | ||||||||||||
146 | - | |||||||||||||
147 | \value WatchForUnregistration watch for service unregistration only, | - | ||||||||||||
148 | ignoring any signals related to other service ownership change. | - | ||||||||||||
149 | - | |||||||||||||
150 | \value WatchForOwnerChange watch for any kind of service ownership | - | ||||||||||||
151 | change. | - | ||||||||||||
152 | */ | - | ||||||||||||
153 | - | |||||||||||||
154 | /*! | - | ||||||||||||
155 | \property QDBusServiceWatcher::watchMode | - | ||||||||||||
156 | - | |||||||||||||
157 | The \c watchMode property holds the current watch mode for this | - | ||||||||||||
158 | QDBusServiceWatcher object. The default value for this property is | - | ||||||||||||
159 | QDBusServiceWatcher::WatchForOwnershipChange. | - | ||||||||||||
160 | */ | - | ||||||||||||
161 | - | |||||||||||||
162 | /*! | - | ||||||||||||
163 | \property QDBusServiceWatcher::watchedServices | - | ||||||||||||
164 | - | |||||||||||||
165 | The \c servicesWatched property holds the list of services watched. | - | ||||||||||||
166 | - | |||||||||||||
167 | Note that modifying this list with setServicesWatched() is an expensive | - | ||||||||||||
168 | operation. If you can, prefer to change it by way of addWatchedService() | - | ||||||||||||
169 | and removeWatchedService(). | - | ||||||||||||
170 | */ | - | ||||||||||||
171 | - | |||||||||||||
172 | /*! | - | ||||||||||||
173 | \fn void QDBusServiceWatcher::serviceRegistered(const QString &serviceName) | - | ||||||||||||
174 | - | |||||||||||||
175 | This signal is emitted whenever this object detects that the service \a | - | ||||||||||||
176 | serviceName became available on the bus. | - | ||||||||||||
177 | - | |||||||||||||
178 | \sa serviceUnregistered(), serviceOwnerChanged() | - | ||||||||||||
179 | */ | - | ||||||||||||
180 | - | |||||||||||||
181 | /*! | - | ||||||||||||
182 | \fn void QDBusServiceWatcher::serviceUnregistered(const QString &serviceName) | - | ||||||||||||
183 | - | |||||||||||||
184 | This signal is emitted whenever this object detects that the service \a | - | ||||||||||||
185 | serviceName was unregistered from the bus and is no longer available. | - | ||||||||||||
186 | - | |||||||||||||
187 | \sa serviceRegistered(), serviceOwnerChanged() | - | ||||||||||||
188 | */ | - | ||||||||||||
189 | - | |||||||||||||
190 | /*! | - | ||||||||||||
191 | \fn void QDBusServiceWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner) | - | ||||||||||||
192 | - | |||||||||||||
193 | This signal is emitted whenever this object detects that there was a | - | ||||||||||||
194 | service ownership change relating to the \a serviceName service. The \a | - | ||||||||||||
195 | oldOwner parameter contains the old owner name and \a newOwner is the new | - | ||||||||||||
196 | owner. Both \a oldOwner and \a newOwner are unique connection names. | - | ||||||||||||
197 | - | |||||||||||||
198 | Note that this signal is also emitted whenever the \a serviceName service | - | ||||||||||||
199 | was registered or unregistered. If it was registered, \a oldOwner will | - | ||||||||||||
200 | contain an empty string, whereas if it was unregistered, \a newOwner will | - | ||||||||||||
201 | contain an empty string. | - | ||||||||||||
202 | - | |||||||||||||
203 | If you need only to find out if the service is registered or unregistered | - | ||||||||||||
204 | only, without being notified that the ownership changed, consider using | - | ||||||||||||
205 | the specific modes for those operations. This class is more efficient if | - | ||||||||||||
206 | you use the more specific modes. | - | ||||||||||||
207 | - | |||||||||||||
208 | \sa serviceRegistered(), serviceUnregistered() | - | ||||||||||||
209 | */ | - | ||||||||||||
210 | - | |||||||||||||
211 | /*! | - | ||||||||||||
212 | Creates a QDBusServiceWatcher object. Note that until you set a | - | ||||||||||||
213 | connection with setConnection(), this object will not emit any signals. | - | ||||||||||||
214 | - | |||||||||||||
215 | The \a parent parameter is passed to QObject to set the parent of this | - | ||||||||||||
216 | object. | - | ||||||||||||
217 | */ | - | ||||||||||||
218 | QDBusServiceWatcher::QDBusServiceWatcher(QObject *parent) | - | ||||||||||||
219 | : QObject(*new QDBusServiceWatcherPrivate(QDBusConnection(QString()), WatchForOwnerChange), parent) | - | ||||||||||||
220 | { | - | ||||||||||||
221 | } never executed: end of block | 0 | ||||||||||||
222 | - | |||||||||||||
223 | /*! | - | ||||||||||||
224 | Creates a QDBusServiceWatcher object and attaches it to the \a connection | - | ||||||||||||
225 | connection. Also, this function immediately starts watching for \a | - | ||||||||||||
226 | watchMode changes to service \a service. | - | ||||||||||||
227 | - | |||||||||||||
228 | The \a parent parameter is passed to QObject to set the parent of this | - | ||||||||||||
229 | object. | - | ||||||||||||
230 | */ | - | ||||||||||||
231 | QDBusServiceWatcher::QDBusServiceWatcher(const QString &service, const QDBusConnection &connection, WatchMode watchMode, QObject *parent) | - | ||||||||||||
232 | : QObject(*new QDBusServiceWatcherPrivate(connection, watchMode), parent) | - | ||||||||||||
233 | { | - | ||||||||||||
234 | d_func()->setConnection(QStringList() << service, connection, watchMode); | - | ||||||||||||
235 | } executed 814 times by 155 tests: end of block Executed by:
| 814 | ||||||||||||
236 | - | |||||||||||||
237 | /*! | - | ||||||||||||
238 | Destroys the QDBusServiceWatcher object and releases any resources | - | ||||||||||||
239 | associated with it. | - | ||||||||||||
240 | */ | - | ||||||||||||
241 | QDBusServiceWatcher::~QDBusServiceWatcher() | - | ||||||||||||
242 | { | - | ||||||||||||
243 | } | - | ||||||||||||
244 | - | |||||||||||||
245 | /*! | - | ||||||||||||
246 | Returns the list of D-Bus services that are being watched. | - | ||||||||||||
247 | - | |||||||||||||
248 | \sa setWatchedServices() | - | ||||||||||||
249 | */ | - | ||||||||||||
250 | QStringList QDBusServiceWatcher::watchedServices() const | - | ||||||||||||
251 | { | - | ||||||||||||
252 | return d_func()->servicesWatched; never executed: return d_func()->servicesWatched; | 0 | ||||||||||||
253 | } | - | ||||||||||||
254 | - | |||||||||||||
255 | /*! | - | ||||||||||||
256 | Sets the list of D-Bus services being watched to be \a services. | - | ||||||||||||
257 | - | |||||||||||||
258 | Note that setting the entire list means removing all previous rules for | - | ||||||||||||
259 | watching services and adding new ones. This is an expensive operation and | - | ||||||||||||
260 | should be avoided, if possible. Instead, use addWatchedService() and | - | ||||||||||||
261 | removeWatchedService() if you can to manipulate entries in the list. | - | ||||||||||||
262 | */ | - | ||||||||||||
263 | void QDBusServiceWatcher::setWatchedServices(const QStringList &services) | - | ||||||||||||
264 | { | - | ||||||||||||
265 | Q_D(QDBusServiceWatcher); | - | ||||||||||||
266 | if (services == d->servicesWatched)
| 0-2 | ||||||||||||
267 | return; never executed: return; | 0 | ||||||||||||
268 | d->setConnection(services, d->connection, d->watchMode); | - | ||||||||||||
269 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
270 | - | |||||||||||||
271 | /*! | - | ||||||||||||
272 | Adds \a newService to the list of services to be watched by this object. | - | ||||||||||||
273 | This function is more efficient than setWatchedServices() and should be | - | ||||||||||||
274 | used whenever possible to add services. | - | ||||||||||||
275 | */ | - | ||||||||||||
276 | void QDBusServiceWatcher::addWatchedService(const QString &newService) | - | ||||||||||||
277 | { | - | ||||||||||||
278 | Q_D(QDBusServiceWatcher); | - | ||||||||||||
279 | if (d->servicesWatched.contains(newService))
| 0-2 | ||||||||||||
280 | return; never executed: return; | 0 | ||||||||||||
281 | d->addService(newService); | - | ||||||||||||
282 | d->servicesWatched << newService; | - | ||||||||||||
283 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
284 | - | |||||||||||||
285 | /*! | - | ||||||||||||
286 | Removes the \a service from the list of services being watched by this | - | ||||||||||||
287 | object. Note that D-Bus notifications are asynchronous, so there may | - | ||||||||||||
288 | still be signals pending delivery about \a service. Those signals will | - | ||||||||||||
289 | still be emitted whenever the D-Bus messages are processed. | - | ||||||||||||
290 | - | |||||||||||||
291 | This function returns \c true if any services were removed. | - | ||||||||||||
292 | */ | - | ||||||||||||
293 | bool QDBusServiceWatcher::removeWatchedService(const QString &service) | - | ||||||||||||
294 | { | - | ||||||||||||
295 | Q_D(QDBusServiceWatcher); | - | ||||||||||||
296 | d->removeService(service); | - | ||||||||||||
297 | return d->servicesWatched.removeOne(service); never executed: return d->servicesWatched.removeOne(service); | 0 | ||||||||||||
298 | } | - | ||||||||||||
299 | - | |||||||||||||
300 | QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const | - | ||||||||||||
301 | { | - | ||||||||||||
302 | return d_func()->watchMode; never executed: return d_func()->watchMode; | 0 | ||||||||||||
303 | } | - | ||||||||||||
304 | - | |||||||||||||
305 | void QDBusServiceWatcher::setWatchMode(WatchMode mode) | - | ||||||||||||
306 | { | - | ||||||||||||
307 | Q_D(QDBusServiceWatcher); | - | ||||||||||||
308 | if (mode == d->watchMode)
| 0-4 | ||||||||||||
309 | return; never executed: return; | 0 | ||||||||||||
310 | d->setConnection(d->servicesWatched, d->connection, mode); | - | ||||||||||||
311 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
312 | - | |||||||||||||
313 | /*! | - | ||||||||||||
314 | Returns the QDBusConnection that this object is attached to. | - | ||||||||||||
315 | - | |||||||||||||
316 | \sa setConnection() | - | ||||||||||||
317 | */ | - | ||||||||||||
318 | QDBusConnection QDBusServiceWatcher::connection() const | - | ||||||||||||
319 | { | - | ||||||||||||
320 | return d_func()->connection; never executed: return d_func()->connection; | 0 | ||||||||||||
321 | } | - | ||||||||||||
322 | - | |||||||||||||
323 | /*! | - | ||||||||||||
324 | Sets the D-Bus connection that this object is attached to be \a | - | ||||||||||||
325 | connection. All services watched will be transferred to this connection. | - | ||||||||||||
326 | - | |||||||||||||
327 | Note that QDBusConnection objects are reference counted: | - | ||||||||||||
328 | QDBusServiceWatcher will keep a reference for this connection while it | - | ||||||||||||
329 | exists. The connection is not closed until the reference count drops to | - | ||||||||||||
330 | zero, so this will ensure that any notifications are received while this | - | ||||||||||||
331 | QDBusServiceWatcher object exists. | - | ||||||||||||
332 | - | |||||||||||||
333 | \sa connection() | - | ||||||||||||
334 | */ | - | ||||||||||||
335 | void QDBusServiceWatcher::setConnection(const QDBusConnection &connection) | - | ||||||||||||
336 | { | - | ||||||||||||
337 | Q_D(QDBusServiceWatcher); | - | ||||||||||||
338 | if (connection.name() == d->connection.name())
| 0-2 | ||||||||||||
339 | return; never executed: return; | 0 | ||||||||||||
340 | d->setConnection(d->servicesWatched, connection, d->watchMode); | - | ||||||||||||
341 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
342 | - | |||||||||||||
343 | QT_END_NAMESPACE | - | ||||||||||||
344 | - | |||||||||||||
345 | #endif // QT_NO_DBUS | - | ||||||||||||
346 | - | |||||||||||||
347 | #include "moc_qdbusservicewatcher.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |