~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
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
/****************************************************************************
**
** Copyright (C) 2013-2015 Jolla Ltd.
** Contact: lorn.potter@jollamobile.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 "qofononetworkoperator.h"
#include "ofono_network_operator_interface.h"

#define SUPER QOfonoObject

class QOfonoNetworkOperator::Private : public SUPER::ExtData
{
public:
    bool registering;
    Private() : registering(false) {}
};

QOfonoNetworkOperator::QOfonoNetworkOperator(QObject *parent) :
    SUPER(new Private, parent)
{
}

// Constructs the object with known set of properties, saves a roundtrip
// via D-Bus and makes the object valid immediately
QOfonoNetworkOperator::QOfonoNetworkOperator(const QString &path,
    const QVariantMap &properties, QObject *parent) :
    SUPER(new Private, parent)
{
    setObjectPath(path, &properties);
}

QOfonoNetworkOperator::~QOfonoNetworkOperator()
{
}

QString QOfonoNetworkOperator::operatorPath() const
{
    return objectPath();
}

void QOfonoNetworkOperator::setOperatorPath(const QString &path)
{
    setObjectPath(path);
}

void QOfonoNetworkOperator::objectPathChanged(const QString &path, const QVariantMap *properties)
{
    SUPER::objectPathChanged(path, properties);
    Q_EMIT operatorPathChanged(path);
}

QDBusAbstractInterface *QOfonoNetworkOperator::createDbusInterface(const QString &path)
{
    OfonoNetworkOperator *iface = new OfonoNetworkOperator("org.ofono", path, QDBusConnection::systemBus(), this);
    iface->setTimeout(120*1000); //increase dbus timeout as registration can take a long time
    return iface;
}

void QOfonoNetworkOperator::dbusInterfaceDropped()
{
    SUPER::dbusInterfaceDropped();
    Private *d_ptr = privateData();
    if (d_ptr->registering) {
        d_ptr->registering = false;
        Q_EMIT registeringChanged(d_ptr->registering);
    }
}

void QOfonoNetworkOperator::registerOperator()
{
    Private *d_ptr = privateData();
    if (!d_ptr->registering) {
        OfonoNetworkOperator *iface = (OfonoNetworkOperator*)dbusInterface();
        if (iface) {
            d_ptr->registering = true;
            Q_EMIT registeringChanged(d_ptr->registering);
            connect(new QDBusPendingCallWatcher(iface->Register(), iface),
                SIGNAL(finished(QDBusPendingCallWatcher*)),
                SLOT(onRegisterFinished(QDBusPendingCallWatcher*)));
        }
    }
}

void QOfonoNetworkOperator::onRegisterFinished(QDBusPendingCallWatcher *watch)
{
    watch->deleteLater();
    QDBusPendingReply<> reply(*watch);
    QOfonoNetworkOperator::Error error = NoError;
    QString errorString;

    if (reply.isError()) {
         qWarning() << "QOfonoNetworkOperator::registerOperator() failed:" << reply.error();
         error = errorNameToEnum(reply.error().name());
         errorString = reply.error().name() + " " + reply.error().message();
    }

    Private *d_ptr = privateData();
    d_ptr->registering = false;
    Q_EMIT registerComplete(error, errorString);
    Q_EMIT registeringChanged(d_ptr->registering);
}

bool QOfonoNetworkOperator::registering() const
{
    return privateData()->registering;
}

QString QOfonoNetworkOperator::name() const
{
    return getString("Name");
}

QString QOfonoNetworkOperator::status() const
{
    return getString("Status");
}

QString QOfonoNetworkOperator::mcc() const
{
    return getString("MobileCountryCode");
}

QString QOfonoNetworkOperator::mnc() const
{
    return getString("MobileNetworkCode");
}

QStringList QOfonoNetworkOperator::technologies() const
{
    return getStringList("Technologies");
}

QString QOfonoNetworkOperator::additionalInfo() const
{
    return getString("AdditionalInformation");
}

bool QOfonoNetworkOperator::isValid() const
{
    return SUPER::isValid();
}

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

void QOfonoNetworkOperator::propertyChanged(const QString &property, const QVariant &value)
{
    SUPER::propertyChanged(property, value);
    if (property == QLatin1String("Name")) {
        Q_EMIT nameChanged(value.value<QString>());
    } else if (property == QLatin1String("Status")) {
        Q_EMIT statusChanged(value.value<QString>());
    } else if (property == QLatin1String("MobileCountryCode")) {
        Q_EMIT mccChanged(value.value<QString>());
    } else if (property == QLatin1String("MobileNetworkCode")) {
        Q_EMIT mncChanged(value.value<QString>());
    } else if (property == QLatin1String("Technologies")) {
        Q_EMIT technologiesChanged(value.value<QStringList>());
    } else if (property == QLatin1String("AdditionalInformation")) {
        Q_EMIT additionalInfoChanged(value.value<QString>());
    }
}

QOfonoNetworkOperator::Error QOfonoNetworkOperator::errorNameToEnum(const QString &errorName)
{
    if (errorName == "")
        return NoError;
    else if (errorName == "org.ofono.Error.NotImplemented")
        return NotImplementedError;
    else if (errorName == "org.ofono.Error.InProgress")
        return InProgressError;
    else if (errorName == "org.ofono.Error.InvalidArguments")
        return InvalidArgumentsError;
    else if (errorName == "org.ofono.Error.InvalidFormat")
        return InvalidFormatError;
    else if (errorName == "org.ofono.Error.Failed")
        return FailedError;
    else
        return UnknownError;
}