LocationDiagnostics.cpp Example File
locationdiagnostics/src/LocationDiagnostics.cpp
#include "LocationDiagnostics.hpp"
#include "LocationSession.hpp"
#include <QCoreApplication>
#include <QSettings>
LocationDiagnostics::LocationDiagnostics(QObject *parent)
: QObject(parent)
{
QSettings settings;
m_positionMethod = settings.value("positionMethod", "all").toString();
m_assistanceMode = settings.value("assistanceMode", "password").toString();
m_frequency = settings.value("frequency", "1").toInt();
m_useSound = settings.value("useSound", true).toBool();
m_backgroundMode = settings.value("backgroundMode", true).toBool();
bool ok = connect(qApp, SIGNAL(manualExit()), SLOT(onManualExit()));
Q_ASSERT(ok);
Q_UNUSED(ok);
}
LocationSession* LocationDiagnostics::createLocationSession(bool trackingMode)
{
LocationSession* session = new LocationSession(this, true);
session->setSoundEnabled(m_useSound);
if (!session->positionSource())
return session;
if (m_positionMethod == QLatin1String("all")) {
session->positionSource()->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
} else if (m_positionMethod == QLatin1String("nonSatellite")) {
session->positionSource()->setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);
} else if (m_positionMethod == QLatin1String("satellite")) {
session->positionSource()->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
} else if (m_positionMethod == QLatin1String("gps")) {
session->positionSource()->setProperty("provider", "gnss");
session->positionSource()->setProperty("fixType", "gps_autonomous");
} else if (m_positionMethod == QLatin1String("cellular")) {
session->positionSource()->setProperty("provider", "network");
session->positionSource()->setProperty("fixType", "cellsite");
} else if (m_positionMethod == QLatin1String("wifi")) {
session->positionSource()->setProperty("provider", "network");
session->positionSource()->setProperty("fixType", "wifi");
} else if (m_positionMethod == QLatin1String("msb")) {
session->positionSource()->setProperty("provider", "gnss");
session->positionSource()->setProperty("fixType", "gps_ms_based");
} else if (m_positionMethod == QLatin1String("msa")) {
session->positionSource()->setProperty("provider", "gnss");
session->positionSource()->setProperty("fixType", "gps_ms_assisted");
}
if (m_assistanceMode == QLatin1String("pde")) {
if (!m_pdeUrl.isEmpty())
session->positionSource()->setProperty("qtaPDEURL", m_pdeUrl);
} else if (m_assistanceMode == QLatin1String("slp")) {
if (!m_slpUrl.isEmpty())
session->positionSource()->setProperty("slpURL", m_slpUrl);
} else if (m_assistanceMode == QLatin1String("password")) {
if (!m_appId.isEmpty())
session->positionSource()->setProperty("qtaAppID", m_appId);
if (!m_password.isEmpty())
session->positionSource()->setProperty("qtaAppPassword", m_password);
}
session->positionSource()->setUpdateInterval(m_frequency * 1000);
session->positionSource()->setProperty("canRunInBackground", m_backgroundMode);
if (trackingMode) {
session->startUpdates();
} else {
session->requestUpdate();
}
return session;
}
void LocationDiagnostics::onManualExit()
{
qApp->exit(0);
}
QString LocationDiagnostics::positionMethod() const
{
return m_positionMethod;
}
void LocationDiagnostics::setPositionMethod(const QString &method)
{
if (m_positionMethod == method)
return;
m_positionMethod = method;
QSettings settings;
settings.setValue("positionMethod", m_positionMethod);
emit positionMethodChanged();
}
QString LocationDiagnostics::assistanceMode() const
{
return m_assistanceMode;
}
void LocationDiagnostics::setAssistanceMode(const QString &mode)
{
if (m_assistanceMode == mode)
return;
m_assistanceMode = mode;
QSettings settings;
settings.setValue("assistanceMode", m_assistanceMode);
emit assistanceModeChanged();
}
QString LocationDiagnostics::pdeUrl() const
{
return m_pdeUrl;
}
void LocationDiagnostics::setPdeUrl(const QString &url)
{
if (m_pdeUrl == url)
return;
m_pdeUrl = url;
emit pdeUrlChanged();
}
QString LocationDiagnostics::slpUrl() const
{
return m_slpUrl;
}
void LocationDiagnostics::setSlpUrl(const QString &url)
{
if (m_slpUrl == url)
return;
m_slpUrl = url;
emit slpUrlChanged();
}
QString LocationDiagnostics::appId() const
{
return m_appId;
}
void LocationDiagnostics::setAppId(const QString &appId)
{
if (m_appId == appId)
return;
m_appId = appId;
emit appIdChanged();
}
QString LocationDiagnostics::password() const
{
return m_password;
}
void LocationDiagnostics::setPassword(const QString &password)
{
if (m_password == password)
return;
m_password = password;
emit passwordChanged();
}
int LocationDiagnostics::frequency() const
{
return m_frequency;
}
void LocationDiagnostics::setFrequency(int frequency)
{
if (m_frequency == frequency)
return;
m_frequency = frequency;
QSettings settings;
settings.setValue("frequency", m_frequency);
emit frequencyChanged();
}
bool LocationDiagnostics::useSound() const
{
return m_useSound;
}
void LocationDiagnostics::setUseSound(bool sound)
{
if (m_useSound == sound)
return;
m_useSound = sound;
QSettings settings;
settings.setValue("useSound", m_useSound);
emit useSoundChanged();
}
bool LocationDiagnostics::backgroundMode() const
{
return m_backgroundMode;
}
void LocationDiagnostics::setBackgroundMode(bool mode)
{
if (m_backgroundMode == mode)
return;
m_backgroundMode = mode;
QSettings settings;
settings.setValue("backgroundMode", m_backgroundMode);
emit backgroundModeChanged();
}