1 /* This file is part of the dbusmenu-qt library
2 Copyright 2010 Canonical
3 Author: Aurelien Gateau <aurelien.gateau@canonical.com>
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
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.
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.
21 #include <QApplication>
22 #include <QDBusConnection>
27 #include <qjson/parser.h>
29 #include <dbusmenuexporter.h>
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>";
35 void createMenuItem(QMenu *menu, const QVariant &item)
37 QVariantMap map = item.toMap();
39 if (map.value("visible").toString() == "false") {
43 QString type = map.value("type").toString();
44 if (type == "separator") {
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);
62 void initMenu(QMenu *menu, const QString &fileName)
67 if (!file.open(QIODevice::ReadOnly)) {
68 qCritical() << "Could not open file" << fileName;
73 QVariant tree = parser.parse(&file, &ok);
75 qCritical() << "Could not parse json data from" << fileName;
79 QVariantList list = tree.toList();
80 Q_FOREACH(const QVariant &item, list) {
81 createMenuItem(menu, item);
85 int main(int argc, char **argv)
87 QApplication app(argc, argv);
94 QString jsonFileName = argv[1];
95 initMenu(&menu, jsonFileName);
97 QDBusConnection connection = QDBusConnection::sessionBus();
98 if (!connection.registerService(DBUS_SERVICE)) {
99 qCritical() << "Could not register" << DBUS_SERVICE;
103 DBusMenuExporter exporter(DBUS_PATH, &menu);