Version 0.6, daemon, gps, battery saver
[googlelatitude] / src / glatitude.cpp
index 68b626f..a4da3b6 100644 (file)
@@ -1,41 +1,57 @@
 #include "glatitude.h"
 
 #include "glatitude.h"
 
-GoogleLatitude::GoogleLatitude(QObject *parent) : QObject(parent) {
-    user = "";
-    pass = "";
-    latitude = 0.;
-    longitude = 0.;
-    accuracy = 0.;
+GoogleLatitude::GoogleLatitude(QObject *parent) :
+        QObject(parent),
+        user(""), pass(""), login_error(false),
+        latitude(0), longitude(0), accuracy(0) {
 
     worker = new QNetworkAccessManager();
 
     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");
 
     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");
-    urlloc = QUrl::fromEncoded("http://www.google.com/loc/json");
 }
 
 }
 
-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;
 }
 
     if ( !u.contains('@') ) u.append("@gmail.com");
     user = u;
     pass = p;
 }
 
-void GoogleLatitude::set(double la, double lo, double ac) {
+void GoogleLatitude::update(double la, double lo, double ac) {
+    if (login_error) return;
+    if (!la) return;
+    if (!lo) return;
+    if (!ac) return;
+
+#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
+    QNetworkConfigurationManager mgr;
+    if (!mgr.isOnline()) {
+        qDebug() << "GoogleLatitude: offline";
+        return;
+    }
+#endif
+
+    qDebug() << "GoogleLatitude: update";
+
     latitude = la;
     longitude = lo;
     accuracy = ac;
     latitude = la;
     longitude = lo;
     accuracy = ac;
+
     worker->get(QNetworkRequest(urllogin));
 }
 
     worker->get(QNetworkRequest(urllogin));
 }
 
-void GoogleLatitude::get() {
-    QByteArray postloc = QByteArray("{version:\"1.1.0\"}");
-    worker->post(QNetworkRequest(urlloc),postloc);
+void GoogleLatitude::reset() {
+    qDebug() << "GoogleLatitude: reset";
+    login_error = false;
+    update(latitude, longitude, accuracy);
 }
 
 }
 
-void GoogleLatitude::finishedreply(QNetworkReply *r) {
+void GoogleLatitude::glat_reply(QNetworkReply *r) {
     if ( r->url() == urllogin ) {
     if ( r->url() == urllogin ) {
+        qDebug() << "GoogleLatitude: login";
         QString aidis = r->readAll();
         QRegExp regexp ("type=\"hidden\".*name=\"GALX\".*value=\"(.*)\"");
         regexp.setMinimal(1);
         QString aidis = r->readAll();
         QRegExp regexp ("type=\"hidden\".*name=\"GALX\".*value=\"(.*)\"");
         regexp.setMinimal(1);
@@ -49,6 +65,7 @@ void GoogleLatitude::finishedreply(QNetworkReply *r) {
         datalogin += "&Passwd=" + pass;
         worker->post(QNetworkRequest(urldologin), datalogin);
     } else if ( r->url() == urldologin ) {
         datalogin += "&Passwd=" + pass;
         worker->post(QNetworkRequest(urldologin), datalogin);
     } else if ( r->url() == urldologin ) {
+        qDebug() << "GoogleLatitude: dologin";
         QByteArray datagps;
         datagps += "t=ul";
         datagps += "&lat=" + QString::number(latitude);
         QByteArray datagps;
         datagps += "t=ul";
         datagps += "&lat=" + QString::number(latitude);
@@ -62,24 +79,16 @@ void GoogleLatitude::finishedreply(QNetworkReply *r) {
         QString output = r->readAll();
         QRegExp regexp ("Authentication required");
         if (regexp.indexIn(output, 1) != -1) {
         QString output = r->readAll();
         QRegExp regexp ("Authentication required");
         if (regexp.indexIn(output, 1) != -1) {
-            emit setERROR();
+            qDebug() << "GoogleLatitude: update error auth";
+            login_error = true;
+            emit glat_error();
         } else {
         } else {
-            emit setOK();
+            qDebug() << "GoogleLatitude: update ok";
+            qDebug() << output;
+            emit glat_ok();
         }
         }
-        qDebug() << output;
-    } else if ( r->url() == urlloc ) {
-        QString loc = r->readAll();
-        QRegExp regexp ("\\{\"latitude\":(.*),\"longitude\":(.*),\"accuracy\":(.*)\\}");
-        regexp.setMinimal(1);
-        regexp.indexIn(loc, 1);
-        latitude = regexp.capturedTexts().at(1).toDouble();
-        longitude = regexp.capturedTexts().at(2).toDouble();
-        accuracy = regexp.capturedTexts().at(3).toDouble();
-        emit getOK();
-        qDebug() << "lat = " + QString::number(latitude) + " lng = " + QString::number(longitude) + " acc = " + QString::number(accuracy);
     } else {
     } else {
-        qDebug() << "Error";
-        qDebug() << "url:" << r->url();
+        qDebug() << "GoogleLatitude Error url" << r->url();
         qDebug() << r->rawHeaderList();
         qDebug() << r->readAll();
     }
         qDebug() << r->rawHeaderList();
         qDebug() << r->readAll();
     }