2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
7 Situare is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 Situare is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Situare; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24 #include <QApplication>
29 #include "gpscommon.h"
30 #include "gpsposition.h"
31 #include "gpspositionprivateliblocation.h"
32 #include "liblocationwrapper.h"
34 const int POWER_SAVE_START_DELAY_MS = 1000*10;
36 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
38 m_liblocationWrapper(0),
42 m_updateInterval(DEFAULT_UPDATE_INTERVAL)
44 qDebug() << __PRETTY_FUNCTION__;
46 m_parent = static_cast<GPSPosition*>(parent);
48 m_delayedPowerSaveTimer = new QTimer(this);
49 m_delayedPowerSaveTimer->setInterval(POWER_SAVE_START_DELAY_MS);
50 connect(m_delayedPowerSaveTimer, SIGNAL(timeout()),
51 this, SLOT(delayedPowerSaveStart()));
54 void GPSPositionPrivate::delayedPowerSaveStart()
56 qDebug() << __PRETTY_FUNCTION__;
59 m_delayedPowerSaveTimer->stop();
60 m_liblocationWrapper->stopUpdates();
63 bool GPSPositionPrivate::isInitialized()
65 qDebug() << __PRETTY_FUNCTION__;
70 void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath)
72 qDebug() << __PRETTY_FUNCTION__;
76 if (m_liblocationWrapper) {
77 disconnect(m_liblocationWrapper, 0, 0, 0);
78 delete m_liblocationWrapper;
81 if (mode == GPSPosition::Default) {
82 m_liblocationWrapper = new LiblocationWrapper(this);
84 if (!m_liblocationWrapper) {
85 m_initialized = false;
86 emit m_parent->error(SituareError::GPS_INITIALIZATION_FAILED);
91 if (m_liblocationWrapper) {
93 m_liblocationWrapper->init(m_updateInterval);
95 connect(m_liblocationWrapper, SIGNAL(locationChanged(const GeoPositionInfo &)),
96 this, SLOT(positionUpdated(const GeoPositionInfo &)));
97 connect(m_liblocationWrapper, SIGNAL(errorMessage(const QString &)),
98 this, SLOT(locationError(const QString &)));
102 void GPSPositionPrivate::setPowerSave(bool enabled)
104 qDebug() << __PRETTY_FUNCTION__;
108 m_delayedPowerSaveTimer->start();
111 m_delayedPowerSaveTimer->stop();
112 m_liblocationWrapper->startUpdates();
120 void GPSPositionPrivate::start()
122 qDebug() << __PRETTY_FUNCTION__;
124 if (m_initialized && !isRunning()) {
125 m_liblocationWrapper->startUpdates();
130 void GPSPositionPrivate::stop()
132 qDebug() << __PRETTY_FUNCTION__;
134 if (m_initialized && isRunning()) {
135 m_liblocationWrapper->stopUpdates();
140 bool GPSPositionPrivate::isRunning()
142 qDebug() << __PRETTY_FUNCTION__;
147 QPointF GPSPositionPrivate::lastPosition()
149 qDebug() << __PRETTY_FUNCTION__;
151 GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
153 return QPointF(positionInfo.coordinate().longitude(), positionInfo.coordinate().latitude());
156 void GPSPositionPrivate::requestLastPosition()
158 qDebug() << __PRETTY_FUNCTION__;
160 GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
162 if (positionInfo.isValid()) {
163 GeoCoordinate coordinate = positionInfo.coordinate();
164 emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
165 accuracy(positionInfo));
169 void GPSPositionPrivate::requestUpdate()
171 qDebug() << __PRETTY_FUNCTION__;
174 m_liblocationWrapper->startUpdates();
176 requestLastPosition();
179 void GPSPositionPrivate::positionUpdated(const GeoPositionInfo &positionInfo)
181 qDebug() << __PRETTY_FUNCTION__;
183 if (positionInfo.coordinate().isValid()) {
184 GeoCoordinate coordinate = positionInfo.coordinate();
185 emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
186 accuracy(positionInfo));
190 m_liblocationWrapper->stopUpdates();
193 void GPSPositionPrivate::locationError(const QString &errorMessage)
195 qDebug() << __PRETTY_FUNCTION__;
197 Q_UNUSED(errorMessage);
199 emit m_parent->error(SituareError::GPS_INITIALIZATION_FAILED);
202 void GPSPositionPrivate::setUpdateInterval(int interval)
204 qDebug() << __PRETTY_FUNCTION__;
206 if (m_updateInterval != interval) {
207 m_updateInterval = interval;
208 m_liblocationWrapper->init(m_updateInterval);
212 qreal GPSPositionPrivate::accuracy(const GeoPositionInfo &positionInfo)
214 qDebug() << __PRETTY_FUNCTION__;
216 if (positionInfo.isAccurate())
217 return positionInfo.accuracy();
219 return GPS_ACCURACY_UNDEFINED;