| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Dmitry Shachnev <mitya57@gmail.com> | 
| 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 "qdbusmenubar_p.h" | 
| 41 | #include "qdbusmenuregistrarproxy_p.h" | 
| 42 |  | 
| 43 | QT_BEGIN_NAMESPACE | 
| 44 |  | 
| 45 | /* note: do not change these to QStringLiteral; | 
| 46 |    we are unloaded before QtDBus is done using the strings. | 
| 47 |  */ | 
| 48 | #define REGISTRAR_SERVICE QLatin1String("com.canonical.AppMenu.Registrar") | 
| 49 | #define REGISTRAR_PATH QLatin1String("/com/canonical/AppMenu/Registrar") | 
| 50 |  | 
| 51 | QDBusMenuBar::() | 
| 52 |     : QPlatformMenuBar() | 
| 53 |     , m_menu(new QDBusPlatformMenu()) | 
| 54 |     , m_menuAdaptor(new QDBusMenuAdaptor(m_menu)) | 
| 55 |     , m_windowId(0) | 
| 56 | { | 
| 57 |     QDBusMenuItem::registerDBusTypes(); | 
| 58 |     connect(sender: m_menu, signal: &QDBusPlatformMenu::propertiesUpdated, | 
| 59 |             receiver: m_menuAdaptor, slot: &QDBusMenuAdaptor::ItemsPropertiesUpdated); | 
| 60 |     connect(sender: m_menu, signal: &QDBusPlatformMenu::updated, | 
| 61 |             receiver: m_menuAdaptor, slot: &QDBusMenuAdaptor::LayoutUpdated); | 
| 62 |     connect(sender: m_menu, signal: &QDBusPlatformMenu::popupRequested, | 
| 63 |             receiver: m_menuAdaptor, slot: &QDBusMenuAdaptor::ItemActivationRequested); | 
| 64 | } | 
| 65 |  | 
| 66 | QDBusMenuBar::() | 
| 67 | { | 
| 68 |     unregisterMenuBar(); | 
| 69 |     delete m_menuAdaptor; | 
| 70 |     delete m_menu; | 
| 71 |     qDeleteAll(c: m_menuItems); | 
| 72 | } | 
| 73 |  | 
| 74 | QDBusPlatformMenuItem *QDBusMenuBar::(QPlatformMenu *) | 
| 75 | { | 
| 76 |     if (!menu) | 
| 77 |         return nullptr; | 
| 78 |     quintptr tag = menu->tag(); | 
| 79 |     const auto it = m_menuItems.constFind(akey: tag); | 
| 80 |     if (it != m_menuItems.cend()) { | 
| 81 |         return *it; | 
| 82 |     } else { | 
| 83 |         QDBusPlatformMenuItem *item = new QDBusPlatformMenuItem; | 
| 84 |         updateMenuItem(item, menu); | 
| 85 |         m_menuItems.insert(akey: tag, avalue: item); | 
| 86 |         return item; | 
| 87 |     } | 
| 88 | } | 
| 89 |  | 
| 90 | void QDBusMenuBar::(QDBusPlatformMenuItem *item, QPlatformMenu *) | 
| 91 | { | 
| 92 |     const QDBusPlatformMenu * = qobject_cast<const QDBusPlatformMenu *>(object: menu); | 
| 93 |     item->setText(ourMenu->text()); | 
| 94 |     item->setIcon(ourMenu->icon()); | 
| 95 |     item->setEnabled(ourMenu->isEnabled()); | 
| 96 |     item->setVisible(ourMenu->isVisible()); | 
| 97 |     item->setMenu(menu); | 
| 98 | } | 
| 99 |  | 
| 100 | void QDBusMenuBar::(QPlatformMenu *, QPlatformMenu *before) | 
| 101 | { | 
| 102 |     QDBusPlatformMenuItem * = menuItemForMenu(menu); | 
| 103 |     QDBusPlatformMenuItem *beforeItem = menuItemForMenu(menu: before); | 
| 104 |     m_menu->insertMenuItem(menuItem, before: beforeItem); | 
| 105 |     m_menu->emitUpdated(); | 
| 106 | } | 
| 107 |  | 
| 108 | void QDBusMenuBar::(QPlatformMenu *) | 
| 109 | { | 
| 110 |     QDBusPlatformMenuItem * = menuItemForMenu(menu); | 
| 111 |     m_menu->removeMenuItem(menuItem); | 
| 112 |     m_menu->emitUpdated(); | 
| 113 | } | 
| 114 |  | 
| 115 | void QDBusMenuBar::(QPlatformMenu *) | 
| 116 | { | 
| 117 |     QDBusPlatformMenuItem * = menuItemForMenu(menu); | 
| 118 |     updateMenuItem(item: menuItem, menu); | 
| 119 | } | 
| 120 |  | 
| 121 | void QDBusMenuBar::handleReparent(QWindow *newParentWindow) | 
| 122 | { | 
| 123 |     if (newParentWindow) { | 
| 124 |         unregisterMenuBar(); | 
| 125 |         m_windowId = newParentWindow->winId(); | 
| 126 |         registerMenuBar(); | 
| 127 |     } | 
| 128 | } | 
| 129 |  | 
| 130 | QPlatformMenu *QDBusMenuBar::(quintptr tag) const | 
| 131 | { | 
| 132 |     QDBusPlatformMenuItem * = m_menuItems.value(akey: tag); | 
| 133 |     if (menuItem) | 
| 134 |         return const_cast<QPlatformMenu *>(menuItem->menu()); | 
| 135 |     return nullptr; | 
| 136 | } | 
| 137 |  | 
| 138 | QPlatformMenu *QDBusMenuBar::() const | 
| 139 | { | 
| 140 |     return new QDBusPlatformMenu; | 
| 141 | } | 
| 142 |  | 
| 143 | void QDBusMenuBar::() | 
| 144 | { | 
| 145 |     static uint  = 0; | 
| 146 |  | 
| 147 |     QDBusConnection connection = QDBusConnection::sessionBus(); | 
| 148 |     m_objectPath = QStringLiteral("/MenuBar/%1" ).arg(a: ++menuBarId); | 
| 149 |     if (!connection.registerObject(path: m_objectPath, object: m_menu)) | 
| 150 |         return; | 
| 151 |  | 
| 152 |     QDBusMenuRegistrarInterface registrar(REGISTRAR_SERVICE, REGISTRAR_PATH, connection, this); | 
| 153 |     QDBusPendingReply<> r = registrar.RegisterWindow(windowId: m_windowId, menuObjectPath: QDBusObjectPath(m_objectPath)); | 
| 154 |     r.waitForFinished(); | 
| 155 |     if (r.isError()) { | 
| 156 |         qWarning(msg: "Failed to register window menu, reason: %s (\"%s\")" , | 
| 157 |                  qUtf8Printable(r.error().name()), qUtf8Printable(r.error().message())); | 
| 158 |         connection.unregisterObject(path: m_objectPath); | 
| 159 |     } | 
| 160 | } | 
| 161 |  | 
| 162 | void QDBusMenuBar::() | 
| 163 | { | 
| 164 |     QDBusConnection connection = QDBusConnection::sessionBus(); | 
| 165 |  | 
| 166 |     if (m_windowId) { | 
| 167 |         QDBusMenuRegistrarInterface registrar(REGISTRAR_SERVICE, REGISTRAR_PATH, connection, this); | 
| 168 |         QDBusPendingReply<> r = registrar.UnregisterWindow(windowId: m_windowId); | 
| 169 |         r.waitForFinished(); | 
| 170 |         if (r.isError()) | 
| 171 |             qWarning(msg: "Failed to unregister window menu, reason: %s (\"%s\")" , | 
| 172 |                      qUtf8Printable(r.error().name()), qUtf8Printable(r.error().message())); | 
| 173 |     } | 
| 174 |  | 
| 175 |     if (!m_objectPath.isEmpty()) | 
| 176 |         connection.unregisterObject(path: m_objectPath); | 
| 177 | } | 
| 178 |  | 
| 179 | QT_END_NAMESPACE | 
| 180 |  |