QDBusPendingCallPrivate: save 8 bytes on 64-bit archs
[profile/ivi/qtbase.git] / src / dbus / qdbusserver.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDBus module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdbusserver.h"
43 #include "qdbusconnection_p.h"
44 #include "qdbusconnectionmanager_p.h"
45
46 #ifndef QT_NO_DBUS
47
48 QT_BEGIN_NAMESPACE
49
50 /*!
51     \class QDBusServer
52     \inmodule QtDBus
53
54     \brief The QDBusServer class provides peer-to-peer communication
55     between processes on the same computer.
56 */
57
58 /*!
59     Constructs a QDBusServer with the given \a address, and the given
60     \a parent.
61 */
62 QDBusServer::QDBusServer(const QString &address, QObject *parent)
63     : QObject(parent)
64 {
65     if (address.isEmpty())
66         return;
67
68     if (!qdbus_loadLibDBus()) {
69         d = 0;
70         return;
71     }
72     d = new QDBusConnectionPrivate(this);
73
74     QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
75     QDBusConnectionManager::instance()->setConnection(QLatin1String("QDBusServer-") + QString::number(reinterpret_cast<qulonglong>(d)), d);
76
77     QObject::connect(d, SIGNAL(newServerConnection(QDBusConnection)),
78                      this, SIGNAL(newConnection(QDBusConnection)));
79
80     QDBusErrorInternal error;
81     d->setServer(q_dbus_server_listen(address.toUtf8().constData(), error), error);
82 }
83
84 /*!
85     Constructs a QDBusServer with the given \a parent. The server will listen
86     for connections in \c {/tmp}.
87 */
88 QDBusServer::QDBusServer(QObject *parent)
89     : QObject(parent)
90 {
91     const QString address = QLatin1String("unix:tmpdir=/tmp");
92
93     if (!qdbus_loadLibDBus()) {
94         d = 0;
95         return;
96     }
97     d = new QDBusConnectionPrivate(this);
98
99     QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
100     QDBusConnectionManager::instance()->setConnection(QLatin1String("QDBusServer-") + QString::number(reinterpret_cast<qulonglong>(d)), d);
101
102     QObject::connect(d, SIGNAL(newServerConnection(QDBusConnection)),
103                      this, SIGNAL(newConnection(QDBusConnection)));
104
105     QDBusErrorInternal error;
106     d->setServer(q_dbus_server_listen(address.toUtf8().constData(), error), error);
107 }
108
109 /*!
110     Destructs a QDBusServer
111 */
112 QDBusServer::~QDBusServer()
113 {
114     if (QDBusConnectionManager::instance()) {
115         QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
116         QDBusConnectionManager::instance()->removeConnection(d->name);
117     }
118 }
119
120 /*!
121     Returns true if this QDBusServer object is connected.
122
123     If it isn't connected, you need to call the constructor again.
124 */
125 bool QDBusServer::isConnected() const
126 {
127     return d && d->server && q_dbus_server_get_is_connected(d->server);
128 }
129
130 /*!
131     Returns the last error that happened in this server.
132
133     This function is provided for low-level code.
134 */
135 QDBusError QDBusServer::lastError() const
136 {
137     return d->lastError;
138 }
139
140 /*!
141     Returns the address this server is associated with.
142 */
143 QString QDBusServer::address() const
144 {
145     QString addr;
146     if (d && d->server) {
147         char *c = q_dbus_server_get_address(d->server);
148         addr = QString::fromUtf8(c);
149         q_dbus_free(c);
150     }
151
152     return addr;
153 }
154
155 /*!
156   \fn void QDBusServer::newConnection(const QDBusConnection &connection)
157
158   This signal is emitted when a new client connection \a connection is
159   established to the server.
160  */
161
162 QT_END_NAMESPACE
163
164 #endif // QT_NO_DBUS