aboutsummaryrefslogtreecommitdiff
path: root/examples/info-menumodel.cpp
blob: d4fd0a718bab8d907a549fc1b6b19b1a10c499f2 (plain)
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
106
/*
 * Copyright 2012 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Olivier Tilloy <olivier.tilloy@canonical.com>
 */

/*
 * Example executable that links against libqmenumodel to monitor
 * and print information about a menu model exported on D-Bus.
 */

// QMenuModel
#include "qmenumodel/qdbusmenumodel.h"

// stdlib
#include <iostream>

// Qt
#include <QtCore/QCoreApplication>
#include <QtCore/QObject>


#define BUS_NAME "org.ayatana.testmenu"
#define BUS_OBJECT_PATH "/org/ayatana/testmenu"


class MenuModelMonitor : public QDBusMenuModel
{
    Q_OBJECT

public:
    MenuModelMonitor(QObject* parent=0)
        : QDBusMenuModel(parent)
    {
        setProperty("busType", DBusEnums::SessionBus);
        setProperty("busName", BUS_NAME);
        setProperty("objectPath", BUS_OBJECT_PATH);
        QObject::connect(this, SIGNAL(statusChanged(DBusEnums::ConnectionStatus)),
                         SLOT(onStatusChanged(DBusEnums::ConnectionStatus)));
        QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
                         SLOT(onModelChanged()));
        QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
                         SLOT(onModelChanged()));
        std::cout << "Monitoring menu model "
                  << property("objectPath").toString().toUtf8().constData()
                  << " on the well-known name "
                  << property("busName").toString().toUtf8().constData()
                  << std::endl;
    }

private Q_SLOTS:
    void onStatusChanged(DBusEnums::ConnectionStatus status)
    {
        std::cout << "Status of menu model changed to " << status << std::endl;
        if (status == DBusEnums::Connected) {
            printModel(this);
        }
    }

    void onModelChanged()
    {
        printModel(this);
    }

private:
    void printModel(QMenuModel* model, int indent=0)
    {
        int count = model->rowCount();
        for (int i = 0; i < count; ++i) {
            QModelIndex index = model->index(i, 0);
            QString label = model->data(index, QMenuModel::Label).toString();
            QVariant submenu = model->data(index, QMenuModel::LinkSubMenu);
            for (int j = 0; j < indent * 2; ++j) std::cout << " ";
            std::cout << " > " << label.toUtf8().constData() << std::endl;
            if (submenu.isValid()) {
                printModel(qobject_cast<QMenuModel*>(submenu.value<QObject*>()), indent + 1);
            }
        }
    }
};

#include "info-menumodel.moc"


int main(int argc, char** argv)
{
    MenuModelMonitor monitor;
    monitor.start();
    int result = QCoreApplication(argc, argv).exec();
    monitor.stop();
    return result;
}