File: qofonosimlistmodel.cpp

package info (click to toggle)
libqofono 0.124-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,556 kB
  • sloc: cpp: 13,292; xml: 978; sh: 33; makefile: 21
file content (281 lines) | stat: -rw-r--r-- 9,235 bytes parent folder | download | duplicates (2)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/****************************************************************************
**
** Copyright (C) 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 "qofonosimlistmodel.h"
#include <QQmlEngine>

QOfonoSimListModel::QOfonoSimListModel(QObject *parent) :
    QAbstractListModel(parent),
    simWatcher(new QOfonoSimWatcher(this))
{
    simWatcher->setRequireSubscriberIdentity(true);
    simList = simWatcher->presentSimList();
    connect(simWatcher,
        SIGNAL(validChanged()),
        SIGNAL(validChanged()));
    connect(simWatcher,
        SIGNAL(presentSimListChanged()),
        SLOT(onPresentSimListChanged()));
}

bool QOfonoSimListModel::valid() const
{
    return simWatcher->isValid();
}

int QOfonoSimListModel::count() const
{
    return simList.count();
}

bool QOfonoSimListModel::requireSubscriberIdentity() const
{
    return simWatcher->requireSubscriberIdentity();
}

void QOfonoSimListModel::setRequireSubscriberIdentity(bool require)
{
    if (simWatcher->requireSubscriberIdentity() != require) {
        simWatcher->setRequireSubscriberIdentity(require);
        Q_EMIT requireSubscriberIdentityChanged();
    }
}

QHash<int,QByteArray> QOfonoSimListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[PathRole]                = "path";
    roles[SubscriberIdentityRole]  = "subscriberIdentity";
    roles[MobileCountryCodeRole]   = "mobileCountryCode";
    roles[MobileNetworkCodeRole]   = "mobileNetworkCode";
    roles[ServiceProviderNameRole] = "serviceProviderName";
    roles[SubscriberNumbersRole]   = "subscriberNumbers";
    roles[ServiceNumbersRole]      = "serviceNumbers";
    roles[PinRequiredRole]         = "pinRequired";
    roles[LockedPinsRole]          = "lockedPins";
    roles[CardIdentifierRole]      = "cardIdentifier";
    roles[PreferredLanguagesRole]  = "preferredLanguages";
    roles[PinRetriesRole]          = "pinRetries";
    roles[FixedDialingRole]        = "fixedDialing";
    roles[BarredDialingRole]       = "barredDialing";
    return roles;
}

int QOfonoSimListModel::rowCount(const QModelIndex&) const
{
    return simList.count();
}

QVariant QOfonoSimListModel::data(const QModelIndex& index, int role) const
{
    const int row = index.row();
    if (row >= 0 && row < simList.count()) {
        const QOfonoSimManager* sim = simList.at(row).data();
        switch (role) {
        case PathRole:                return sim->modemPath();
        case SubscriberIdentityRole:  return sim->subscriberIdentity();
        case MobileCountryCodeRole:   return sim->mobileCountryCode();
        case MobileNetworkCodeRole:   return sim->mobileNetworkCode();
        case ServiceProviderNameRole: return sim->serviceProviderName();
        case SubscriberNumbersRole:   return sim->subscriberNumbers();
        case ServiceNumbersRole:      return sim->serviceNumbers();
        case PinRequiredRole:         return sim->pinRequired();
        case LockedPinsRole:          return sim->lockedPins();
        case CardIdentifierRole:      return sim->cardIdentifier();
        case PreferredLanguagesRole:  return sim->preferredLanguages();
        case PinRetriesRole:          return sim->pinRetries();
        case FixedDialingRole:        return sim->fixedDialing();
        case BarredDialingRole:       return sim->barredDialing();
        }
    } else {
        qWarning() << index << role;
    }
    return QVariant();
}

bool QOfonoSimListModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
    const int row = index.row();
    if (row >= 0 && row < simList.count() && role == SubscriberNumbersRole) {
        simList.at(row)->setSubscriberNumbers(value.toStringList());
        return true;
    } else {
        qWarning() << index << role;
    }
    return false;
}

int QOfonoSimListModel::indexOf(QOfonoSimManager* sim)
{
    // We cann't just do simList.indexOf(sim) because that would create temporary
    // QOfonoSimManager::SharedPointer on stack, pass it to simList.indexOf, and
    // eventually destroy QOfonoSimManager.
    const int n = simList.count();
    for (int i=0; i<n; i++) {
        if (simList.at(i).data() == sim) {
            return i;
        }
    }
    return -1;
}

void QOfonoSimListModel::simPropertyChanged(Role role)
{
    int row = indexOf((QOfonoSimManager*)sender());
    if (row >= 0) {
        QModelIndex modelIndex = index(row);
        QVector<int> roles;
        roles.append(role);
        Q_EMIT dataChanged(modelIndex, modelIndex, roles);
    }
}

void QOfonoSimListModel::onPresentSimListChanged()
{
    QList<QOfonoSimManager::SharedPointer> newList = simWatcher->presentSimList();
    const bool countHasChanged = simList.count() != simWatcher->presentSimCount();
    int i;
    for (i=simList.count()-1; i>=0; i--) {
        QOfonoSimManager::SharedPointer sim = simList.at(i);
        if (!newList.contains(sim)) {
            beginRemoveRows(QModelIndex(), i, i);
            sim->disconnect(this);
            simList.removeAt(i);
            endRemoveRows();
            Q_EMIT simRemoved(sim.data());
        }
    }

    QList<QOfonoSimManager::SharedPointer> oldList = simList;
    simList = newList;
    for (i=0; i<newList.count(); i++) {
        if (!oldList.contains(newList.at(i))) {
            QOfonoSimManager* sim = newList.at(i).data();
            QQmlEngine::setObjectOwnership(sim, QQmlEngine::CppOwnership);
            beginInsertRows(QModelIndex(), i, i);
            sim->disconnect(this);
            connect(sim,
                SIGNAL(subscriberIdentityChanged(QString)),
                SLOT(onSubscriberIdentityChanged()));
            connect(sim,
                SIGNAL(mobileCountryCodeChanged(QString)),
                SLOT(onMobileCountryCodeChanged()));
            connect(sim,
                SIGNAL(mobileNetworkCodeChanged(QString)),
                SLOT(onMobileNetworkCodeChanged()));
            connect(sim,
                SIGNAL(serviceProviderNameChanged(QString)),
                SLOT(onServiceProviderNameChanged()));
            connect(sim,
                SIGNAL(subscriberNumbersChanged(QStringList)),
                SLOT(onSubscriberNumbersChanged()));
            connect(sim,
                SIGNAL(serviceNumbersChanged(QVariantMap)),
                SLOT(onServiceNumbersChanged()));
            connect(sim,
                SIGNAL(pinRequiredChanged(int)),
                SLOT(onPinRequiredChanged()));
            connect(sim,
                SIGNAL(lockedPinsChanged(QVariantList)),
                SLOT(onLockedPinsChanged()));
            connect(sim,
                SIGNAL(cardIdentifierChanged(QString)),
                SLOT(onCardIdentifierChanged()));
            connect(sim,
                SIGNAL(preferredLanguagesChanged(QStringList)),
                SLOT(onPreferredLanguagesChanged()));
            connect(sim,
                SIGNAL(pinRetriesChanged(QVariantMap)),
                SLOT(onPinRetriesChanged()));
            connect(sim,
                SIGNAL(fixedDialingChanged(bool)),
                SLOT(onFixedDialingChanged()));
            connect(sim,
                SIGNAL(barredDialingChanged(bool)),
                SLOT(onBarredDialingChanged()));
            endInsertRows();
            Q_EMIT simAdded(sim);
        }
    }

    if (countHasChanged) {
        Q_EMIT countChanged();
    }
}

void QOfonoSimListModel::onSubscriberIdentityChanged()
{
    simPropertyChanged(SubscriberIdentityRole);
}

void QOfonoSimListModel::onMobileCountryCodeChanged()
{
    simPropertyChanged(MobileCountryCodeRole);
}

void QOfonoSimListModel::onMobileNetworkCodeChanged()
{
    simPropertyChanged(MobileNetworkCodeRole);
}

void QOfonoSimListModel::onServiceProviderNameChanged()
{
    simPropertyChanged(ServiceProviderNameRole);
}

void QOfonoSimListModel::onSubscriberNumbersChanged()
{
    simPropertyChanged(SubscriberNumbersRole);
}

void QOfonoSimListModel::onServiceNumbersChanged()
{
    simPropertyChanged(ServiceNumbersRole);
}

void QOfonoSimListModel::onPinRequiredChanged()
{
    simPropertyChanged(PinRequiredRole);
}

void QOfonoSimListModel::onLockedPinsChanged()
{
    simPropertyChanged(LockedPinsRole);
}

void QOfonoSimListModel::onCardIdentifierChanged()
{
    simPropertyChanged(CardIdentifierRole);
}

void QOfonoSimListModel::onPreferredLanguagesChanged()
{
    simPropertyChanged(PreferredLanguagesRole);
}

void QOfonoSimListModel::onPinRetriesChanged()
{
    simPropertyChanged(PinRetriesRole);
}

void QOfonoSimListModel::onFixedDialingChanged()
{
    simPropertyChanged(FixedDialingRole);
}

void QOfonoSimListModel::onBarredDialingChanged()
{
    simPropertyChanged(BarredDialingRole);
}