| 1 | /* This file is part of the dbusmenu-qt library |
| 2 | Copyright 2010 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 <QApplication> |
| 22 | #include <QDBusConnection> |
| 23 | #include <QDebug> |
| 24 | #include <QFile> |
| 25 | #include <QMenu> |
| 26 | |
| 27 | #include <qjson/parser.h> |
| 28 | |
| 29 | #include <dbusmenuexporter.h> |
| 30 | |
| 31 | static const char *DBUS_SERVICE = "org.dbusmenu.test"; |
| 32 | static const char *DBUS_PATH = "/MenuBar"; |
| 33 | static const char *USAGE = "dbusmenubench-qtapp <path/to/menu.json>"; |
| 34 | |
| 35 | void createMenuItem(QMenu *menu, const QVariant &item) |
| 36 | { |
| 37 | QVariantMap map = item.toMap(); |
| 38 | |
| 39 | if (map.value("visible").toString() == "false") { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | QString type = map.value("type").toString(); |
| 44 | if (type == "separator") { |
| 45 | menu->addSeparator(); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | QString label = map.value("label").toString(); |
| 50 | QAction *action = menu->addAction(label); |
| 51 | action->setEnabled(map.value("sensitive").toString() == "true"); |
| 52 | if (map.contains("submenu")) { |
| 53 | QVariantList items = map.value("submenu").toList(); |
| 54 | Q_FOREACH(const QVariant &item, items) { |
| 55 | QMenu *subMenu = new QMenu; |
| 56 | action->setMenu(subMenu); |
| 57 | createMenuItem(subMenu, item); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void initMenu(QMenu *menu, const QString &fileName) |
| 63 | { |
| 64 | QJson::Parser parser; |
| 65 | |
| 66 | QFile file(fileName); |
| 67 | if (!file.open(QIODevice::ReadOnly)) { |
| 68 | qCritical() << "Could not open file" << fileName; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | bool ok; |
| 73 | QVariant tree = parser.parse(&file, &ok); |
| 74 | if (!ok) { |
| 75 | qCritical() << "Could not parse json data from" << fileName; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | QVariantList list = tree.toList(); |
| 80 | Q_FOREACH(const QVariant &item, list) { |
| 81 | createMenuItem(menu, item); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | int main(int argc, char **argv) |
| 86 | { |
| 87 | QApplication app(argc, argv); |
| 88 | QMenu menu; |
| 89 | |
| 90 | if (argc != 2) { |
| 91 | qCritical() << USAGE; |
| 92 | return 1; |
| 93 | } |
| 94 | QString jsonFileName = argv[1]; |
| 95 | initMenu(&menu, jsonFileName); |
| 96 | |
| 97 | QDBusConnection connection = QDBusConnection::sessionBus(); |
| 98 | if (!connection.registerService(DBUS_SERVICE)) { |
| 99 | qCritical() << "Could not register" << DBUS_SERVICE; |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | DBusMenuExporter exporter(DBUS_PATH, &menu); |
| 104 | return app.exec(); |
| 105 | } |