1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDBus module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdbusserver.h"
43 #include "qdbusconnection_p.h"
44 #include "qdbusconnectionmanager_p.h"
54 \brief The QDBusServer class provides peer-to-peer communication
55 between processes on the same computer.
59 Constructs a QDBusServer with the given \a address, and the given
62 QDBusServer::QDBusServer(const QString &address, QObject *parent)
65 if (address.isEmpty())
68 if (!qdbus_loadLibDBus()) {
72 d = new QDBusConnectionPrivate(this);
74 QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
75 QDBusConnectionManager::instance()->setConnection(QLatin1String("QDBusServer-") + QString::number(reinterpret_cast<qulonglong>(d)), d);
77 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnection)),
78 this, SIGNAL(newConnection(QDBusConnection)));
80 QDBusErrorInternal error;
81 d->setServer(q_dbus_server_listen(address.toUtf8().constData(), error), error);
85 Constructs a QDBusServer with the given \a parent. The server will listen
86 for connections in \c {/tmp}.
88 QDBusServer::QDBusServer(QObject *parent)
91 const QString address = QLatin1String("unix:tmpdir=/tmp");
93 if (!qdbus_loadLibDBus()) {
97 d = new QDBusConnectionPrivate(this);
99 QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
100 QDBusConnectionManager::instance()->setConnection(QLatin1String("QDBusServer-") + QString::number(reinterpret_cast<qulonglong>(d)), d);
102 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnection)),
103 this, SIGNAL(newConnection(QDBusConnection)));
105 QDBusErrorInternal error;
106 d->setServer(q_dbus_server_listen(address.toUtf8().constData(), error), error);
110 Destructs a QDBusServer
112 QDBusServer::~QDBusServer()
114 if (QDBusConnectionManager::instance()) {
115 QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
116 QDBusConnectionManager::instance()->removeConnection(d->name);
121 Returns true if this QDBusServer object is connected.
123 If it isn't connected, you need to call the constructor again.
125 bool QDBusServer::isConnected() const
127 return d && d->server && q_dbus_server_get_is_connected(d->server);
131 Returns the last error that happened in this server.
133 This function is provided for low-level code.
135 QDBusError QDBusServer::lastError() const
141 Returns the address this server is associated with.
143 QString QDBusServer::address() const
146 if (d && d->server) {
147 char *c = q_dbus_server_get_address(d->server);
148 addr = QString::fromUtf8(c);
156 \fn void QDBusServer::newConnection(const QDBusConnection &connection)
158 This signal is emitted when a new client connection \a connection is
159 established to the server.