aboutsummaryrefslogtreecommitdiff
path: root/src/QMenuModel/qdbusmenumodel.cpp
blob: 96f936cb0a1c7fc3b3098bd17ff5825bab28dbe8 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "qdbusmenumodel.h"
#include <QDebug>

QDBusMenuModel::QDBusMenuModel(QObject *parent)
    :QMenuModel(parent),
     m_watchId(0),
     m_busType(None)
{
}

QDBusMenuModel::~QDBusMenuModel()
{
    disconnect();
}

QDBusMenuModel::BusType QDBusMenuModel::busType() const
{
    return m_busType;
}

void QDBusMenuModel::setBusType(QDBusMenuModel::BusType type)
{
    if (m_busType != type) {
        if (isConnected())
            disconnect();
        m_busType = type;
        Q_EMIT busTypeChanged(m_busType);
    }
}

QString QDBusMenuModel::busName() const
{
    return m_busName;
}

void QDBusMenuModel::setBusName(const QString &busName)
{
    if (m_busName != busName) {
        if (isConnected())
            disconnect();
        m_busName = busName;
        Q_EMIT busNameChanged(m_busName);
    }
}

QString QDBusMenuModel::objectPath() const
{
    return m_objectPath;
}

void QDBusMenuModel::setObjectPath(const QString &objectPath)
{
    if (m_objectPath != objectPath) {
        if (isConnected())
            disconnect();
        m_objectPath = objectPath;
        Q_EMIT objectPathChanged(m_objectPath);
    }
}

void QDBusMenuModel::connect()
{
    if (isConnected() || (m_watchId > 0)) {
        return;
    } else if ((m_busType > None) && !m_objectPath.isEmpty() && !m_busName.isEmpty()) {
        qDebug() << "Wait for service";
        GBusType type = m_busType == SessionBus ? G_BUS_TYPE_SESSION : G_BUS_TYPE_SYSTEM;
        m_watchId = g_bus_watch_name (type,
                                      m_busName.toLatin1(),
                                      G_BUS_NAME_WATCHER_FLAGS_NONE,
                                      QDBusMenuModel::onServiceAppeared,
                                      QDBusMenuModel::onServiceFanished,
                                      this,
                                      NULL);
    } else {
        Q_EMIT connectionError("Invalid menu model connection args");
    }
}

void QDBusMenuModel::disconnect()
{
    if (isConnected()) {
        g_bus_unwatch_name (m_watchId);
        m_watchId = 0;

        setMenuModel(NULL);
        Q_EMIT disconnected();
    }
}

bool QDBusMenuModel::isConnected() const
{
    return (m_watchId != 0);
}

void QDBusMenuModel::setIntBusType(int busType)
{
    if ((busType > None) && (busType < LastBusType)) {
        setBusType(static_cast<BusType>(busType));
    }
}

void QDBusMenuModel::onServiceAppeared(GDBusConnection *connection, const gchar *, const gchar *, gpointer data)
{
    qDebug() << "Service appears";
    QDBusMenuModel *self = reinterpret_cast<QDBusMenuModel*>(data);
    GMenuModel *model = reinterpret_cast<GMenuModel*>(g_dbus_menu_model_get(connection,
                                                                            self->m_busName.toLatin1(),
                                                                            self->m_objectPath.toLatin1()));
    self->setMenuModel(model);
    if (model) {
        Q_EMIT self->connected();
    } else {
        Q_EMIT self->connectionError("Fail to retrieve menu model");
        self->disconnect();
    }
}

void QDBusMenuModel::onServiceFanished(GDBusConnection *, const gchar *, gpointer data)
{
    qDebug() << "Service fanished";
    QDBusMenuModel *self = reinterpret_cast<QDBusMenuModel*>(data);
    Q_EMIT self->connectionError("Menu model disapear");
    self->disconnect();
}