1
/****************************************************************************
3
** Copyright (C) 2015 Jolla Ltd.
4
** Contact: slava.monich@jolla.com
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.
14
****************************************************************************/
16
#include "qofonosimwatcher.h"
17
#include "qofonomanager.h"
19
class QOfonoSimWatcher::Private : public QObject {
23
Private(QOfonoSimWatcher *parent);
25
QOfonoSimWatcher *watcher;
26
QSharedPointer<QOfonoManager> ofono;
27
QHash<QString, QSharedPointer<QOfonoSimManager> > allSims;
28
QList<QSharedPointer<QOfonoSimManager> > presentSims;
32
void onOfonoAvailableChanged();
38
QOfonoSimWatcher::Private::Private(QOfonoSimWatcher *parent) :
41
ofono(QOfonoManager::instance()),
44
connect(ofono.data(), SIGNAL(availableChanged(bool)), SLOT(onOfonoAvailableChanged()));
45
connect(ofono.data(), SIGNAL(modemsChanged(QStringList)), SLOT(updateModems()));
46
if (ofono->available()) {
47
onOfonoAvailableChanged();
51
void QOfonoSimWatcher::Private::onOfonoAvailableChanged()
53
if (ofono->available()) {
59
if (!presentSims.isEmpty()) {
61
Q_EMIT watcher->presentSimListChanged();
62
Q_EMIT watcher->presentSimCountChanged();
67
void QOfonoSimWatcher::Private::updateModems()
69
QStringList oldModems = allSims.keys();
70
QStringList newModems = ofono->modems();
73
if (oldModems != newModems) {
74
const int oldCount = oldModems.count();
75
const int newCount = newModems.count();
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);
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));
99
void QOfonoSimWatcher::Private::updateSims()
101
QList<QSharedPointer<QOfonoSimManager> > sims;
102
QStringList modems = allSims.keys();
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()) {
111
if (sims.count() != presentSims.count()) {
113
Q_EMIT watcher->presentSimListChanged();
114
Q_EMIT watcher->presentSimCountChanged();
117
for (i=0; i<n; i++) {
118
if (sims.at(i).data() != presentSims.at(i).data()) {
120
Q_EMIT watcher->presentSimListChanged();
128
void QOfonoSimWatcher::Private::updateValid()
130
// This object is valid if QOfonoManager and all SIM managers are valid.
131
bool isValid = ofono->available();
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();
139
if (valid != isValid) {
141
Q_EMIT watcher->validChanged();
145
QOfonoSimWatcher::QOfonoSimWatcher(QObject *parent) :
147
d_ptr(new Private(this))
151
QOfonoSimWatcher::~QOfonoSimWatcher()
153
// d_ptr is a child object, it gets deleted automatically
156
bool QOfonoSimWatcher::isValid() const
161
int QOfonoSimWatcher::presentSimCount() const
163
return d_ptr->presentSims.count();
166
QList<QSharedPointer<QOfonoSimManager> > QOfonoSimWatcher::presentSimList() const
168
return d_ptr->presentSims;
171
#include "qofonosimwatcher.moc"