Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Commit 82e3bd2

Browse files
author
Chris Adams
committed
[systemsettings] Expose LocationMode. Contributes to JB#33753
Previously, the UI determined the location mode from the other values. This commit moves this logic into systemsettings directly.
1 parent e588cc1 commit 82e3bd2

File tree

3 files changed

+192
-16
lines changed

3 files changed

+192
-16
lines changed

src/locationsettings.cpp

Lines changed: 170 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535

3636
#include <QFile>
3737
#include <QSettings>
38-
#include <QDebug>
3938
#include <QStringList>
39+
#include <QTimer>
40+
#include <QDebug>
4041

4142
#include <sailfishkeyprovider.h>
4243
#include <sailfishkeyprovider_iniparser.h>
@@ -51,6 +52,7 @@ const QString LocationSettingsDir = QStringLiteral("/etc/location/");
5152
const QString LocationSettingsFile = QStringLiteral("/etc/location/location.conf");
5253
const QString LocationSettingsKeys = QStringLiteral(
5354
"enabled" ";"
55+
"custom_mode" ";"
5456
"agps_providers" ";"
5557
"gps\\enabled" ";"
5658
"mls\\enabled" ";"
@@ -74,6 +76,9 @@ LocationSettingsPrivate::LocationSettingsPrivate(LocationSettings::Mode mode, Lo
7476
, m_mlsEnabled(true)
7577
, m_mlsOnlineState(LocationSettings::OnlineAGpsAgreementNotAccepted)
7678
, m_hereState(LocationSettings::OnlineAGpsAgreementNotAccepted)
79+
, m_locationMode(LocationSettings::CustomMode)
80+
, m_settingLocationMode(true)
81+
, m_settingMultipleSettings(false)
7782
, m_connMan(Q_NULLPTR)
7883
, m_gpsTech(Q_NULLPTR)
7984
, m_gpsTechInterface(mode == LocationSettings::AsynchronousMode
@@ -83,6 +88,14 @@ LocationSettingsPrivate::LocationSettingsPrivate(LocationSettings::Mode mode, Lo
8388
"net.connman.Technology",
8489
QDBusConnection::systemBus()))
8590
{
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);
8699

87100
connect(&m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(readSettings()));
88101
connect(&m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(readSettings()));
@@ -94,6 +107,8 @@ LocationSettingsPrivate::LocationSettingsPrivate(LocationSettings::Mode mode, Lo
94107
qWarning() << "Unable to follow location configuration file changes";
95108
}
96109

110+
this->m_settingLocationMode = false;
111+
97112
if (m_gpsTechInterface) {
98113
QDBusConnection::systemBus().connect("net.connman",
99114
"/net/connman/technology/gps",
@@ -146,6 +161,59 @@ void LocationSettingsPrivate::findGpsTech()
146161
emit q->gpsFlightModeChanged();
147162
}
148163

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+
149217
LocationSettings::LocationSettings(QObject *parent)
150218
: QObject(parent)
151219
, d_ptr(new LocationSettingsPrivate(LocationSettings::AsynchronousMode, this))
@@ -269,7 +337,8 @@ void LocationSettings::setMlsOnlineState(LocationSettings::OnlineAGpsState state
269337

270338
bool LocationSettings::mlsAvailable() const
271339
{
272-
return QFile::exists(QStringLiteral("/usr/libexec/geoclue-mlsdb"));
340+
Q_D(const LocationSettings);
341+
return d->mlsAvailable();
273342
}
274343

275344
LocationSettings::OnlineAGpsState LocationSettings::hereState() const
@@ -291,7 +360,75 @@ void LocationSettings::setHereState(LocationSettings::OnlineAGpsState state)
291360

292361
bool LocationSettings::hereAvailable() const
293362
{
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;
295432
}
296433

297434
void LocationSettingsPrivate::readSettings()
@@ -314,21 +451,22 @@ void LocationSettingsPrivate::readSettings()
314451
}
315452

316453
// 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;
320457
// then read the new key values (overriding with deprecated values if needed):
321458
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();
332470
for (int i = 0; i < expectedCount; ++i) {
333471
if (locationSettingsValues[i] != NULL) {
334472
free(locationSettingsValues[i]);
@@ -366,10 +504,24 @@ void LocationSettingsPrivate::readSettings()
366504
m_mlsOnlineState = mlsOnlineState;
367505
emit q->mlsOnlineStateChanged();
368506
}
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+
}
369517
}
370518

371519
void LocationSettingsPrivate::writeSettings()
372520
{
521+
if (m_settingMultipleSettings) {
522+
return; // wait to write settings until all settings have been set.
523+
}
524+
373525
// new file would be owned by creating process uid. we cannot allow this since the access is handled with group
374526
if (!QFile(LocationSettingsFile).exists()) {
375527
qWarning() << "Location settings configuration file does not exist. Refusing to create new.";
@@ -389,6 +541,8 @@ void LocationSettingsPrivate::writeSettings()
389541
QString locationSettingsValues;
390542
locationSettingsValues.append(boolToString(m_locationEnabled));
391543
locationSettingsValues.append(";");
544+
locationSettingsValues.append(boolToString(m_locationMode == LocationSettings::CustomMode));
545+
locationSettingsValues.append(";");
392546
locationSettingsValues.append(agps_providers);
393547
locationSettingsValues.append(";");
394548
locationSettingsValues.append(boolToString(m_gpsEnabled));

src/locationsettings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class SYSTEMSETTINGS_EXPORT LocationSettings : public QObject
5656
Q_PROPERTY(OnlineAGpsState mlsOnlineState READ mlsOnlineState WRITE setMlsOnlineState NOTIFY mlsOnlineStateChanged)
5757
Q_PROPERTY(bool mlsAvailable READ mlsAvailable CONSTANT)
5858

59+
Q_PROPERTY(LocationMode locationMode READ locationMode WRITE setLocationMode NOTIFY locationModeChanged)
60+
5961
Q_ENUMS(OnlineAGpsState)
62+
Q_ENUMS(LocationMode)
6063

6164
public:
6265
enum Mode {
@@ -93,13 +96,24 @@ class SYSTEMSETTINGS_EXPORT LocationSettings : public QObject
9396
void setMlsOnlineState(OnlineAGpsState state);
9497
bool mlsAvailable() const;
9598

99+
enum LocationMode {
100+
HighAccuracyMode,
101+
BatterySavingMode,
102+
DeviceOnlyMode,
103+
CustomMode
104+
};
105+
106+
LocationMode locationMode() const;
107+
void setLocationMode(LocationMode locationMode);
108+
96109
signals:
97110
void hereStateChanged();
98111
void locationEnabledChanged();
99112
void gpsEnabledChanged();
100113
void gpsFlightModeChanged();
101114
void mlsEnabledChanged();
102115
void mlsOnlineStateChanged();
116+
void locationModeChanged();
103117

104118
private:
105119
LocationSettingsPrivate *d_ptr;

src/locationsettings_p.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ class LocationSettingsPrivate : public QObject
5656
LocationSettingsPrivate(LocationSettings::Mode mode, LocationSettings *settings);
5757
~LocationSettingsPrivate();
5858

59+
LocationSettings::LocationMode calculateLocationMode() const;
5960
void writeSettings();
6061

62+
bool mlsAvailable() const;
63+
bool hereAvailable() const;
64+
6165
QFileSystemWatcher m_watcher;
6266
bool m_locationEnabled;
6367
bool m_gpsEnabled;
6468
bool m_mlsEnabled;
6569
LocationSettings::OnlineAGpsState m_mlsOnlineState;
6670
LocationSettings::OnlineAGpsState m_hereState;
71+
LocationSettings::LocationMode m_locationMode;
72+
bool m_settingLocationMode;
73+
bool m_settingMultipleSettings;
6774
NetworkManager *m_connMan;
6875
NetworkTechnology *m_gpsTech;
6976
QDBusInterface *m_gpsTechInterface;
@@ -73,6 +80,7 @@ private slots:
7380
void readSettings();
7481
void findGpsTech();
7582
void gpsTechPropertyChanged(const QString &propertyName, const QVariant &value);
83+
void recalculateLocationMode();
7684
};
7785

7886
#endif // NEMO_SYSTEMSETTINGS_LOCATIONSETTINGS_P_H

0 commit comments

Comments
 (0)