File: main.cpp

package info (click to toggle)
libdbusmenu-qt 0.9.3%2B16.04.20160218-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 384 kB
  • sloc: cpp: 2,560; xml: 306; makefile: 8
file content (105 lines) | stat: -rw-r--r-- 3,046 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This file is part of the dbusmenu-qt library
   Copyright 2010 Canonical
   Author: Aurelien Gateau <aurelien.gateau@canonical.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License (LGPL) as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later
   version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include <QApplication>
#include <QDBusConnection>
#include <QDebug>
#include <QFile>
#include <QMenu>

#include <qjson/parser.h>

#include <dbusmenuexporter.h>

static const char *DBUS_SERVICE = "org.dbusmenu.test";
static const char *DBUS_PATH    = "/MenuBar";
static const char *USAGE        = "dbusmenubench-qtapp <path/to/menu.json>";

void createMenuItem(QMenu *menu, const QVariant &item)
{
    QVariantMap map = item.toMap();

    if (map.value("visible").toString() == "false") {
        return;
    }

    QString type = map.value("type").toString();
    if (type == "separator") {
        menu->addSeparator();
        return;
    }

    QString label = map.value("label").toString();
    QAction *action = menu->addAction(label);
    action->setEnabled(map.value("sensitive").toString() == "true");
    if (map.contains("submenu")) {
        QVariantList items = map.value("submenu").toList();
        Q_FOREACH(const QVariant &item, items) {
            QMenu *subMenu = new QMenu;
            action->setMenu(subMenu);
            createMenuItem(subMenu, item);
        }
    }
}

void initMenu(QMenu *menu, const QString &fileName)
{
    QJson::Parser parser;

    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        qCritical() << "Could not open file" << fileName;
        return;
    }

    bool ok;
    QVariant tree = parser.parse(&file, &ok);
    if (!ok) {
        qCritical() << "Could not parse json data from" << fileName;
        return;
    }

    QVariantList list = tree.toList();
    Q_FOREACH(const QVariant &item, list) {
        createMenuItem(menu, item);
    }
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QMenu menu;

    if (argc != 2) {
        qCritical() << USAGE;
        return 1;
    }
    QString jsonFileName = argv[1];
    initMenu(&menu, jsonFileName);

    QDBusConnection connection = QDBusConnection::sessionBus();
    if (!connection.registerService(DBUS_SERVICE)) {
        qCritical() << "Could not register" << DBUS_SERVICE;
        return 1;
    }

    DBusMenuExporter exporter(DBUS_PATH, &menu);
    return app.exec();
}