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 QtGui 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 <QtGui/qtgui-config.h>
41
42#ifndef QT_NO_SYSTEMTRAYICON
43#include "qdbustrayicon_p.h"
44#endif
45#include "qdbusmenuconnection_p.h"
46#include "qdbusmenuadaptor_p.h"
47#include "qdbusplatformmenu_p.h"
48
49#include <QtDBus/QDBusMessage>
50#include <QtDBus/QDBusServiceWatcher>
51#include <QtDBus/QDBusConnectionInterface>
52#include <qdebug.h>
53#include <qcoreapplication.h>
54
55QT_BEGIN_NAMESPACE
56
57Q_DECLARE_LOGGING_CATEGORY(qLcMenu)
58
59const QString StatusNotifierWatcherService = QLatin1String("org.kde.StatusNotifierWatcher");
60const QString StatusNotifierWatcherPath = QLatin1String("/StatusNotifierWatcher");
61const QString StatusNotifierItemPath = QLatin1String("/StatusNotifierItem");
62const QString MenuBarPath = QLatin1String("/MenuBar");
63
64/*!
65 \class QDBusMenuConnection
66 \internal
67 A D-Bus connection which is used for both menu and tray icon services.
68 Connects to the session bus and registers with the respective watcher services.
69*/
70QDBusMenuConnection::QDBusMenuConnection(QObject *parent, const QString &serviceName)
71 : QObject(parent)
72 , m_connection(serviceName.isNull() ? QDBusConnection::sessionBus()
73 : QDBusConnection::connectToBus(type: QDBusConnection::SessionBus, name: serviceName))
74 , m_dbusWatcher(new QDBusServiceWatcher(StatusNotifierWatcherService, m_connection, QDBusServiceWatcher::WatchForRegistration, this))
75 , m_statusNotifierHostRegistered(false)
76{
77#ifndef QT_NO_SYSTEMTRAYICON
78 QDBusInterface systrayHost(StatusNotifierWatcherService, StatusNotifierWatcherPath, StatusNotifierWatcherService, m_connection);
79 if (systrayHost.isValid() && systrayHost.property(name: "IsStatusNotifierHostRegistered").toBool())
80 m_statusNotifierHostRegistered = true;
81 else
82 qCDebug(qLcMenu) << "StatusNotifierHost is not registered";
83#endif
84}
85
86void QDBusMenuConnection::dbusError(const QDBusError &error)
87{
88 qWarning() << "QDBusTrayIcon encountered a D-Bus error:" << error;
89}
90
91#ifndef QT_NO_SYSTEMTRAYICON
92bool QDBusMenuConnection::registerTrayIconMenu(QDBusTrayIcon *item)
93{
94 bool success = connection().registerObject(path: MenuBarPath, object: item->menu());
95 if (!success) // success == false is normal, because the object may be already registered
96 qCDebug(qLcMenu) << "failed to register" << item->instanceId() << MenuBarPath;
97 return success;
98}
99
100void QDBusMenuConnection::unregisterTrayIconMenu(QDBusTrayIcon *item)
101{
102 if (item->menu())
103 connection().unregisterObject(path: MenuBarPath);
104}
105
106bool QDBusMenuConnection::registerTrayIcon(QDBusTrayIcon *item)
107{
108 bool success = connection().registerService(serviceName: item->instanceId());
109 if (!success) {
110 qWarning() << "failed to register service" << item->instanceId();
111 return false;
112 }
113
114 success = connection().registerObject(path: StatusNotifierItemPath, object: item);
115 if (!success) {
116 unregisterTrayIcon(item);
117 qWarning() << "failed to register" << item->instanceId() << StatusNotifierItemPath;
118 return false;
119 }
120
121 if (item->menu())
122 registerTrayIconMenu(item);
123
124 return registerTrayIconWithWatcher(item);
125}
126
127bool QDBusMenuConnection::registerTrayIconWithWatcher(QDBusTrayIcon *item)
128{
129 QDBusMessage registerMethod = QDBusMessage::createMethodCall(
130 destination: StatusNotifierWatcherService, path: StatusNotifierWatcherPath, interface: StatusNotifierWatcherService,
131 method: QLatin1String("RegisterStatusNotifierItem"));
132 registerMethod.setArguments(QVariantList() << item->instanceId());
133 return m_connection.callWithCallback(message: registerMethod, receiver: this, SIGNAL(trayIconRegistered()), SLOT(dbusError(QDBusError)));
134}
135
136bool QDBusMenuConnection::unregisterTrayIcon(QDBusTrayIcon *item)
137{
138 unregisterTrayIconMenu(item);
139 connection().unregisterObject(path: StatusNotifierItemPath);
140 bool success = connection().unregisterService(serviceName: item->instanceId());
141 if (!success)
142 qWarning() << "failed to unregister service" << item->instanceId();
143 return success;
144}
145#endif // QT_NO_SYSTEMTRAYICON
146
147QT_END_NAMESPACE
148

source code of qtbase/src/platformsupport/themes/genericunix/dbusmenu/qdbusmenuconnection.cpp