Conflict solved: mainwindow.cpp and calculate.cpp
[speedfreak] / Client / maemo5locationprivate.cpp
1 /*
2  * Maemo5LocationPrivate
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "maemo5locationprivate.h"
10 #include <QDebug>
11
12 /**
13   * Default constructor of this class.
14   *
15   * @param Maemo5Location pointer to public interface.
16   */
17 Maemo5LocationPrivate::Maemo5LocationPrivate(Maemo5Location* location):QObject(location)
18 {
19     qDebug() << "__Maemo5LocationPrivate";
20     //Initialize variables
21     gps_online = false;
22     usegps = -1;
23     resetAll();
24     //Get gps control object
25     control = NULL;
26     control = location_gpsd_control_get_default();
27     //create gps device
28     device = NULL;
29     device = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
30
31     g_signal_connect(device, "changed", G_CALLBACK(gps_data_changed), this);
32     g_signal_connect(device, "connected", G_CALLBACK(gps_connected_func), this);
33     g_signal_connect(device, "disconnected", G_CALLBACK(gps_disconnected_func), this);
34
35     g_signal_connect(control, "error-verbose", G_CALLBACK(gps_error_func), this);
36     g_signal_connect(control, "gpsd_running", G_CALLBACK(gpsd_running_func), this);
37     g_signal_connect(control, "gpsd_stopped", G_CALLBACK(gpsd_running_func), this);
38
39 }
40 /**
41   * Destructor of this class. Should be used to release all allocated resources.
42   */
43 Maemo5LocationPrivate::~Maemo5LocationPrivate()
44 {
45     qDebug() << "__~Maemo5LocationPrivate";
46     //delete device;
47     //delete control;
48 }
49
50 /**
51   * This function is used to start to poll with gprs
52   */
53 void Maemo5LocationPrivate::get_acwp()
54 {
55     g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
56     usegps = 0;
57     restart();
58 }
59
60 /**
61   * This function starts to poll via gps interface
62   */
63 void Maemo5LocationPrivate::get_agnss()
64 {
65     g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_AGNSS, NULL);
66     usegps = 1;
67     restart();
68 }
69
70 /**
71   * Stop polling gps
72   */
73 void Maemo5LocationPrivate::stop()
74 {
75     location_gpsd_control_stop(control);
76 }
77
78 /**
79   * Stop and restart polling
80   */
81 void Maemo5LocationPrivate::restart()
82 {
83     location_gpsd_control_stop(control);
84     location_gpsd_control_start(control);
85 }
86
87 /**
88   * This function is called when device managed to connect to the lcoation server.
89   * Function emits gps_connected signal.
90   *
91   * @param Pointer to LocationGPSDevice class
92   * @param Pointer to Maemo5LocationPrivate class
93   */
94 void gps_connected_func(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
95 {
96     emit gps->gps_connected();
97 }
98
99 /**
100   * This function is called when device is disconnected from the location server.
101   * Function emits gps_disconnected signal.
102   * Also this function resets all arguments of Maemo5LcoationPrivate class.
103   *
104   * @param Pointer to LocationGPSDevice class
105   * @param Pointer to Maemo5LocationPrivate class
106   */
107 void gps_disconnected_func(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
108 {
109     gps->resetAll();
110     emit gps->gps_disconnected();
111 }
112
113 /**
114   * This function is called after the location_gpsd_control_stop has been called.
115   *
116   * @param Pointer to LocationGPSDControl class
117   * @param Pointer to Maemo5LocationPrivate class
118   */
119 void gpsd_stopped_func(LocationGPSDControl *control, Maemo5LocationPrivate *gps)
120 {
121     emit gps->gpsd_stopped();
122 }
123
124 /**
125   * This function is called when an error has occurred.
126   *
127   * @param Pointer to LocationGPSDControl class
128   * @param error code
129   * @param Pointer to Maemo5LocationPrivate class
130   */
131 void gps_error_func(LocationGPSDControl *control, gint error, Maemo5LocationPrivate *gps)
132 {
133
134     switch (error) {
135       case LOCATION_ERROR_USER_REJECTED_DIALOG:
136         emit gps->gps_error(0);
137         g_debug("User didn't enable requested methods");
138         break;
139       case LOCATION_ERROR_USER_REJECTED_SETTINGS:
140         emit gps->gps_error(1);
141         g_debug("User changed settings, which disabled location");
142         break;
143       case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
144         emit gps->gps_error(2);
145         g_debug("Problems with BT GPS");
146         break;
147       case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
148         emit gps->gps_error(3);
149         g_debug("Requested method is not allowed in offline mode");
150         break;
151       case LOCATION_ERROR_SYSTEM:
152         emit gps->gps_error(4);
153         g_debug("System error");
154         break;
155       }
156 }
157
158 /**
159   * This function is called after the location_gpsd_control_start has been called.
160   *
161   * @param Pointer to LocationGPSDControl class
162   * @param Pointer to Maemo5LocationPrivate class
163   */
164 void gpsd_running_func(LocationGPSDControl *control, Maemo5LocationPrivate *gps)
165 {
166     emit gps->gpsd_running();
167 }
168
169 /**
170   * Callback function to catch gps signals.
171   *
172   * @param Pointer to LocationGPSDControl class
173   * @param Pointer to Maemo5LocationPrivate class
174   */
175 void gps_data_changed(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
176 {
177     //First check that LocationGpsDeviceFix can be found...this data structure contains the location info.
178     if(gps->device->fix)
179     {
180         //Check that there are fields
181         if(gps->device->fix->fields)
182         {
183             //Store values and emit signal
184             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
185             {
186                 gps->latitude = gps->device->fix->latitude;
187                 gps->longitude = gps->device->fix->longitude;
188                 gps->eph = gps->device->fix->eph;
189             }
190
191
192             gps->satellites_in_use = gps->device->satellites_in_use;
193             gps->satellites_in_view = gps->device->satellites_in_view;
194
195             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET)
196             {
197                 gps->time = gps->device->fix->time;
198                 gps->ept = gps->device->fix->ept;
199             }
200
201             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
202             {
203                 gps->altitude = gps->device->fix->altitude;
204                 gps->epv = gps->device->fix->epv;
205             }
206
207             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
208             {
209                 gps->track = gps->device->fix->track;
210                 gps->epd = gps->device->fix->epd;
211             }
212
213             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
214             {
215                 gps->speed = gps->device->fix->speed;
216                 gps->eps = gps->device->fix->eps;
217             }
218
219             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
220             {
221                 gps->climb = gps->device->fix->climb;
222                 gps->epc = gps->device->fix->epc;
223             }
224
225
226             // Calculate average signal strength of satellites in use
227             int temp = 0;
228             if(gps->satellites_in_use != 0)
229             {
230                 for(int i=0 ; i < gps->satellites_in_use ; i++)
231                 {
232                     LocationGPSDeviceSatellite *view = (LocationGPSDeviceSatellite*) g_ptr_array_index (gps->device->satellites, i);
233                     temp = temp + view->signal_strength;
234                 }
235                 gps->signal_strength = (temp / gps->satellites_in_use);
236             }
237
238
239             if(gps->usegps == 0)
240             {
241                 emit gps->awcp();
242             }
243             else if(gps->usegps == 1)
244             {
245                 emit gps->agnss();
246             }
247             else
248             {
249                 emit gps->locationUpdated();
250             }
251         }
252     }
253
254 }
255
256 /**
257   * Resets all arguments of Maemo5LocationPrivate class.
258   */
259 void Maemo5LocationPrivate::resetAll()
260 {
261     time = 0;
262     latitude = 0;
263     longitude = 0;
264     satellites_in_view = 0;
265     satellites_in_use = 0;
266     ept = 0;
267     eph = 0;
268     altitude = 0;
269     epv = 0;
270     track = 0;
271     epd = 0;
272     speed = 0;
273     eps = 0;
274     climb = 0;
275     epc = 0;
276 }
277
278 /**
279   * Get distance between two points in kilometers.
280   *
281   * @param double latitude of first point
282   * @param double longitude of first point
283   * @param double latitude of second point
284   * @param double longitude of second point
285   * @return double distance
286   */
287 double Maemo5LocationPrivate::distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f)
288 {
289     double distance = 0;
290     return distance = location_distance_between(latitude_s, longitude_s, latitude_f, longitude_f);
291 }