~ken-vandine/libqofono/0.90-suggests

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
/****************************************************************************
**
** Copyright (C) 2014-2015 Jolla Ltd.
** Contact: slava.monich@jolla.com
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/

#include "qofonomodeminterface.h"
#include "qofonomodem.h"

#define SUPER QOfonoObject

class QOfonoModemInterface::Private : public QOfonoObject::ExtData
{
public:
    QString interfaceName;
    QSharedPointer<QOfonoModem> modem;
    QOfonoModem::ExtData *ext;
    bool modemValid;

    Private(const QString &iface, QOfonoModem::ExtData *data) :
        interfaceName(iface), ext(data), modemValid(false) {}
    ~Private() { delete ext; }
};

QOfonoModemInterface::QOfonoModemInterface(const QString &iface, ExtData *ext, QObject *parent) :
    SUPER(new Private(iface, ext), parent)
{
}

QOfonoModemInterface::QOfonoModemInterface(const QString &iface, QObject *parent) :
    SUPER(new Private(iface, NULL), parent)
{
}

QOfonoModemInterface::~QOfonoModemInterface()
{
}

QOfonoModemInterface::Private* QOfonoModemInterface::privateData() const
{
    return (Private*)SUPER::extData();
}

QOfonoObject::ExtData* QOfonoModemInterface::extData() const
{
    return privateData()->ext;
}

QString QOfonoModemInterface::modemPath() const
{
    return objectPath();
}

void QOfonoModemInterface::setModemPath(const QString &path)
{
    setObjectPath(path);
}

void QOfonoModemInterface::fixModemPath(const QString &path)
{
    fixObjectPath(path);
}

bool QOfonoModemInterface::isValid() const
{
    return privateData()->modemValid && SUPER::isValid();
}

void QOfonoModemInterface::objectPathChanged(const QString &path, const QVariantMap *)
{
    // The base implementation would immediately create the D-Bus interface
    // object. However we need to check if our interface is actually available
    // and postpone creation of the D-Bus interface object if our interface
    // isn't there (see onModemInterfacesChanged below)
    bool wasReady = isReady();

    ValidTracker track(this);
    Private *d_ptr = privateData();
    if (!d_ptr->modem.isNull()) {
        QOfonoModem *modem = d_ptr->modem.data();
        disconnect(modem, SIGNAL(interfacesChanged(QStringList)),
            this, SLOT(onModemInterfacesChanged(QStringList)));
        disconnect(modem, SIGNAL(validChanged(bool)),
            this, SLOT(onModemValidChanged(bool)));
        d_ptr->modemValid = false;
        d_ptr->modem.reset();
    }

    setDbusInterface(NULL, NULL);

    d_ptr->modem = QOfonoModem::instance(objectPath());
    QOfonoModem *modem = d_ptr->modem.data();
    connect(modem, SIGNAL(interfacesChanged(QStringList)),
        this, SLOT(onModemInterfacesChanged(QStringList)));
    connect(modem, SIGNAL(validChanged(bool)),
        this, SLOT(onModemValidChanged(bool)));
    d_ptr->modemValid = modem->isValid();

    Q_EMIT modemPathChanged(path);
    onModemInterfacesChanged(d_ptr->modem->interfaces());
    if (wasReady != isReady()) {
        Q_EMIT readyChanged();
    }
}

void QOfonoModemInterface::onModemValidChanged(bool valid)
{
    ValidTracker track(this);
    privateData()->modemValid = valid;
}

void QOfonoModemInterface::onModemInterfacesChanged(const QStringList &interfaces)
{
    if (interfaces.contains(privateData()->interfaceName)) {
        Q_ASSERT(!objectPath().isEmpty());
        if (!dbusInterface()) {
            setDbusInterface(createDbusInterface(objectPath()), NULL);
        }
    } else {
        setDbusInterface(NULL, NULL);
    }
}

// The usefullness if the 'ready' property is questionable but it has to exist
// for backward compatibility.
bool QOfonoModemInterface::isReady() const
{
    return isValid() && !getProperties().isEmpty();
}

void QOfonoModemInterface::updateProperty(const QString &key, const QVariant &value)
{
    bool wasReady = isReady();
    SUPER::updateProperty(key, value);
    if (wasReady != isReady()) {
        Q_EMIT readyChanged();
    }
}

void QOfonoModemInterface::getPropertiesFinished(const QVariantMap &properties, const QDBusError *error)
{
    bool wasReady = isReady();
    SUPER::getPropertiesFinished(properties, error);
    if (wasReady != isReady()) {
        Q_EMIT readyChanged();
    }
}