Added error handler to LiblocationWrapper.
[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(tr("Unable to use GPS"));
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     GeoPositionInfo positionInfo =  m_liblocationWrapper->lastKnownPosition();
104
105     return QPointF(positionInfo.coordinate().longitude(), positionInfo.coordinate().latitude());
106 }
107
108 void GPSPositionPrivate::requestLastPosition()
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     GeoPositionInfo positionInfo = m_liblocationWrapper->lastKnownPosition();
113
114     if (positionInfo.isValid()) {
115         GeoCoordinate coordinate = positionInfo.coordinate();
116         emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
117                       accuracy(positionInfo));
118     }
119 }
120
121 void GPSPositionPrivate::positionUpdated(const GeoPositionInfo &positionInfo)
122 {
123     qDebug() << __PRETTY_FUNCTION__ << positionInfo;
124
125     if (positionInfo.coordinate().isValid()) {
126         GeoCoordinate coordinate = positionInfo.coordinate();
127         emit m_parent->position(QPointF(coordinate.longitude(), coordinate.latitude()),
128                       accuracy(positionInfo));
129     }
130 }
131
132 void GPSPositionPrivate::locationError(const QString &errorMessage)
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     emit m_parent->error(errorMessage);
137 }
138
139 void GPSPositionPrivate::setUpdateInterval(int interval)
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142
143     if (m_updateInterval != interval) {
144         m_updateInterval = interval;
145         m_liblocationWrapper->init(m_updateInterval);
146     }
147 }
148
149 qreal GPSPositionPrivate::accuracy(const GeoPositionInfo &positionInfo)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     if (!positionInfo.timestamp().isValid())
154         return GPS_ACCURACY_UNDEFINED;
155
156     if (positionInfo.isAccurate())
157         return positionInfo.accuracy();
158     else
159         return GPS_ACCURACY_UNDEFINED;
160 }