Location service: restructure the way GpsSource is managed

It makes much more sense to have this as a private member of the class instead
of a static in one of the functions.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-01-01 17:19:28 -08:00
parent 87fc84a5ee
commit a611bb22f7
2 changed files with 14 additions and 12 deletions

View file

@ -19,10 +19,15 @@ GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent)
{ {
Q_ASSERT_X(m_Instance == NULL, "GpsLocation", "GpsLocation recreated"); Q_ASSERT_X(m_Instance == NULL, "GpsLocation", "GpsLocation recreated");
m_Instance = this; m_Instance = this;
m_GpsSource = 0;
showMessageCB = showMsgCB; showMessageCB = showMsgCB;
// create a QSettings object that's separate from the main application settings // create a QSettings object that's separate from the main application settings
geoSettings = new QSettings(QSettings::NativeFormat, QSettings::UserScope, geoSettings = new QSettings(QSettings::NativeFormat, QSettings::UserScope,
QString("org.subsurfacedivelog"), QString("subsurfacelocation"), this); QString("org.subsurfacedivelog"), QString("subsurfacelocation"), this);
#ifdef SUBSURFACE_MOBILE
if (hasLocationsSource())
status("Found GPS");
#endif
userAgent = getUserAgent(); userAgent = getUserAgent();
} }
@ -40,28 +45,24 @@ GpsLocation::~GpsLocation()
QGeoPositionInfoSource *GpsLocation::getGpsSource() QGeoPositionInfoSource *GpsLocation::getGpsSource()
{ {
static QGeoPositionInfoSource *gpsSource = NULL; if (!m_GpsSource) {
static bool initGpsSource = false; m_GpsSource = QGeoPositionInfoSource::createDefaultSource(this);
if (m_GpsSource != 0) {
if (!initGpsSource) {
gpsSource = QGeoPositionInfoSource::createDefaultSource(this);
initGpsSource = true;
if (gpsSource != 0) {
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
if (verbose) if (verbose)
#endif #endif
status("created GPS source"); status("created GPS source");
QString msg = QString("have position source %1").arg(gpsSource->sourceName()); QString msg = QString("have position source %1").arg(m_GpsSource->sourceName());
connect(gpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(newPosition(QGeoPositionInfo))); connect(m_GpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(newPosition(QGeoPositionInfo)));
connect(gpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout())); connect(m_GpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
gpsSource->setUpdateInterval(5 * 60 * 1000); // 5 minutes so the device doesn't drain the battery m_GpsSource->setUpdateInterval(5 * 60 * 1000); // 5 minutes so the device doesn't drain the battery
} else { } else {
#ifdef SUBSURFACE_MOBILE #ifdef SUBSURFACE_MOBILE
status("don't have GPS source"); status("don't have GPS source");
#endif #endif
} }
} }
return gpsSource; return m_GpsSource;
} }
bool GpsLocation::hasLocationsSource() bool GpsLocation::hasLocationsSource()

View file

@ -24,6 +24,7 @@ public:
private: private:
QGeoPositionInfo lastPos; QGeoPositionInfo lastPos;
QGeoPositionInfoSource *getGpsSource(); QGeoPositionInfoSource *getGpsSource();
QGeoPositionInfoSource *m_GpsSource;
void status(QString msg); void status(QString msg);
QSettings *geoSettings; QSettings *geoSettings;
QNetworkReply *reply; QNetworkReply *reply;