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
qdbusmenuconnection.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
4#include <QtGui/qtgui-config.h>
5
6#include <QDBusMessage>
7#include <QDBusInterface>
8#include <QDBusServiceWatcher>
9#include <QDBusConnectionInterface>
10#include <QDebug>
11#include <QCoreApplication>
12
13#ifndef QT_NO_SYSTEMTRAYICON
14#include <private/qdbustrayicon_p.h>
15#endif
16#include <private/qdbusmenuconnection_p.h>
17#include <private/qdbusmenuadaptor_p.h>
18#include <private/qdbusplatformmenu_p.h>
19
21
22using namespace Qt::StringLiterals;
23
24const QString StatusNotifierWatcherService = "org.kde.StatusNotifierWatcher"_L1;
25const QString StatusNotifierWatcherPath = "/StatusNotifierWatcher"_L1;
26const QString StatusNotifierItemPath = "/StatusNotifierItem"_L1;
27const QString MenuBarPath = "/MenuBar"_L1;
28
36 : QObject(parent)
37 , m_serviceName(serviceName)
38 , m_connection(serviceName.isNull() ? QDBusConnection::sessionBus()
39 : QDBusConnection::connectToBus(QDBusConnection::SessionBus, serviceName))
40 , m_dbusWatcher(new QDBusServiceWatcher(StatusNotifierWatcherService, m_connection, QDBusServiceWatcher::WatchForRegistration, this))
41 , m_watcherRegistered(false)
42{
43#ifndef QT_NO_SYSTEMTRAYICON
44 // Start monitoring if any known tray-related services are registered.
46 m_watcherRegistered = true;
47 else
48 qCDebug(qLcMenu) << "failed to find service" << StatusNotifierWatcherService;
49#endif
50}
51
53{
54 if (!m_serviceName.isEmpty() && m_connection.isConnected())
56}
57
58void QDBusMenuConnection::dbusError(const QDBusError &error)
59{
60 qWarning() << "QDBusTrayIcon encountered a D-Bus error:" << error;
61}
62
63#ifndef QT_NO_SYSTEMTRAYICON
65{
66 bool success = connection().registerObject(MenuBarPath, item->menu());
67 if (!success) // success == false is normal, because the object may be already registered
68 qCDebug(qLcMenu) << "failed to register" << item->instanceId() << MenuBarPath;
69 return success;
70}
71
77
79{
81 if (!success) {
83 qWarning() << "failed to register" << item->instanceId() << StatusNotifierItemPath;
84 return false;
85 }
86
87 if (item->menu())
89
91}
92
94{
98 "RegisterStatusNotifierItem"_L1);
99 registerMethod.setArguments(QVariantList() << m_connection.baseService());
100 return m_connection.callWithCallback(registerMethod, this, SIGNAL(trayIconRegistered()), SLOT(dbusError(QDBusError)));
101}
102
108#endif // QT_NO_SYSTEMTRAYICON
109
111
112#include "moc_qdbusmenuconnection_p.cpp"
QDBusReply< bool > isServiceRegistered(const QString &serviceName) const
Returns true if the service name serviceName has is currently registered.
\inmodule QtDBus
bool callWithCallback(const QDBusMessage &message, QObject *receiver, const char *returnMethod, const char *errorMethod, int timeout=-1) const
Sends the message over this connection and returns immediately.
QDBusConnectionInterface * interface() const
Returns a QDBusConnectionInterface object that represents the D-Bus server interface on this connecti...
bool isConnected() const
Returns true if this QDBusConnection object is connected.
static void disconnectFromBus(const QString &name)
Closes the bus connection of name name.
void unregisterObject(const QString &path, UnregisterMode mode=UnregisterNode)
Unregisters an object that was registered with the registerObject() at the object path given by path ...
QString baseService() const
Returns the unique connection name for this connection, if this QDBusConnection object is connected,...
bool registerObject(const QString &path, QObject *object, RegisterOptions options=ExportAdaptors)
Registers the object object at path path and returns true if the registration was successful.
\inmodule QtDBus
Definition qdbuserror.h:21
QDBusMenuConnection(QObject *parent=nullptr, const QString &serviceName=QString())
bool registerTrayIconMenu(QDBusTrayIcon *item)
bool registerTrayIcon(QDBusTrayIcon *item)
void unregisterTrayIconMenu(QDBusTrayIcon *item)
void unregisterTrayIcon(QDBusTrayIcon *item)
bool registerTrayIconWithWatcher(QDBusTrayIcon *item)
QDBusConnection connection() const
\inmodule QtDBus
static QDBusMessage createMethodCall(const QString &destination, const QString &path, const QString &interface, const QString &method)
Constructs a new DBus message representing a method call.
The QDBusServiceWatcher class allows the user to watch for a bus service change.
\inmodule QtCore
Definition qobject.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
const QString StatusNotifierWatcherService
const QString StatusNotifierWatcherPath
const QString StatusNotifierItemPath
const QString MenuBarPath
QList< QVariant > QVariantList
Definition qjsonarray.h:15
#define qWarning
Definition qlogging.h:167
#define qCDebug(category,...)
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
#define Q_UNUSED(x)
QGraphicsItem * item