aboutsummaryrefslogtreecommitdiff
path: root/src/common/qdbusobject.cpp
blob: 174a9d81dcf8bf30966841462bf6a6d31da75dda (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
/*
 * 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>
 */

#include "qdbusobject.h"

#include <QDebug>

QDBusObject::QDBusObject()
    :m_watchId(0),
     m_busType(None),
     m_status(QDBusObject::Disconnected)
{
    qDebug() << "DBUS CREATED";
    qRegisterMetaType<QDBusObject::ConnectionStatus>("QDBusObject::ConnectionStatus");
}

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

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

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

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

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

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

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

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

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

void QDBusObject::connect()
{
    if (m_status != QDBusObject::Disconnected) {
        return;
    } else if ((m_busType > None) && !m_objectPath.isEmpty() && !m_busName.isEmpty()) {
        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,
                                      QDBusObject::onServiceAppeared,
                                      QDBusObject::onServiceFanished,
                                      this,
                                      NULL);

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

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

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

    self->setStatus(QDBusObject::Connected);
    self->serviceAppear(connection);
}

void QDBusObject::onServiceFanished(GDBusConnection *connection, const gchar *, gpointer data)
{
    QDBusObject *self = reinterpret_cast<QDBusObject*>(data);    
    qDebug() << "service disappear";

    self->setStatus(QDBusObject::Connecting);
    self->serviceVanish(connection);
}