| 1 | /* This file is part of the dbusmenu-qt library |
| 2 | Copyright 2009 Canonical |
| 3 | Author: Aurelien Gateau <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Library General Public |
| 7 | License (LGPL) as published by the Free Software Foundation; |
| 8 | either version 2 of the License, or (at your option) any later |
| 9 | version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Library General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Library General Public License |
| 17 | along with this library; see the file COPYING.LIB. If not, write to |
| 18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | #include "dbusmenuimporter.h" |
| 22 | |
| 23 | // Qt |
| 24 | #include <QCoreApplication> |
| 25 | #include <QDBusConnection> |
| 26 | #include <QDBusInterface> |
| 27 | #include <QDBusReply> |
| 28 | #include <QDBusVariant> |
| 29 | #include <QFont> |
| 30 | #include <QMenu> |
| 31 | #include <QPointer> |
| 32 | #include <QSignalMapper> |
| 33 | #include <QTime> |
| 34 | #include <QTimer> |
| 35 | #include <QToolButton> |
| 36 | #include <QWidgetAction> |
| 37 | |
| 38 | // Local |
| 39 | #include "dbusmenutypes_p.h" |
| 40 | #include "dbusmenushortcut_p.h" |
| 41 | #include "debug_p.h" |
| 42 | #include "utils_p.h" |
| 43 | |
| 44 | //#define BENCHMARK |
| 45 | #ifdef BENCHMARK |
| 46 | #include <QTime> |
| 47 | static QTime sChrono; |
| 48 | #endif |
| 49 | |
| 50 | static const char *DBUSMENU_INTERFACE = "com.canonical.dbusmenu"; |
| 51 | |
| 52 | static const int ABOUT_TO_SHOW_TIMEOUT = 3000; |
| 53 | static const int REFRESH_TIMEOUT = 4000; |
| 54 | |
| 55 | static const char *DBUSMENU_PROPERTY_ID = "_dbusmenu_id"; |
| 56 | static const char *DBUSMENU_PROPERTY_ICON_NAME = "_dbusmenu_icon_name"; |
| 57 | static const char *DBUSMENU_PROPERTY_ICON_DATA_HASH = "_dbusmenu_icon_data_hash"; |
| 58 | |
| 59 | static QAction *createKdeTitle(QAction *action, QWidget *parent) |
| 60 | { |
| 61 | QToolButton *titleWidget = new QToolButton(0); |
| 62 | QFont font = titleWidget->font(); |
| 63 | font.setBold(true); |
| 64 | titleWidget->setFont(font); |
| 65 | titleWidget->setIcon(action->icon()); |
| 66 | titleWidget->setText(action->text()); |
| 67 | titleWidget->setDown(true); |
| 68 | titleWidget->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 69 | |
| 70 | QWidgetAction *titleAction = new QWidgetAction(parent); |
| 71 | titleAction->setDefaultWidget(titleWidget); |
| 72 | return titleAction; |
| 73 | } |
| 74 | |
| 75 | class DBusMenuImporterPrivate |
| 76 | { |
| 77 | public: |
| 78 | DBusMenuImporter *q; |
| 79 | |
| 80 | QDBusAbstractInterface *m_interface; |
| 81 | QMenu *m_menu; |
| 82 | typedef QMap<int, QPointer<QAction> > ActionForId; |
| 83 | ActionForId m_actionForId; |
| 84 | QSignalMapper m_mapper; |
| 85 | QTimer *m_pendingLayoutUpdateTimer; |
| 86 | |
| 87 | QSet<int> m_idsRefreshedByAboutToShow; |
| 88 | QSet<int> m_pendingLayoutUpdates; |
| 89 | |
| 90 | bool m_mustEmitMenuUpdated; |
| 91 | |
| 92 | DBusMenuImporterType m_type; |
| 93 | |
| 94 | QDBusPendingCallWatcher *refresh(int id) |
| 95 | { |
| 96 | #ifdef BENCHMARK |
| 97 | DMDEBUG << "Starting refresh chrono for id" << id; |
| 98 | sChrono.start(); |
| 99 | #endif |
| 100 | QDBusPendingCall call = m_interface->asyncCall("GetLayout", id, 1, QStringList()); |
| 101 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, q); |
| 102 | watcher->setProperty(DBUSMENU_PROPERTY_ID, id); |
| 103 | QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), |
| 104 | q, SLOT(slotGetLayoutFinished(QDBusPendingCallWatcher*))); |
| 105 | |
| 106 | return watcher; |
| 107 | } |
| 108 | |
| 109 | QMenu *createMenu(QWidget *parent) |
| 110 | { |
| 111 | QMenu *menu = q->createMenu(parent); |
| 112 | QObject::connect(menu, SIGNAL(aboutToShow()), |
| 113 | q, SLOT(slotMenuAboutToShow())); |
| 114 | QObject::connect(menu, SIGNAL(aboutToHide()), |
| 115 | q, SLOT(slotMenuAboutToHide())); |
| 116 | return menu; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Init all the immutable action properties here |
| 121 | * TODO: Document immutable properties? |
| 122 | * |
| 123 | * Note: we remove properties we handle from the map (using QMap::take() |
| 124 | * instead of QMap::value()) to avoid warnings about these properties in |
| 125 | * updateAction() |
| 126 | */ |
| 127 | QAction *createAction(int id, const QVariantMap &_map, QWidget *parent) |
| 128 | { |
| 129 | QVariantMap map = _map; |
| 130 | QAction *action = new QAction(parent); |
| 131 | action->setProperty(DBUSMENU_PROPERTY_ID, id); |
| 132 | |
| 133 | QString type = map.take("type").toString(); |
| 134 | if (type == "separator") { |
| 135 | action->setSeparator(true); |
| 136 | } |
| 137 | |
| 138 | if (map.take("children-display").toString() == "submenu") { |
| 139 | QMenu *menu = createMenu(parent); |
| 140 | action->setMenu(menu); |
| 141 | } |
| 142 | |
| 143 | QString toggleType = map.take("toggle-type").toString(); |
| 144 | if (!toggleType.isEmpty()) { |
| 145 | action->setCheckable(true); |
| 146 | if (toggleType == "radio") { |
| 147 | QActionGroup *group = new QActionGroup(action); |
| 148 | group->addAction(action); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | bool isKdeTitle = map.take("x-kde-title").toBool(); |
| 153 | updateAction(action, map, map.keys()); |
| 154 | |
| 155 | if (isKdeTitle) { |
| 156 | action = createKdeTitle(action, parent); |
| 157 | } |
| 158 | |
| 159 | return action; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Update mutable properties of an action. A property may be listed in |
| 164 | * requestedProperties but not in map, this means we should use the default value |
| 165 | * for this property. |
| 166 | * |
| 167 | * @param action the action to update |
| 168 | * @param map holds the property values |
| 169 | * @param requestedProperties which properties has been requested |
| 170 | */ |
| 171 | void updateAction(QAction *action, const QVariantMap &map, const QStringList &requestedProperties) |
| 172 | { |
| 173 | Q_FOREACH(const QString &key, requestedProperties) { |
| 174 | updateActionProperty(action, key, map.value(key)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void updateActionProperty(QAction *action, const QString &key, const QVariant &value) |
| 179 | { |
| 180 | if (key == "label") { |
| 181 | updateActionLabel(action, value); |
| 182 | } else if (key == "enabled") { |
| 183 | updateActionEnabled(action, value); |
| 184 | } else if (key == "toggle-state") { |
| 185 | updateActionChecked(action, value); |
| 186 | } else if (key == "icon-name") { |
| 187 | updateActionIconByName(action, value); |
| 188 | } else if (key == "icon-data") { |
| 189 | updateActionIconByData(action, value); |
| 190 | } else if (key == "visible") { |
| 191 | updateActionVisible(action, value); |
| 192 | } else if (key == "shortcut") { |
| 193 | updateActionShortcut(action, value); |
| 194 | } else if (key == "children-display") { |
| 195 | } else { |
| 196 | DMWARNING << "Unhandled property update" << key; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void updateActionLabel(QAction *action, const QVariant &value) |
| 201 | { |
| 202 | QString text = swapMnemonicChar(value.toString(), '_', '&'); |
| 203 | action->setText(text); |
| 204 | } |
| 205 | |
| 206 | void updateActionEnabled(QAction *action, const QVariant &value) |
| 207 | { |
| 208 | action->setEnabled(value.isValid() ? value.toBool(): true); |
| 209 | } |
| 210 | |
| 211 | void updateActionChecked(QAction *action, const QVariant &value) |
| 212 | { |
| 213 | if (action->isCheckable() && value.isValid()) { |
| 214 | action->setChecked(value.toInt() == 1); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void updateActionIconByName(QAction *action, const QVariant &value) |
| 219 | { |
| 220 | QString iconName = value.toString(); |
| 221 | QString previous = action->property(DBUSMENU_PROPERTY_ICON_NAME).toString(); |
| 222 | if (previous == iconName) { |
| 223 | return; |
| 224 | } |
| 225 | action->setProperty(DBUSMENU_PROPERTY_ICON_NAME, iconName); |
| 226 | if (iconName.isEmpty()) { |
| 227 | action->setIcon(QIcon()); |
| 228 | return; |
| 229 | } |
| 230 | action->setIcon(q->iconForName(iconName)); |
| 231 | } |
| 232 | |
| 233 | void updateActionIconByData(QAction *action, const QVariant &value) |
| 234 | { |
| 235 | QByteArray data = value.toByteArray(); |
| 236 | uint dataHash = qHash(data); |
| 237 | uint previousDataHash = action->property(DBUSMENU_PROPERTY_ICON_DATA_HASH).toUInt(); |
| 238 | if (previousDataHash == dataHash) { |
| 239 | return; |
| 240 | } |
| 241 | action->setProperty(DBUSMENU_PROPERTY_ICON_DATA_HASH, dataHash); |
| 242 | QPixmap pix; |
| 243 | if (!pix.loadFromData(data)) { |
| 244 | DMWARNING << "Failed to decode icon-data property for action" << action->text(); |
| 245 | action->setIcon(QIcon()); |
| 246 | return; |
| 247 | } |
| 248 | action->setIcon(QIcon(pix)); |
| 249 | } |
| 250 | |
| 251 | void updateActionVisible(QAction *action, const QVariant &value) |
| 252 | { |
| 253 | action->setVisible(value.isValid() ? value.toBool() : true); |
| 254 | } |
| 255 | |
| 256 | void updateActionShortcut(QAction *action, const QVariant &value) |
| 257 | { |
| 258 | QDBusArgument arg = value.value<QDBusArgument>(); |
| 259 | DBusMenuShortcut dmShortcut; |
| 260 | arg >> dmShortcut; |
| 261 | QKeySequence keySequence = dmShortcut.toKeySequence(); |
| 262 | action->setShortcut(keySequence); |
| 263 | } |
| 264 | |
| 265 | QMenu *menuForId(int id) const |
| 266 | { |
| 267 | if (id == 0) { |
| 268 | return q->menu(); |
| 269 | } |
| 270 | QAction *action = m_actionForId.value(id); |
| 271 | if (!action) { |
| 272 | return 0; |
| 273 | } |
| 274 | return action->menu(); |
| 275 | } |
| 276 | |
| 277 | void slotItemsPropertiesUpdated(const DBusMenuItemList &updatedList, const DBusMenuItemKeysList &removedList); |
| 278 | |
| 279 | void sendEvent(int id, const QString &eventId) |
| 280 | { |
| 281 | QVariant empty = QVariant::fromValue(QDBusVariant(QString())); |
| 282 | m_interface->asyncCall("Event", id, eventId, empty, 0u); |
| 283 | } |
| 284 | |
| 285 | bool waitForWatcher(QDBusPendingCallWatcher * _watcher, int maxWait) |
| 286 | { |
| 287 | QPointer<QDBusPendingCallWatcher> watcher(_watcher); |
| 288 | |
| 289 | if(m_type == ASYNCHRONOUS) { |
| 290 | QTimer timer; |
| 291 | timer.setSingleShot(true); |
| 292 | QEventLoop loop; |
| 293 | loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); |
| 294 | loop.connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher *)), SLOT(quit())); |
| 295 | timer.start(maxWait); |
| 296 | loop.exec(); |
| 297 | timer.stop(); |
| 298 | |
| 299 | if (!watcher) { |
| 300 | // Watcher died. This can happen if importer got deleted while we were |
| 301 | // waiting. See: |
| 302 | // https://bugs.kde.org/show_bug.cgi?id=237156 |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | if(!watcher->isFinished()) { |
| 307 | // Timed out |
| 308 | return false; |
| 309 | } |
| 310 | } else { |
| 311 | watcher->waitForFinished(); |
| 312 | } |
| 313 | |
| 314 | if (watcher->isError()) { |
| 315 | DMWARNING << watcher->error().message(); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | DBusMenuImporter::DBusMenuImporter(const QString &service, const QString &path, QObject *parent) |
| 324 | : DBusMenuImporter(service, path, ASYNCHRONOUS, parent) |
| 325 | { |
| 326 | } |
| 327 | |
| 328 | DBusMenuImporter::DBusMenuImporter(const QString &service, const QString &path, DBusMenuImporterType type, QObject *parent) |
| 329 | : QObject(parent) |
| 330 | , d(new DBusMenuImporterPrivate) |
| 331 | { |
| 332 | DBusMenuTypes_register(); |
| 333 | |
| 334 | d->q = this; |
| 335 | d->m_interface = new QDBusInterface(service, path, DBUSMENU_INTERFACE, QDBusConnection::sessionBus(), this); |
| 336 | d->m_menu = 0; |
| 337 | d->m_mustEmitMenuUpdated = false; |
| 338 | |
| 339 | d->m_type = type; |
| 340 | |
| 341 | connect(&d->m_mapper, SIGNAL(mapped(int)), SLOT(sendClickedEvent(int))); |
| 342 | |
| 343 | d->m_pendingLayoutUpdateTimer = new QTimer(this); |
| 344 | d->m_pendingLayoutUpdateTimer->setSingleShot(true); |
| 345 | connect(d->m_pendingLayoutUpdateTimer, SIGNAL(timeout()), SLOT(processPendingLayoutUpdates())); |
| 346 | |
| 347 | // For some reason, using QObject::connect() does not work but |
| 348 | // QDBusConnect::connect() does |
| 349 | QDBusConnection::sessionBus().connect(service, path, DBUSMENU_INTERFACE, "LayoutUpdated", "ui", |
| 350 | this, SLOT(slotLayoutUpdated(uint, int))); |
| 351 | QDBusConnection::sessionBus().connect(service, path, DBUSMENU_INTERFACE, "ItemsPropertiesUpdated", "a(ia{sv})a(ias)", |
| 352 | this, SLOT(slotItemsPropertiesUpdated(DBusMenuItemList, DBusMenuItemKeysList))); |
| 353 | QDBusConnection::sessionBus().connect(service, path, DBUSMENU_INTERFACE, "ItemActivationRequested", "iu", |
| 354 | this, SLOT(slotItemActivationRequested(int, uint))); |
| 355 | |
| 356 | d->refresh(0); |
| 357 | } |
| 358 | |
| 359 | DBusMenuImporter::~DBusMenuImporter() |
| 360 | { |
| 361 | // Do not use "delete d->m_menu": even if we are being deleted we should |
| 362 | // leave enough time for the menu to finish what it was doing, for example |
| 363 | // if it was being displayed. |
| 364 | d->m_menu->deleteLater(); |
| 365 | delete d; |
| 366 | } |
| 367 | |
| 368 | void DBusMenuImporter::slotLayoutUpdated(uint revision, int parentId) |
| 369 | { |
| 370 | if (d->m_idsRefreshedByAboutToShow.remove(parentId)) { |
| 371 | return; |
| 372 | } |
| 373 | d->m_pendingLayoutUpdates << parentId; |
| 374 | if (!d->m_pendingLayoutUpdateTimer->isActive()) { |
| 375 | d->m_pendingLayoutUpdateTimer->start(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void DBusMenuImporter::processPendingLayoutUpdates() |
| 380 | { |
| 381 | QSet<int> ids = d->m_pendingLayoutUpdates; |
| 382 | d->m_pendingLayoutUpdates.clear(); |
| 383 | Q_FOREACH(int id, ids) { |
| 384 | d->refresh(id); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | QMenu *DBusMenuImporter::menu() const |
| 389 | { |
| 390 | if (!d->m_menu) { |
| 391 | d->m_menu = d->createMenu(0); |
| 392 | } |
| 393 | return d->m_menu; |
| 394 | } |
| 395 | |
| 396 | void DBusMenuImporterPrivate::slotItemsPropertiesUpdated(const DBusMenuItemList &updatedList, const DBusMenuItemKeysList &removedList) |
| 397 | { |
| 398 | Q_FOREACH(const DBusMenuItem &item, updatedList) { |
| 399 | QAction *action = m_actionForId.value(item.id); |
| 400 | if (!action) { |
| 401 | // We don't know this action. It probably is in a menu we haven't fetched yet. |
| 402 | continue; |
| 403 | } |
| 404 | |
| 405 | QVariantMap::ConstIterator |
| 406 | it = item.properties.constBegin(), |
| 407 | end = item.properties.constEnd(); |
| 408 | for(; it != end; ++it) { |
| 409 | updateActionProperty(action, it.key(), it.value()); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | Q_FOREACH(const DBusMenuItemKeys &item, removedList) { |
| 414 | QAction *action = m_actionForId.value(item.id); |
| 415 | if (!action) { |
| 416 | // We don't know this action. It probably is in a menu we haven't fetched yet. |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | Q_FOREACH(const QString &key, item.properties) { |
| 421 | updateActionProperty(action, key, QVariant()); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void DBusMenuImporter::slotItemActivationRequested(int id, uint /*timestamp*/) |
| 427 | { |
| 428 | QAction *action = d->m_actionForId.value(id); |
| 429 | DMRETURN_IF_FAIL(action); |
| 430 | actionActivationRequested(action); |
| 431 | } |
| 432 | |
| 433 | void DBusMenuImporter::slotGetLayoutFinished(QDBusPendingCallWatcher *watcher) |
| 434 | { |
| 435 | int parentId = watcher->property(DBUSMENU_PROPERTY_ID).toInt(); |
| 436 | watcher->deleteLater(); |
| 437 | |
| 438 | QDBusPendingReply<uint, DBusMenuLayoutItem> reply = *watcher; |
| 439 | if (!reply.isValid()) { |
| 440 | DMWARNING << reply.error().message(); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | #ifdef BENCHMARK |
| 445 | DMDEBUG << "- items received:" << sChrono.elapsed() << "ms"; |
| 446 | #endif |
| 447 | DBusMenuLayoutItem rootItem = reply.argumentAt<1>(); |
| 448 | |
| 449 | QMenu *menu = d->menuForId(parentId); |
| 450 | if (!menu) { |
| 451 | DMWARNING << "No menu for id" << parentId; |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | menu->clear(); |
| 456 | |
| 457 | Q_FOREACH(const DBusMenuLayoutItem &dbusMenuItem, rootItem.children) { |
| 458 | QAction *action = d->createAction(dbusMenuItem.id, dbusMenuItem.properties, menu); |
| 459 | DBusMenuImporterPrivate::ActionForId::Iterator it = d->m_actionForId.find(dbusMenuItem.id); |
| 460 | if (it == d->m_actionForId.end()) { |
| 461 | d->m_actionForId.insert(dbusMenuItem.id, action); |
| 462 | } else { |
| 463 | delete *it; |
| 464 | *it = action; |
| 465 | } |
| 466 | menu->addAction(action); |
| 467 | |
| 468 | connect(action, SIGNAL(triggered()), |
| 469 | &d->m_mapper, SLOT(map())); |
| 470 | d->m_mapper.setMapping(action, dbusMenuItem.id); |
| 471 | |
| 472 | if( action->menu() ) |
| 473 | { |
| 474 | d->refresh( dbusMenuItem.id )->waitForFinished(); |
| 475 | } |
| 476 | } |
| 477 | #ifdef BENCHMARK |
| 478 | DMDEBUG << "- Menu filled:" << sChrono.elapsed() << "ms"; |
| 479 | #endif |
| 480 | } |
| 481 | |
| 482 | void DBusMenuImporter::sendClickedEvent(int id) |
| 483 | { |
| 484 | d->sendEvent(id, QString("clicked")); |
| 485 | } |
| 486 | |
| 487 | void DBusMenuImporter::updateMenu() |
| 488 | { |
| 489 | d->m_mustEmitMenuUpdated = true; |
| 490 | QMetaObject::invokeMethod(menu(), "aboutToShow"); |
| 491 | } |
| 492 | |
| 493 | void DBusMenuImporter::slotMenuAboutToShow() |
| 494 | { |
| 495 | QMenu *menu = qobject_cast<QMenu*>(sender()); |
| 496 | Q_ASSERT(menu); |
| 497 | |
| 498 | QAction *action = menu->menuAction(); |
| 499 | Q_ASSERT(action); |
| 500 | |
| 501 | int id = action->property(DBUSMENU_PROPERTY_ID).toInt(); |
| 502 | |
| 503 | #ifdef BENCHMARK |
| 504 | QTime time; |
| 505 | time.start(); |
| 506 | #endif |
| 507 | |
| 508 | QDBusPendingCall call = d->m_interface->asyncCall("AboutToShow", id); |
| 509 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this); |
| 510 | watcher->setProperty(DBUSMENU_PROPERTY_ID, id); |
| 511 | connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), |
| 512 | SLOT(slotAboutToShowDBusCallFinished(QDBusPendingCallWatcher*))); |
| 513 | |
| 514 | QPointer<QObject> guard(this); |
| 515 | |
| 516 | if (!d->waitForWatcher(watcher, ABOUT_TO_SHOW_TIMEOUT)) { |
| 517 | DMWARNING << "Application did not answer to AboutToShow() before timeout"; |
| 518 | } |
| 519 | |
| 520 | #ifdef BENCHMARK |
| 521 | DMVAR(time.elapsed()); |
| 522 | #endif |
| 523 | // "this" got deleted during the call to waitForWatcher(), get out |
| 524 | if (!guard) { |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | if (menu == d->m_menu && d->m_mustEmitMenuUpdated) { |
| 529 | d->m_mustEmitMenuUpdated = false; |
| 530 | menuUpdated(); |
| 531 | } |
| 532 | if (menu == d->m_menu) { |
| 533 | menuReadyToBeShown(); |
| 534 | } |
| 535 | |
| 536 | d->sendEvent(id, QString("opened")); |
| 537 | } |
| 538 | |
| 539 | void DBusMenuImporter::slotAboutToShowDBusCallFinished(QDBusPendingCallWatcher *watcher) |
| 540 | { |
| 541 | int id = watcher->property(DBUSMENU_PROPERTY_ID).toInt(); |
| 542 | watcher->deleteLater(); |
| 543 | |
| 544 | QDBusPendingReply<bool> reply = *watcher; |
| 545 | if (reply.isError()) { |
| 546 | DMWARNING << "Call to AboutToShow() failed:" << reply.error().message(); |
| 547 | return; |
| 548 | } |
| 549 | bool needRefresh = reply.argumentAt<0>(); |
| 550 | |
| 551 | QMenu *menu = d->menuForId(id); |
| 552 | DMRETURN_IF_FAIL(menu); |
| 553 | |
| 554 | if (needRefresh || menu->actions().isEmpty()) { |
| 555 | d->m_idsRefreshedByAboutToShow << id; |
| 556 | QDBusPendingCallWatcher *watcher2 = d->refresh(id); |
| 557 | if (!d->waitForWatcher(watcher2, REFRESH_TIMEOUT)) { |
| 558 | DMWARNING << "Application did not refresh before timeout"; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | void DBusMenuImporter::slotMenuAboutToHide() |
| 564 | { |
| 565 | QMenu *menu = qobject_cast<QMenu*>(sender()); |
| 566 | Q_ASSERT(menu); |
| 567 | |
| 568 | QAction *action = menu->menuAction(); |
| 569 | Q_ASSERT(action); |
| 570 | |
| 571 | int id = action->property(DBUSMENU_PROPERTY_ID).toInt(); |
| 572 | d->sendEvent(id, QString("closed")); |
| 573 | } |
| 574 | |
| 575 | QMenu *DBusMenuImporter::createMenu(QWidget *parent) |
| 576 | { |
| 577 | return new QMenu(parent); |
| 578 | } |
| 579 | |
| 580 | QIcon DBusMenuImporter::iconForName(const QString &/*name*/) |
| 581 | { |
| 582 | return QIcon(); |
| 583 | } |
| 584 | |
| 585 | #include "dbusmenuimporter.moc" |