Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdbusconnectioninterface.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/QByteArray>
7#include <QtCore/QList>
8#include <QtCore/QMap>
9#include <QtCore/QMetaMethod>
10#include <QtCore/QString>
11#include <QtCore/QStringList>
12#include <QtCore/QVariant>
13#include <QtCore/QDebug>
14
15#include "qdbusutil_p.h" // for the DBUS_* constants
16
17#ifndef QT_NO_DBUS
18
20
21using namespace Qt::StringLiterals;
22
23/*
24 * Implementation of interface class QDBusConnectionInterface
25 */
26
121const char *QDBusConnectionInterface::staticInterfaceName()
122{ return "org.freedesktop.DBus"; }
123
127QDBusConnectionInterface::QDBusConnectionInterface(const QDBusConnection &connection,
128 QObject *parent)
132{
137}
138
142QDBusConnectionInterface::~QDBusConnectionInterface()
143{
144}
145
151QDBusReply<QString> QDBusConnectionInterface::serviceOwner(const QString &name) const
152{
153 return internalConstCall(QDBus::AutoDetect, "GetNameOwner"_L1, QList<QVariant>() << name);
154}
155
163{
164 return internalConstCall(QDBus::AutoDetect, "ListNames"_L1);
165}
166
175{
176 return internalConstCall(QDBus::AutoDetect, "ListActivatableNames"_L1);
177}
178
183QDBusReply<bool> QDBusConnectionInterface::isServiceRegistered(const QString &serviceName) const
184{
185 return internalConstCall(QDBus::AutoDetect, "NameHasOwner"_L1,
186 QList<QVariant>() << serviceName);
187}
188
193QDBusReply<uint> QDBusConnectionInterface::servicePid(const QString &serviceName) const
194{
195 return internalConstCall(QDBus::AutoDetect, "GetConnectionUnixProcessID"_L1,
196 QList<QVariant>() << serviceName);
197}
198
203QDBusReply<uint> QDBusConnectionInterface::serviceUid(const QString &serviceName) const
204{
205 return internalConstCall(QDBus::AutoDetect, "GetConnectionUnixUser"_L1,
206 QList<QVariant>() << serviceName);
207}
208
213{
214 return call("StartServiceByName"_L1, name, uint(0));
215}
216
232QDBusReply<QDBusConnectionInterface::RegisterServiceReply>
234 ServiceQueueOptions qoption,
236{
237 // reconstruct the low-level flags
238 uint flags = 0;
239 switch (qoption) {
240 case DontQueueService:
242 break;
243 case QueueService:
244 flags = 0;
245 break;
248 break;
249 }
250
251 switch (roption) {
253 break;
254 case AllowReplacement:
256 break;
257 }
258
259 QDBusMessage reply = call("RequestName"_L1, serviceName, flags);
260// qDebug() << "QDBusConnectionInterface::registerService" << serviceName << "Reply:" << reply;
261
262 // convert the low-level flags to something that we can use
263 if (reply.type() == QDBusMessage::ReplyMessage) {
264 uint code = 0;
265
266 switch (reply.arguments().at(0).toUInt()) {
269 code = uint(ServiceRegistered);
270 break;
271
274 break;
275
277 code = uint(ServiceQueued);
278 break;
279 }
280
281 reply.setArguments(QVariantList() << code);
282 }
283
284 return reply;
285}
286
294QDBusReply<bool>
296{
297 QDBusMessage reply = call("ReleaseName"_L1, serviceName);
298 if (reply.type() == QDBusMessage::ReplyMessage) {
299 bool success = reply.arguments().at(0).toUInt() == DBUS_RELEASE_NAME_REPLY_RELEASED;
300 reply.setArguments(QVariantList() << success);
301 }
302 return reply;
303}
304
309{
310 // translate the signal names to what we really want
311 // this avoids setting hooks for signals that don't exist on the bus
318 if (signal == serviceRegisteredSignal)
319 QDBusAbstractInterface::connectNotify(NameAcquiredSignal);
320
321 else if (signal == serviceUnregisteredSignal)
323
324 else if (signal == serviceOwnerChangedSignal) {
325 static bool warningPrinted = false;
326 if (!warningPrinted) {
327 qWarning("Connecting to deprecated signal QDBusConnectionInterface::serviceOwnerChanged(QString,QString,QString)");
328 warningPrinted = true;
329 }
330 QDBusAbstractInterface::connectNotify(NameOwnerChangedSignal);
331 }
332}
333
338{
339 // translate the signal names to what we really want
340 // this avoids setting hooks for signals that don't exist on the bus
347 if (signal == serviceRegisteredSignal)
349
350 else if (signal == serviceUnregisteredSignal)
352
353 else if (signal == serviceOwnerChangedSignal)
354 QDBusAbstractInterface::disconnectNotify(NameOwnerChangedSignal);
355}
356
357// signals
410
411#include "moc_qdbusconnectioninterface.cpp"
412
413#endif // QT_NO_DBUS
QDBusMessage call(const QString &method)
void disconnectNotify(const QMetaMethod &signal) override
void connectNotify(const QMetaMethod &signal) override
QDBusMessage internalConstCall(QDBus::CallMode mode, const QString &method, const QList< QVariant > &args=QList< QVariant >()) const
void serviceRegistered(const QString &service)
This signal is emitted by the D-Bus server when the bus service name (unique connection name or well-...
void connectNotify(const QMetaMethod &) override
void NameAcquired(const QString &)
QDBusReply< uint > serviceUid(const QString &serviceName) const
Returns the Unix User ID (UID) for the process currently holding the bus service serviceName.
void disconnectNotify(const QMetaMethod &) override
ServiceQueueOptions
Flags for determining how a service registration should behave, in case the service name is already r...
QDBusReply< void > startService(const QString &name)
Requests that the bus start the service given by the name name.
ServiceReplacementOptions
Flags for determining if the D-Bus server should allow another application to replace a name that thi...
QDBusReply< bool > unregisterService(const QString &serviceName)
Releases the claim on the bus service name serviceName, that had been previously registered with regi...
QDBusReply< QStringList > activatableServiceNames
holds the activatable service names
QDBusReply< bool > isServiceRegistered(const QString &serviceName) const
Returns true if the service name serviceName has is currently registered.
void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
QDBusReply< uint > servicePid(const QString &serviceName) const
Returns the Unix Process ID (PID) for the process currently holding the bus service serviceName.
QDBusReply< QDBusConnectionInterface::RegisterServiceReply > registerService(const QString &serviceName, ServiceQueueOptions qoption=DontQueueService, ServiceReplacementOptions roption=DontAllowReplacement)
Requests to register the service name serviceName on the bus.
QDBusReply< QStringList > registeredServiceNames
holds the registered service names
void serviceUnregistered(const QString &service)
This signal is emitted by the D-Bus server when this application loses ownership of the bus service n...
void NameOwnerChanged(const QString &, const QString &, const QString &)
QDBusReply< QString > serviceOwner(const QString &name) const
Returns the unique connection name of the primary owner of the name name.
void NameLost(const QString &)
\inmodule QtDBus
\inmodule QtDBus
\inmodule QtCore
Definition qmetaobject.h:19
static QMetaMethod fromSignal(PointerToMemberFunction signal)
\inmodule QtCore
Definition qobject.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
#define DBUS_NAME_FLAG_REPLACE_EXISTING
#define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
#define DBUS_NAME_FLAG_DO_NOT_QUEUE
#define DBUS_NAME_FLAG_ALLOW_REPLACEMENT
#define DBUS_INTERFACE_DBUS
#define DBUS_REQUEST_NAME_REPLY_EXISTS
#define DBUS_REQUEST_NAME_REPLY_IN_QUEUE
#define DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
#define DBUS_RELEASE_NAME_REPLY_RELEASED
auto signal
\inmodule QtDBus
QString dbusPath()
QString dbusService()
Combined button and popup list for selecting options.
DBusConnection * connection
QList< QVariant > QVariantList
Definition qjsonarray.h:15
#define qWarning
Definition qlogging.h:166
GLbitfield flags
GLuint name
#define emit
unsigned int uint
Definition qtypes.h:34
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QNetworkReply * reply