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
25
26const QString StatusNotifierWatcherService = "org.kde.StatusNotifierWatcher"_L1;
27const QString StatusNotifierWatcherPath = "/StatusNotifierWatcher"_L1;
28const QString StatusNotifierItemPath = "/StatusNotifierItem"_L1;
29const QString MenuBarPath = "/MenuBar"_L1;
30
38 : QObject(parent)
39 , m_serviceName(serviceName)
40 , m_connection(serviceName.isNull() ? QDBusConnection::sessionBus()
41 : QDBusConnection::connectToBus(QDBusConnection::SessionBus, serviceName))
42 , m_dbusWatcher(new QDBusServiceWatcher(StatusNotifierWatcherService, m_connection, QDBusServiceWatcher::WatchForRegistration, this))
43 , m_watcherRegistered(false)
44{
45#ifndef QT_NO_SYSTEMTRAYICON
46 // Start monitoring if any known tray-related services are registered.
47 if (m_connection.interface()->isServiceRegistered(StatusNotifierWatcherService))
48 m_watcherRegistered = true;
49 else
50 qCDebug(qLcMenu) << "failed to find service" << StatusNotifierWatcherService;
51#endif
52}
53
55{
56 if (!m_serviceName.isEmpty() && m_connection.isConnected())
58}
59
60void QDBusMenuConnection::dbusError(const QDBusError &error)
61{
62 qWarning() << "QDBusTrayIcon encountered a D-Bus error:" << error;
63}
64
65#ifndef QT_NO_SYSTEMTRAYICON
67{
68 bool success = connection().registerObject(MenuBarPath, item->menu());
69 if (!success) // success == false is normal, because the object may be already registered
70 qCDebug(qLcMenu) << "failed to register" << item->instanceId() << MenuBarPath;
71 return success;
72}
73
79
81{
83 if (!success) {
85 qWarning() << "failed to register" << item->instanceId() << StatusNotifierItemPath;
86 return false;
87 }
88
89 if (item->menu())
91
93}
94
96{
100 "RegisterStatusNotifierItem"_L1);
101 registerMethod.setArguments(QVariantList() << m_connection.baseService());
102 return m_connection.callWithCallback(registerMethod, this, SIGNAL(trayIconRegistered()), SLOT(dbusError(QDBusError)));
103}
104
110#endif // QT_NO_SYSTEMTRAYICON
111
113
114#include "moc_qdbusmenuconnection_p.cpp"
\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.
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
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:166
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
#define Q_UNUSED(x)
QGraphicsItem * item