3 GpsMaemo5::GpsMaemo5(QObject *parent) :
5 latitude(0), longitude(0), accuracy(0),
6 interval(1800), wait(30), method("cell"),
7 emitfix(false), stopgps(true) {
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);
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
19 void GpsMaemo5::refresh() {
20 if ( stopgps ) return;
21 qDebug() << "GpsMaemo5: refresh";
23 QTimer::singleShot(interval*1000, this, SLOT(refresh()));
24 QTimer::singleShot(wait*1000, this, SLOT(stop()));
26 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
27 QNetworkConfigurationManager mgr;
28 if (!mgr.isOnline()) {
29 qDebug() << "GpsMaemo5: offline";
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);
42 g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
44 location_gpsd_control_start(control);
46 worker->post(QNetworkRequest(urlloc), QByteArray("{version:\"1.1.0\"}"));
47 #endif // Q_WS_MAEMO_5
50 void GpsMaemo5::stop() {
51 qDebug() << "GpsMaemo5: stop";
53 location_gpsd_control_stop(control);
55 #endif // Q_WS_MAEMO_5
62 void GpsMaemo5::forcestop() {
63 qDebug() << "GpsMaemo5: forcestop";
69 int GpsMaemo5::config(int i, int w, QString m) {
70 qDebug() << "GpsMaemo5: config";
79 void GpsMaemo5_changed(LocationGPSDevice *device, GpsMaemo5 *gps) {
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.;
92 void GpsMaemo5::gloc_reply(QNetworkReply *r) {
93 if ( r->url() == urlloc ) {
94 QString loc = r->readAll();
95 QRegExp regexp ("\\{\"latitude\":(.*),\"longitude\":(.*),\"accuracy\":(.*)\\}");
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;
105 qDebug() << "GpsIP Error url" << r->url();
106 qDebug() << r->rawHeaderList();
107 qDebug() << r->readAll();
110 #endif // Q_WS_MAEMO_5