Qt 4.8
qdbusconnectioninterface.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 
43 
44 #include <QtCore/QByteArray>
45 #include <QtCore/QList>
46 #include <QtCore/QMap>
47 #include <QtCore/QString>
48 #include <QtCore/QStringList>
49 #include <QtCore/QVariant>
50 #include <QtCore/QDebug>
51 
52 #include "qdbus_symbols_p.h" // for the DBUS_* constants
53 
54 #ifndef QT_NO_DBUS
55 
57 
58 /*
59  * Implementation of interface class QDBusConnectionInterface
60  */
61 
169 { return "org.freedesktop.DBus"; }
170 
175  QObject *parent)
176  : QDBusAbstractInterface(QLatin1String(DBUS_SERVICE_DBUS),
177  QLatin1String(DBUS_PATH_DBUS),
178  DBUS_INTERFACE_DBUS, connection, parent)
179 {
180  connect(this, SIGNAL(NameAcquired(QString)), this, SIGNAL(serviceRegistered(QString)));
181  connect(this, SIGNAL(NameLost(QString)), this, SIGNAL(serviceUnregistered(QString)));
182  connect(this, SIGNAL(NameOwnerChanged(QString,QString,QString)),
184 }
185 
190 {
191 }
192 
199 {
200  return internalConstCall(QDBus::AutoDetect, QLatin1String("GetNameOwner"), QList<QVariant>() << name);
201 }
202 
213 {
214  return internalConstCall(QDBus::AutoDetect, QLatin1String("ListNames"));
215 }
216 
222 {
223  return internalConstCall(QDBus::AutoDetect, QLatin1String("NameHasOwner"),
224  QList<QVariant>() << serviceName);
225 }
226 
232 {
233  return internalConstCall(QDBus::AutoDetect, QLatin1String("GetConnectionUnixProcessID"),
234  QList<QVariant>() << serviceName);
235 }
236 
242 {
243  return internalConstCall(QDBus::AutoDetect, QLatin1String("GetConnectionUnixUser"),
244  QList<QVariant>() << serviceName);
245 }
246 
251 {
252  return call(QLatin1String("StartServiceByName"), name, uint(0));
253 }
254 
272  ServiceQueueOptions qoption,
274 {
275  // reconstruct the low-level flags
276  uint flags = 0;
277  switch (qoption) {
278  case DontQueueService:
279  flags = DBUS_NAME_FLAG_DO_NOT_QUEUE;
280  break;
281  case QueueService:
282  flags = 0;
283  break;
285  flags = DBUS_NAME_FLAG_DO_NOT_QUEUE | DBUS_NAME_FLAG_REPLACE_EXISTING;
286  break;
287  }
288 
289  switch (roption) {
291  break;
292  case AllowReplacement:
293  flags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
294  break;
295  }
296 
297  QDBusMessage reply = call(QLatin1String("RequestName"), serviceName, flags);
298 // qDebug() << "QDBusConnectionInterface::registerService" << serviceName << "Reply:" << reply;
299 
300  // convert the low-level flags to something that we can use
301  if (reply.type() == QDBusMessage::ReplyMessage) {
302  uint code = 0;
303 
304  switch (reply.arguments().at(0).toUInt()) {
305  case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
306  case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
307  code = uint(ServiceRegistered);
308  break;
309 
310  case DBUS_REQUEST_NAME_REPLY_EXISTS:
311  code = uint(ServiceNotRegistered);
312  break;
313 
314  case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
315  code = uint(ServiceQueued);
316  break;
317  }
318 
319  reply.setArguments(QVariantList() << code);
320  }
321 
322  return reply;
323 }
324 
334 {
335  QDBusMessage reply = call(QLatin1String("ReleaseName"), serviceName);
336  if (reply.type() == QDBusMessage::ReplyMessage) {
337  bool success = reply.arguments().at(0).toUInt() == DBUS_RELEASE_NAME_REPLY_RELEASED;
338  reply.setArguments(QVariantList() << success);
339  }
340  return reply;
341 }
342 
347 {
348  // translate the signal names to what we really want
349  // this avoids setting hooks for signals that don't exist on the bus
350  if (qstrcmp(signalName, SIGNAL(serviceRegistered(QString))) == 0)
352 
353  else if (qstrcmp(signalName, SIGNAL(serviceUnregistered(QString))) == 0)
355 
356  else if (qstrcmp(signalName, SIGNAL(serviceOwnerChanged(QString,QString,QString))) == 0) {
357  static bool warningPrinted = false;
358  if (!warningPrinted) {
359  qWarning("Connecting to deprecated signal QDBusConnectionInterface::serviceOwnerChanged(QString,QString,QString)");
360  warningPrinted = true;
361  }
363  }
364 }
365 
370 {
371  // translate the signal names to what we really want
372  // this avoids setting hooks for signals that don't exist on the bus
373  if (qstrcmp(signalName, SIGNAL(serviceRegistered(QString))) == 0)
375 
376  else if (qstrcmp(signalName, SIGNAL(serviceUnregistered(QString))) == 0)
378 
379  else if (qstrcmp(signalName, SIGNAL(serviceOwnerChanged(QString,QString,QString))) == 0)
381 }
382 
383 // signals
445 
446 #endif // QT_NO_DBUS
QDBusReply< bool > unregisterService(const QString &serviceName)
Releases the claim on the bus service name serviceName, that had been previously registered with regi...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDBusReply< void > startService(const QString &name)
Requests that the bus start the service given by the name name.
void disconnectNotify(const char *signal)
Catch signal disconnections.
static const char * staticInterfaceName()
QDBusReply< QStringList > registeredServiceNames() const
The QDBusReply class stores the reply for a method call to a remote object.
Definition: qdbusreply.h:65
QDBusConnection connection() const
Returns the connection this interface is assocated with.
QDBusReply< bool > isServiceRegistered(const QString &serviceName) const
Returns true if the service name serviceName has is currently registered.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QDBusReply< QString > serviceOwner(const QString &name) const
Returns the unique connection name of the primary owner of the name name.
The QString class provides a Unicode character string.
Definition: qstring.h:83
void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
This signal is emitted by the D-Bus server whenever a service ownership change happens in the bus...
ServiceQueueOptions
Flags for determining how a service registration should behave, in case the service name is already r...
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define SIGNAL(a)
Definition: qobjectdefs.h:227
The QDBusAbstractInterface class is the base class for all D-Bus interfaces in the QtDBus binding...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QDBusConnectionInterface(const QDBusConnection &connection, QObject *parent)
QList< QVariant > arguments() const
Returns the list of arguments that are going to be sent or were received from D-Bus.
QDBusMessage internalConstCall(QDBus::CallMode mode, const QString &method, const QList< QVariant > &args=QList< QVariant >()) const
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
ServiceReplacementOptions
Flags for determining if the D-Bus server should allow another application to replace a name that thi...
The QDBusConnection class represents a connection to the D-Bus bus daemon.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QList< QVariant > QVariantList
Definition: qvariant.h:443
QDBusReply< QDBusConnectionInterface::RegisterServiceReply > registerService(const QString &serviceName, ServiceQueueOptions qoption=DontQueueService, ServiceReplacementOptions roption=DontAllowReplacement)
Requests to register the service name serviceName on the bus.
uint toUInt(bool *ok=0) const
Returns the variant as an unsigned int if the variant has type() UInt , Bool , ByteArray ...
Definition: qvariant.cpp:2644
QDBusReply< uint > servicePid(const QString &serviceName) const
Returns the Unix Process ID (PID) for the process currently holding the bus service serviceName...
QDBusReply< uint > serviceUid(const QString &serviceName) const
Returns the Unix User ID (UID) for the process currently holding the bus service serviceName.
The QDBusMessage class represents one message sent or received over the D-Bus bus.
Definition: qdbusmessage.h:59
MessageType type() const
Returns the message type.
void serviceRegistered(const QString &service)
This signal is emitted by the D-Bus server when the bus service name (unique connection name or well-...
void setArguments(const QList< QVariant > &arguments)
Sets the arguments that are going to be sent over D-Bus to arguments.
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
void serviceUnregistered(const QString &service)
This signal is emitted by the D-Bus server when this application loses ownership of the bus service n...
void connectNotify(const char *signal)
Catch signal connections.
QDBusMessage call(const QString &method, const QVariant &arg1=QVariant(), const QVariant &arg2=QVariant(), const QVariant &arg3=QVariant(), const QVariant &arg4=QVariant(), const QVariant &arg5=QVariant(), const QVariant &arg6=QVariant(), const QVariant &arg7=QVariant(), const QVariant &arg8=QVariant())
Calls the method method on this interface and passes the parameters to this function to the method...