~ken-vandine/libqofono/0.90-suggests

3 by Lorn Potter
2nd initial commit. Long Live QOfono!
1
/****************************************************************************
2
**
207.9.1 by Slava Monich
[libqofono] Retry GetModems on timeout. Contributes to JB#33995
3
** Copyright (C) 2013-2016 Jolla Ltd.
13 by Lorn Potter
change email
4
** Contact: lorn.potter@jollamobile.com
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
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 "qofonomanager.h"
207.9.1 by Slava Monich
[libqofono] Retry GetModems on timeout. Contributes to JB#33995
17
#include "qofonoutils_p.h"
181.1.1 by Slava Monich
[libqofono] Generate D-Bus stubs at compile time
18
#include "ofono_manager_interface.h"
171.1.1 by Slava Monich
[libqofono] Added base classes for modem interfaces
19
20
class QOfonoManager::Private
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
21
{
22
public:
23
    OfonoManager *ofonoManager;
24
    QStringList modems;
27 by Lorn Potter
add currentOperator to net reg and other fixes
25
    bool available;
171.1.1 by Slava Monich
[libqofono] Added base classes for modem interfaces
26
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
27
    Private() : ofonoManager(NULL), available(false) {}
207.9.1 by Slava Monich
[libqofono] Retry GetModems on timeout. Contributes to JB#33995
28
29
    void getModems(QOfonoManager *manager);
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
30
};
31
207.9.1 by Slava Monich
[libqofono] Retry GetModems on timeout. Contributes to JB#33995
32
void QOfonoManager::Private::getModems(QOfonoManager *manager)
33
{
34
    if (ofonoManager) {
35
        connect(new QDBusPendingCallWatcher(
36
            ofonoManager->GetModems(), ofonoManager),
37
            SIGNAL(finished(QDBusPendingCallWatcher*)), manager,
38
            SLOT(onGetModemsFinished(QDBusPendingCallWatcher*)));
39
    }
40
}
41
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
42
QOfonoManager::QOfonoManager(QObject *parent) :
171.1.1 by Slava Monich
[libqofono] Added base classes for modem interfaces
43
    QObject(parent),
44
    d_ptr(new Private)
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
45
{
171.1.1 by Slava Monich
[libqofono] Added base classes for modem interfaces
46
    QOfonoDbusTypes::registerObjectPathProperties();
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
47
    QDBusConnection systemBus(QDBusConnection::systemBus());
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
48
    QDBusServiceWatcher *ofonoWatcher = new QDBusServiceWatcher("org.ofono", systemBus,
27 by Lorn Potter
add currentOperator to net reg and other fixes
49
            QDBusServiceWatcher::WatchForRegistration |
50
            QDBusServiceWatcher::WatchForUnregistration, this);
51
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
52
    connect(ofonoWatcher, SIGNAL(serviceRegistered(QString)),
27 by Lorn Potter
add currentOperator to net reg and other fixes
53
            this, SLOT(connectToOfono(QString)));
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
54
    connect(ofonoWatcher, SIGNAL(serviceUnregistered(QString)),
27 by Lorn Potter
add currentOperator to net reg and other fixes
55
            this, SLOT(ofonoUnregistered(QString)));
56
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
57
    if (systemBus.interface()->isServiceRegistered("org.ofono")) {
27 by Lorn Potter
add currentOperator to net reg and other fixes
58
        connectToOfono(QString());
19 by Lorn Potter
fix rpm
59
    }
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
60
}
61
62
QOfonoManager::~QOfonoManager()
63
{
64
    delete d_ptr;
65
}
66
8 by Lorn Potter
fix contexts and modems bug
67
QStringList QOfonoManager::modems()
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
68
{
69
    return d_ptr->modems;
70
}
71
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
72
QString QOfonoManager::defaultModem()
73
{
74
    return d_ptr->modems.isEmpty() ? QString() : d_ptr->modems[0];
75
}
76
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
77
bool QOfonoManager::available() const
78
{
79
    return d_ptr->available;
80
}
81
82
bool QOfonoManager::isValid() const
83
{
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
84
    // isValid() is essentially the same as available(), keeping it around for
85
    // backward compatibility
86
    return d_ptr->available;
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
87
}
88
89
void QOfonoManager::onModemAdded(const QDBusObjectPath& path, const QVariantMap&)
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
90
{
91
    QString pathStr = path.path();
92
    if (!d_ptr->modems.contains(pathStr)) {
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
93
        QString prevDefault = defaultModem();
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
94
        d_ptr->modems.append(pathStr);
204.2.1 by Slava Monich
[libqofono] Sort modems by path. Contributes to MER#1110
95
        qSort(d_ptr->modems);
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
96
        Q_EMIT modemAdded(pathStr);
27 by Lorn Potter
add currentOperator to net reg and other fixes
97
        Q_EMIT modemsChanged(d_ptr->modems);
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
98
        QString newDefault = defaultModem();
99
        if (newDefault != prevDefault) {
100
            Q_EMIT defaultModemChanged(newDefault);
101
        }
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
102
    }
103
}
104
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
105
void QOfonoManager::onModemRemoved(const QDBusObjectPath& path)
3 by Lorn Potter
2nd initial commit. Long Live QOfono!
106
{
107
    QString pathStr = path.path();
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
108
    QString prevDefault = defaultModem();
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
109
    if (d_ptr->modems.removeOne(pathStr)) {
110
        Q_EMIT modemRemoved(pathStr);
27 by Lorn Potter
add currentOperator to net reg and other fixes
111
        Q_EMIT modemsChanged(d_ptr->modems);
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
112
        QString newDefault = defaultModem();
113
        if (newDefault != prevDefault) {
114
            Q_EMIT defaultModemChanged(newDefault);
115
        }
27 by Lorn Potter
add currentOperator to net reg and other fixes
116
    }
117
}
118
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
119
void QOfonoManager::onGetModemsFinished(QDBusPendingCallWatcher* watcher)
120
{
121
    QDBusPendingReply<ObjectPathPropertiesList> reply(*watcher);
207.5.1 by Slava Monich
[libqofono] Delete QDBusPendingCallWatcher prior to emitting signals. Fixes JB#32810
122
    watcher->deleteLater();
207.9.1 by Slava Monich
[libqofono] Retry GetModems on timeout. Contributes to JB#33995
123
    if (reply.isError()) {
124
        if (qofono::isTimeout(reply.error())) {
125
            qDebug() << "Retrying GetModems...";
126
            d_ptr->getModems(this);
127
        } else {
128
            qWarning() << reply.error();
129
        }
130
    } else {
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
131
        QString prevDefault = defaultModem();
204.2.1 by Slava Monich
[libqofono] Sort modems by path. Contributes to MER#1110
132
        QStringList newModems;
133
        Q_FOREACH(ObjectPathProperties modem, reply.value()) {
134
            newModems.append(modem.path.path());
135
        }
136
        qSort(newModems);
137
        if (d_ptr->modems != newModems) {
138
            d_ptr->modems = newModems;
139
            Q_EMIT modemsChanged(d_ptr->modems);
140
        }
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
141
        QString newDefault = defaultModem();
142
        if (newDefault != prevDefault) {
143
            Q_EMIT defaultModemChanged(newDefault);
144
        }
194.1.1 by Slava Monich
[libqofono] Moved availableChanged to GetModems completion handler
145
        d_ptr->available = true;
146
        Q_EMIT availableChanged(true);
27 by Lorn Potter
add currentOperator to net reg and other fixes
147
    }
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
148
}
149
150
void QOfonoManager::connectToOfono(const QString &)
151
{
152
    if (!d_ptr->ofonoManager) {
153
        OfonoManager* mgr = new OfonoManager("org.ofono", "/", QDBusConnection::systemBus(), this);
154
        if (mgr->isValid()) {
155
            d_ptr->ofonoManager = mgr;
156
            connect(mgr,
157
                SIGNAL(ModemAdded(QDBusObjectPath,QVariantMap)),
158
                SLOT(onModemAdded(QDBusObjectPath,QVariantMap)));
159
            connect(mgr,
160
                SIGNAL(ModemRemoved(QDBusObjectPath)),
161
                SLOT(onModemRemoved(QDBusObjectPath)));
207.9.1 by Slava Monich
[libqofono] Retry GetModems on timeout. Contributes to JB#33995
162
            d_ptr->getModems(this);
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
163
        } else {
164
            delete mgr;
165
        }
166
    }
27 by Lorn Potter
add currentOperator to net reg and other fixes
167
}
168
169
void QOfonoManager::ofonoUnregistered(const QString &)
170
{
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
171
    if (d_ptr->available) {
172
        d_ptr->available = false;
173
        Q_EMIT availableChanged(false);
174
    }
175
    if (d_ptr->ofonoManager) {
176
        delete d_ptr->ofonoManager;
177
        d_ptr->ofonoManager = NULL;
178
        if (!d_ptr->modems.isEmpty()) {
179
            Q_FOREACH(QString modem, d_ptr->modems) {
180
                Q_EMIT modemRemoved(modem);
181
            }
182
            d_ptr->modems.clear();
183
            Q_EMIT modemsChanged(d_ptr->modems);
156.1.2 by Slava Monich
[libqofono] Add defaultModem property to QOfonoManager
184
            Q_EMIT defaultModemChanged(QString());
156.1.1 by Slava Monich
[libqofono] Eliminate blocking D-Bus calls in QOfonoManager
185
        }
186
    }
59 by Lorn Potter
[libqofono] add isValid to many classes.
187
}
207.1.1 by Slava Monich
[libqofono] Mark QOfonoModem and other objects invalid if modem goes away. Contributes to MER#1110
188
189
QSharedPointer<QOfonoManager> QOfonoManager::instance()
190
{
191
    static QWeakPointer<QOfonoManager> sharedInstance;
192
    QSharedPointer<QOfonoManager> mgr = sharedInstance;
193
    if (mgr.isNull()) {
194
        mgr = QSharedPointer<QOfonoManager>::create();
195
        sharedInstance = mgr;
196
    }
197
    return mgr;
198
}