~ken-vandine/libqofono/0.90-suggests

« back to all changes in this revision

Viewing changes to src/qofonosimwatcher.cpp

  • Committer: Slava Monich
  • Date: 2015-09-24 14:20:37 UTC
  • mto: (207.2.3)
  • mto: This revision was merged to the branch mainline in revision 215.
  • Revision ID: git-v1:d733746c9774aa2cfdd8c1e74f27a3393d6a7a0a
[libqofono] Added QOfonoSimWatcher. Contributes to JB#28413

This object is tracking present SIM cards.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2015 Jolla Ltd.
 
4
** Contact: slava.monich@jolla.com
 
5
**
 
6
** GNU Lesser General Public License Usage
 
7
** Alternatively, this file may be used under the terms of the GNU Lesser
 
8
** General Public License version 2.1 as published by the Free Software
 
9
** Foundation and appearing in the file LICENSE.LGPL included in the
 
10
** packaging of this file.  Please review the following information to
 
11
** ensure the GNU Lesser General Public License version 2.1 requirements
 
12
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
13
**
 
14
****************************************************************************/
 
15
 
 
16
#include "qofonosimwatcher.h"
 
17
#include "qofonomanager.h"
 
18
 
 
19
class QOfonoSimWatcher::Private : public QObject {
 
20
    Q_OBJECT
 
21
 
 
22
public:
 
23
    Private(QOfonoSimWatcher *parent);
 
24
 
 
25
    QOfonoSimWatcher *watcher;
 
26
    QSharedPointer<QOfonoManager> ofono;
 
27
    QHash<QString, QSharedPointer<QOfonoSimManager> > allSims;
 
28
    QList<QSharedPointer<QOfonoSimManager> > presentSims;
 
29
    bool valid;
 
30
 
 
31
private Q_SLOTS:
 
32
    void onOfonoAvailableChanged();
 
33
    void updateValid();
 
34
    void updateModems();
 
35
    void updateSims();
 
36
};
 
37
 
 
38
QOfonoSimWatcher::Private::Private(QOfonoSimWatcher *parent) :
 
39
    QObject(parent),
 
40
    watcher(parent),
 
41
    ofono(QOfonoManager::instance()),
 
42
    valid(false)
 
43
{
 
44
    connect(ofono.data(), SIGNAL(availableChanged(bool)), SLOT(onOfonoAvailableChanged()));
 
45
    connect(ofono.data(), SIGNAL(modemsChanged(QStringList)), SLOT(updateModems()));
 
46
    if (ofono->available()) {
 
47
        onOfonoAvailableChanged();
 
48
    }
 
49
}
 
50
 
 
51
void QOfonoSimWatcher::Private::onOfonoAvailableChanged()
 
52
{
 
53
    if (ofono->available()) {
 
54
        updateModems();
 
55
        updateValid();
 
56
    } else {
 
57
        updateValid();
 
58
        allSims.clear();
 
59
        if (!presentSims.isEmpty()) {
 
60
            presentSims.clear();
 
61
            Q_EMIT watcher->presentSimListChanged();
 
62
            Q_EMIT watcher->presentSimCountChanged();
 
63
        }
 
64
    }
 
65
}
 
66
 
 
67
void QOfonoSimWatcher::Private::updateModems()
 
68
{
 
69
    QStringList oldModems = allSims.keys();
 
70
    QStringList newModems = ofono->modems();
 
71
    oldModems.sort();
 
72
    newModems.sort();
 
73
    if (oldModems != newModems) {
 
74
        const int oldCount = oldModems.count();
 
75
        const int newCount = newModems.count();
 
76
        int i;
 
77
        for (i=0; i<oldCount; i++) {
 
78
            QString path(oldModems.at(i));
 
79
            if (!newModems.contains(path)) {
 
80
                // This modem is no more
 
81
                allSims.take(path)->disconnect(this);
 
82
            }
 
83
        }
 
84
        for (i=0; i<newCount; i++) {
 
85
            QString path(newModems.at(i));
 
86
            if (!allSims.contains(path)) {
 
87
                // This is a new modem
 
88
                QOfonoSimManager *sim = new QOfonoSimManager(this);
 
89
                sim->fixModemPath(path);
 
90
                connect(sim, SIGNAL(validChanged(bool)), SLOT(updateSims()));
 
91
                connect(sim, SIGNAL(presenceChanged(bool)), SLOT(updateSims()));
 
92
                allSims.insert(path, QSharedPointer<QOfonoSimManager>(sim));
 
93
            }
 
94
        }
 
95
        updateSims();
 
96
    }
 
97
}
 
98
 
 
99
void QOfonoSimWatcher::Private::updateSims()
 
100
{
 
101
    QList<QSharedPointer<QOfonoSimManager> > sims;
 
102
    QStringList modems = allSims.keys();
 
103
    modems.sort();
 
104
    int i, n = modems.count();
 
105
    for (i=0; i<n; i++) {
 
106
        QSharedPointer<QOfonoSimManager> sim = allSims.value(modems.at(i));
 
107
        if (sim->isValid() && sim->present()) {
 
108
            sims.append(sim);
 
109
        }
 
110
    }
 
111
    if (sims.count() != presentSims.count()) {
 
112
        presentSims = sims;
 
113
        Q_EMIT watcher->presentSimListChanged();
 
114
        Q_EMIT watcher->presentSimCountChanged();
 
115
    } else {
 
116
        n = sims.count();
 
117
        for (i=0; i<n; i++) {
 
118
            if (sims.at(i).data() != presentSims.at(i).data()) {
 
119
                presentSims = sims;
 
120
                Q_EMIT watcher->presentSimListChanged();
 
121
                break;
 
122
            }
 
123
        }
 
124
    }
 
125
    updateValid();
 
126
}
 
127
 
 
128
void QOfonoSimWatcher::Private::updateValid()
 
129
{
 
130
    // This object is valid if QOfonoManager and all SIM managers are valid.
 
131
    bool isValid = ofono->available();
 
132
    if (isValid) {
 
133
        QList<QSharedPointer<QOfonoSimManager> > sims = allSims.values();
 
134
        const int n = sims.count();
 
135
        for (int i=0; i<n && isValid; i++) {
 
136
            isValid = sims.at(i)->isValid();
 
137
        }
 
138
    }
 
139
    if (valid != isValid) {
 
140
        valid = isValid;
 
141
        Q_EMIT watcher->validChanged();
 
142
    }
 
143
}
 
144
 
 
145
QOfonoSimWatcher::QOfonoSimWatcher(QObject *parent) :
 
146
    QObject(parent),
 
147
    d_ptr(new Private(this))
 
148
{
 
149
}
 
150
 
 
151
QOfonoSimWatcher::~QOfonoSimWatcher()
 
152
{
 
153
    // d_ptr is a child object, it gets deleted automatically
 
154
}
 
155
 
 
156
bool QOfonoSimWatcher::isValid() const
 
157
{
 
158
    return d_ptr->valid;
 
159
}
 
160
 
 
161
int QOfonoSimWatcher::presentSimCount() const
 
162
{
 
163
    return d_ptr->presentSims.count();
 
164
}
 
165
 
 
166
QList<QSharedPointer<QOfonoSimManager> > QOfonoSimWatcher::presentSimList() const
 
167
{
 
168
    return d_ptr->presentSims;
 
169
}
 
170
 
 
171
#include "qofonosimwatcher.moc"