File: qofonoextmodemlistmodel.cpp

package info (click to toggle)
libqofonoext 1.0.32-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: cpp: 2,683; makefile: 8
file content (224 lines) | stat: -rw-r--r-- 7,773 bytes parent folder | download
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
/****************************************************************************
**
** Copyright (C) 2015-2021 Jolla Ltd.
** Copyright (C) 2015-2021 Slava Monich <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 "qofonoextmodemlistmodel.h"

QOfonoExtModemListModel::QOfonoExtModemListModel(QObject* aParent) :
    QAbstractListModel(aParent),
    iModemManager(QOfonoExtModemManager::instance()),
    iAvailableModems(iModemManager->availableModems()),
    iEnabledModems(iModemManager->enabledModems()),
    iDefaultVoiceModem(iModemManager->defaultVoiceModem()),
    iDefaultDataModem(iModemManager->defaultDataModem())
{
    connect(iModemManager.data(),
        SIGNAL(validChanged(bool)),
        SLOT(onValidChanged(bool)));
    connect(iModemManager.data(),
        SIGNAL(availableModemsChanged(QStringList)),
        SLOT(onAvailableModemsChanged(QStringList)));
    connect(iModemManager.data(),
        SIGNAL(enabledModemsChanged(QStringList)),
        SLOT(onEnabledModemsChanged(QStringList)));
    connect(iModemManager.data(),
        SIGNAL(defaultDataModemChanged(QString)),
        SLOT(onDefaultDataModemChanged(QString)));
    connect(iModemManager.data(),
        SIGNAL(defaultVoiceModemChanged(QString)),
        SLOT(onDefaultVoiceModemChanged(QString)));
    connect(iModemManager.data(),
        SIGNAL(presentSimChanged(int,bool)),
        SLOT(onPresentSimChanged(int,bool)));
    connect(iModemManager.data(),
        SIGNAL(imeiCodesChanged(QStringList)),
        SLOT(onImeiCodesChanged(QStringList)));
    connect(iModemManager.data(),
        SIGNAL(imeisvCodesChanged(QStringList)),
        SLOT(onImeisvCodesChanged(QStringList)));
}

bool QOfonoExtModemListModel::valid() const
{
    return iModemManager->valid();
}

int QOfonoExtModemListModel::count() const
{
    return iAvailableModems.count();
}

QHash<int,QByteArray> QOfonoExtModemListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[PathRole]         = "path";
    roles[EnabledRole]      = "enabled";
    roles[DefaultDataRole]  = "defaultForData";
    roles[DefaultVoiceRole] = "defaultForVoice";
    roles[SimPresentRole]   = "simPresent";
    roles[IMEIRole]         = "imei";
    roles[IMEISVRole]       = "imeisv";
    return roles;
}

int QOfonoExtModemListModel::rowCount(const QModelIndex& aParent) const
{
    return iAvailableModems.count();
}

QVariant QOfonoExtModemListModel::data(const QModelIndex& aIndex, int aRole) const
{
    const int row = aIndex.row();
    if (row >= 0 && row < iAvailableModems.count()) {
        switch (aRole) {
        case PathRole:         return iAvailableModems.at(row);
        case EnabledRole:      return iEnabledModems.contains(iAvailableModems.at(row));
        case DefaultDataRole:  return iAvailableModems.indexOf(iDefaultDataModem) == row;
        case DefaultVoiceRole: return iAvailableModems.indexOf(iDefaultVoiceModem) == row;
        case SimPresentRole:   return iModemManager->simPresentAt(row);
        case IMEIRole:         return iModemManager->imeiAt(row);
        case IMEISVRole:       return iModemManager->imeisvAt(row);
        }
    }
    qWarning() << aIndex << aRole;
    return QVariant();
}

bool QOfonoExtModemListModel::setData(const QModelIndex& aIndex, const QVariant& aValue, int aRole)
{
    const int row = aIndex.row();
    if (row >= 0 && row < iAvailableModems.count() && aRole == EnabledRole) {
        const bool enabled = aValue.toBool();
        const QString& path(iAvailableModems.at(row));
        const int index = iEnabledModems.indexOf(path);
        if (enabled != (index >= 0)) {
            QStringList enabledModems = iEnabledModems;
            if (enabled) {
                enabledModems.append(path);
            } else {
                enabledModems.removeAt(index);
            }
            iModemManager->setEnabledModems(enabledModems);
        }
        return true;
    }
    return false;
}

void QOfonoExtModemListModel::onValidChanged(bool aValid)
{
    if (aValid) {
        beginResetModel();
        endResetModel();
    }
    Q_EMIT validChanged(aValid);
}

void QOfonoExtModemListModel::onAvailableModemsChanged(QStringList aModems)
{
    const bool countHasChanged = iAvailableModems.count() != aModems.count();
    beginResetModel();
    iAvailableModems = aModems;
    endResetModel();
    if (countHasChanged) {
        Q_EMIT countChanged(iAvailableModems.count());
    }
}

void QOfonoExtModemListModel::onEnabledModemsChanged(QStringList aModems)
{
    if (iEnabledModems != aModems) {
        QStringList prevModems = iEnabledModems;
        iEnabledModems = aModems;
        const int n = iAvailableModems.count();
        QVector<int> role;
        role.append(EnabledRole);
        for (int i=0; i<n; i++) {
            const QString& path(iAvailableModems.at(i));
            if (prevModems.contains(path) != aModems.contains(path)) {
                QModelIndex index(createIndex(i, 0));
                Q_EMIT dataChanged(index, index, role);
            }
        }
    }
}

void QOfonoExtModemListModel::onDefaultDataModemChanged(QString aModemPath)
{
    const int prevIndex = iAvailableModems.indexOf(iDefaultDataModem);
    iDefaultDataModem = aModemPath;
    defaultModemChanged(DefaultDataRole, prevIndex, iAvailableModems.indexOf(aModemPath));
}

void QOfonoExtModemListModel::onDefaultVoiceModemChanged(QString aModemPath)
{
    const int prevIndex = iAvailableModems.indexOf(iDefaultVoiceModem);
    iDefaultVoiceModem = aModemPath;
    defaultModemChanged(DefaultVoiceRole, prevIndex, iAvailableModems.indexOf(aModemPath));
}

void QOfonoExtModemListModel::onPresentSimChanged(int aIndex, bool aPresent)
{
    QVector<int> role;
    role.append(SimPresentRole);
    QModelIndex index(createIndex(aIndex, 0));
    Q_EMIT dataChanged(index, index, role);
}

void QOfonoExtModemListModel::defaultModemChanged(Role aRole, int aPrevRow, int aNewRow)
{
    if (aPrevRow != aNewRow) {
        QVector<int> role;
        role.append(aRole);
        if (aPrevRow >= 0) {
            QModelIndex index(createIndex(aPrevRow, 0));
            Q_EMIT dataChanged(index, index, role);
        }
        if (aNewRow >= 0) {
            QModelIndex index(createIndex(aNewRow, 0));
            Q_EMIT dataChanged(index, index, role);
        }
    }
}

void QOfonoExtModemListModel::onImeiCodesChanged(QStringList aList)
{
    QStringList prev = iImeiList;
    iImeiList = aList;
    roleChanged(IMEIRole, prev, aList);
}

void QOfonoExtModemListModel::onImeisvCodesChanged(QStringList aList)
{
    QStringList prev = iImeisvList;
    iImeisvList = aList;
    roleChanged(IMEISVRole, prev, aList);
}

void QOfonoExtModemListModel::roleChanged(Role aRole, QStringList aPrevList, QStringList aNewList)
{
    // This is slightly paranoid... All these 3 counts should be the same
    const int n1 = iAvailableModems.count();
    const int n2 = aPrevList.count();
    const int n3 = aNewList.count();
    const int n = qMin(n1, qMin(n2, n3));
    QVector<int> role;
    role.append(aRole);
    for (int i=0; i<n; i++) {
        if (aPrevList.at(i) != aNewList.at(i)) {
            QModelIndex index(createIndex(i, 0));
            Q_EMIT dataChanged(index, index, role);
        }
    }
}