Version 0.6, daemon, gps, battery saver
[googlelatitude] / src / glatitude.cpp
index 0573772..a4da3b6 100644 (file)
@@ -1,54 +1,55 @@
 #include "glatitude.h"
 
-GoogleLatitude::GoogleLatitude(QObject *parent) : QObject(parent) {
-    user = "";
-    pass = "";
-    latitude = 0.;
-    longitude = 0.;
-    accuracy = 0.;
-    interval = 120;
+GoogleLatitude::GoogleLatitude(QObject *parent) :
+        QObject(parent),
+        user(""), pass(""), login_error(false),
+        latitude(0), longitude(0), accuracy(0) {
 
     worker = new QNetworkAccessManager();
-    connect(worker, SIGNAL(finished(QNetworkReply *)), this, SLOT(finishedreply(QNetworkReply *)));
+    connect(worker, SIGNAL(finished(QNetworkReply *)), this, SLOT(glat_reply(QNetworkReply *)));
 
     urllogin = QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview");
     urldologin = QUrl::fromEncoded("https://www.google.com/accounts/ServiceLoginAuth?service=friendview");
     urlupdate = QUrl::fromEncoded("http://maps.google.com/glm/mmap/mwmfr?hl=en");
-
-    lastupdate = 0;
-    login_error = false;
 }
 
-void GoogleLatitude::login(QString u, QString p) {
+void GoogleLatitude::set_login(QString u, QString p) {
+    qDebug() << "GoogleLatitude: set_login";
     if ( !u.contains('@') ) u.append("@gmail.com");
     user = u;
     pass = p;
 }
 
-void GoogleLatitude::freq(int f) {
-    interval = f;
-}
+void GoogleLatitude::update(double la, double lo, double ac) {
+    if (login_error) return;
+    if (!la) return;
+    if (!lo) return;
+    if (!ac) return;
 
-void GoogleLatitude::reset() {
-    lastupdate = 0;
-    login_error = false;
-    set(latitude, longitude, accuracy);
-}
+#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
+    QNetworkConfigurationManager mgr;
+    if (!mgr.isOnline()) {
+        qDebug() << "GoogleLatitude: offline";
+        return;
+    }
+#endif
+
+    qDebug() << "GoogleLatitude: update";
 
-void GoogleLatitude::set(double la, double lo, double ac) {
     latitude = la;
     longitude = lo;
     accuracy = ac;
 
-    qDebug() << "GoogleLatitude: set la = " << la << " lo = " << lo << " ac = " << ac;
-    qDebug() << "GoogleLatitude: set lastupdate = " << lastupdate << " current = " << QDateTime::currentMSecsSinceEpoch();
-    if (login_error) return;
-    if (la*lo == 0) return;
-    if ( QDateTime::currentMSecsSinceEpoch() < lastupdate + interval*1000 ) return;
     worker->get(QNetworkRequest(urllogin));
 }
 
-void GoogleLatitude::finishedreply(QNetworkReply *r) {
+void GoogleLatitude::reset() {
+    qDebug() << "GoogleLatitude: reset";
+    login_error = false;
+    update(latitude, longitude, accuracy);
+}
+
+void GoogleLatitude::glat_reply(QNetworkReply *r) {
     if ( r->url() == urllogin ) {
         qDebug() << "GoogleLatitude: login";
         QString aidis = r->readAll();
@@ -78,16 +79,16 @@ void GoogleLatitude::finishedreply(QNetworkReply *r) {
         QString output = r->readAll();
         QRegExp regexp ("Authentication required");
         if (regexp.indexIn(output, 1) != -1) {
-            qDebug() << "GoogleLatitude: update error";
+            qDebug() << "GoogleLatitude: update error auth";
             login_error = true;
-            emit ERROR();
+            emit glat_error();
         } else {
-            lastupdate = QDateTime::currentMSecsSinceEpoch();
-            qDebug() << "GoogleLatitude: update ok " << " lastupdate = " << lastupdate;
-            emit OK();
+            qDebug() << "GoogleLatitude: update ok";
+            qDebug() << output;
+            emit glat_ok();
         }
     } else {
-        qDebug() << "GoogleLatitude Error url:" << r->url();
+        qDebug() << "GoogleLatitude Error url" << r->url();
         qDebug() << r->rawHeaderList();
         qDebug() << r->readAll();
     }