35
35
36
36
#include < QFile>
37
37
#include < QSettings>
38
- #include < QDebug>
39
38
#include < QStringList>
39
+ #include < QTimer>
40
+ #include < QDebug>
40
41
41
42
#include < sailfishkeyprovider.h>
42
43
#include < sailfishkeyprovider_iniparser.h>
@@ -51,6 +52,7 @@ const QString LocationSettingsDir = QStringLiteral("/etc/location/");
51
52
const QString LocationSettingsFile = QStringLiteral(" /etc/location/location.conf" );
52
53
const QString LocationSettingsKeys = QStringLiteral(
53
54
" enabled" " ;"
55
+ " custom_mode" " ;"
54
56
" agps_providers" " ;"
55
57
" gps\\ enabled" " ;"
56
58
" mls\\ enabled" " ;"
@@ -74,6 +76,9 @@ LocationSettingsPrivate::LocationSettingsPrivate(LocationSettings::Mode mode, Lo
74
76
, m_mlsEnabled(true )
75
77
, m_mlsOnlineState(LocationSettings::OnlineAGpsAgreementNotAccepted)
76
78
, m_hereState(LocationSettings::OnlineAGpsAgreementNotAccepted)
79
+ , m_locationMode(LocationSettings::CustomMode)
80
+ , m_settingLocationMode(true )
81
+ , m_settingMultipleSettings(false )
77
82
, m_connMan(Q_NULLPTR)
78
83
, m_gpsTech(Q_NULLPTR)
79
84
, m_gpsTechInterface(mode == LocationSettings::AsynchronousMode
@@ -83,6 +88,14 @@ LocationSettingsPrivate::LocationSettingsPrivate(LocationSettings::Mode mode, Lo
83
88
" net.connman.Technology" ,
84
89
QDBusConnection::systemBus ()))
85
90
{
91
+ connect (q, &LocationSettings::gpsEnabledChanged,
92
+ this , &LocationSettingsPrivate::recalculateLocationMode);
93
+ connect (q, &LocationSettings::mlsEnabledChanged,
94
+ this , &LocationSettingsPrivate::recalculateLocationMode);
95
+ connect (q, &LocationSettings::mlsOnlineStateChanged,
96
+ this , &LocationSettingsPrivate::recalculateLocationMode);
97
+ connect (q, &LocationSettings::hereStateChanged,
98
+ this , &LocationSettingsPrivate::recalculateLocationMode);
86
99
87
100
connect (&m_watcher, SIGNAL (fileChanged (QString)), this , SLOT (readSettings ()));
88
101
connect (&m_watcher, SIGNAL (directoryChanged (QString)), this , SLOT (readSettings ()));
@@ -94,6 +107,8 @@ LocationSettingsPrivate::LocationSettingsPrivate(LocationSettings::Mode mode, Lo
94
107
qWarning () << " Unable to follow location configuration file changes" ;
95
108
}
96
109
110
+ this ->m_settingLocationMode = false ;
111
+
97
112
if (m_gpsTechInterface) {
98
113
QDBusConnection::systemBus ().connect (" net.connman" ,
99
114
" /net/connman/technology/gps" ,
@@ -146,6 +161,59 @@ void LocationSettingsPrivate::findGpsTech()
146
161
emit q->gpsFlightModeChanged ();
147
162
}
148
163
164
+ LocationSettings::LocationMode
165
+ LocationSettingsPrivate::calculateLocationMode () const
166
+ {
167
+ if (m_gpsEnabled
168
+ && (!mlsAvailable () ||
169
+ (m_mlsEnabled && m_mlsOnlineState == LocationSettings::OnlineAGpsEnabled))
170
+ && (!hereAvailable () || m_hereState == LocationSettings::OnlineAGpsEnabled)) {
171
+ return LocationSettings::HighAccuracyMode;
172
+ } else if (!m_gpsEnabled
173
+ && (!mlsAvailable () ||
174
+ (m_mlsEnabled &&
175
+ (m_mlsOnlineState == LocationSettings::OnlineAGpsEnabled
176
+ || m_mlsOnlineState == LocationSettings::OnlineAGpsAgreementNotAccepted)))
177
+ && (!hereAvailable () ||
178
+ (m_hereState == LocationSettings::OnlineAGpsEnabled
179
+ || m_hereState == LocationSettings::OnlineAGpsAgreementNotAccepted))) {
180
+ return LocationSettings::BatterySavingMode;
181
+ } else if (m_gpsEnabled
182
+ && (!mlsAvailable () ||
183
+ (m_mlsEnabled &&
184
+ (m_mlsOnlineState == LocationSettings::OnlineAGpsDisabled
185
+ || m_mlsOnlineState == LocationSettings::OnlineAGpsAgreementNotAccepted)))
186
+ && (!hereAvailable () ||
187
+ (m_hereState == LocationSettings::OnlineAGpsDisabled
188
+ || m_hereState == LocationSettings::OnlineAGpsAgreementNotAccepted))) {
189
+ return LocationSettings::DeviceOnlyMode;
190
+ } else {
191
+ return LocationSettings::CustomMode;
192
+ }
193
+ }
194
+
195
+ void LocationSettingsPrivate::recalculateLocationMode ()
196
+ {
197
+ if (!m_settingLocationMode && m_locationMode != LocationSettings::CustomMode) {
198
+ LocationSettings::LocationMode currentMode = calculateLocationMode ();
199
+ if (currentMode != m_locationMode) {
200
+ m_locationMode = currentMode;
201
+ emit q->locationModeChanged ();
202
+ }
203
+ }
204
+ }
205
+
206
+ bool LocationSettingsPrivate::mlsAvailable () const
207
+ {
208
+ return QFile::exists (QStringLiteral (" /usr/libexec/geoclue-mlsdb" ));
209
+ }
210
+
211
+ bool LocationSettingsPrivate::hereAvailable () const
212
+ {
213
+ return QFile::exists (QStringLiteral (" /usr/libexec/geoclue-here" ));
214
+ }
215
+
216
+
149
217
LocationSettings::LocationSettings (QObject *parent)
150
218
: QObject(parent)
151
219
, d_ptr(new LocationSettingsPrivate(LocationSettings::AsynchronousMode, this ))
@@ -269,7 +337,8 @@ void LocationSettings::setMlsOnlineState(LocationSettings::OnlineAGpsState state
269
337
270
338
bool LocationSettings::mlsAvailable () const
271
339
{
272
- return QFile::exists (QStringLiteral (" /usr/libexec/geoclue-mlsdb" ));
340
+ Q_D (const LocationSettings);
341
+ return d->mlsAvailable ();
273
342
}
274
343
275
344
LocationSettings::OnlineAGpsState LocationSettings::hereState () const
@@ -291,7 +360,75 @@ void LocationSettings::setHereState(LocationSettings::OnlineAGpsState state)
291
360
292
361
bool LocationSettings::hereAvailable () const
293
362
{
294
- return QFile::exists (QStringLiteral (" /usr/libexec/geoclue-here" ));
363
+ Q_D (const LocationSettings);
364
+ return d->hereAvailable ();
365
+ }
366
+
367
+ LocationSettings::LocationMode LocationSettings::locationMode () const
368
+ {
369
+ Q_D (const LocationSettings);
370
+ return d->m_locationMode ;
371
+ }
372
+
373
+ void LocationSettings::setLocationMode (LocationMode locationMode)
374
+ {
375
+ Q_D (LocationSettings);
376
+
377
+ LocationSettings::LocationMode oldLocationMode = this ->locationMode ();
378
+ if (oldLocationMode == locationMode) {
379
+ return ;
380
+ }
381
+
382
+ d->m_settingLocationMode = true ;
383
+ d->m_settingMultipleSettings = true ;
384
+ d->m_locationMode = locationMode;
385
+
386
+ if (locationMode == HighAccuracyMode) {
387
+ setGpsEnabled (true );
388
+ if (mlsAvailable ()) {
389
+ setMlsEnabled (true );
390
+ if (mlsOnlineState () != LocationSettings::OnlineAGpsAgreementNotAccepted) {
391
+ setMlsOnlineState (LocationSettings::OnlineAGpsEnabled);
392
+ }
393
+ }
394
+ if (hereAvailable ()) {
395
+ if (hereState () != LocationSettings::OnlineAGpsAgreementNotAccepted) {
396
+ setHereState (LocationSettings::OnlineAGpsEnabled);
397
+ }
398
+ }
399
+ } else if (locationMode == BatterySavingMode) {
400
+ setGpsEnabled (false );
401
+ if (mlsAvailable ()) {
402
+ setMlsEnabled (true );
403
+ if (mlsOnlineState () != LocationSettings::OnlineAGpsAgreementNotAccepted) {
404
+ setMlsOnlineState (LocationSettings::OnlineAGpsEnabled);
405
+ }
406
+ }
407
+ if (hereAvailable ()) {
408
+ if (hereState () != LocationSettings::OnlineAGpsAgreementNotAccepted) {
409
+ setHereState (LocationSettings::OnlineAGpsEnabled);
410
+ }
411
+ }
412
+ } else if (locationMode == DeviceOnlyMode) {
413
+ setGpsEnabled (true );
414
+ if (mlsAvailable ()) {
415
+ setMlsEnabled (true );
416
+ if (mlsOnlineState () != LocationSettings::OnlineAGpsAgreementNotAccepted) {
417
+ setMlsOnlineState (LocationSettings::OnlineAGpsDisabled);
418
+ }
419
+ }
420
+ if (hereAvailable ()) {
421
+ if (hereState () != LocationSettings::OnlineAGpsAgreementNotAccepted) {
422
+ setHereState (LocationSettings::OnlineAGpsDisabled);
423
+ }
424
+ }
425
+ }
426
+
427
+ d->m_settingMultipleSettings = false ;
428
+ d->writeSettings ();
429
+ emit locationModeChanged ();
430
+
431
+ d->m_settingLocationMode = false ;
295
432
}
296
433
297
434
void LocationSettingsPrivate::readSettings ()
@@ -314,21 +451,22 @@ void LocationSettingsPrivate::readSettings()
314
451
}
315
452
316
453
// read the deprecated keys first, for compatibility purposes:
317
- bool oldMlsEnabled = locationSettingsValues[9 ] != NULL && strcmp (locationSettingsValues[9 ], " true" ) == 0 ;
318
- bool oldHereEnabled = locationSettingsValues[10 ] != NULL && strcmp (locationSettingsValues[10 ], " true" ) == 0 ;
319
- bool oldHereAgreementAccepted = locationSettingsValues[11 ] != NULL && strcmp (locationSettingsValues[11 ], " true" ) == 0 ;
454
+ bool oldMlsEnabled = locationSettingsValues[10 ] != NULL && strcmp (locationSettingsValues[10 ], " true" ) == 0 ;
455
+ bool oldHereEnabled = locationSettingsValues[11 ] != NULL && strcmp (locationSettingsValues[11 ], " true" ) == 0 ;
456
+ bool oldHereAgreementAccepted = locationSettingsValues[12 ] != NULL && strcmp (locationSettingsValues[12 ], " true" ) == 0 ;
320
457
// then read the new key values (overriding with deprecated values if needed):
321
458
bool locationEnabled = locationSettingsValues[0 ] != NULL && strcmp (locationSettingsValues[0 ], " true" ) == 0 ;
322
- // skip over the agps_providers value at [1]
323
- bool gpsEnabled = locationSettingsValues[2 ] != NULL && strcmp (locationSettingsValues[2 ], " true" ) == 0 ;
324
- bool mlsEnabled = oldMlsEnabled || (locationSettingsValues[3 ] != NULL && strcmp (locationSettingsValues[3 ], " true" ) == 0 );
325
- bool mlsAgreementAccepted = locationSettingsValues[4 ] != NULL && strcmp (locationSettingsValues[4 ], " true" ) == 0 ;
326
- bool mlsOnlineEnabled = locationSettingsValues[5 ] != NULL && strcmp (locationSettingsValues[5 ], " true" ) == 0 ;
327
- bool hereEnabled = oldHereEnabled || (locationSettingsValues[6 ] != NULL && strcmp (locationSettingsValues[6 ], " true" ) == 0 );
328
- bool hereAgreementAccepted = oldHereAgreementAccepted || (locationSettingsValues[7 ] != NULL && strcmp (locationSettingsValues[7 ], " true" ) == 0 );
329
- // skip over here\online_enabled value at [8]
330
-
331
- const int expectedCount = 12 ; // should equal: LocationSettingsKeys.split(';').count();
459
+ bool customMode = locationSettingsValues[1 ] != NULL && strcmp (locationSettingsValues[1 ], " true" ) == 0 ;
460
+ // skip over the agps_providers value at [2]
461
+ bool gpsEnabled = locationSettingsValues[3 ] != NULL && strcmp (locationSettingsValues[3 ], " true" ) == 0 ;
462
+ bool mlsEnabled = oldMlsEnabled || (locationSettingsValues[4 ] != NULL && strcmp (locationSettingsValues[4 ], " true" ) == 0 );
463
+ bool mlsAgreementAccepted = locationSettingsValues[5 ] != NULL && strcmp (locationSettingsValues[5 ], " true" ) == 0 ;
464
+ bool mlsOnlineEnabled = locationSettingsValues[6 ] != NULL && strcmp (locationSettingsValues[6 ], " true" ) == 0 ;
465
+ bool hereEnabled = oldHereEnabled || (locationSettingsValues[7 ] != NULL && strcmp (locationSettingsValues[7 ], " true" ) == 0 );
466
+ bool hereAgreementAccepted = oldHereAgreementAccepted || (locationSettingsValues[8 ] != NULL && strcmp (locationSettingsValues[8 ], " true" ) == 0 );
467
+ // skip over here\online_enabled value at [9]
468
+
469
+ const int expectedCount = 13 ; // should equal: LocationSettingsKeys.split(';').count();
332
470
for (int i = 0 ; i < expectedCount; ++i) {
333
471
if (locationSettingsValues[i] != NULL ) {
334
472
free (locationSettingsValues[i]);
@@ -366,10 +504,24 @@ void LocationSettingsPrivate::readSettings()
366
504
m_mlsOnlineState = mlsOnlineState;
367
505
emit q->mlsOnlineStateChanged ();
368
506
}
507
+
508
+ if ((m_locationMode == LocationSettings::CustomMode) != customMode) {
509
+ if (customMode) {
510
+ m_locationMode = LocationSettings::CustomMode;
511
+ emit q->locationModeChanged ();
512
+ } else {
513
+ m_locationMode = calculateLocationMode ();
514
+ emit q->locationModeChanged ();
515
+ }
516
+ }
369
517
}
370
518
371
519
void LocationSettingsPrivate::writeSettings ()
372
520
{
521
+ if (m_settingMultipleSettings) {
522
+ return ; // wait to write settings until all settings have been set.
523
+ }
524
+
373
525
// new file would be owned by creating process uid. we cannot allow this since the access is handled with group
374
526
if (!QFile (LocationSettingsFile).exists ()) {
375
527
qWarning () << " Location settings configuration file does not exist. Refusing to create new." ;
@@ -389,6 +541,8 @@ void LocationSettingsPrivate::writeSettings()
389
541
QString locationSettingsValues;
390
542
locationSettingsValues.append (boolToString (m_locationEnabled));
391
543
locationSettingsValues.append (" ;" );
544
+ locationSettingsValues.append (boolToString (m_locationMode == LocationSettings::CustomMode));
545
+ locationSettingsValues.append (" ;" );
392
546
locationSettingsValues.append (agps_providers);
393
547
locationSettingsValues.append (" ;" );
394
548
locationSettingsValues.append (boolToString (m_gpsEnabled));
0 commit comments