Changed singleton from NetworkAccessManager to NetworkHandler.
[situare] / src / gps / liblocationwrapper.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 <QDebug>
23
24 #include "liblocationwrapper.h"
25 #include "geopositioninfo.h"
26 #include "coordinates/geocoordinate.h"
27
28 const int INTERVAL_1S = 1500;       ///< Maximum value for 1 sec interval
29 const int INTERVAL_2S = 2500;       ///< Maximum value for 2 sec interval
30 const int INTERVAL_5S = 7500;       ///< Maximum value for 5 sec interval
31 const int INTERVAL_10S = 15000;     ///< Maximum value for 10 sec interval
32 const int INTERVAL_20S = 25000;     ///< Maximum value for 20 sec interval
33 const int INTERVAL_30S = 45000;     ///< Maximum value for 30 sec interval
34 const int INTERVAL_60S = 90000;     ///< Maximum value for 60 sec interval
35
36 LiblocationWrapper::LiblocationWrapper(QObject *parent)
37     : QObject(parent),
38       m_control(0),
39       m_device(0),
40       m_lastKnownPosition(GeoPositionInfo()),
41       m_state(LiblocationWrapper::Undefined)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     m_control = location_gpsd_control_get_default();
46     m_device = (LocationGPSDevice*)g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
47 }
48
49 LiblocationWrapper::~LiblocationWrapper()
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52
53     if (m_device)
54         g_object_unref(m_device);
55     if (m_control)
56         g_object_unref(m_control);
57 }
58
59 void LiblocationWrapper::changed(LocationGPSDevice *device, gpointer data)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     const int METRES_COEFFICIENT = 100; // Coefficient to get metres from centimetres
64
65     if (!device || !data)
66         return;
67
68     GeoPositionInfo positionInfo;
69     LiblocationWrapper *wrapper;
70     wrapper = (LiblocationWrapper*)data;
71
72     if (device && wrapper->isRunning()) {
73         if (device->fix) {
74
75             if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
76                 positionInfo.setTimestamp(device->fix->time);
77                 positionInfo.setAccuracy(true, device->fix->eph / METRES_COEFFICIENT);
78             }
79             else {
80                 positionInfo.setAccuracy(false, device->fix->eph / METRES_COEFFICIENT);
81             }
82
83             if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
84                 GeoCoordinate coordinate;
85
86                 coordinate.setLatitude(device->fix->latitude);
87                 coordinate.setLongitude(device->fix->longitude);
88                 positionInfo.setCoordinate(coordinate);
89             }    
90         }
91         wrapper->setLastKnownPosition(positionInfo);
92         emit wrapper->locationChanged(positionInfo);
93     }
94 }
95
96 void LiblocationWrapper::error(LocationGPSDevice *device, gpointer data)
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     if (!device || !data)
101         return;
102
103     LiblocationWrapper *wrapper;
104     wrapper = (LiblocationWrapper*)data;
105
106     wrapper->errorMessage(tr("Error in GPS"));
107 }
108
109 bool LiblocationWrapper::isRunning()
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     return (state() == LiblocationWrapper::Running);
114 }
115
116 GeoPositionInfo LiblocationWrapper::lastKnownPosition() const
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     return m_lastKnownPosition;
121 }
122
123 void LiblocationWrapper::setLastKnownPosition(const GeoPositionInfo &positionInfo)
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     m_lastKnownPosition = positionInfo;
128 }
129
130 void LiblocationWrapper::init(int updateInterval)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     if (state() == LiblocationWrapper::Undefined) {
135
136         int locationInterval = LOCATION_INTERVAL_DEFAULT;
137
138         if ((updateInterval > 0) && (updateInterval <= INTERVAL_1S))
139             locationInterval = LOCATION_INTERVAL_1S;
140         else if (updateInterval <= INTERVAL_2S)
141             locationInterval = LOCATION_INTERVAL_2S;
142         else if (updateInterval <= INTERVAL_5S)
143             locationInterval = LOCATION_INTERVAL_5S;
144         else if (updateInterval <= INTERVAL_10S)
145             locationInterval = LOCATION_INTERVAL_10S;
146         else if (updateInterval <= INTERVAL_20S)
147             locationInterval = LOCATION_INTERVAL_20S;
148         else if (updateInterval <= INTERVAL_30S)
149             locationInterval = LOCATION_INTERVAL_30S;
150         else if (updateInterval <= INTERVAL_60S)
151             locationInterval = LOCATION_INTERVAL_60S;
152         else
153             locationInterval = LOCATION_INTERVAL_120S;
154
155
156         g_object_set(G_OBJECT(m_control), "preferred-method", LOCATION_METHOD_USER_SELECTED,
157                 "preferred-interval", locationInterval, ((void *)0));
158
159         g_signal_connect(G_OBJECT(m_device), "error", G_CALLBACK(&error),
160                          static_cast<void*>(this));
161
162         g_signal_connect(G_OBJECT(m_device), "changed", G_CALLBACK(&changed),
163                          static_cast<void*>(this));
164
165         setState(LiblocationWrapper::Initialized);
166     }
167 }
168
169 void LiblocationWrapper::startUpdates()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     if (!isRunning()) {
174         setState(LiblocationWrapper::Running);
175         location_gpsd_control_start(m_control);
176     }
177 }
178
179 void LiblocationWrapper::setState(int state)
180 {
181     qDebug() << __PRETTY_FUNCTION__;
182
183     m_state = state;
184 }
185
186 int LiblocationWrapper::state()
187 {
188     qDebug() << __PRETTY_FUNCTION__;
189
190     return m_state;
191 }
192
193 void LiblocationWrapper::stopUpdates()
194 {
195     qDebug() << __PRETTY_FUNCTION__;
196
197     if (isRunning()) {
198         setState(LiblocationWrapper::Initialized);
199         location_gpsd_control_stop(m_control);
200     }
201 }