1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qdbusserver.h"
6#include "qdbusconnection_p.h"
7#include "qdbusconnectionmanager_p.h"
8#include "qdbusutil_p.h"
9
10#include <QtCore/private/qlocking_p.h>
11
12#ifndef QT_NO_DBUS
13
14QT_BEGIN_NAMESPACE
15
16/*!
17 \class QDBusServer
18 \inmodule QtDBus
19
20 \brief The QDBusServer class provides peer-to-peer communication
21 between processes on the same computer.
22*/
23
24/*!
25 Constructs a QDBusServer with the given \a address, and the given
26 \a parent.
27*/
28QDBusServer::QDBusServer(const QString &address, QObject *parent)
29 : QObject(parent), d(nullptr)
30{
31 if (address.isEmpty())
32 return;
33
34 if (!qdbus_loadLibDBus())
35 return;
36
37 QDBusConnectionManager *instance = QDBusConnectionManager::instance();
38 if (!instance)
39 return;
40
41 emit instance->serverRequested(address, server: this);
42 Q_ASSERT(d != nullptr);
43
44 QObject::connect(sender: d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
45 receiver: this, SLOT(_q_newConnection(QDBusConnectionPrivate*)), Qt::QueuedConnection);
46}
47
48/*!
49 Constructs a QDBusServer with the given \a parent. The server will listen
50 for connections in \c {/tmp} (on Unix systems) or on a TCP port bound to
51 localhost (elsewhere).
52*/
53QDBusServer::QDBusServer(QObject *parent)
54 : QObject(parent), d(nullptr)
55{
56#ifdef Q_OS_UNIX
57 // Use Unix sockets on Unix systems only
58 const QString address = QStringLiteral("unix:tmpdir=/tmp");
59#else
60 const QString address = QStringLiteral("tcp:");
61#endif
62
63 if (!qdbus_loadLibDBus())
64 return;
65
66 QDBusConnectionManager *instance = QDBusConnectionManager::instance();
67 if (!instance)
68 return;
69
70 emit instance->serverRequested(address, server: this);
71 Q_ASSERT(d != nullptr);
72
73 QObject::connect(sender: d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
74 receiver: this, SLOT(_q_newConnection(QDBusConnectionPrivate*)), Qt::QueuedConnection);
75}
76
77/*!
78 Destructs a QDBusServer
79*/
80QDBusServer::~QDBusServer()
81{
82 if (!d)
83 return;
84
85 auto manager = QDBusConnectionManager::instance();
86 if (!manager)
87 return;
88
89 QMutexLocker locker(&manager->mutex);
90 QWriteLocker writeLocker(&d->lock);
91 for (const QString &name : std::as_const(t&: d->serverConnectionNames))
92 manager->removeConnection(name);
93 d->serverConnectionNames.clear();
94 locker.unlock();
95
96 d->serverObject = nullptr;
97 d->ref.storeRelaxed(newValue: 0);
98 d->deleteLater();
99}
100
101/*!
102 Returns \c true if this QDBusServer object is connected.
103
104 If it isn't connected, you need to call the constructor again.
105*/
106bool QDBusServer::isConnected() const
107{
108 return d && d->server && q_dbus_server_get_is_connected(server: d->server);
109}
110
111/*!
112 Returns the last error that happened in this server.
113
114 This function is provided for low-level code.
115*/
116QDBusError QDBusServer::lastError() const
117{
118 return d ? d->lastError : QDBusError(QDBusError::Disconnected, QDBusUtil::disconnectedErrorMessage());
119}
120
121/*!
122 Returns the address this server is associated with.
123*/
124QString QDBusServer::address() const
125{
126 QString addr;
127 if (d && d->server) {
128 char *c = q_dbus_server_get_address(server: d->server);
129 addr = QString::fromUtf8(utf8: c);
130 q_dbus_free(memory: c);
131 }
132
133 return addr;
134}
135
136/*!
137 \since 5.3
138
139 If \a value is set to true, an incoming connection can proceed even if the
140 connecting client is not authenticated as a user.
141
142 By default, this value is false.
143
144 \sa isAnonymousAuthenticationAllowed()
145*/
146void QDBusServer::setAnonymousAuthenticationAllowed(bool value)
147{
148 if (!d)
149 return;
150
151 d->anonymousAuthenticationAllowed = value;
152}
153
154/*!
155 \since 5.3
156
157 Returns true if anonymous authentication is allowed.
158
159 \sa setAnonymousAuthenticationAllowed()
160*/
161bool QDBusServer::isAnonymousAuthenticationAllowed() const
162{
163 if (!d)
164 return false;
165
166 return d->anonymousAuthenticationAllowed;
167}
168
169/*!
170 \fn void QDBusServer::newConnection(const QDBusConnection &connection)
171
172 This signal is emitted when a new client connection \a connection is
173 established to the server.
174 */
175
176QT_END_NAMESPACE
177
178#include "moc_qdbusserver.cpp"
179
180#endif // QT_NO_DBUS
181

source code of qtbase/src/dbus/qdbusserver.cpp