~pete-woods/indicator-network/wapi-lp1597681

« back to all changes in this revision

Viewing changes to src/indicator/nmofono/manager-impl.cpp

New Connectivity Service Private API for managing mobile data and SIM cards. Tests. (LP: #1373463)

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
    Private(Manager& parent) :
97
97
        p(parent)
98
98
    {
99
 
        m_settings = std::make_shared<ConnectivityServiceSettings>();
100
 
 
101
 
        m_simManager = make_shared<wwan::SimManager>(m_settings);
102
 
        m_sims = m_simManager->knownSims();
103
 
        connect(m_simManager.get(), &wwan::SimManager::simAdded, this, &Private::simAdded);
104
 
 
105
 
        QVariant ret = m_settings->mobileDataEnabled();
106
 
        if (ret.isNull())
107
 
        {
108
 
            /* This is the first time we are running on a system.
109
 
             *
110
 
             * We need to figure out the status of mobile data from looking
111
 
             * at the individual modems.
112
 
             */
113
 
            m_mobileDataEnabledPending = true;
114
 
            m_settings->setMobileDataEnabled(false);
115
 
        }
116
 
        else
117
 
        {
118
 
            m_mobileDataEnabled = ret.toBool();
119
 
        }
120
 
 
121
 
        ret = m_settings->simForMobileData();
122
 
        if (ret.isNull())
123
 
        {
124
 
            /* This is the first time we are running on a system.
125
 
             *
126
 
             * We need to figure out the SIM used for mobile data
127
 
             * from the individual modems.
128
 
             */
129
 
            m_simForMobileDataPending = true;
130
 
            m_settings->setSimForMobileData(QString());
131
 
        }
132
 
        else
133
 
        {
134
 
            QString imsi = ret.toString();
135
 
            wwan::Sim::Ptr sim;
136
 
            for(auto i = m_sims.begin(); i != m_sims.end(); i++)
137
 
            {
138
 
                if ((*i)->imsi() == imsi) {
139
 
                    sim = *i;
140
 
                    break;
141
 
                }
142
 
            }
143
 
            m_simForMobileData = sim;
144
 
        }
145
 
 
146
99
    }
147
100
 
148
101
    void matchModemsAndSims()
149
102
    {
150
 
        for (wwan::Modem::Ptr modem: m_modems)
 
103
        for (wwan::Modem::Ptr modem: m_ofonoLinks)
151
104
        {
152
105
            bool match = false;
153
106
            for(wwan::Sim::Ptr sim : m_sims)
169
122
    void simAdded(wwan::Sim::Ptr sim)
170
123
    {
171
124
        m_sims.append(sim);
172
 
        matchModemsAndSims();
 
125
        connect(sim.get(), &wwan::Sim::presentChanged, this, &Private::matchModemsAndSims);
173
126
        Q_EMIT p.simsChanged();
 
127
        matchModemsAndSims();
 
128
    }
 
129
 
 
130
    void initialDataOnSet()
 
131
    {
 
132
        wwan::Sim *sim_raw = qobject_cast<wwan::Sim*>(sender());
 
133
        if (!sim_raw)
 
134
        {
 
135
            Q_ASSERT(0);
 
136
            return;
 
137
        }
 
138
        wwan::Sim::Ptr sim = sim_raw->shared_from_this();
 
139
 
 
140
        if (!m_mobileDataEnabledPending && !m_simForMobileDataPending)
 
141
        {
 
142
            return;
 
143
        }
 
144
 
 
145
        if (sim->initialDataOn())
 
146
        {
 
147
            setMobileDataEnabled(true);
 
148
            setSimForMobileData(sim);
 
149
        }
174
150
    }
175
151
 
176
152
    void modemReady()
181
157
            Q_ASSERT(0);
182
158
            return;
183
159
        }
184
 
        m_modems.append(m_ofonoLinks[modem_raw->name()]);
185
 
        matchModemsAndSims();
 
160
        auto modem = m_ofonoLinks[modem_raw->name()];
 
161
        if (!modem->sim())
 
162
        {
 
163
            matchModemsAndSims();
 
164
        }
 
165
 
 
166
        if (m_mobileDataEnabledPending || m_simForMobileDataPending)
 
167
        {
 
168
            connect(modem->sim().get(), &wwan::Sim::initialDataOnSet, this, &Private::initialDataOnSet);
 
169
            if (modem->sim()->initialDataOn())
 
170
            {
 
171
                modem->sim()->initialDataOnSet();
 
172
            }
 
173
        }
 
174
        m_modems.append(modem);
186
175
        Q_EMIT p.modemsChanged();
187
176
    }
188
177
 
197
186
        Q_EMIT p.unstoppableOperationHappeningUpdated(m_unstoppableOperationHappening);
198
187
    }
199
188
 
 
189
    void loadSettings()
 
190
    {
 
191
        QVariant ret = m_settings->mobileDataEnabled();
 
192
        if (ret.isNull())
 
193
        {
 
194
            /* This is the first time we are running on a system.
 
195
             *
 
196
             * We need to figure out the status of mobile data from looking
 
197
             * at the individual modems.
 
198
             */
 
199
            m_mobileDataEnabledPending = true;
 
200
            m_settings->setMobileDataEnabled(false);
 
201
        }
 
202
        else
 
203
        {
 
204
            setMobileDataEnabled(ret.toBool());
 
205
        }
 
206
 
 
207
        ret = m_settings->simForMobileData();
 
208
        if (ret.isNull())
 
209
        {
 
210
            /* This is the first time we are running on a system.
 
211
             *
 
212
             * We need to figure out the SIM used for mobile data
 
213
             * from the individual modems.
 
214
             */
 
215
            m_simForMobileDataPending = true;
 
216
            m_settings->setSimForMobileData(QString());
 
217
        }
 
218
        else
 
219
        {
 
220
            QString iccid = ret.toString();
 
221
            wwan::Sim::Ptr sim;
 
222
            for(auto i = m_sims.begin(); i != m_sims.end(); i++)
 
223
            {
 
224
                if ((*i)->iccid() == iccid) {
 
225
                    sim = *i;
 
226
                    break;
 
227
                }
 
228
            }
 
229
            setSimForMobileData(sim);
 
230
        }
 
231
    }
 
232
 
200
233
public Q_SLOTS:
201
234
    void updateHasWifi()
202
235
    {
316
349
            m_ofonoLinks[path] = modem;
317
350
            connect(modem.get(), &wwan::Modem::readyToUnlock, this, &Private::modemReadyToUnlock);
318
351
            connect(modem.get(), &wwan::Modem::ready, this, &Private::modemReady);
319
 
            matchModemsAndSims();
320
352
        }
321
353
 
322
354
        Q_EMIT p.linksUpdated();
326
358
    }
327
359
 
328
360
    void setMobileDataEnabled(bool value) {
329
 
        if (m_mobileDataEnabled == value)
 
361
        if (m_mobileDataEnabled == value && !m_mobileDataEnabledPending)
330
362
        {
331
363
            return;
332
364
        }
 
365
        m_mobileDataEnabledPending = false;
333
366
 
334
367
        m_settings->setMobileDataEnabled(value);
335
368
        m_mobileDataEnabled = value;
357
390
    }
358
391
 
359
392
    void setSimForMobileData(wwan::Sim::Ptr sim) {
360
 
        if (m_simForMobileData == sim)
 
393
        if (m_simForMobileData == sim && !m_simForMobileDataPending)
361
394
        {
362
395
            return;
363
396
        }
 
397
        if (m_simForMobileDataPending) {
 
398
            m_simForMobileDataPending = false;
 
399
            setMobileDataEnabled(m_mobileDataEnabled);
 
400
        }
364
401
 
365
402
        if (m_simForMobileData)
366
403
        {
373
410
        }
374
411
        else
375
412
        {
376
 
            m_settings->setSimForMobileData(sim->imsi());
 
413
            m_settings->setSimForMobileData(sim->iccid());
377
414
        }
378
415
 
379
416
 
380
417
        m_simForMobileData = sim;
381
418
        if (m_simForMobileData)
382
419
        {
383
 
            m_simForMobileData->setMobileDataEnabled(p.mobileDataEnabled());
 
420
            m_simForMobileData->setMobileDataEnabled(m_mobileDataEnabled);
384
421
        }
385
422
 
386
423
        Q_EMIT p.simForMobileDataChanged();
428
465
    connect(d->m_unlockDialog.get(), &SimUnlockDialog::ready, d.get(), &Private::sim_unlock_ready);
429
466
 
430
467
    d->m_ofono = make_shared<QOfonoManager>();
 
468
 
 
469
    // Load the SIM manager before we connect to the signals
 
470
    d->m_settings = make_shared<ConnectivityServiceSettings>();
 
471
    d->m_simManager = make_shared<wwan::SimManager>(d->m_ofono, d->m_settings);
 
472
    connect(d->m_simManager.get(), &wwan::SimManager::simAdded, d.get(), &Private::simAdded);
 
473
    d->m_sims = d->m_simManager->knownSims();
 
474
    for (auto sim : d->m_sims)
 
475
    {
 
476
        connect(sim.get(), &wwan::Sim::presentChanged, d.get(), &Private::matchModemsAndSims);
 
477
    }
 
478
 
431
479
    connect(d->m_ofono.get(), &QOfonoManager::modemsChanged, d.get(), &Private::modems_changed);
432
480
    d->modems_changed(d->m_ofono->modems());
433
481
 
460
508
    /// @todo set by the default connections.
461
509
    d->m_characteristics = Link::Characteristics::empty;
462
510
 
 
511
    d->loadSettings();
 
512
 
463
513
    d->updateHasWifi();
464
514
}
465
515