Qt 4.8
qdbusservicewatcher.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qdbusservicewatcher.h"
43 #include "qdbusconnection.h"
44 #include "qdbus_symbols_p.h"
45 
46 #include <QStringList>
47 
48 #include <private/qobject_p.h>
49 
50 #ifndef QT_NO_DBUS
51 
53 
54 Q_GLOBAL_STATIC_WITH_ARGS(QString, busService, (QLatin1String(DBUS_SERVICE_DBUS)))
56 Q_GLOBAL_STATIC_WITH_ARGS(QString, signalName, (QLatin1String("NameOwnerChanged")))
57 
58 class QDBusServiceWatcherPrivate: public QObjectPrivate
59 {
61 public:
62  QDBusServiceWatcherPrivate(const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
63  : connection(c), watchMode(wm)
64  {
65  }
66 
67  QStringList servicesWatched;
68  QDBusConnection connection;
69  QDBusServiceWatcher::WatchMode watchMode;
70 
71  void _q_serviceOwnerChanged(const QString &, const QString &, const QString &);
72  void setConnection(const QStringList &services, const QDBusConnection &c, QDBusServiceWatcher::WatchMode watchMode);
73 
74  QStringList matchArgsForService(const QString &service);
75  void addService(const QString &service);
76  void removeService(const QString &service);
77 };
78 
79 void QDBusServiceWatcherPrivate::_q_serviceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner)
80 {
82  emit q->serviceOwnerChanged(service, oldOwner, newOwner);
83  if (oldOwner.isEmpty())
84  emit q->serviceRegistered(service);
85  else if (newOwner.isEmpty())
86  emit q->serviceUnregistered(service);
87 }
88 
89 void QDBusServiceWatcherPrivate::setConnection(const QStringList &s, const QDBusConnection &c, QDBusServiceWatcher::WatchMode wm)
90 {
91  if (connection.isConnected()) {
92  // remove older rules
93  foreach (const QString &s, servicesWatched)
94  removeService(s);
95  }
96 
97  connection = c;
98  watchMode = wm;
99  servicesWatched = s;
100 
101  if (connection.isConnected()) {
102  // add new rules
103  foreach (const QString &s, servicesWatched)
104  addService(s);
105  }
106 }
107 
108 QStringList QDBusServiceWatcherPrivate::matchArgsForService(const QString &service)
109 {
110  QStringList matchArgs;
111  matchArgs << service;
112 
113  switch (watchMode) {
115  break;
116 
118  matchArgs << QString::fromLatin1("", 0);
119  break;
120 
122  matchArgs << QString() << QString::fromLatin1("", 0);
123  break;
124  }
125  return matchArgs;
126 }
127 
128 void QDBusServiceWatcherPrivate::addService(const QString &service)
129 {
130  QStringList matchArgs = matchArgsForService(service);
131  connection.connect(*busService(), QString(), *busInterface(), *signalName(),
132  matchArgs, QString(), q_func(),
133  SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
134 }
135 
136 void QDBusServiceWatcherPrivate::removeService(const QString &service)
137 {
138  QStringList matchArgs = matchArgsForService(service);
139  connection.disconnect(*busService(), QString(), *busInterface(), *signalName(),
140  matchArgs, QString(), q_func(),
141  SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
142 }
143 
274  : QObject(*new QDBusServiceWatcherPrivate(QDBusConnection(QString()), WatchForOwnerChange), parent)
275 {
276 }
277 
287  : QObject(*new QDBusServiceWatcherPrivate(connection, watchMode), parent)
288 {
289  d_func()->setConnection(QStringList() << service, connection, watchMode);
290 }
291 
297 {
298 }
299 
306 {
307  return d_func()->servicesWatched;
308 }
309 
319 {
321  if (services == d->servicesWatched)
322  return;
323  d->setConnection(services, d->connection, d->watchMode);
324 }
325 
332 {
334  if (d->servicesWatched.contains(newService))
335  return;
336  d->addService(newService);
337  d->servicesWatched << newService;
338 }
339 
349 {
351  d->removeService(service);
352  return d->servicesWatched.removeOne(service);
353 }
354 
355 QDBusServiceWatcher::WatchMode QDBusServiceWatcher::watchMode() const
356 {
357  return d_func()->watchMode;
358 }
359 
361 {
363  if (mode == d->watchMode)
364  return;
365  d->setConnection(d->servicesWatched, d->connection, mode);
366 }
367 
374 {
375  return d_func()->connection;
376 }
377 
391 {
393  if (connection.name() == d->connection.name())
394  return;
395  d->setConnection(d->servicesWatched, connection, d->watchMode);
396 }
397 
399 
400 #endif // QT_NO_DBUS
401 
402 #include "moc_qdbusservicewatcher.cpp"
double d
Definition: qnumeric_p.h:62
void setWatchedServices(const QStringList &services)
Sets the list of D-Bus services being watched to be services.
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define SLOT(a)
Definition: qobjectdefs.h:226
void setWatchMode(WatchMode mode)
QStringList watchedServices() const
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool removeWatchedService(const QString &service)
Removes the service from the list of services being watched by this object.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
QString name() const
Returns the connection name for this connection, as given as the name parameter to connectToBus()...
#define Q_Q(Class)
Definition: qglobal.h:2483
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
WatchMode watchMode() const
Q_GLOBAL_STATIC_WITH_ARGS(QString, busService,(QLatin1String(DBUS_SERVICE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void addWatchedService(const QString &newService)
Adds newService to the list of services to be watched by this object.
~QDBusServiceWatcher()
Destroys the QDBusServiceWatcher object and releases any resources associated with it...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QDBusConnection class represents a connection to the D-Bus bus daemon.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QDBusServiceWatcher(QObject *parent=0)
Creates a QDBusServiceWatcher object.
#define class
The QDBusServiceWatcher class allows the user to watch for a bus service change.
void setConnection(const QDBusConnection &connection)
Sets the D-Bus connection that this object is attached to be connection.
QDBusConnection connection() const
Returns the QDBusConnection that this object is attached to.