daemon mode and bugs fixes
[googlelatitude] / src / glatitude.cpp
index a00f7c9..be927e9 100644 (file)
@@ -13,6 +13,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");
+
+    lastupdate = 0;
+    login_error = false;
 }
 
 void GoogleLatitude::login(QString u, QString p) {
@@ -26,12 +29,17 @@ void GoogleLatitude::set(double la, double lo, double ac) {
     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 + 120*1000 ) 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);
@@ -45,6 +53,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);
@@ -58,14 +67,16 @@ void GoogleLatitude::finishedreply(QNetworkReply *r) {
         QString output = r->readAll();
         QRegExp regexp ("Authentication required");
         if (regexp.indexIn(output, 1) != -1) {
+            qDebug() << "GoogleLatitude: update error";
+            login_error = true;
             emit ERROR();
         } else {
+            lastupdate = QDateTime::currentMSecsSinceEpoch();
+            qDebug() << "GoogleLatitude: update ok " << " lastupdate = " << lastupdate;
             emit OK();
         }
-        qDebug() << output;
     } else {
-        qDebug() << "Error";
-        qDebug() << "url:" << r->url();
+        qDebug() << "GoogleLatitude Error url:" << r->url();
         qDebug() << r->rawHeaderList();
         qDebug() << r->readAll();
     }