Merge branch 'master' of https://vcs.maemo.org/git/situare
[situare] / src / engine / engine.cpp
index b40e693..0b749bf 100644 (file)
@@ -22,6 +22,7 @@
     USA.
  */
 
+#include <QMessageBox>
 
 #include "common.h"
 #include "facebookservice/facebookauthentication.h"
@@ -38,14 +39,17 @@ 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()),
-      m_userMoved(false)
+      m_userMoved(false),
+      m_automaticUpdateEnabled(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
     m_ui = new MainWindow;
@@ -84,6 +88,10 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
 
+    m_automaticUpdateIntervalTimer = new QTimer(this);
+    connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
+            this, SLOT(automaticUpdateIntervalTimerTimeout()));
+
     // signals connected, now it's time to show the main window
     // but init the MapEngine before so starting location is set
     m_mapEngine->init();
@@ -92,10 +100,6 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     m_facebookAuthenticator->start();
 
     initializeGpsAndAutocentering();
-
-    m_automaticUpdateIntervalTimer = new QTimer(this);
-    connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
-            this, SLOT(automaticUpdateIntervalTimerTimeout()));
 }
 
 SituareEngine::~SituareEngine()
@@ -105,7 +109,6 @@ SituareEngine::~SituareEngine()
     delete m_ui;
 
     QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
 }
@@ -154,14 +157,18 @@ void SituareEngine::enableGPS(bool enabled)
     m_ui->setGPSButtonEnabled(enabled);
     m_mapEngine->setGPSEnabled(enabled);
 
-    if (enabled) {
+    if (enabled && !m_gps->isRunning()) {
         m_gps->start();
         enableAutoCentering(m_autoCenteringEnabled);
         m_gps->requestLastPosition();
+
+        if (!m_automaticUpdateEnabled && m_loggedIn)
+            m_ui->requestAutomaticLocationUpdateSettings();
     }
-    else {
+    else if (!enabled && m_gps->isRunning()) {
         m_gps->stop();
         enableAutoCentering(false);
+        enableAutomaticLocationUpdate(false);
     }
 }
 
@@ -169,14 +176,32 @@ void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateInterv
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (m_automaticUpdateIntervalTimer) {
+    bool accepted = true;
+
+    m_automaticUpdateEnabled = enabled;
+
+    //Show automatic update confirmation dialog
+    if (m_automaticUpdateFirstStart && m_gps->isRunning() && m_automaticUpdateEnabled) {
+        m_ui->showEnableAutomaticUpdateLocationDialog(
+                tr("Do you want to enable automatic location update with %1 min update interval?")
+                .arg(updateIntervalMsecs/1000/60));
+        m_automaticUpdateFirstStart = false;
+    }
+    else {
+        if (!m_automaticUpdateFirstStart && m_automaticUpdateEnabled && accepted)
+            m_ui->buildInformationBox(tr("Automatic location update enabled"));
+
+        if (accepted && m_gps->isRunning() && m_automaticUpdateEnabled) {
+            if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
+                m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
+            else
+                m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
 
-        if (enabled && m_gps->isRunning()) {
-            m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
             m_automaticUpdateIntervalTimer->start();
-        }
-        else
+
+        } else {
             m_automaticUpdateIntervalTimer->stop();
+        }
     }
 }
 
@@ -254,13 +279,16 @@ 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
+
+    if (m_gps->isRunning())
+        m_ui->requestAutomaticLocationUpdateSettings();
 }
 
 void SituareEngine::loginProcessCancelled()