Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/dbus/qdbusconnectioninterface.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count |
---|---|---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - |
4 | ** Contact: https://www.qt.io/licensing/ | - |
5 | ** | - |
6 | ** This file is part of the QtDBus module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and The Qt Company. For licensing terms | - |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - |
15 | ** information use the contact form at https://www.qt.io/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 3 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - |
24 | ** | - |
25 | ** GNU General Public License Usage | - |
26 | ** Alternatively, this file may be used under the terms of the GNU | - |
27 | ** General Public License version 2.0 or (at your option) the GNU General | - |
28 | ** Public license version 3 or any later version approved by the KDE Free | - |
29 | ** Qt Foundation. The licenses are as published by the Free Software | - |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - |
31 | ** included in the packaging of this file. Please review the following | - |
32 | ** information to ensure the GNU General Public License requirements will | - |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - |
35 | ** | - |
36 | ** $QT_END_LICENSE$ | - |
37 | ** | - |
38 | ****************************************************************************/ | - |
39 | - | |
40 | #include "qdbusconnectioninterface.h" | - |
41 | - | |
42 | #include <QtCore/QByteArray> | - |
43 | #include <QtCore/QList> | - |
44 | #include <QtCore/QMap> | - |
45 | #include <QtCore/QMetaMethod> | - |
46 | #include <QtCore/QString> | - |
47 | #include <QtCore/QStringList> | - |
48 | #include <QtCore/QVariant> | - |
49 | #include <QtCore/QDebug> | - |
50 | - | |
51 | #include "qdbusutil_p.h" // for the DBUS_* constants | - |
52 | - | |
53 | #ifndef QT_NO_DBUS | - |
54 | - | |
55 | QT_BEGIN_NAMESPACE | - |
56 | - | |
57 | /* | - |
58 | * Implementation of interface class QDBusConnectionInterface | - |
59 | */ | - |
60 | - | |
61 | /*! | - |
62 | \class QDBusConnectionInterface | - |
63 | \inmodule QtDBus | - |
64 | \since 4.2 | - |
65 | - | |
66 | \brief The QDBusConnectionInterface class provides access to the D-Bus bus daemon service. | - |
67 | - | |
68 | The D-Bus bus server daemon provides one special interface \c | - |
69 | org.freedesktop.DBus that allows clients to access certain | - |
70 | properties of the bus, such as the current list of clients | - |
71 | connected. The QDBusConnectionInterface class provides access to that | - |
72 | interface. | - |
73 | - | |
74 | The most common uses of this class are to register and unregister | - |
75 | service names on the bus using the registerService() and | - |
76 | unregisterService() functions, query about existing names using | - |
77 | the isServiceRegistered(), registeredServiceNames() and | - |
78 | serviceOwner() functions, and to receive notification that a | - |
79 | client has registered or de-registered through the | - |
80 | serviceRegistered(), serviceUnregistered() and serviceOwnerChanged() | - |
81 | signals. | - |
82 | */ | - |
83 | - | |
84 | /*! | - |
85 | \enum QDBusConnectionInterface::ServiceQueueOptions | - |
86 | - | |
87 | Flags for determining how a service registration should behave, in | - |
88 | case the service name is already registered. | - |
89 | - | |
90 | \value DontQueueService If an application requests a name that | - |
91 | is already owned, no queueing will be | - |
92 | performed. The registeredService() | - |
93 | call will simply fail. | - |
94 | This is the default. | - |
95 | - | |
96 | \value QueueService Attempts to register the requested | - |
97 | service, but do not try to replace it | - |
98 | if another application already has it | - |
99 | registered. Instead, simply put this | - |
100 | application in queue, until it is | - |
101 | given up. The serviceRegistered() | - |
102 | signal will be emitted when that | - |
103 | happens. | - |
104 | - | |
105 | \value ReplaceExistingService If another application already has | - |
106 | the service name registered, attempt | - |
107 | to replace it. | - |
108 | - | |
109 | \sa ServiceReplacementOptions | - |
110 | */ | - |
111 | - | |
112 | /*! | - |
113 | \enum QDBusConnectionInterface::ServiceReplacementOptions | - |
114 | - | |
115 | Flags for determining if the D-Bus server should allow another | - |
116 | application to replace a name that this application has registered | - |
117 | with the ReplaceExistingService option. | - |
118 | - | |
119 | The possible values are: | - |
120 | - | |
121 | \value DontAllowReplacement Do not allow another application to | - |
122 | replace us. The service must be | - |
123 | explicitly unregistered with | - |
124 | unregisterService() for another | - |
125 | application to acquire it. | - |
126 | This is the default. | - |
127 | - | |
128 | \value AllowReplacement Allow other applications to replace us | - |
129 | with the ReplaceExistingService option | - |
130 | to registerService() without | - |
131 | intervention. If that happens, the | - |
132 | serviceUnregistered() signal will be | - |
133 | emitted. | - |
134 | - | |
135 | \sa ServiceQueueOptions | - |
136 | */ | - |
137 | - | |
138 | /*! | - |
139 | \enum QDBusConnectionInterface::RegisterServiceReply | - |
140 | - | |
141 | The possible return values from registerService(): | - |
142 | - | |
143 | \value ServiceNotRegistered The call failed and the service name was not registered. | - |
144 | \value ServiceRegistered The caller is now the owner of the service name. | - |
145 | \value ServiceQueued The caller specified the QueueService flag and the | - |
146 | service was already registered, so we are in queue. | - |
147 | - | |
148 | The serviceRegistered() signal will be emitted when the service is | - |
149 | acquired by this application. | - |
150 | */ | - |
151 | - | |
152 | /*! | - |
153 | \internal | - |
154 | */ | - |
155 | const char *QDBusConnectionInterface::staticInterfaceName() | - |
156 | { return "org.freedesktop.DBus"; } | - |
157 | - | |
158 | /*! | - |
159 | \internal | - |
160 | */ | - |
161 | QDBusConnectionInterface::QDBusConnectionInterface(const QDBusConnection &connection, | - |
162 | QObject *parent) | - |
163 | : QDBusAbstractInterface(QDBusUtil::dbusService(), | - |
164 | QDBusUtil::dbusPath(), | - |
165 | DBUS_INTERFACE_DBUS, connection, parent) | - |
166 | { | - |
167 | connect(this, SIGNAL(&QDBusConnectionInterface::NameAcquired(QString)),, this, SIGNAL(emit &QDBusConnectionInterface::serviceRegistered(QString)));); | - |
168 | connect(this, SIGNAL(&QDBusConnectionInterface::NameLost(QString)),, this, SIGNAL(emit &QDBusConnectionInterface::serviceUnregistered(QString)));); | - |
169 | connect(this, SIGNAL(&QDBusConnectionInterface::NameOwnerChanged(QString,QString, | - |
170 | QString)),this, SIGNAL(emit &QDBusConnectionInterface::serviceOwnerChanged(QString,QString,QString)));); | - |
171 | } executed 326 times by 160 tests: end of block Executed by:
| 326 |
172 | - | |
173 | /*! | - |
174 | \internal | - |
175 | */ | - |
176 | QDBusConnectionInterface::~QDBusConnectionInterface() | - |
177 | { | - |
178 | } | - |
179 | - | |
180 | /*! | - |
181 | Returns the unique connection name of the primary owner of the | - |
182 | name \a name. If the requested name doesn't have an owner, returns | - |
183 | a \c org.freedesktop.DBus.Error.NameHasNoOwner error. | - |
184 | */ | - |
185 | QDBusReply<QString> QDBusConnectionInterface::serviceOwner(const QString &name) const | - |
186 | { | - |
187 | return internalConstCall(QDBus::AutoDetect, QLatin1String("GetNameOwner"), QList<QVariant>() << name); | - |
188 | } | - |
189 | - | |
190 | /*! | - |
191 | \property QDBusConnectionInterface::registeredServiceNames | - |
192 | \brief holds the registered service names | - |
193 | - | |
194 | Lists all names currently registered on the bus. | - |
195 | */ | - |
196 | QDBusReply<QStringList> QDBusConnectionInterface::registeredServiceNames() const | - |
197 | { | - |
198 | return internalConstCall(QDBus::AutoDetect, QLatin1String("ListNames")); | - |
199 | } | - |
200 | - | |
201 | /*! | - |
202 | Returns \c true if the service name \a serviceName has is currently | - |
203 | registered. | - |
204 | */ | - |
205 | QDBusReply<bool> QDBusConnectionInterface::isServiceRegistered(const QString &serviceName) const | - |
206 | { | - |
207 | return internalConstCall(QDBus::AutoDetect, QLatin1String("NameHasOwner"), | - |
208 | QList<QVariant>() << serviceName); | - |
209 | } | - |
210 | - | |
211 | /*! | - |
212 | Returns the Unix Process ID (PID) for the process currently | - |
213 | holding the bus service \a serviceName. | - |
214 | */ | - |
215 | QDBusReply<uint> QDBusConnectionInterface::servicePid(const QString &serviceName) const | - |
216 | { | - |
217 | return internalConstCall(QDBus::AutoDetect, QLatin1String("GetConnectionUnixProcessID"), | - |
218 | QList<QVariant>() << serviceName); | - |
219 | } | - |
220 | - | |
221 | /*! | - |
222 | Returns the Unix User ID (UID) for the process currently holding | - |
223 | the bus service \a serviceName. | - |
224 | */ | - |
225 | QDBusReply<uint> QDBusConnectionInterface::serviceUid(const QString &serviceName) const | - |
226 | { | - |
227 | return internalConstCall(QDBus::AutoDetect, QLatin1String("GetConnectionUnixUser"), | - |
228 | QList<QVariant>() << serviceName); | - |
229 | } | - |
230 | - | |
231 | /*! | - |
232 | Requests that the bus start the service given by the name \a name. | - |
233 | */ | - |
234 | QDBusReply<void> QDBusConnectionInterface::startService(const QString &name) | - |
235 | { | - |
236 | return call(QLatin1String("StartServiceByName"), name, uint(0)); | - |
237 | } | - |
238 | - | |
239 | /*! | - |
240 | Requests to register the service name \a serviceName on the | - |
241 | bus. The \a qoption flag specifies how the D-Bus server should behave | - |
242 | if \a serviceName is already registered. The \a roption flag | - |
243 | specifies if the server should allow another application to | - |
244 | replace our registered name. | - |
245 | - | |
246 | If the service registration succeeds, the serviceRegistered() | - |
247 | signal will be emitted. If we are placed in queue, the signal will | - |
248 | be emitted when we obtain the name. If \a roption is | - |
249 | AllowReplacement, the serviceUnregistered() signal will be emitted | - |
250 | if another application replaces this one. | - |
251 | - | |
252 | \sa unregisterService() | - |
253 | */ | - |
254 | QDBusReply<QDBusConnectionInterface::RegisterServiceReply> | - |
255 | QDBusConnectionInterface::registerService(const QString &serviceName, | - |
256 | ServiceQueueOptions qoption, | - |
257 | ServiceReplacementOptions roption) | - |
258 | { | - |
259 | // reconstruct the low-level flags | - |
260 | uint flags = 0; | - |
261 | switch (qoption) { | - |
262 | case DontQueueService: | - |
263 | flags = DBUS_NAME_FLAG_DO_NOT_QUEUE; | - |
264 | break; | - |
265 | case QueueService: | - |
266 | flags = 0; | - |
267 | break; | - |
268 | case ReplaceExistingService: | - |
269 | flags = DBUS_NAME_FLAG_DO_NOT_QUEUE | DBUS_NAME_FLAG_REPLACE_EXISTING; | - |
270 | break; | - |
271 | } | - |
272 | - | |
273 | switch (roption) { | - |
274 | case DontAllowReplacement: | - |
275 | break; | - |
276 | case AllowReplacement: | - |
277 | flags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT; | - |
278 | break; | - |
279 | } | - |
280 | - | |
281 | QDBusMessage reply = call(QLatin1String("RequestName"), serviceName, flags); | - |
282 | // qDebug() << "QDBusConnectionInterface::registerService" << serviceName << "Reply:" << reply; | - |
283 | - | |
284 | // convert the low-level flags to something that we can use | - |
285 | if (reply.type() == QDBusMessage::ReplyMessage) { | - |
286 | uint code = 0; | - |
287 | - | |
288 | switch (reply.arguments().at(0).toUInt()) { | - |
289 | case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: | - |
290 | case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER: | - |
291 | code = uint(ServiceRegistered); | - |
292 | break; | - |
293 | - | |
294 | case DBUS_REQUEST_NAME_REPLY_EXISTS: | - |
295 | code = uint(ServiceNotRegistered); | - |
296 | break; | - |
297 | - | |
298 | case DBUS_REQUEST_NAME_REPLY_IN_QUEUE: | - |
299 | code = uint(ServiceQueued); | - |
300 | break; | - |
301 | } | - |
302 | - | |
303 | reply.setArguments(QVariantList() << code); | - |
304 | } | - |
305 | - | |
306 | return reply; | - |
307 | } | - |
308 | - | |
309 | /*! | - |
310 | Releases the claim on the bus service name \a serviceName, that | - |
311 | had been previously registered with registerService(). If this | - |
312 | application had ownership of the name, it will be released for | - |
313 | other applications to claim. If it only had the name queued, it | - |
314 | gives up its position in the queue. | - |
315 | */ | - |
316 | QDBusReply<bool> | - |
317 | QDBusConnectionInterface::unregisterService(const QString &serviceName) | - |
318 | { | - |
319 | QDBusMessage reply = call(QLatin1String("ReleaseName"), serviceName); | - |
320 | if (reply.type() == QDBusMessage::ReplyMessage) { | - |
321 | bool success = reply.arguments().at(0).toUInt() == DBUS_RELEASE_NAME_REPLY_RELEASED; | - |
322 | reply.setArguments(QVariantList() << success); | - |
323 | } | - |
324 | return reply; | - |
325 | } | - |
326 | - | |
327 | /*! | - |
328 | \internal | - |
329 | */ | - |
330 | void QDBusConnectionInterface::connectNotify(const QMetaMethod &signal) | - |
331 | { | - |
332 | // translate the signal names to what we really want | - |
333 | // this avoids setting hooks for signals that don't exist on the bus | - |
334 | static const QMetaMethod serviceRegisteredSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::serviceRegistered); | - |
335 | static const QMetaMethod serviceUnregisteredSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::serviceUnregistered); | - |
336 | static const QMetaMethod serviceOwnerChangedSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::serviceOwnerChanged); | - |
337 | static const QMetaMethod NameAcquiredSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::NameAcquired); | - |
338 | static const QMetaMethod NameLostSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::NameLost); | - |
339 | static const QMetaMethod NameOwnerChangedSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::NameOwnerChanged); | - |
340 | if (signal == serviceRegisteredSignal) | - |
341 | QDBusAbstractInterface::connectNotify(NameAcquiredSignal); | - |
342 | - | |
343 | else if (signal == serviceUnregisteredSignal) | - |
344 | QDBusAbstractInterface::connectNotify(NameLostSignal); | - |
345 | - | |
346 | else if (signal == serviceOwnerChangedSignal) { | - |
347 | static bool warningPrinted = false; | - |
348 | if (!warningPrinted) { | - |
349 | qWarning("Connecting to deprecated signal QDBusConnectionInterface::serviceOwnerChanged(QString,QString,QString)"); | - |
350 | warningPrinted = true; | - |
351 | } | - |
352 | QDBusAbstractInterface::connectNotify(NameOwnerChangedSignal); | - |
353 | } | - |
354 | } | - |
355 | - | |
356 | /*! | - |
357 | \internal | - |
358 | */ | - |
359 | void QDBusConnectionInterface::disconnectNotify(const QMetaMethod &signal) | - |
360 | { | - |
361 | // translate the signal names to what we really want | - |
362 | // this avoids setting hooks for signals that don't exist on the bus | - |
363 | static const QMetaMethod serviceRegisteredSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::serviceRegistered); | - |
364 | static const QMetaMethod serviceUnregisteredSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::serviceUnregistered); | - |
365 | static const QMetaMethod serviceOwnerChangedSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::serviceOwnerChanged); | - |
366 | static const QMetaMethod NameAcquiredSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::NameAcquired); | - |
367 | static const QMetaMethod NameLostSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::NameLost); | - |
368 | static const QMetaMethod NameOwnerChangedSignal = QMetaMethod::fromSignal(&QDBusConnectionInterface::NameOwnerChanged); | - |
369 | if (signal == serviceRegisteredSignal) | - |
370 | QDBusAbstractInterface::disconnectNotify(NameAcquiredSignal); | - |
371 | - | |
372 | else if (signal == serviceUnregisteredSignal) | - |
373 | QDBusAbstractInterface::disconnectNotify(NameLostSignal); | - |
374 | - | |
375 | else if (signal == serviceOwnerChangedSignal) | - |
376 | QDBusAbstractInterface::disconnectNotify(NameOwnerChangedSignal); | - |
377 | } | - |
378 | - | |
379 | // signals | - |
380 | /*! | - |
381 | \fn QDBusConnectionInterface::serviceRegistered(const QString &serviceName) | - |
382 | - | |
383 | This signal is emitted by the D-Bus server when the bus service | - |
384 | name (unique connection name or well-known service name) given by | - |
385 | \a serviceName is acquired by this application. | - |
386 | - | |
387 | Acquisition happens after this application has requested a name using | - |
388 | registerService(). | - |
389 | */ | - |
390 | - | |
391 | /*! | - |
392 | \fn QDBusConnectionInterface::serviceUnregistered(const QString &serviceName) | - |
393 | - | |
394 | This signal is emitted by the D-Bus server when this application | - |
395 | loses ownership of the bus service name given by \a serviceName. | - |
396 | */ | - |
397 | - | |
398 | /*! | - |
399 | \fn QDBusConnectionInterface::serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner) | - |
400 | \deprecated | - |
401 | - | |
402 | Use QDBusServiceWatcher instead. | - |
403 | - | |
404 | This signal is emitted by the D-Bus server whenever a service | - |
405 | ownership change happens in the bus, including apparition and | - |
406 | disparition of names. | - |
407 | - | |
408 | This signal means the application \a oldOwner lost ownership of | - |
409 | bus name \a name to application \a newOwner. If \a oldOwner is an | - |
410 | empty string, it means the name \a name has just been created; if | - |
411 | \a newOwner is empty, the name \a name has no current owner and is | - |
412 | no longer available. | - |
413 | - | |
414 | \note connecting to this signal will make the application listen for and | - |
415 | receive every single service ownership change on the bus. Depending on | - |
416 | how many services are running, this make the application be activated to | - |
417 | receive more signals than it needs. To avoid this problem, use the | - |
418 | QDBusServiceWatcher class, which can listen for specific changes. | - |
419 | */ | - |
420 | - | |
421 | /*! | - |
422 | \fn void QDBusConnectionInterface::callWithCallbackFailed(const QDBusError &error, const QDBusMessage &call) | - |
423 | - | |
424 | This signal is emitted when there is an error during a | - |
425 | QDBusConnection::callWithCallback(). \a error specifies the error. | - |
426 | \a call is the message that couldn't be delivered. | - |
427 | - | |
428 | \sa QDBusConnection::callWithCallback() | - |
429 | */ | - |
430 | - | |
431 | QT_END_NAMESPACE | - |
432 | - | |
433 | #endif // QT_NO_DBUS | - |
Source code | Switch to Preprocessed file |