separate daemon from gui, not auto connect
[googlelatitude] / src / glatitude.cpp
index 8a91f3d..b9a2ace 100644 (file)
@@ -6,6 +6,7 @@ GoogleLatitude::GoogleLatitude(QObject *parent) : QObject(parent) {
     latitude = 0.;
     longitude = 0.;
     accuracy = 0.;
+    interval = 120;
 
     worker = new QNetworkAccessManager();
     connect(worker, SIGNAL(finished(QNetworkReply *)), this, SLOT(finishedreply(QNetworkReply *)));
@@ -13,7 +14,9 @@ GoogleLatitude::GoogleLatitude(QObject *parent) : QObject(parent) {
     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");
+
+    lastupdate = 0;
+    login_error = false;
 }
 
 void GoogleLatitude::login(QString u, QString p) {
@@ -22,20 +25,38 @@ void GoogleLatitude::login(QString u, QString p) {
     pass = p;
 }
 
+void GoogleLatitude::freq(int f) {
+    interval = f;
+}
+
+void GoogleLatitude::reset() {
+    lastupdate = 0;
+    login_error = false;
+    set(latitude, longitude, accuracy);
+}
+
 void GoogleLatitude::set(double la, double lo, double ac) {
     latitude = la;
     longitude = lo;
     accuracy = ac;
-    worker->get(QNetworkRequest(urllogin));
-}
 
-void GoogleLatitude::get() {
-    QByteArray postloc = QByteArray("{version:\"1.1.0\"}");
-    worker->post(QNetworkRequest(urlloc),postloc);
+    qDebug() << "GoogleLatitude: set la" << la << "lo" << lo << "ac" << ac << "current" << QDateTime::currentDateTime().toUTC().toTime_t();
+    if (login_error) return;
+    if (la*lo == 0) return;
+    if ( QDateTime::currentDateTime().toUTC().toTime_t() < lastupdate + interval ) return;
+
+    QNetworkConfigurationManager mgr;
+    if (!mgr.isOnline()) {
+        qDebug() << "GoogleLatitude: offline";
+        return;
+    }
+
+    worker->get(QNetworkRequest(urllogin));
 }
 
 void GoogleLatitude::finishedreply(QNetworkReply *r) {
     if ( r->url() == urllogin ) {
+        qDebug() << "GoogleLatitude: login";
         QString aidis = r->readAll();
         QRegExp regexp ("type=\"hidden\".*name=\"GALX\".*value=\"(.*)\"");
         regexp.setMinimal(1);
@@ -49,6 +70,7 @@ void GoogleLatitude::finishedreply(QNetworkReply *r) {
         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);
@@ -62,23 +84,16 @@ void GoogleLatitude::finishedreply(QNetworkReply *r) {
         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 ERROR();
         } else {
-            emit setOK();
+            lastupdate = QDateTime::currentDateTime().toUTC().toTime_t();
+            qDebug() << "GoogleLatitude: update ok" << "lastupdate" << lastupdate;
+            emit 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();
     } else {
-        qDebug() << "Error";
-        qDebug() << "url:" << r->url();
+        qDebug() << "GoogleLatitude Error url" << r->url();
         qDebug() << r->rawHeaderList();
         qDebug() << r->readAll();
     }