Changed the old code to work as a daemon
[googlelatitude] / src / daemon / gps.cpp
1 #include "gps.h"
2
3 #include <QDebug>
4
5 #ifdef Q_WS_MAEMO_5
6
7 bool ValidQReal(qreal value);
8
9 GpsMaemo5::GpsMaemo5(LocationGPSDControlInterval location_interval,
10                      LocationGPSDControlMethod location_method,
11                      QObject *parent) :
12            QObject(parent)
13 {
14     latitude = 0;
15     longitude = 0;
16     accuracy = 0;
17
18     control = location_gpsd_control_get_default();
19     device = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
20
21     updateGPSConfig (location_interval, location_method);
22
23     g_signal_connect(device, "changed", G_CALLBACK(GpsMaemo5_changed), this);
24 }
25
26 void GpsMaemo5::enable()
27 {
28     location_gpsd_control_start(control);
29 }
30 void GpsMaemo5::disable()
31 {
32     location_gpsd_control_stop(control);
33 }
34
35 void GpsMaemo5::updateGPSConfig (LocationGPSDControlInterval location_interval,
36                                 LocationGPSDControlMethod location_method)
37 {
38     g_object_set(G_OBJECT(control), "preferred-interval", location_interval, NULL);
39     g_object_set(G_OBJECT(control), "preferred-method", location_method, NULL);
40     qDebug() << "preferred-interval: " << location_interval;
41     qDebug() << "preferred-method: " << location_method;
42 }
43
44 void GpsMaemo5_changed(LocationGPSDevice *device, GpsMaemo5 *gps) {
45     if (device->fix) {
46         if (device->fix->fields) {
47             if ( !ValidQReal(gps->device->fix->eph/100.) )
48                 return;
49             g_print("lat = %f, long = %f, eph = %f\n", gps->device->fix->latitude, gps->device->fix->longitude, gps->device->fix->eph/100.);
50             gps->latitude = gps->device->fix->latitude;
51             gps->longitude = gps->device->fix->longitude;
52             gps->accuracy = gps->device->fix->eph/100.;
53             emit gps->fix();
54         }
55     }
56 }
57
58 bool ValidQReal(qreal value)
59 {
60     if (value != value){
61         return false;
62     }
63     else if (value > std::numeric_limits<qreal>::max()){
64         return false;
65     }
66     else if (value < -std::numeric_limits<qreal>::max()){
67         return false;
68     }
69     else
70         return true;
71 }
72
73 #endif // Q_WS_MAEMO_5