Power save mode review and iteration.
[situare] / src / engine / engine.cpp
index 1bdac7b..f5358c8 100644 (file)
@@ -4,7 +4,8 @@
 
         Kaj Wallin - kaj.wallin@ixonos.com
         Henri Lampela - henri.lampela@ixonos.com
-        Jussi Laitinen jussi.laitinen@ixonos.com
+        Jussi Laitinen - jussi.laitinen@ixonos.com
+        Sami Rämö - sami.ramo@ixonos.com
 
     Situare is free software; you can redistribute it and/or
     modify it under the terms of the GNU General Public License
     USA.
  */
 
-#include "engine.h"
+#include <QMessageBox>
+#include <QNetworkReply>
+
 #include "common.h"
-#include "ui/mainwindow.h"
-#include "gps/gpspositioninterface.h"
+#include "facebookservice/facebookauthentication.h"
+#include "gps/gpsposition.h"
 #include "map/mapengine.h"
-#include "map/mapview.h"
+#include "situareservice/situareservice.h"
+#include "ui/mainwindow.h"
+#include "mce.h"
+#include <cmath>
+
+#include "engine.h"
 
-#ifdef Q_WS_MAEMO_5
-#include "gps/gpsposition.h"
-#else
-#include "gps/gpspositionmockup.h"
-#endif
 
-const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
-const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
+const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
+const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
+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_latestLocation()
-{
+      m_automaticUpdateFirstStart(true),
+      m_automaticUpdateRequest(false),
+      m_userMoved(false),
+      m_automaticUpdateIntervalTimer(0),
+      m_lastUpdatedGPSPosition(QPointF())
+{    
     qDebug() << __PRETTY_FUNCTION__;
     m_ui = new MainWindow;
+    m_ui->updateItemVisibility();
 
     // build MapEngine
-
     m_mapEngine = new MapEngine(this);
     m_ui->setMapViewScene(m_mapEngine->scene());
 
     // build GPS
-
-#ifdef Q_WS_MAEMO_5
     m_gps = new GPSPosition(this);
-#else
-    m_gps = new GPSPositionMockup(this);
-#endif
-    m_gps->setMode(GPSPositionInterface::Default);
 
     // build SituareService
-
     m_situareService = new SituareService(this);
 
     // build FacebookAuthenticator
-
     m_facebookAuthenticator = new FacebookAuthentication(this);
 
     // connect signals
-
     signalsFromMapEngine();
     signalsFromGPS();
     signalsFromSituareService();
     signalsFromMainWindow();
-
-    connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
-            m_situareService, SLOT(credentialsReady(FacebookCredentials)));
-    connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
-            this, SLOT(loginOk()));
+    signalsFromFacebookAuthenticator();
 
     connect(this, SIGNAL(userLocationReady(User*)),
             m_ui, SIGNAL(userLocationReady(User*)));
+
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
 
-    connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
-            m_ui, SLOT(startLoginProcess(QUrl)));
-    connect(m_ui, SIGNAL(updateCredentials(QUrl)),
-            m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
-    connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
-            m_ui, SLOT(loginFailed()));
+    connect(this, SIGNAL(userLocationReady(User*)),
+            m_mapEngine, SLOT(receiveOwnLocation(User*)));
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
-    QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
+    connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
+            m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
 
-    m_ui->setGPS(gpsEnabled.toBool());
-    m_ui->setAutoCentering(autoCenteringEnabled.toBool());
+    m_automaticUpdateIntervalTimer = new QTimer(this);
+    connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
+            this, SLOT(startAutomaticUpdate()));
 
+    // signals connected, now it's time to show the main window
+    // but init the MapEngine before so starting location is set
     m_mapEngine->init();
+    m_ui->show();
 
     m_facebookAuthenticator->start();
+
+    m_gps->setMode(GPSPosition::Default);
+    initializeGpsAndAutocentering();
+
+    m_mce = new MCE(this);
+    connect(m_mce, SIGNAL(displayStateChanged(bool)), this, SLOT(displayStateChanged(bool)));
 }
 
 SituareEngine::~SituareEngine()
@@ -110,41 +114,405 @@ 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);
 }
 
+void SituareEngine::changeAutoCenteringSetting(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_autoCenteringEnabled = enabled;
+    enableAutoCentering(enabled);
+}
+
+void SituareEngine::disableAutoCentering()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    changeAutoCenteringSetting(false);
+    m_ui->buildInformationBox(tr("Auto centering disabled"));
+}
+
+void SituareEngine::displayStateChanged(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_gps->enablePowerSave(!enabled);
+
+    if (m_autoCenteringEnabled)
+        enableAutoCentering(enabled);
+}
+
+void SituareEngine::enableAutoCentering(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->setAutoCenteringButtonEnabled(enabled);
+    m_mapEngine->setAutoCentering(enabled);
+
+    if (enabled)
+        m_gps->requestLastPosition();
+}
+
+void SituareEngine::enableGPS(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->setOwnLocationCrosshairVisibility(!enabled);
+
+    if (m_gps->isInitialized()) {
+        m_ui->setGPSButtonEnabled(enabled);
+        m_mapEngine->setGPSEnabled(enabled);
+
+        if (enabled && !m_gps->isRunning()) {
+            m_gps->start();
+            enableAutoCentering(m_autoCenteringEnabled);
+            m_gps->requestLastPosition();
+
+            if(m_ui->loginState())
+                m_ui->readAutomaticLocationUpdateSettings();
+        }
+        else if (!enabled && m_gps->isRunning()) {
+            m_gps->stop();
+            enableAutoCentering(false);
+            enableAutomaticLocationUpdate(false);
+        }
+    }
+    else {
+        if (enabled)
+            m_ui->buildInformationBox(tr("Unable to start GPS"));
+        m_ui->setGPSButtonEnabled(false);
+        m_mapEngine->setGPSEnabled(false);
+    }
+}
+
+void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    //Show automatic update confirmation dialog
+    if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
+        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 (enabled && m_gps->isRunning()) {
+            m_ui->buildInformationBox(tr("Automatic location update enabled"));
+            if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
+                m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
+            else
+                m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
+
+            connect(m_gps, SIGNAL(position(QPointF,qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+
+            m_automaticUpdateIntervalTimer->start();
+
+        } else {
+            disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+
+            m_automaticUpdateIntervalTimer->stop();
+        }
+    }
+}
+
+void SituareEngine::error(const int error)
+{
+    qDebug() << __PRETTY_FUNCTION__;    
+
+    switch(error)
+    {
+    case QNetworkReply::ConnectionRefusedError:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Connection refused by the server"), true);
+        break;
+    case QNetworkReply::RemoteHostClosedError:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Connection closed by the server"), true);
+        break;
+    case QNetworkReply::HostNotFoundError:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Remote server not found"), true);
+        break;
+    case QNetworkReply::TimeoutError:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Connection timed out"), true);
+        break;
+    case SituareError::SESSION_EXPIRED:
+        m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
+        m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
+        m_situareService->clearUserData();
+        m_ui->loggedIn(false);
+        m_ui->loginFailed();
+        break;
+    case SituareError::LOGIN_FAILED:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
+        m_ui->loginFailed();
+        break;
+    case SituareError::UPDATE_FAILED:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Update failed, please try again"), true);
+        break;
+    case SituareError::DATA_RETRIEVAL_FAILED:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
+        break;
+    case SituareError::ADDRESS_RETRIEVAL_FAILED:
+        m_ui->buildInformationBox(tr("Address retrieval failed"), true);
+        break;
+    case SituareError::IMAGE_DOWNLOAD_FAILED:
+        m_ui->buildInformationBox(tr("Image download failed"), true);
+        break;
+    case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
+        m_ui->buildInformationBox(tr("Map image download failed"), true);
+        break;
+    case SituareError::GPS_INITIALIZATION_FAILED:
+        enableGPS(false);
+        m_ui->buildInformationBox(tr("GPS initialization failed"), true);
+        break;
+    case SituareError::UNKNOWN_REPLY:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Unknown server response"), true);
+        break;
+    case SituareError::INVALID_JSON:
+        m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
+        m_ui->loggedIn(false);
+        m_facebookAuthenticator->clearAccountInformation(false); // clean all
+        break;
+    default:
+        m_ui->toggleProgressIndicator(false);
+        qCritical() << "QNetworkReply::NetworkError :" << error;
+        break;
+    }
+}
+
+void SituareEngine::fetchUsernameFromSettings()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->setUsername(m_facebookAuthenticator->loadUsername());
+}
+
+void SituareEngine::initializeGpsAndAutocentering()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
+    QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
+
+    if (m_gps->isInitialized()) {
+
+        if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
+
+            connect(m_gps, SIGNAL(position(QPointF,qreal)),
+                    this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+
+            changeAutoCenteringSetting(true);
+            enableGPS(true);
+
+            m_ui->buildInformationBox(tr("GPS enabled"));
+            m_ui->buildInformationBox(tr("Auto centering enabled"));
+
+        } else { // Normal start
+            changeAutoCenteringSetting(autoCenteringEnabled.toBool());
+            enableGPS(gpsEnabled.toBool());
+
+            if (gpsEnabled.toBool())
+                m_ui->buildInformationBox(tr("GPS enabled"));
+
+            if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
+                m_ui->buildInformationBox(tr("Auto centering enabled"));
+        }
+    } else {
+        enableGPS(false);
+    }
+}
+
+void SituareEngine::loginActionPressed()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_ui->loginState()) {
+        logout();
+        m_situareService->clearUserData();
+    } else {
+        m_facebookAuthenticator->start();
+    }
+}
+
+void SituareEngine::loginOk()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->loggedIn(true);
+
+    m_ui->show();
+    m_situareService->fetchLocations(); // request user locations
+
+    if (m_gps->isRunning())
+        m_ui->readAutomaticLocationUpdateSettings();
+}
+
+void SituareEngine::loginProcessCancelled()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->toggleProgressIndicator(false);
+    m_ui->updateItemVisibility();
+}
+
+void SituareEngine::logout()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->loggedIn(false);
+
+    // signal to clear locationUpdateDialog's data
+    connect(this, SIGNAL(clearUpdateLocationDialogData()),
+            m_ui, SIGNAL(clearUpdateLocationDialogData()));
+    emit clearUpdateLocationDialogData();
+
+    m_facebookAuthenticator->clearAccountInformation(); // clear all
+    m_automaticUpdateFirstStart = true;
+}
+
+void SituareEngine::refreshUserData()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->toggleProgressIndicator(true);
+
+    m_situareService->fetchLocations();
+}
+
+void SituareEngine::requestAddress()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_gps->isRunning())
+        m_situareService->reverseGeo(m_gps->lastPosition());
+    else
+        m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
+}
+
+void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->toggleProgressIndicator(true);
+
+    if (m_gps->isRunning())
+        m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
+    else
+        m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
+}
+
+void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
+         USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
+        (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
+         USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
+
+        m_lastUpdatedGPSPosition = position;
+        m_userMoved = true;
+    }
+
+    if (m_automaticUpdateRequest && m_userMoved) {
+        requestUpdateLocation(tr("Automatic location update."));
+        m_automaticUpdateRequest = false;
+        m_userMoved = false;
+    }
+}
+
+void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    Q_UNUSED(latLonCoordinate);
+    Q_UNUSED(accuracy);
+
+    if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled        
+        m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
+
+    disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
+               this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+}
+
+void SituareEngine::signalsFromFacebookAuthenticator()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_facebookAuthenticator, SIGNAL(error(int)),
+            this, SLOT(error(int)));
+
+    connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
+            m_situareService, SLOT(credentialsReady(FacebookCredentials)));
+
+    connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
+            this, SLOT(loginOk()));
+
+    connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
+            m_ui, SLOT(startLoginProcess()));
+
+    connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
+            m_ui, SLOT(saveCookies()));
+
+    connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
+            m_ui, SLOT(loginUsingCookies()));
+}
+
 void SituareEngine::signalsFromGPS()
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     connect(m_gps, SIGNAL(position(QPointF,qreal)),
             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
 
     connect(m_gps, SIGNAL(timeout()),
             m_ui, SLOT(gpsTimeout()));
 
-    connect(m_gps, SIGNAL(error(QString)),
-            m_ui, SLOT(gpsError(QString)));
+    connect(m_gps, SIGNAL(error(int)),
+            this, SLOT(error(int)));
 }
 
 void SituareEngine::signalsFromMainWindow()
 {
-    // signals from map view
+    qDebug() << __PRETTY_FUNCTION__;    
+
+    connect(m_ui, SIGNAL(error(int)),
+            this, SLOT(error(int)));
+
+    connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
+            this, SLOT(fetchUsernameFromSettings()));
+
+    connect(m_ui, SIGNAL(loginActionPressed()),
+            this, SLOT(loginActionPressed()));
+
+    connect(m_ui, SIGNAL(saveUsername(QString)),
+            m_facebookAuthenticator, SLOT(saveUsername(QString)));
 
+    connect(m_ui, SIGNAL(updateCredentials(QUrl)),
+            m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
+
+    // signals from map view
     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
             m_mapEngine, SLOT(setLocation(QPoint)));
 
     connect(m_ui, SIGNAL(mapViewResized(QSize)),
             m_mapEngine, SLOT(viewResized(QSize)));
 
-    connect(m_ui, SIGNAL(updateMapViewContent(QRect)),
-            m_mapEngine, SLOT(receiveViewSceneRect(QRect)));
-
     connect(m_ui, SIGNAL(viewZoomFinished()),
             m_mapEngine, SLOT(viewZoomFinished()));
 
     // signals from zoom buttons (zoom panel and volume buttons)
-
     connect(m_ui, SIGNAL(zoomIn()),
             m_mapEngine, SLOT(zoomIn()));
 
@@ -152,21 +520,13 @@ void SituareEngine::signalsFromMainWindow()
             m_mapEngine, SLOT(zoomOut()));
 
     // signals from menu buttons
+    connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
+            this, SLOT(changeAutoCenteringSetting(bool)));
 
-    connect(m_ui, SIGNAL(autoCenteringToggled(bool)),
-            m_mapEngine, SLOT(setAutoCentering(bool)));
-
-    connect(m_ui, SIGNAL(autoCenteringToggled(bool)),
-            this, SLOT(enableAutoCentering(bool)));
-
-    connect(m_ui, SIGNAL(gpsToggled(bool)),
-            m_mapEngine, SLOT(gpsEnabled(bool)));
-
-    connect(m_ui, SIGNAL(gpsToggled(bool)),
+    connect(m_ui, SIGNAL(gpsTriggered(bool)),
             this, SLOT(enableGPS(bool)));
 
     //signals from dialogs
-
     connect(m_ui, SIGNAL(cancelLoginProcess()),
             this, SLOT(loginProcessCancelled()));
 
@@ -176,19 +536,28 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
             this, SLOT(requestUpdateLocation(QString,bool)));
 
-    // signals from user info tab
+    connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
+            this, SLOT(enableAutomaticLocationUpdate(bool, int)));    
 
+    // signals from user info tab
     connect(m_ui, SIGNAL(refreshUserData()),
             this, SLOT(refreshUserData()));
 
-    // signals from friend list tab
+    connect(m_ui, SIGNAL(findUser(QPointF)),
+            m_mapEngine, SLOT(setViewLocation(QPointF)));
 
+    // signals from friend list tab
     connect(m_ui, SIGNAL(findFriend(QPointF)),
             m_mapEngine, SLOT(setViewLocation(QPointF)));
 }
 
 void SituareEngine::signalsFromMapEngine()
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_mapEngine, SIGNAL(error(int)),
+            this, SLOT(error(int)));
+
     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
 
@@ -198,91 +567,48 @@ void SituareEngine::signalsFromMapEngine()
     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
             this, SLOT(disableAutoCentering()));
 
-//    connect(m_mapEngine, SIGNAL(ownLocation(QPointF)),
-//            this, SIGNAL(ownLocation(QPointF)));
-
     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
             m_ui, SIGNAL(maxZoomLevelReached()));
 
     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
             m_ui, SIGNAL(minZoomLevelReached()));
+
+    connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
+            m_ui, SIGNAL(locationItemClicked(QList<QString>)));
+
+    connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
+            m_ui, SIGNAL(newMapResolution(qreal)));
 }
 
 void SituareEngine::signalsFromSituareService()
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_situareService, SIGNAL(error(int)),
+            this, SLOT(error(int)));
+
+    connect(m_situareService, SIGNAL(error(int)),
+            m_ui, SIGNAL(messageSendingFailed(int)));
+
     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
             m_ui, SIGNAL(reverseGeoReady(QString)));
 
     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
             this, SLOT(userDataChanged(User*, QList<User*>&)));
 
-    connect(m_situareService, SIGNAL(error(QString)),
-            this, SLOT(error(QString)));
-
     connect(m_situareService, SIGNAL(updateWasSuccessful()),
             this, SLOT(updateWasSuccessful()));
 
-    connect(this, SIGNAL(userLocationReady(User*)),
-            m_mapEngine, SLOT(receiveOwnLocation(User*)));
-
-    connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
-            m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
-}
-
-void SituareEngine::disableAutoCentering()
-{
-    m_ui->setAutoCentering(false);
-}
-
-void SituareEngine::loginProcessCancelled()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ui->toggleProgressIndicator(false);
-    //ToDo: do something
-}
-
-void SituareEngine::error(const QString &error)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-    qDebug() << error;
-    // ToDo: signal UI?
-}
-
-void SituareEngine::loginOk()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ui->show();
-    m_situareService->fetchLocations(); // request user locations
-}
-
-void SituareEngine::requestAddress()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (m_gps->isRunning()) {
-        m_latestLocation = m_gps->lastPosition();
-        m_situareService->reverseGeo(m_latestLocation);
-    }
-    else {
-        m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
-    }
+    connect(m_situareService, SIGNAL(updateWasSuccessful()),
+            m_ui, SIGNAL(clearUpdateLocationDialogData()));
 }
 
-void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
+void SituareEngine::startAutomaticUpdate()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_ui->toggleProgressIndicator(true);
-
-    if (m_gps->isRunning()) {
-        m_latestLocation = m_gps->lastPosition();
-        m_situareService->updateLocation(m_latestLocation, status, publish);
-    }
-    else {
-        m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
-    }
+    m_gps->requestUpdate();
+    m_automaticUpdateRequest = true;
 }
 
 void SituareEngine::updateWasSuccessful()
@@ -292,45 +618,13 @@ void SituareEngine::updateWasSuccessful()
     m_situareService->fetchLocations();
 }
 
-void SituareEngine::refreshUserData()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ui->toggleProgressIndicator(true);
-
-    m_situareService->fetchLocations();
-}
-
 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     m_ui->toggleProgressIndicator(false);
+    m_ui->showPanels();
 
     emit userLocationReady(user);
     emit friendsLocationsReady(friendsList);
 }
-
-void SituareEngine::enableGPS(bool enabled)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (enabled) {
-        /// @todo is this required?
-        // m_gps->requestLastPosition();
-        m_gps->start();
-    }
-    else {
-        m_gps->stop();
-    }
-}
-
-void SituareEngine::enableAutoCentering(bool enabled)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_autoCenteringEnabled = enabled;
-
-    if (enabled)
-        m_gps->requestLastPosition();
-}