LatitudeUpdater 0.1, fix the previous commit
[googlelatitude] / gpscontrol.cpp
1 #include "gpscontrol.h"
2 #include <QtNetwork/QNetworkConfigurationManager>
3
4 GpsControl::GpsControl(QObject *parent) :
5     QObject(parent),
6     GpsSettings(this),
7     GpsSource(QGeoPositionInfoSource::createDefaultSource(this)),
8     GpsPosition(),
9     GpsTimeout(this),
10     GpsInterval(this) {
11     qDebug() << "* GpsControl::GpsControl";
12
13     GpsTimeout.setSingleShot(true);
14     GpsInterval.setSingleShot(true);
15
16     setPositioningMethod(GpsSettings.value("gps_method", "cell").toString());
17     setTimeOut(GpsSettings.value("gps_timeout", 60).toInt());
18     setInterval(GpsSettings.value("gps_interval", 15*60).toInt());
19
20     if (!GpsSource) qDebug() << "* GpsControl::GpsControl" << "Not GpsSource";
21
22     connect(GpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
23             this, SLOT(setCurrentLocation(QGeoPositionInfo)));
24
25     connect(this, SIGNAL(gotUpdate()),
26             this, SLOT(onGotUpdate()));
27
28     connect(this, SIGNAL(gotFix()),
29             this, SLOT(onGotFix()));
30
31 }
32
33 void GpsControl::setTimeOut(int sec) {
34     qDebug() << "* GpsControl::setTimeOut" << sec;
35     GpsSettings.setValue("gps_timeout", sec);
36 }
37
38 int GpsControl::getTimeOut() {
39     qDebug() << "* GpsControl::getTimeOut";
40     return GpsSettings.value("gps_timeout").toInt();
41 }
42
43 void GpsControl::setInterval(int sec) {
44     qDebug() << "* GpsControl::setInterval" << sec;
45     GpsSettings.setValue("gps_interval", sec);
46 }
47
48 int GpsControl::getInterval() {
49     qDebug() << "* GpsControl::getInterval";
50     return GpsSettings.value("gps_interval").toInt();
51 }
52
53 void GpsControl::setPositioningMethod(QString method) {
54     qDebug() << "* GpsControl::setPositioningMethod" << method;
55     if (method == "gps") {
56         GpsSettings.setValue("gps_method", method);
57         if (GpsSource) GpsSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
58     }
59     if (method == "cell") {
60         GpsSettings.setValue("gps_method", method);
61         if (GpsSource) GpsSource->setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);
62     }
63     if (method == "all") {
64         GpsSettings.setValue("gps_method", method);
65         if (GpsSource) GpsSource->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
66     }
67 }
68
69 void GpsControl::startUpdates() {
70     qDebug() << "* GpsControl::start";
71     if (!GpsSettings.value("net_auto").toBool()) {
72         QNetworkConfigurationManager mgr;
73         if (!mgr.isOnline()) {
74             qDebug() << "* GpsControl::start" << "offline";
75             return;
76         }
77     }
78
79     if (!GpsSource) return;
80
81     GpsTimeout.singleShot(GpsSettings.value("gps_timeout").toInt()*1000, this, SLOT(stopUpdates()));
82     GpsInterval.singleShot(GpsSettings.value("gps_interval").toInt()*1000, this, SLOT(startUpdates()));
83     GpsSource->startUpdates();
84 }
85
86 void GpsControl::stopUpdates(bool force) {
87     qDebug() << "* GpsControl::stopUpdates" << force;
88     if (!GpsSource) return;
89     GpsSource->stopUpdates();
90
91     if (force) {
92         GpsTimeout.stop();
93         GpsInterval.stop();
94     } else {
95         if (GpsSource->lastKnownPosition(GpsSettings.value("gps_method") == "gps"?true:false).isValid() ) {
96             emit gotFix();
97         }
98     }
99 }
100
101 double GpsControl::getCurrentLatitude() {
102     qDebug() << "* GpsControl::getCurrentLatitude";
103     return GpsPosition.coordinate().latitude();
104 }
105
106 double GpsControl::getCurrentLongitude() {
107     qDebug() << "* GpsControl::getCurrentLongitude";
108     return GpsPosition.coordinate().longitude();
109 }
110
111 double GpsControl::getCurrentAccuracy() {
112     qDebug() << "* GpsControl::getCurrentAccuracy";
113     return GpsPosition.attribute(QGeoPositionInfo::HorizontalAccuracy);
114 }
115
116 unsigned int GpsControl::getCurrentTimestamp() {
117     qDebug() << "* GpsControl::getCurrentTimestamp";
118     return GpsPosition.timestamp().toTime_t();
119 }
120
121 void GpsControl::setCurrentLocation(QGeoPositionInfo pos) {
122     qDebug() << "* GpsControl::setCurrentLocation" << pos;
123     if (pos.isValid()) {
124         GpsPosition = pos;
125         emit gotUpdate();
126     }
127 }