OSDN Git Service

move the test application to tests sub-directory
[kde/libdbusmenu-qt.git] / tests / testapp / main.cpp
1 /* This file is part of the dbusmenu-qt library
2    Copyright 2010 Canonical
3    Author: Aurelien Gateau <aurelien.gateau@canonical.com>
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 <QJsonDocument>
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     QFile file(fileName);
65     if (!file.open(QIODevice::ReadOnly)) {
66         qCritical() << "Could not open file" << fileName;
67         return;
68     }
69
70     QJsonDocument json = QJsonDocument::fromJson(file.readAll());
71     if (json.isNull()) {
72         qCritical() << "Could not parse json data from" << fileName;
73         return;
74     }
75
76     QVariantList list = json.toVariant().toList();
77     Q_FOREACH(const QVariant &item, list) {
78         createMenuItem(menu, item);
79     }
80 }
81
82 int main(int argc, char **argv)
83 {
84     QApplication app(argc, argv);
85     QMenu menu;
86
87     if (argc != 2) {
88         qCritical() << USAGE;
89         return 1;
90     }
91     QString jsonFileName = argv[1];
92     initMenu(&menu, jsonFileName);
93
94     QDBusConnection connection = QDBusConnection::sessionBus();
95     if (!connection.registerService(DBUS_SERVICE)) {
96         qCritical() << "Could not register" << DBUS_SERVICE;
97         return 1;
98     }
99
100     DBusMenuExporter exporter(DBUS_PATH, &menu);
101     return app.exec();
102 }