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 | |
14 | QT_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 | */ |
28 | QDBusServer::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 | instance->createServer(address, server: this); |
42 | Q_ASSERT(d != nullptr); |
43 | } |
44 | |
45 | /*! |
46 | Constructs a QDBusServer with the given \a parent. The server will listen |
47 | for connections in \c {/tmp} (on Unix systems) or on a TCP port bound to |
48 | localhost (elsewhere). |
49 | */ |
50 | QDBusServer::QDBusServer(QObject *parent) |
51 | : QDBusServer( |
52 | #ifdef Q_OS_UNIX |
53 | // Use Unix sockets on Unix systems only |
54 | QStringLiteral("unix:tmpdir=/tmp" ), |
55 | #else |
56 | QStringLiteral("tcp:" ), |
57 | #endif |
58 | parent) |
59 | { |
60 | } |
61 | |
62 | /*! |
63 | Destructs a QDBusServer |
64 | */ |
65 | QDBusServer::~QDBusServer() |
66 | { |
67 | if (!d) |
68 | return; |
69 | |
70 | auto manager = QDBusConnectionManager::instance(); |
71 | if (!manager) |
72 | return; |
73 | |
74 | QWriteLocker writeLocker(&d->lock); |
75 | manager->removeConnections(names: d->serverConnectionNames); |
76 | d->serverConnectionNames.clear(); |
77 | |
78 | d->serverObject = nullptr; |
79 | d->ref.storeRelaxed(newValue: 0); |
80 | d->deleteLater(); |
81 | } |
82 | |
83 | /*! |
84 | Returns \c true if this QDBusServer object is connected. |
85 | |
86 | If it isn't connected, you need to call the constructor again. |
87 | */ |
88 | bool QDBusServer::isConnected() const |
89 | { |
90 | return d && d->server && q_dbus_server_get_is_connected(server: d->server); |
91 | } |
92 | |
93 | /*! |
94 | Returns the last error that happened in this server. |
95 | |
96 | This function is provided for low-level code. |
97 | */ |
98 | QDBusError QDBusServer::lastError() const |
99 | { |
100 | return d ? d->lastError : QDBusError(QDBusError::Disconnected, QDBusUtil::disconnectedErrorMessage()); |
101 | } |
102 | |
103 | /*! |
104 | Returns the address this server is associated with. |
105 | */ |
106 | QString QDBusServer::address() const |
107 | { |
108 | QString addr; |
109 | if (d && d->server) { |
110 | char *c = q_dbus_server_get_address(server: d->server); |
111 | addr = QString::fromUtf8(utf8: c); |
112 | q_dbus_free(memory: c); |
113 | } |
114 | |
115 | return addr; |
116 | } |
117 | |
118 | /*! |
119 | \since 5.3 |
120 | |
121 | If \a value is set to true, an incoming connection can proceed even if the |
122 | connecting client is not authenticated as a user. |
123 | |
124 | By default, this value is false. |
125 | |
126 | \sa isAnonymousAuthenticationAllowed() |
127 | */ |
128 | void QDBusServer::setAnonymousAuthenticationAllowed(bool value) |
129 | { |
130 | if (!d) |
131 | return; |
132 | |
133 | d->anonymousAuthenticationAllowed = value; |
134 | } |
135 | |
136 | /*! |
137 | \since 5.3 |
138 | |
139 | Returns true if anonymous authentication is allowed. |
140 | |
141 | \sa setAnonymousAuthenticationAllowed() |
142 | */ |
143 | bool QDBusServer::isAnonymousAuthenticationAllowed() const |
144 | { |
145 | if (!d) |
146 | return false; |
147 | |
148 | return d->anonymousAuthenticationAllowed; |
149 | } |
150 | |
151 | /*! |
152 | \fn void QDBusServer::newConnection(const QDBusConnection &connection) |
153 | |
154 | This signal is emitted when a new client connection \a connection is |
155 | established to the server. |
156 | */ |
157 | |
158 | QT_END_NAMESPACE |
159 | |
160 | #include "moc_qdbusserver.cpp" |
161 | |
162 | #endif // QT_NO_DBUS |
163 | |