Updated some comments
[situare] / src / gps / gpspositionprivateliblocation.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
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.
10
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.
15
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,
19    USA.
20 */
21
22 #include <QFile>
23 #include <QDir>
24 #include <QApplication>
25 #include <QDebug>
26 #include <QTimer>
27
28 #include "common.h"
29 #include "gpscommon.h"
30 #include "gpsposition.h"
31 #include "gpspositionprivateliblocation.h"
32 #include "liblocationwrapper.h"
33
34 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
35     : QObject(parent),
36       m_liblocationWrapper(0),
37       m_running(false),
38       m_updateInterval(DEFAULT_UPDATE_INTERVAL)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41
42     m_parent = static_cast<GPSPosition*>(parent);
43 }
44
45 void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath)
46 {
47     qDebug() << __PRETTY_FUNCTION__;
48
49     Q_UNUSED(filePath);
50
51     if (m_liblocationWrapper) {
52         disconnect(m_liblocationWrapper, 0, 0, 0);
53         delete m_liblocationWrapper;
54     }
55
56     if (mode == GPSPosition::Default) {
57         m_liblocationWrapper = new LiblocationWrapper(this);
58
59         if (!m_liblocationWrapper) {
60             emit m_parent->error(SituareError::GPS_INITIALIZATION_FAILED);
61             return;
62         }
63     }
64
65     if (m_liblocationWrapper) {
66         m_liblocationWrapper->init(m_updateInterval);
67
68         connect(m_liblocationWrapper, SIGNAL(locationChanged(const GeoPositionInfo &)),
69                 this, SLOT(positionUpdated(const GeoPositionInfo &)));
70         connect(m_liblocationWrapper, SIGNAL(errorMessage(const QString &)),
71                 this, SLOT(locationError(const QString &)));
72     }
73 }
74
75 void GPSPositionPrivate::start()
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78
79     if (m_liblocationWrapper && !isRunning()) {
80         m_liblocationWrapper->startUpdates();
81         m_running = true;
82     }
83 }
84
85 void GPSPositionPrivate::stop()
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     if (m_liblocationWrapper && isRunning()) {
90         m_liblocationWrapper->stopUpdates();
91         m_running = false;
92     }
93 }
94
95 bool GPSPositionPrivate::isRunning()
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     return m_running;
100 }
101
102 QPointF GPSPositionPrivate::lastPosition()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     GeoPositionInfo positionInfo =  m_liblocationWrapper->lastKnownPosition();
107
108     return QPointF(positionInfo.coordinate().longitude(), positionInfo.coordinate().latitude());
109 }
110
111 void GPSPositionPrivate::requestLastPosition()
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
116
117     if (positionInfo.isValid()) {
118         GeoCoordinate coordinate = positionInfo.coordinate();
119         emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
120                       accuracy(positionInfo));
121     }
122 }
123
124 void GPSPositionPrivate::positionUpdated(const GeoPositionInfo &positionInfo)
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     if (positionInfo.coordinate().isValid()) {
129         GeoCoordinate coordinate = positionInfo.coordinate();
130         emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
131                       accuracy(positionInfo));
132     }
133 }
134
135 void GPSPositionPrivate::locationError(const QString &errorMessage)
136 {
137     qDebug() << __PRETTY_FUNCTION__;
138
139     Q_UNUSED(errorMessage);
140
141     emit m_parent->error(SituareError::GPS_INITIALIZATION_FAILED);
142 }
143
144 void GPSPositionPrivate::setUpdateInterval(int interval)
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     if (m_updateInterval != interval) {
149         m_updateInterval = interval;
150         m_liblocationWrapper->init(m_updateInterval);
151     }
152 }
153
154 qreal GPSPositionPrivate::accuracy(const GeoPositionInfo &positionInfo)
155 {
156     qDebug() << __PRETTY_FUNCTION__;
157
158     if (!positionInfo.timestamp().isValid())
159         return GPS_ACCURACY_UNDEFINED;
160
161     if (positionInfo.isAccurate())
162         return positionInfo.accuracy();
163     else
164         return GPS_ACCURACY_UNDEFINED;
165 }