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