aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/qdbusobject.cpp
blob: fab2df775308afebf60a755aa6593d55a196f911 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * 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:
 *      Renato Araujo Oliveira Filho <renato@canonical.com>
 */

extern "C" {
#include <glib-object.h>
#include <gio/gio.h>
}

#include "qdbusobject.h"
#include "qmenumodelevents.h"

#include <QDebug>
#include <QCoreApplication>

/*!
    \qmltype QDBusObject
    \brief The QDBusObject is a base class

    \b {This component is under heavy development.}

    This is a abstracted class used by QDBusMenuModel and QDBusActionGroup
*/

/*!
    \qmlproperty int QDBusObject::busType
    This property holds the dbus session type which will be used during the connection.

    This must be seteed before call start method
    The valid values are:
    \list
        \li 1 - SessionBus
        \li 2 - SystemBus
    \endlist
*/

/*!
    \qmlproperty int QDBusObject::busName
    This property holds the dbus service name related with menu.

    This must be seteed before call start method
*/

/*!
    \qmlproperty int QDBusObject::objectPath
    This property holds the dbus object path related with the menu.

    This must be seteed before call start method
*/

/*!
    \qmlproperty int QDBusObject::status
    This property holds current dbus connection status

    Te velid status are:
    \list
        \li 0 - Disconnected
        \li 1 - Connecting
        \li 2 - Connected
    \endlist
*/

QDBusObject::QDBusObject(QObject* listener)
    :m_listener(listener),
     m_watchId(0),
     m_busType(DBusEnums::None),
     m_status(DBusEnums::Disconnected)
{
    qRegisterMetaType<DBusEnums::ConnectionStatus>("DBusEnums::ConnectionStatus");
}

QDBusObject::~QDBusObject()
{
    if (m_watchId != 0) {
        g_bus_unwatch_name (m_watchId);
        m_watchId = 0;
    }
}

DBusEnums::BusType QDBusObject::busType() const
{
    return m_busType;
}

void QDBusObject::setBusType(DBusEnums::BusType type)
{
    if (m_busType != type) {
        if (m_status != DBusEnums::Disconnected)
            disconnect();
        m_busType = type;
        Q_EMIT busTypeChanged(m_busType);
    }
}

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

void QDBusObject::setBusName(const QString &busName)
{
    if (m_busName != busName) {
        if (m_status != DBusEnums::Disconnected)
            disconnect();
        m_busName = busName;
        Q_EMIT busNameChanged(m_busName);
    }
}

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

void QDBusObject::setObjectPath(const QString &objectPath)
{
    if (m_objectPath != objectPath) {
        if (m_status != DBusEnums::Disconnected)
            disconnect();
        m_objectPath = objectPath;
        Q_EMIT objectPathChanged(m_objectPath);
    }
}

void QDBusObject::setStatus(DBusEnums::ConnectionStatus status)
{
    if (m_status != status) {
        m_status = status;
        Q_EMIT statusChanged(m_status);
    }
}

DBusEnums::ConnectionStatus QDBusObject::status() const
{
    return m_status;
}

void QDBusObject::connect()
{
    if (m_status != DBusEnums::Disconnected) {
        return;
    } else if ((m_busType > DBusEnums::None) && !m_objectPath.isEmpty() && !m_busName.isEmpty()) {
        GBusType type = m_busType == DBusEnums::SessionBus ? G_BUS_TYPE_SESSION : G_BUS_TYPE_SYSTEM;
        m_watchId = g_bus_watch_name (type,
                                      m_busName.toUtf8().data(),
                                      G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
                                      QDBusObject::onServiceAppeared,
                                      QDBusObject::onServiceVanished,
                                      this,
                                      NULL);

        setStatus(DBusEnums::Connecting);
    } else {
        qWarning() << "Invalid dbus connection args";
    }
}

void QDBusObject::disconnect()
{
    if (m_status != DBusEnums::Disconnected) {
        g_bus_unwatch_name (m_watchId);
        m_watchId = 0;
        setStatus(DBusEnums::Disconnected);
    }
}

void QDBusObject::onServiceAppeared(GDBusConnection *connection, const gchar *, const gchar *, gpointer data)
{
    QDBusObject *self = reinterpret_cast<QDBusObject*>(data);

    if (self->m_listener) {
        DbusObjectServiceEvent dose(connection, true);
        QCoreApplication::sendEvent(self->m_listener, &dose);
    }
}

void QDBusObject::onServiceVanished(GDBusConnection *connection, const gchar *, gpointer data)
{
    QDBusObject *self = reinterpret_cast<QDBusObject*>(data);

    if (self->m_listener) {
        DbusObjectServiceEvent dose(connection, false);
        QCoreApplication::sendEvent(self->m_listener, &dose);
    }
}

bool QDBusObject::event(QEvent* e)
{
    if (e->type() == DbusObjectServiceEvent::eventType) {
        DbusObjectServiceEvent *dose = static_cast<DbusObjectServiceEvent*>(e);
        if (dose->visible) {
            serviceAppear(dose->connection);
            setStatus(DBusEnums::Connected);
        } else {
            setStatus(DBusEnums::Connecting);
            serviceVanish(dose->connection);
        }
        return true;
    }
    return false;
}