Added GeoPositionInfo and LiblocationWrapper classes.
[situare] / src / gps / gpspositionprivate.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 "gpscommon.h"
29 #include "gpsposition.h"
30 #include "gpspositionprivate.h"
31 #include "liblocationwrapper.h"
32
33 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
34     : QObject(parent),
35       m_gpsSource(0),
36       m_running(false),
37       m_updateInterval(DEFAULT_UPDATE_INTERVAL)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     m_parent = static_cast<GPSPosition*>(parent);
42 }
43
44 void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     if (m_gpsSource) {
49         disconnect(m_gpsSource, 0, 0, 0);
50         delete m_gpsSource;
51     }
52
53     if (mode == GPSPosition::Default) {
54         m_gpsSource = new LiblocationWrapper(this);
55
56         if (!m_gpsSource) {
57             emit m_parent->error(tr("Unable to use GPS"));
58             return;
59         }
60     }
61     else if (mode == GPSPosition::Simulation) {
62 //        QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(
63 //                QNmeaPositionInfoSource::SimulationMode, this);
64 //        QFile *logFile = new QFile(filePath, this);
65
66 //        nmeaSource->setDevice(logFile);
67 //        m_gpsSource = nmeaSource;
68     }
69
70     if (m_gpsSource) {
71         connect(m_gpsSource, SIGNAL(positionUpdated(const GeoPositionInfo &)),
72                 this, SLOT(positionUpdated(const GeoPositionInfo &)));
73         //connect(m_gpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
74
75         m_gpsSource->setUpdateInterval(m_updateInterval);
76     }
77 }
78
79 void GPSPositionPrivate::start()
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     if (m_gpsSource && !isRunning()) {
84         m_gpsSource->startUpdates();
85         m_running = true;
86     }
87 }
88
89 void GPSPositionPrivate::stop()
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     if (m_gpsSource && isRunning()) {
94         m_gpsSource->stopUpdates();
95         m_running = false;
96     }
97 }
98
99 bool GPSPositionPrivate::isRunning()
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102
103     return m_running;
104 }
105
106 QPointF GPSPositionPrivate::lastPosition()
107 {
108     GeoPositionInfo positionInfo = m_gpsSource->lastKnownPosition();
109
110     return QPointF(positionInfo.longitude(), positionInfo.latitude());
111 }
112
113 void GPSPositionPrivate::requestLastPosition()
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     GeoPositionInfo positionInfo = m_gpsSource->lastKnownPosition();
118
119     if (positionInfo.isValid()) {
120         emit m_parent->position(QPointF(positionInfo.longitude(), positionInfo.latitude()),
121                       accuracy(m_gpsSource->lastKnownPosition()));
122     }
123 }
124
125 void GPSPositionPrivate::positionUpdated(const GeoPositionInfo &positionInfo)
126 {
127     qDebug() << __PRETTY_FUNCTION__ << positionInfo;
128
129     if (positionInfo.coordinate().isValid()) {
130         emit m_parent->position(QPointF(positionInfo.longitude(),
131                               positionInfo.latitude()),
132                       accuracy(positionInfo));
133     }
134 }
135
136 void GPSPositionPrivate::updateTimeout()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     emit m_parent->timeout();
141 }
142
143 void GPSPositionPrivate::setUpdateInterval(int interval)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     if (m_updateInterval != interval) {
148         m_updateInterval = interval;
149         m_gpsSource->setUpdateInterval(m_updateInterval);
150     }
151 }
152
153 qreal GPSPositionPrivate::accuracy(const GeoPositionInfo &positionInfo)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     if (!positionInfo.timestamp().isValid())
158         return GPS_ACCURACY_UNDEFINED;
159
160     if (positionInfo.hasAttribute(GeoPositionInfo::HorizontalAccuracy))
161         return positionInfo.attribute(GeoPositionInfo::HorizontalAccuracy);
162     else
163         return GPS_ACCURACY_UNDEFINED;
164 }