From 5048a695aa1085a7eb03263b95a466e28ff40ab0 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Fri, 1 Jan 2021 12:55:47 -0800 Subject: [PATCH] mobile: turn GpsLocation into a regular singleton construct Simply move the initialization of the logging function into its own method and call that in the QMLManager constructor. Signed-off-by: Dirk Hohndel --- core/gpslocation.cpp | 25 +++++++++---------------- core/gpslocation.h | 5 ++--- mobile-widgets/qmlmanager.cpp | 3 ++- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/core/gpslocation.cpp b/core/gpslocation.cpp index fd598683f..1da12fe5b 100644 --- a/core/gpslocation.cpp +++ b/core/gpslocation.cpp @@ -15,17 +15,12 @@ #include #include -GpsLocation *GpsLocation::m_Instance = NULL; - -GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) : - QObject(parent), +GpsLocation::GpsLocation() : m_GpsSource(0), + showMessageCB(0), waitingForPosition(false), haveSource(UNKNOWN) { - Q_ASSERT_X(m_Instance == NULL, "GpsLocation", "GpsLocation recreated"); - m_Instance = this; - showMessageCB = showMsgCB; // create a QSettings object that's separate from the main application settings geoSettings = new QSettings(QSettings::NativeFormat, QSettings::UserScope, QStringLiteral("org.subsurfacedivelog"), QStringLiteral("subsurfacelocation"), this); @@ -39,19 +34,17 @@ GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) : GpsLocation *GpsLocation::instance() { - Q_ASSERT(m_Instance != NULL); - - return m_Instance; -} - -bool GpsLocation::hasInstance() -{ - return m_Instance != NULL; + static GpsLocation self; + return &self; } GpsLocation::~GpsLocation() { - m_Instance = NULL; +} + +void GpsLocation::setLogCallBack(void (*showMsgCB)(const char *)) +{ + showMessageCB = showMsgCB; } void GpsLocation::setGpsTimeThreshold(int seconds) diff --git a/core/gpslocation.h b/core/gpslocation.h index d376c5ec7..ea3373d3b 100644 --- a/core/gpslocation.h +++ b/core/gpslocation.h @@ -29,14 +29,14 @@ struct DiveAndLocation { class GpsLocation : public QObject { Q_OBJECT public: - GpsLocation(void (*showMsgCB)(const char *msg), QObject *parent); + GpsLocation(); ~GpsLocation(); static GpsLocation *instance(); - static bool hasInstance(); std::vector getLocations(); int getGpsNum() const; bool hasLocationsSource(); QString currentPosition(); + void setLogCallBack(void (*showMsgCB)(const char *msg)); QMap currentGPSInfo() const; @@ -49,7 +49,6 @@ private: QNetworkReply *reply; QString userAgent; void (*showMessageCB)(const char *msg); - static GpsLocation *m_Instance; bool waitingForPosition; QMap m_trackers; QList m_deletedTrackers; diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index 96fecff96..219b70ea7 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -290,7 +290,8 @@ QMLManager::QMLManager() : m_locationServiceEnabled(false), this, &QMLManager::btHostModeChange); } // create location manager service - locationProvider = new GpsLocation(&appendTextToLogStandalone, this); + locationProvider = GpsLocation::instance(); + locationProvider->setLogCallBack(&appendTextToLogStandalone); progress_callback = &progressCallback; connect(locationProvider, SIGNAL(haveSourceChanged()), this, SLOT(hasLocationSourceChanged())); setLocationServiceAvailable(locationProvider->hasLocationsSource());