separate daemon from gui, not auto connect
[googlelatitude] / src / glatitude.cpp
index be927e9..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 *)));
@@ -24,16 +25,32 @@ 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;
 
-    qDebug() << "GoogleLatitude: set la = " << la << " lo = " << lo << " ac = " << ac;
-    qDebug() << "GoogleLatitude: set lastupdate = " << lastupdate << " current = " << QDateTime::currentMSecsSinceEpoch();
+    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::currentMSecsSinceEpoch() < lastupdate + 120*1000 ) return;
+    if ( QDateTime::currentDateTime().toUTC().toTime_t() < lastupdate + interval ) return;
+
+    QNetworkConfigurationManager mgr;
+    if (!mgr.isOnline()) {
+        qDebug() << "GoogleLatitude: offline";
+        return;
+    }
+
     worker->get(QNetworkRequest(urllogin));
 }
 
@@ -67,16 +84,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();
         } else {
-            lastupdate = QDateTime::currentMSecsSinceEpoch();
-            qDebug() << "GoogleLatitude: update ok " << " lastupdate = " << lastupdate;
+            lastupdate = QDateTime::currentDateTime().toUTC().toTime_t();
+            qDebug() << "GoogleLatitude: update ok" << "lastupdate" << lastupdate;
             emit OK();
         }
     } else {
-        qDebug() << "GoogleLatitude Error url:" << r->url();
+        qDebug() << "GoogleLatitude Error url" << r->url();
         qDebug() << r->rawHeaderList();
         qDebug() << r->readAll();
     }