2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
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.
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.
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,
24 #include "liblocationwrapper.h"
25 #include "geopositioninfo.h"
26 #include "coordinates/geocoordinate.h"
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
36 LiblocationWrapper::LiblocationWrapper(QObject *parent)
40 m_lastKnownPosition(GeoPositionInfo()),
41 m_state(LiblocationWrapper::Undefined)
43 qDebug() << __PRETTY_FUNCTION__;
45 m_control = location_gpsd_control_get_default();
46 m_device = (LocationGPSDevice*)g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
49 LiblocationWrapper::~LiblocationWrapper()
51 qDebug() << __PRETTY_FUNCTION__;
54 g_object_unref(m_device);
56 g_object_unref(m_control);
59 void LiblocationWrapper::changed(LocationGPSDevice *device, gpointer data)
61 qDebug() << __PRETTY_FUNCTION__;
63 const int METRES_COEFFICIENT = 100; // Coefficient to get metres from centimetres
68 GeoPositionInfo positionInfo;
69 LiblocationWrapper *wrapper;
70 wrapper = (LiblocationWrapper*)data;
72 if (device && wrapper->isRunning()) {
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);
80 positionInfo.setAccuracy(false, device->fix->eph / METRES_COEFFICIENT);
83 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
84 GeoCoordinate coordinate;
86 coordinate.setLatitude(device->fix->latitude);
87 coordinate.setLongitude(device->fix->longitude);
88 positionInfo.setCoordinate(coordinate);
91 wrapper->setLastKnownPosition(positionInfo);
92 emit wrapper->locationChanged(positionInfo);
96 void LiblocationWrapper::error(LocationGPSDevice *device, gpointer data)
98 qDebug() << __PRETTY_FUNCTION__;
100 if (!device || !data)
103 LiblocationWrapper *wrapper;
104 wrapper = (LiblocationWrapper*)data;
106 wrapper->errorMessage(tr("Error in GPS"));
109 bool LiblocationWrapper::isRunning()
111 qDebug() << __PRETTY_FUNCTION__;
113 return (state() == LiblocationWrapper::Running);
116 GeoPositionInfo LiblocationWrapper::lastKnownPosition() const
118 qDebug() << __PRETTY_FUNCTION__;
120 return m_lastKnownPosition;
123 void LiblocationWrapper::setLastKnownPosition(const GeoPositionInfo &positionInfo)
125 qDebug() << __PRETTY_FUNCTION__;
127 m_lastKnownPosition = positionInfo;
130 void LiblocationWrapper::init(int updateInterval)
132 qDebug() << __PRETTY_FUNCTION__;
134 if (state() == LiblocationWrapper::Undefined) {
136 int locationInterval = LOCATION_INTERVAL_DEFAULT;
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;
153 locationInterval = LOCATION_INTERVAL_120S;
156 g_object_set(G_OBJECT(m_control), "preferred-method", LOCATION_METHOD_USER_SELECTED,
157 "preferred-interval", locationInterval, ((void *)0));
159 g_signal_connect(G_OBJECT(m_device), "error", G_CALLBACK(&error),
160 static_cast<void*>(this));
162 g_signal_connect(G_OBJECT(m_device), "changed", G_CALLBACK(&changed),
163 static_cast<void*>(this));
165 setState(LiblocationWrapper::Initialized);
169 void LiblocationWrapper::startUpdates()
171 qDebug() << __PRETTY_FUNCTION__;
174 setState(LiblocationWrapper::Running);
175 location_gpsd_control_start(m_control);
179 void LiblocationWrapper::setState(int state)
181 qDebug() << __PRETTY_FUNCTION__;
186 int LiblocationWrapper::state()
188 qDebug() << __PRETTY_FUNCTION__;
193 void LiblocationWrapper::stopUpdates()
195 qDebug() << __PRETTY_FUNCTION__;
198 setState(LiblocationWrapper::Initialized);
199 location_gpsd_control_stop(m_control);