Debian Codegraph @

src/dbusmenuexporter.cpp

raw symbols SCIP
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 "dbusmenuexporter.h"
22
23 // Qt
24 #include <QBuffer>
25 #include <QDateTime>
26 #include <QMap>
27 #include <QMenu>
28 #include <QSet>
29 #include <QTimer>
30 #include <QToolButton>
31 #include <QWidgetAction>
32
33 // Local
34 #include "dbusmenu_config.h"
35 #include "dbusmenu_p.h"
36 #include "dbusmenuexporterdbus_p.h"
37 #include "dbusmenuexporterprivate_p.h"
38 #include "dbusmenutypes_p.h"
39 #include "dbusmenushortcut_p.h"
40 #include "debug_p.h"
41 #include "utils_p.h"
42
43 static const char *KMENU_TITLE = "kmenu_title";
44
45 //-------------------------------------------------
46 //
47 // DBusMenuExporterPrivate
48 //
49 //-------------------------------------------------
50 int DBusMenuExporterPrivate::idForAction(QAction *action) const
51 {
52 DMRETURN_VALUE_IF_FAIL(action, -1);
53 return m_idForAction.value(action, -2);
54 }
55
56 void DBusMenuExporterPrivate::addMenu(QMenu *menu, int parentId)
57 {
58 if (menu->findChild<DBusMenu *>()) {
59 // This can happen if a menu is removed from its parent and added back
60 // See KDE bug 254066
61 return;
62 }
63 new DBusMenu(menu, q, parentId);
64 Q_FOREACH(QAction *action, menu->actions()) {
65 addAction(action, parentId);
66 }
67 }
68
69 QVariantMap DBusMenuExporterPrivate::propertiesForAction(QAction *action) const
70 {
71 DMRETURN_VALUE_IF_FAIL(action, QVariantMap());
72
73 if (action->objectName() == KMENU_TITLE) {
74 // Hack: Support for KDE menu titles in a Qt-only library...
75 return propertiesForKMenuTitleAction(action);
76 } else if (action->isSeparator()) {
77 return propertiesForSeparatorAction(action);
78 } else {
79 return propertiesForStandardAction(action);
80 }
81 }
82
83 QVariantMap DBusMenuExporterPrivate::propertiesForKMenuTitleAction(QAction *action_) const
84 {
85 QVariantMap map;
86 // In case the other side does not know about x-kde-title, show a disabled item
87 map.insert("enabled", false);
88 map.insert("x-kde-title", true);
89
90 const QWidgetAction *widgetAction = qobject_cast<const QWidgetAction *>(action_);
91 DMRETURN_VALUE_IF_FAIL(widgetAction, map);
92 QToolButton *button = qobject_cast<QToolButton *>(widgetAction->defaultWidget());
93 DMRETURN_VALUE_IF_FAIL(button, map);
94 QAction *action = button->defaultAction();
95 DMRETURN_VALUE_IF_FAIL(action, map);
96
97 map.insert("label", swapMnemonicChar(action->text(), '&', '_'));
98 insertIconProperty(&map, action);
99 if (!action->isVisible()) {
100 map.insert("visible", false);
101 }
102 return map;
103 }
104
105 QVariantMap DBusMenuExporterPrivate::propertiesForSeparatorAction(QAction *action) const
106 {
107 QVariantMap map;
108 map.insert("type", "separator");
109 if (!action->isVisible()) {
110 map.insert("visible", false);
111 }
112 return map;
113 }
114
115 QVariantMap DBusMenuExporterPrivate::propertiesForStandardAction(QAction *action) const
116 {
117 QVariantMap map;
118 map.insert("label", swapMnemonicChar(action->text(), '&', '_'));
119 if (!action->isEnabled()) {
120 map.insert("enabled", false);
121 }
122 if (!action->isVisible()) {
123 map.insert("visible", false);
124 }
125 if (action->menu()) {
126 map.insert("children-display", "submenu");
127 }
128 if (action->isCheckable()) {
129 bool exclusive = action->actionGroup() && action->actionGroup()->isExclusive();
130 map.insert("toggle-type", exclusive ? "radio" : "checkmark");
131 map.insert("toggle-state", action->isChecked() ? 1 : 0);
132 }
133 insertIconProperty(&map, action);
134 QKeySequence keySequence = action->shortcut();
135 if (!keySequence.isEmpty()) {
136 DBusMenuShortcut shortcut = DBusMenuShortcut::fromKeySequence(keySequence);
137 map.insert("shortcut", QVariant::fromValue(shortcut));
138 }
139 return map;
140 }
141
142 QMenu *DBusMenuExporterPrivate::menuForId(int id) const
143 {
144 if (id == 0) {
145 return m_rootMenu;
146 }
147 QAction *action = m_actionForId.value(id);
148 // Action may not be in m_actionForId if it has been deleted between the
149 // time it was announced by the exporter and the time the importer asks for
150 // it.
151 return action ? action->menu() : 0;
152 }
153
154 void DBusMenuExporterPrivate::fillLayoutItem(DBusMenuLayoutItem *item, QMenu *menu, int id, int depth, const QStringList &propertyNames)
155 {
156 item->id = id;
157 item->properties = m_dbusObject->getProperties(id, propertyNames);
158
159 if (depth != 0 && menu) {
160 Q_FOREACH(QAction *action, menu->actions()) {
161 int actionId = m_idForAction.value(action, -1);
162 if (actionId == -1) {
163 DMWARNING << "No id for action";
164 continue;
165 }
166
167 DBusMenuLayoutItem child;
168 fillLayoutItem(&child, action->menu(), actionId, depth - 1, propertyNames);
169 item->children << child;
170 }
171 }
172 }
173
174 void DBusMenuExporterPrivate::updateAction(QAction *action)
175 {
176 int id = idForAction(action);
177 if (m_itemUpdatedIds.contains(id)) {
178 return;
179 }
180 m_itemUpdatedIds << id;
181 m_itemUpdatedTimer->start();
182 }
183
184 void DBusMenuExporterPrivate::addAction(QAction *action, int parentId)
185 {
186 int id = m_idForAction.value(action, -1);
187 if (id != -1) {
188 DMWARNING << "Already tracking action" << action->text() << "under id" << id;
189 return;
190 }
191 QVariantMap map = propertiesForAction(action);
192 id = m_nextId++;
193 QObject::connect(action, SIGNAL(destroyed(QObject*)), q, SLOT(slotActionDestroyed(QObject*)));
194 m_actionForId.insert(id, action);
195 m_idForAction.insert(action, id);
196 m_actionProperties.insert(action, map);
197 if (action->menu()) {
198 addMenu(action->menu(), id);
199 }
200 ++m_revision;
201 emitLayoutUpdated(parentId);
202 }
203
204 /**
205 * IMPORTANT: action might have already been destroyed when this method is
206 * called, so don't dereference the pointer (it is a QObject to avoid being
207 * tempted to dereference)
208 */
209 void DBusMenuExporterPrivate::removeActionInternal(QObject *object)
210 {
211 QAction* action = static_cast<QAction*>(object);
212 m_actionProperties.remove(action);
213 int id = m_idForAction.take(action);
214 m_actionForId.remove(id);
215 }
216
217 void DBusMenuExporterPrivate::removeAction(QAction *action, int parentId)
218 {
219 removeActionInternal(action);
220 QObject::disconnect(action, SIGNAL(destroyed(QObject*)), q, SLOT(slotActionDestroyed(QObject*)));
221 ++m_revision;
222 emitLayoutUpdated(parentId);
223 }
224
225 void DBusMenuExporterPrivate::emitLayoutUpdated(int id)
226 {
227 if (m_layoutUpdatedIds.contains(id)) {
228 return;
229 }
230 m_layoutUpdatedIds << id;
231 m_layoutUpdatedTimer->start();
232 }
233
234 void DBusMenuExporterPrivate::insertIconProperty(QVariantMap *map, QAction *action) const
235 {
236 // provide the icon name for per-theme lookups
237 const QString iconName = q->iconNameForAction(action);
238 if (!iconName.isEmpty()) {
239 map->insert("icon-name", iconName);
240 }
241
242 // provide the serialized icon data in case the icon
243 // is unnamed or the name isn't supported by the theme
244 const QIcon icon = action->icon();
245 if (!icon.isNull()) {
246 QBuffer buffer;
247 icon.pixmap(16).save(&buffer, "PNG");
248 map->insert("icon-data", buffer.data());
249 }
250 }
251
252 static void collapseSeparator(QAction* action)
253 {
254 action->setVisible(false);
255 }
256
257 // Unless the separatorsCollapsible property is set to false, Qt will get rid
258 // of separators at the beginning and at the end of menus as well as collapse
259 // multiple separators in the middle. For example, a menu like this:
260 //
261 // ---
262 // Open
263 // ---
264 // ---
265 // Quit
266 // ---
267 //
268 // is displayed like this:
269 //
270 // Open
271 // ---
272 // Quit
273 //
274 // We fake this by setting separators invisible before exporting them.
275 //
276 // cf. https://bugs.launchpad.net/libdbusmenu-qt/+bug/793339
277 void DBusMenuExporterPrivate::collapseSeparators(QMenu* menu)
278 {
279 QList<QAction*> actions = menu->actions();
280 if (actions.isEmpty()) {
281 return;
282 }
283
284 QList<QAction*>::Iterator it, begin = actions.begin(), end = actions.end();
285
286 // Get rid of separators at end
287 it = end - 1;
288 for (; it != begin; --it) {
289 if ((*it)->isSeparator()) {
290 collapseSeparator(*it);
291 } else {
292 break;
293 }
294 }
295 // end now points after the last visible entry
296 end = it + 1;
297 it = begin;
298
299 // Get rid of separators at beginnning
300 for (; it != end; ++it) {
301 if ((*it)->isSeparator()) {
302 collapseSeparator(*it);
303 } else {
304 break;
305 }
306 }
307
308 // Collapse separators in between
309 bool previousWasSeparator = false;
310 for (; it != end; ++it) {
311 QAction* action = *it;
312 if (action->isSeparator()) {
313 if (previousWasSeparator) {
314 collapseSeparator(action);
315 } else {
316 previousWasSeparator = true;
317 }
318 } else {
319 previousWasSeparator = false;
320 }
321 }
322 }
323
324 //-------------------------------------------------
325 //
326 // DBusMenuExporter
327 //
328 //-------------------------------------------------
329 DBusMenuExporter::DBusMenuExporter(const QString &objectPath, QMenu *menu, const QDBusConnection &_connection)
330 : QObject(menu)
331 , d(new DBusMenuExporterPrivate)
332 {
333 d->q = this;
334 d->m_objectPath = objectPath;
335 d->m_rootMenu = menu;
336 d->m_nextId = 1;
337 d->m_revision = 1;
338 d->m_emittedLayoutUpdatedOnce = false;
339 d->m_itemUpdatedTimer = new QTimer(this);
340 d->m_layoutUpdatedTimer = new QTimer(this);
341 d->m_dbusObject = new DBusMenuExporterDBus(this);
342
343 d->addMenu(d->m_rootMenu, 0);
344
345 d->m_itemUpdatedTimer->setInterval(0);
346 d->m_itemUpdatedTimer->setSingleShot(true);
347 connect(d->m_itemUpdatedTimer, SIGNAL(timeout()), SLOT(doUpdateActions()));
348
349 d->m_layoutUpdatedTimer->setInterval(0);
350 d->m_layoutUpdatedTimer->setSingleShot(true);
351 connect(d->m_layoutUpdatedTimer, SIGNAL(timeout()), SLOT(doEmitLayoutUpdated()));
352
353 QDBusConnection connection(_connection);
354 connection.registerObject(objectPath, d->m_dbusObject, QDBusConnection::ExportAllContents);
355 }
356
357 DBusMenuExporter::~DBusMenuExporter()
358 {
359 delete d;
360 }
361
362 void DBusMenuExporter::doUpdateActions()
363 {
364 if (d->m_itemUpdatedIds.isEmpty()) {
365 return;
366 }
367 DBusMenuItemList updatedList;
368 DBusMenuItemKeysList removedList;
369
370 Q_FOREACH(int id, d->m_itemUpdatedIds) {
371 QAction *action = d->m_actionForId.value(id);
372 if (!action) {
373 // Action does not exist anymore
374 continue;
375 }
376
377 QVariantMap& oldProperties = d->m_actionProperties[action];
378 QVariantMap newProperties = d->propertiesForAction(action);
379 QVariantMap updatedProperties;
380 QStringList removedProperties;
381
382 // Find updated and removed properties
383 QVariantMap::ConstIterator newEnd = newProperties.constEnd();
384
385 QVariantMap::ConstIterator
386 oldIt = oldProperties.constBegin(),
387 oldEnd = oldProperties.constEnd();
388 for(; oldIt != oldEnd; ++oldIt) {
389 QString key = oldIt.key();
390 QVariantMap::ConstIterator newIt = newProperties.constFind(key);
391 if (newIt != newEnd) {
392 if (newIt.value() != oldIt.value()) {
393 updatedProperties.insert(key, newIt.value());
394 }
395 } else {
396 removedProperties << key;
397 }
398 }
399
400 // Find new properties (treat them as updated properties)
401 QVariantMap::ConstIterator newIt = newProperties.constBegin();
402 for (; newIt != newEnd; ++newIt) {
403 QString key = newIt.key();
404 oldIt = oldProperties.constFind(key);
405 if (oldIt == oldEnd) {
406 updatedProperties.insert(key, newIt.value());
407 }
408 }
409
410 // Update our data (oldProperties is a reference)
411 oldProperties = newProperties;
412 QMenu *menu = action->menu();
413 if (menu) {
414 d->addMenu(menu, id);
415 }
416
417 if (!updatedProperties.isEmpty()) {
418 DBusMenuItem item;
419 item.id = id;
420 item.properties = updatedProperties;
421 updatedList << item;
422 }
423 if (!removedProperties.isEmpty()) {
424 DBusMenuItemKeys itemKeys;
425 itemKeys.id = id;
426 itemKeys.properties = removedProperties;
427 removedList << itemKeys;
428 }
429 }
430 d->m_itemUpdatedIds.clear();
431 if (!d->m_emittedLayoutUpdatedOnce) {
432 // No need to tell the world about action changes: nobody knows the
433 // menu layout so nobody knows about the actions.
434 // Note: We can't stop in DBusMenuExporterPrivate::addAction(), we
435 // still need to reach this method because we want our properties to be
436 // updated, even if we don't announce changes.
437 return;
438 }
439 if (!updatedList.isEmpty() || !removedList.isEmpty()) {
440 d->m_dbusObject->ItemsPropertiesUpdated(updatedList, removedList);
441 }
442 }
443
444 void DBusMenuExporter::doEmitLayoutUpdated()
445 {
446 // Collapse separators for all updated menus
447 Q_FOREACH(int id, d->m_layoutUpdatedIds) {
448 QMenu* menu = d->menuForId(id);
449 if (menu && menu->separatorsCollapsible()) {
450 d->collapseSeparators(menu);
451 }
452 }
453
454 // Tell the world about the update
455 if (d->m_emittedLayoutUpdatedOnce) {
456 Q_FOREACH(int id, d->m_layoutUpdatedIds) {
457 d->m_dbusObject->LayoutUpdated(d->m_revision, id);
458 }
459 } else {
460 // First time we emit LayoutUpdated, no need to emit several layout
461 // updates, signals the whole layout (id==0) has been updated
462 d->m_dbusObject->LayoutUpdated(d->m_revision, 0);
463 d->m_emittedLayoutUpdatedOnce = true;
464 }
465 d->m_layoutUpdatedIds.clear();
466 }
467
468 QString DBusMenuExporter::iconNameForAction(QAction *action)
469 {
470 DMRETURN_VALUE_IF_FAIL(action, QString());
471 #ifdef HAVE_QICON_NAME
472 QIcon icon = action->icon();
473 if (action->isIconVisibleInMenu() && !icon.isNull()) {
474 return icon.name();
475 } else {
476 return QString();
477 }
478 #else
479 return QString();
480 #endif
481 }
482
483 void DBusMenuExporter::activateAction(QAction *action)
484 {
485 int id = d->idForAction(action);
486 DMRETURN_IF_FAIL(id >= 0);
487 uint timeStamp = QDateTime::currentDateTime().toTime_t();
488 d->m_dbusObject->ItemActivationRequested(id, timeStamp);
489 }
490
491 void DBusMenuExporter::slotActionDestroyed(QObject* object)
492 {
493 d->removeActionInternal(object);
494 }
495
496 void DBusMenuExporter::setStatus(const QString& status)
497 {
498 d->m_dbusObject->setStatus(status);
499 }
500
501 QString DBusMenuExporter::status() const
502 {
503 return d->m_dbusObject->status();
504 }
505
506 #include "dbusmenuexporter.moc"