if not network, not start up gps
[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), usegps(false),
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     g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
36     if (usegps) {
37         g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_AGNSS, NULL);
38     }
39     location_gpsd_control_start(control);
40 #else
41     worker->post(QNetworkRequest(urlloc), QByteArray("{version:\"1.1.0\"}"));
42 #endif // Q_WS_MAEMO_5
43 }
44
45 void GpsMaemo5::stop() {
46     qDebug() << "GpsMaemo5: stop";
47 #ifdef Q_WS_MAEMO_5
48     location_gpsd_control_stop(control);
49 #else
50 #endif // Q_WS_MAEMO_5
51     if (emitfix) {
52         emitfix = false;
53         emit fix();
54     }
55 }
56
57 void GpsMaemo5::forcestop() {
58     qDebug() << "GpsMaemo5: forcestop";
59     stopgps = true;
60     emitfix = false;
61     stop();
62 }
63
64 int GpsMaemo5::config(int i, int w, bool g) {
65     qDebug() << "GpsMaemo5: config";
66     stopgps = false;
67     interval = i;
68     wait = w;
69     usegps = g;
70     return 0;
71 }
72
73 #ifdef Q_WS_MAEMO_5
74 void GpsMaemo5_changed(LocationGPSDevice *device, GpsMaemo5 *gps) {
75     if (device->fix) {
76         if (device->fix->fields) {
77             if ( isnan(gps->device->fix->eph) ) return;
78             g_print("GpsMaemo5 lat %f lon %f eph %f\n", gps->device->fix->latitude, gps->device->fix->longitude, gps->device->fix->eph/100.);
79             gps->latitude = gps->device->fix->latitude;
80             gps->longitude = gps->device->fix->longitude;
81             gps->accuracy = gps->device->fix->eph/100.;
82             gps->emitfix = true;
83         }
84     }
85 }
86 #else
87 void GpsMaemo5::gloc_reply(QNetworkReply *r) {
88     if ( r->url() == urlloc ) {
89         QString loc = r->readAll();
90         QRegExp regexp ("\\{\"latitude\":(.*),\"longitude\":(.*),\"accuracy\":(.*)\\}");
91         regexp.setMinimal(1);
92         regexp.indexIn(loc, 1);
93         latitude = regexp.capturedTexts().at(1).toDouble();
94         longitude = regexp.capturedTexts().at(2).toDouble();
95         accuracy = regexp.capturedTexts().at(3).toDouble();
96         if ( accuracy > 100000 ) accuracy = 100000;
97         qDebug() << "GpsIP lat" << latitude << "lon" << longitude << "acc" << accuracy;
98         emitfix = true;
99     } else {
100         qDebug() << "GpsIP Error url" << r->url();
101         qDebug() << r->rawHeaderList();
102         qDebug() << r->readAll();
103     }
104 }
105 #endif // Q_WS_MAEMO_5