1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtDBus module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "qdbusserver.h"
42#include "qdbusconnection_p.h"
43#include "qdbusconnectionmanager_p.h"
44#include "qdbusutil_p.h"
45
46#ifndef QT_NO_DBUS
47
48QT_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*/
62QDBusServer::QDBusServer(const QString &address, QObject *parent)
63 : QObject(parent), d(nullptr)
64{
65 if (address.isEmpty())
66 return;
67
68 if (!qdbus_loadLibDBus())
69 return;
70
71 QDBusConnectionManager *instance = QDBusConnectionManager::instance();
72 if (!instance)
73 return;
74
75 emit instance->serverRequested(address, this);
76 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
77 this, SLOT(_q_newConnection(QDBusConnectionPrivate*)), Qt::QueuedConnection);
78}
79
80/*!
81 Constructs a QDBusServer with the given \a parent. The server will listen
82 for connections in \c {/tmp} (on Unix systems) or on a TCP port bound to
83 localhost (elsewhere).
84*/
85QDBusServer::QDBusServer(QObject *parent)
86 : QObject(parent), d(nullptr)
87{
88#ifdef Q_OS_UNIX
89 // Use Unix sockets on Unix systems only
90 const QString address = QStringLiteral("unix:tmpdir=/tmp");
91#else
92 const QString address = QStringLiteral("tcp:");
93#endif
94
95 if (!qdbus_loadLibDBus())
96 return;
97
98 QDBusConnectionManager *instance = QDBusConnectionManager::instance();
99 if (!instance)
100 return;
101
102 emit instance->serverRequested(address, this);
103 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
104 this, SLOT(_q_newConnection(QDBusConnectionPrivate*)), Qt::QueuedConnection);
105}
106
107/*!
108 Destructs a QDBusServer
109*/
110QDBusServer::~QDBusServer()
111{
112 QWriteLocker locker(&d->lock);
113 if (QDBusConnectionManager::instance()) {
114 QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
115 for (const QString &name : qAsConst(d->serverConnectionNames))
116 QDBusConnectionManager::instance()->removeConnection(name);
117 d->serverConnectionNames.clear();
118 }
119 d->serverObject = nullptr;
120 d->ref.storeRelaxed(0);
121 d->deleteLater();
122}
123
124/*!
125 Returns \c true if this QDBusServer object is connected.
126
127 If it isn't connected, you need to call the constructor again.
128*/
129bool QDBusServer::isConnected() const
130{
131 return d && d->server && q_dbus_server_get_is_connected(d->server);
132}
133
134/*!
135 Returns the last error that happened in this server.
136
137 This function is provided for low-level code.
138*/
139QDBusError QDBusServer::lastError() const
140{
141 return d ? d->lastError : QDBusError(QDBusError::Disconnected, QDBusUtil::disconnectedErrorMessage());
142}
143
144/*!
145 Returns the address this server is associated with.
146*/
147QString QDBusServer::address() const
148{
149 QString addr;
150 if (d && d->server) {
151 char *c = q_dbus_server_get_address(d->server);
152 addr = QString::fromUtf8(c);
153 q_dbus_free(c);
154 }
155
156 return addr;
157}
158
159/*!
160 \since 5.3
161
162 If \a value is set to true, an incoming connection can proceed even if the
163 connecting client is not authenticated as a user.
164
165 By default, this value is false.
166
167 \sa isAnonymousAuthenticationAllowed()
168*/
169void QDBusServer::setAnonymousAuthenticationAllowed(bool value)
170{
171 d->anonymousAuthenticationAllowed = value;
172}
173
174/*!
175 \since 5.3
176
177 Returns true if anonymous authentication is allowed.
178
179 \sa setAnonymousAuthenticationAllowed()
180*/
181bool QDBusServer::isAnonymousAuthenticationAllowed() const
182{
183 return d->anonymousAuthenticationAllowed;
184}
185
186/*!
187 \fn void QDBusServer::newConnection(const QDBusConnection &connection)
188
189 This signal is emitted when a new client connection \a connection is
190 established to the server.
191 */
192
193QT_END_NAMESPACE
194
195#include "moc_qdbusserver.cpp"
196
197#endif // QT_NO_DBUS
198