e0af1ae305761ac194dfb98b07b2659626f38a4b
[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 "gpscommon.h"
29 #include "gpsposition.h"
30 #include "gpspositionprivateliblocation.h"
31 #include "liblocationwrapper.h"
32
33 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
34     : QObject(parent),
35       m_liblocationWrapper(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     Q_UNUSED(filePath);
49
50     if (m_liblocationWrapper) {
51         disconnect(m_liblocationWrapper, 0, 0, 0);
52         delete m_liblocationWrapper;
53     }
54
55     if (mode == GPSPosition::Default) {
56         m_liblocationWrapper = new LiblocationWrapper(this);
57
58         if (!m_liblocationWrapper) {
59             emit m_parent->error(123);  ///Change to correct error code
60             return;
61         }
62     }
63
64     if (m_liblocationWrapper) {
65         m_liblocationWrapper->init(m_updateInterval);
66
67         connect(m_liblocationWrapper, SIGNAL(locationChanged(const GeoPositionInfo &)),
68                 this, SLOT(positionUpdated(const GeoPositionInfo &)));
69         connect(m_liblocationWrapper, SIGNAL(errorMessage(const QString &)),
70                 this, SLOT(locationError(const QString &)));
71     }
72 }
73
74 void GPSPositionPrivate::start()
75 {
76     qDebug() << __PRETTY_FUNCTION__;
77
78     if (m_liblocationWrapper && !isRunning()) {
79         m_liblocationWrapper->startUpdates();
80         m_running = true;
81     }
82 }
83
84 void GPSPositionPrivate::stop()
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     if (m_liblocationWrapper && isRunning()) {
89         m_liblocationWrapper->stopUpdates();
90         m_running = false;
91     }
92 }
93
94 bool GPSPositionPrivate::isRunning()
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     return m_running;
99 }
100
101 QPointF GPSPositionPrivate::lastPosition()
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     GeoPositionInfo positionInfo =  m_liblocationWrapper->lastKnownPosition();
106
107     return QPointF(positionInfo.coordinate().longitude(), positionInfo.coordinate().latitude());
108 }
109
110 void GPSPositionPrivate::requestLastPosition()
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
115
116     if (positionInfo.isValid()) {
117         GeoCoordinate coordinate = positionInfo.coordinate();
118         emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
119                       accuracy(positionInfo));
120     }
121 }
122
123 void GPSPositionPrivate::positionUpdated(const GeoPositionInfo &positionInfo)
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     if (positionInfo.coordinate().isValid()) {
128         GeoCoordinate coordinate = positionInfo.coordinate();
129         emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
130                       accuracy(positionInfo));
131     }
132 }
133
134 void GPSPositionPrivate::locationError(const QString &errorMessage)
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     Q_UNUSED(errorMessage);
139
140     emit m_parent->error(123);
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_liblocationWrapper->init(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.isAccurate())
161         return positionInfo.accuracy();
162     else
163         return GPS_ACCURACY_UNDEFINED;
164 }