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 "liblocationwrapper.h"
33 #include "gpspositionprivateliblocation.h"
35 const int POWER_SAVE_START_DELAY_MS = 1000 * 60; // 60 seconds
37 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
39 m_liblocationWrapper(0),
43 m_updateInterval(DEFAULT_UPDATE_INTERVAL)
45 qDebug() << __PRETTY_FUNCTION__;
47 m_parent = static_cast<GPSPosition*>(parent);
49 m_delayedPowerSaveTimer = new QTimer(this);
50 m_delayedPowerSaveTimer->setInterval(POWER_SAVE_START_DELAY_MS);
51 connect(m_delayedPowerSaveTimer, SIGNAL(timeout()),
52 this, SLOT(delayedPowerSaveStart()));
55 void GPSPositionPrivate::delayedPowerSaveStart()
57 qDebug() << __PRETTY_FUNCTION__;
60 m_delayedPowerSaveTimer->stop();
61 m_liblocationWrapper->stopUpdates();
64 bool GPSPositionPrivate::isInitialized() const
66 qDebug() << __PRETTY_FUNCTION__;
71 void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath)
73 qDebug() << __PRETTY_FUNCTION__;
77 if (m_liblocationWrapper) {
78 disconnect(m_liblocationWrapper, 0, 0, 0);
79 delete m_liblocationWrapper;
82 if (mode == GPSPosition::Default) {
83 m_liblocationWrapper = new LiblocationWrapper(this);
85 if (!m_liblocationWrapper) {
86 m_initialized = false;
87 emit m_parent->error(ErrorContext::SITUARE, SituareError::GPS_INITIALIZATION_FAILED);
92 if (m_liblocationWrapper) {
94 m_liblocationWrapper->init(m_updateInterval);
96 connect(m_liblocationWrapper, SIGNAL(locationChanged(const GeoPositionInfo &)),
97 this, SLOT(positionUpdated(const GeoPositionInfo &)));
98 connect(m_liblocationWrapper, SIGNAL(errorMessage(const QString &)),
99 this, SLOT(locationError(const QString &)));
103 void GPSPositionPrivate::setPowerSave(bool enabled)
105 qDebug() << __PRETTY_FUNCTION__;
109 m_delayedPowerSaveTimer->start();
112 m_delayedPowerSaveTimer->stop();
113 m_liblocationWrapper->startUpdates();
121 void GPSPositionPrivate::start()
123 qDebug() << __PRETTY_FUNCTION__;
125 if (m_initialized && !isRunning()) {
126 m_liblocationWrapper->startUpdates();
133 void GPSPositionPrivate::stop()
135 qDebug() << __PRETTY_FUNCTION__;
137 if (m_initialized && isRunning()) {
138 m_liblocationWrapper->stopUpdates();
143 bool GPSPositionPrivate::isRunning() const
145 qDebug() << __PRETTY_FUNCTION__;
150 GeoCoordinate GPSPositionPrivate::lastPosition() const
152 qDebug() << __PRETTY_FUNCTION__;
154 GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
156 return positionInfo.coordinate();
159 void GPSPositionPrivate::requestLastPosition()
161 qDebug() << __PRETTY_FUNCTION__;
163 GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
165 if (positionInfo.isValid())
166 emit m_parent->position(positionInfo.coordinate(), 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 emit m_parent->position(positionInfo.coordinate(), accuracy(positionInfo));
187 m_liblocationWrapper->stopUpdates();
190 void GPSPositionPrivate::locationError(const QString &errorMessage)
192 qDebug() << __PRETTY_FUNCTION__;
194 Q_UNUSED(errorMessage);
196 emit m_parent->error(ErrorContext::SITUARE, SituareError::GPS_INITIALIZATION_FAILED);
199 void GPSPositionPrivate::setUpdateInterval(int interval)
201 qDebug() << __PRETTY_FUNCTION__;
203 if (m_updateInterval != interval) {
204 m_updateInterval = interval;
205 m_liblocationWrapper->init(m_updateInterval);
209 qreal GPSPositionPrivate::accuracy(const GeoPositionInfo &positionInfo)
211 qDebug() << __PRETTY_FUNCTION__;
213 return positionInfo.accuracy();