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 <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2021-01-01 12:55:47 -08:00
parent 53b572f987
commit 5048a695aa
3 changed files with 13 additions and 20 deletions

View file

@ -15,17 +15,12 @@
#include <QApplication> #include <QApplication>
#include <QTimer> #include <QTimer>
GpsLocation *GpsLocation::m_Instance = NULL; GpsLocation::GpsLocation() :
GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) :
QObject(parent),
m_GpsSource(0), m_GpsSource(0),
showMessageCB(0),
waitingForPosition(false), waitingForPosition(false),
haveSource(UNKNOWN) 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 // 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,
QStringLiteral("org.subsurfacedivelog"), QStringLiteral("subsurfacelocation"), this); QStringLiteral("org.subsurfacedivelog"), QStringLiteral("subsurfacelocation"), this);
@ -39,19 +34,17 @@ GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) :
GpsLocation *GpsLocation::instance() GpsLocation *GpsLocation::instance()
{ {
Q_ASSERT(m_Instance != NULL); static GpsLocation self;
return &self;
return m_Instance;
}
bool GpsLocation::hasInstance()
{
return m_Instance != NULL;
} }
GpsLocation::~GpsLocation() GpsLocation::~GpsLocation()
{ {
m_Instance = NULL; }
void GpsLocation::setLogCallBack(void (*showMsgCB)(const char *))
{
showMessageCB = showMsgCB;
} }
void GpsLocation::setGpsTimeThreshold(int seconds) void GpsLocation::setGpsTimeThreshold(int seconds)

View file

@ -29,14 +29,14 @@ struct DiveAndLocation {
class GpsLocation : public QObject { class GpsLocation : public QObject {
Q_OBJECT Q_OBJECT
public: public:
GpsLocation(void (*showMsgCB)(const char *msg), QObject *parent); GpsLocation();
~GpsLocation(); ~GpsLocation();
static GpsLocation *instance(); static GpsLocation *instance();
static bool hasInstance();
std::vector<DiveAndLocation> getLocations(); std::vector<DiveAndLocation> getLocations();
int getGpsNum() const; int getGpsNum() const;
bool hasLocationsSource(); bool hasLocationsSource();
QString currentPosition(); QString currentPosition();
void setLogCallBack(void (*showMsgCB)(const char *msg));
QMap<qint64, gpsTracker> currentGPSInfo() const; QMap<qint64, gpsTracker> currentGPSInfo() const;
@ -49,7 +49,6 @@ private:
QNetworkReply *reply; QNetworkReply *reply;
QString userAgent; QString userAgent;
void (*showMessageCB)(const char *msg); void (*showMessageCB)(const char *msg);
static GpsLocation *m_Instance;
bool waitingForPosition; bool waitingForPosition;
QMap<qint64, gpsTracker> m_trackers; QMap<qint64, gpsTracker> m_trackers;
QList<gpsTracker> m_deletedTrackers; QList<gpsTracker> m_deletedTrackers;

View file

@ -290,7 +290,8 @@ QMLManager::QMLManager() : m_locationServiceEnabled(false),
this, &QMLManager::btHostModeChange); this, &QMLManager::btHostModeChange);
} }
// create location manager service // create location manager service
locationProvider = new GpsLocation(&appendTextToLogStandalone, this); locationProvider = GpsLocation::instance();
locationProvider->setLogCallBack(&appendTextToLogStandalone);
progress_callback = &progressCallback; progress_callback = &progressCallback;
connect(locationProvider, SIGNAL(haveSourceChanged()), this, SLOT(hasLocationSourceChanged())); connect(locationProvider, SIGNAL(haveSourceChanged()), this, SLOT(hasLocationSourceChanged()));
setLocationServiceAvailable(locationProvider->hasLocationsSource()); setLocationServiceAvailable(locationProvider->hasLocationsSource());