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