Version 0.6-2, use of cell+gps, fix "start daemon"
[googlelatitude] / src / gps.cpp
1 #include "gps.h"
2
3 GpsMaemo5::GpsMaemo5(QObject *parent) :
4         QObject(parent),
5         latitude(0), longitude(0), accuracy(0),
6         interval(1800), wait(30), method("cell"),
7         emitfix(false), stopgps(true) {
8 #ifdef Q_WS_MAEMO_5
9     control = location_gpsd_control_get_default();
10     device = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
11     g_signal_connect(device, "changed", G_CALLBACK(GpsMaemo5_changed), this);
12 #else
13     urlloc = QUrl::fromEncoded("http://www.google.com/loc/json");
14     worker = new QNetworkAccessManager();
15     connect(worker, SIGNAL(finished(QNetworkReply *)), this, SLOT(gloc_reply(QNetworkReply *)));
16 #endif // Q_WS_MAEMO_5
17 }
18
19 void GpsMaemo5::refresh() {
20     if ( stopgps ) return;
21     qDebug() << "GpsMaemo5: refresh";
22
23     QTimer::singleShot(interval*1000, this, SLOT(refresh()));
24     QTimer::singleShot(wait*1000, this, SLOT(stop()));
25
26 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
27     QNetworkConfigurationManager mgr;
28     if (!mgr.isOnline()) {
29         qDebug() << "GpsMaemo5: offline";
30         return;
31     }
32 #endif
33
34 #ifdef Q_WS_MAEMO_5
35     if ( method == QString("cell") ) {
36         g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
37     } else if ( method == QString("both") ) {
38         g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_USER_SELECTED, NULL);
39     } else if ( method == QString("agps") ) {
40         g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_AGNSS, NULL);
41     } else {
42         g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
43     }
44     location_gpsd_control_start(control);
45 #else
46     worker->post(QNetworkRequest(urlloc), QByteArray("{version:\"1.1.0\"}"));
47 #endif // Q_WS_MAEMO_5
48 }
49
50 void GpsMaemo5::stop() {
51     qDebug() << "GpsMaemo5: stop";
52 #ifdef Q_WS_MAEMO_5
53     location_gpsd_control_stop(control);
54 #else
55 #endif // Q_WS_MAEMO_5
56     if (emitfix) {
57         emitfix = false;
58         emit fix();
59     }
60 }
61
62 void GpsMaemo5::forcestop() {
63     qDebug() << "GpsMaemo5: forcestop";
64     stopgps = true;
65     emitfix = false;
66     stop();
67 }
68
69 int GpsMaemo5::config(int i, int w, QString m) {
70     qDebug() << "GpsMaemo5: config";
71     stopgps = false;
72     interval = i;
73     wait = w;
74     method = m;
75     return 0;
76 }
77
78 #ifdef Q_WS_MAEMO_5
79 void GpsMaemo5_changed(LocationGPSDevice *device, GpsMaemo5 *gps) {
80     if (device->fix) {
81         if (device->fix->fields) {
82             if ( isnan(gps->device->fix->eph) ) return;
83             g_print("GpsMaemo5 lat %f lon %f eph %f\n", gps->device->fix->latitude, gps->device->fix->longitude, gps->device->fix->eph/100.);
84             gps->latitude = gps->device->fix->latitude;
85             gps->longitude = gps->device->fix->longitude;
86             gps->accuracy = gps->device->fix->eph/100.;
87             gps->emitfix = true;
88         }
89     }
90 }
91 #else
92 void GpsMaemo5::gloc_reply(QNetworkReply *r) {
93     if ( r->url() == urlloc ) {
94         QString loc = r->readAll();
95         QRegExp regexp ("\\{\"latitude\":(.*),\"longitude\":(.*),\"accuracy\":(.*)\\}");
96         regexp.setMinimal(1);
97         regexp.indexIn(loc, 1);
98         latitude = regexp.capturedTexts().at(1).toDouble();
99         longitude = regexp.capturedTexts().at(2).toDouble();
100         accuracy = regexp.capturedTexts().at(3).toDouble();
101         if ( accuracy > 100000 ) accuracy = 100000;
102         qDebug() << "GpsIP lat" << latitude << "lon" << longitude << "acc" << accuracy;
103         emitfix = true;
104     } else {
105         qDebug() << "GpsIP Error url" << r->url();
106         qDebug() << r->rawHeaderList();
107         qDebug() << r->readAll();
108     }
109 }
110 #endif // Q_WS_MAEMO_5