Merge branch 'master' into settings_auto_update
[situare] / src / engine / engine.cpp
index f569991..02d8597 100644 (file)
@@ -22,6 +22,7 @@
     USA.
  */
 
+#include <QMessageBox>
 
 #include "common.h"
 #include "facebookservice/facebookauthentication.h"
@@ -38,10 +39,12 @@ const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Au
 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12;  ///< Default zoom level when GPS available
 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
+const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
 
 SituareEngine::SituareEngine(QMainWindow *parent)
     : QObject(parent),
       m_autoCenteringEnabled(false),
+      m_automaticUpdateFirstStart(true),
       m_loggedIn(false),
       m_automaticUpdateIntervalTimer(0),
       m_lastUpdatedGPSPosition(QPointF()),
@@ -112,10 +115,11 @@ SituareEngine::~SituareEngine()
 
 void SituareEngine::automaticUpdateIntervalTimerTimeout()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__;
 
     if (m_gps->isRunning() && m_userMoved) {
-        requestUpdateLocation();
+//        requestUpdateLocation();
+        qWarning() << __PRETTY_FUNCTION__ << "requestUpdateLocation()";
         m_userMoved = false;
     }
 }
@@ -167,16 +171,34 @@ void SituareEngine::enableGPS(bool enabled)
 
 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__ << enabled;
 
-    if (m_automaticUpdateIntervalTimer) {
+    bool accepted = false;
 
-        if (enabled && m_gps->isRunning()) {
-            m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
-            m_automaticUpdateIntervalTimer->start();
+    if (m_automaticUpdateFirstStart && enabled) {
+        accepted = m_ui->showEnableAutomaticUpdateLocationDialog();
+        m_automaticUpdateFirstStart = false;
+        m_ui->automaticLocationUpdateEnabled(accepted);
+    }
+    else {
+        accepted = true;
+    }
+
+    qWarning() << __PRETTY_FUNCTION__ << accepted;
+
+    if (accepted) {
+        if (m_automaticUpdateIntervalTimer) {
+
+            if (enabled && m_gps->isRunning()) {
+                if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
+                    m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
+                else
+                    m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
+                m_automaticUpdateIntervalTimer->start();
+            }
+            else
+                m_automaticUpdateIntervalTimer->stop();
         }
-        else
-            m_automaticUpdateIntervalTimer->stop();
     }
 }
 
@@ -254,13 +276,14 @@ void SituareEngine::loginActionPressed()
 
 void SituareEngine::loginOk()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__;
 
     m_loggedIn = true;
     m_ui->loggedIn(m_loggedIn);
 
     m_ui->show();
     m_situareService->fetchLocations(); // request user locations
+    m_ui->requestAutomaticLocationUpdateSettings();
 }
 
 void SituareEngine::loginProcessCancelled()