Added RouteWaypointList- and RouteWaypointListView classes.
[situare] / src / engine / engine.cpp
index 338a721..57c14be 100644 (file)
     USA.
  */
 
+#include <cmath>
+
 #include <QMessageBox>
+#include <QNetworkReply>
+
+#ifdef Q_WS_MAEMO_5
+#include "application.h"
+#endif
 
 #include "common.h"
+#include "error.h"
 #include "facebookservice/facebookauthentication.h"
 #include "gps/gpsposition.h"
 #include "map/mapengine.h"
+#include "routing/geocodingservice.h"
+#include "routing/routingservice.h"
+#include "mce.h"
+#include "network/networkaccessmanager.h"
 #include "situareservice/situareservice.h"
 #include "ui/mainwindow.h"
-#include <cmath>
 
 #include "engine.h"
 
@@ -41,18 +52,28 @@ const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value fo
 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),
+SituareEngine::SituareEngine()
+    : m_autoCenteringEnabled(false),
       m_automaticUpdateFirstStart(true),
-      m_loggedIn(false),
+      m_automaticUpdateRequest(false),
+      m_userMoved(false),
       m_automaticUpdateIntervalTimer(0),
-      m_lastUpdatedGPSPosition(QPointF()),
-      m_userMoved(false)
+      m_lastUpdatedGPSPosition(GeoCoordinate())
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     m_ui = new MainWindow;
-    m_ui->updateItemVisibility(m_loggedIn);
+    m_ui->updateItemVisibility();
+
+#ifdef Q_WS_MAEMO_5
+    m_app = static_cast<Application *>(qApp);
+    m_app->registerWindow(m_ui->winId());
+
+    connect(m_app, SIGNAL(topmostChanged(bool)),
+            this, SLOT(enablePowerSave(bool)));
+#endif
+
+    m_networkAccessManager = new NetworkAccessManager(this);
 
     // build MapEngine
     m_mapEngine = new MapEngine(this);
@@ -60,7 +81,6 @@ SituareEngine::SituareEngine(QMainWindow *parent)
 
     // build GPS
     m_gps = new GPSPosition(this);
-    m_gps->setMode(GPSPosition::Default);
 
     // build SituareService
     m_situareService = new SituareService(this);
@@ -68,9 +88,17 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     // build FacebookAuthenticator
     m_facebookAuthenticator = new FacebookAuthentication(this);
 
+    // build routing service
+    m_routingService = new RoutingService(this); // create this when needed, not in constructor!
+
+    // build geocoding service
+    m_geocodingService = new GeocodingService(this);
+
     // connect signals
     signalsFromMapEngine();
+    signalsFromGeocodingService();
     signalsFromGPS();
+    signalsFromRoutingService();
     signalsFromSituareService();
     signalsFromMainWindow();
     signalsFromFacebookAuthenticator();
@@ -87,6 +115,16 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
 
+    connect(this, SIGNAL(friendImageReady(User*)),
+            m_ui, SIGNAL(friendImageReady(User*)));
+
+    connect(this, SIGNAL(friendImageReady(User*)),
+            m_mapEngine, SIGNAL(friendImageReady(User*)));
+
+    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();
@@ -94,11 +132,11 @@ SituareEngine::SituareEngine(QMainWindow *parent)
 
     m_facebookAuthenticator->start();
 
+    m_gps->setMode(GPSPosition::Default);
     initializeGpsAndAutocentering();
 
-    m_automaticUpdateIntervalTimer = new QTimer(this);
-    connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
-            this, SLOT(automaticUpdateIntervalTimerTimeout()));
+    m_mce = new MCE(this);
+    connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(enablePowerSave(bool)));
 }
 
 SituareEngine::~SituareEngine()
@@ -108,28 +146,16 @@ 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::automaticUpdateIntervalTimerTimeout()
-{
-    qWarning() << __PRETTY_FUNCTION__;
-
-    if (m_gps->isRunning() && m_userMoved) {
-//        requestUpdateLocation();
-        qWarning() << __PRETTY_FUNCTION__ << "requestUpdateLocation()";
-        m_userMoved = false;
-    }
-}
-
 void SituareEngine::changeAutoCenteringSetting(bool enabled)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
 
     m_autoCenteringEnabled = enabled;
-    enableAutoCentering(enabled);
+    setAutoCentering(enabled);
 }
 
 void SituareEngine::disableAutoCentering()
@@ -137,82 +163,155 @@ void SituareEngine::disableAutoCentering()
     qDebug() << __PRETTY_FUNCTION__;
 
     changeAutoCenteringSetting(false);
-    m_ui->buildInformationBox(tr("Auto centering disabled"));
 }
 
-void SituareEngine::enableAutoCentering(bool enabled)
+void SituareEngine::draggingModeTriggered()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_ui->setAutoCenteringButtonEnabled(enabled);
-    m_mapEngine->setAutoCentering(enabled);
-
-    if (enabled)
-        m_gps->requestLastPosition();
+    if (m_mce)
+        m_mce->vibrationFeedback();
 }
 
-void SituareEngine::enableGPS(bool enabled)
+void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_ui->setGPSButtonEnabled(enabled);
-    m_mapEngine->setGPSEnabled(enabled);
+    //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);
 
-    if (enabled) {
-        m_gps->start();
-        enableAutoCentering(m_autoCenteringEnabled);
-        m_gps->requestLastPosition();
-    }
-    else {
-        m_gps->stop();
-        enableAutoCentering(false);
-    }
-}
+            connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
 
-void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
-{
-    qWarning() << __PRETTY_FUNCTION__ << enabled;
+            m_automaticUpdateIntervalTimer->start();
 
-    bool accepted = false;
+        } else {
+            disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
 
-    if (m_automaticUpdateFirstStart && enabled) {
-        accepted = m_ui->showEnableAutomaticUpdateLocationDialog();
-        m_automaticUpdateFirstStart = false;
-        m_ui->automaticLocationUpdateEnabled(accepted);
-    }
-    else {
-        accepted = true;
+            m_automaticUpdateIntervalTimer->stop();
+        }
     }
+}
 
-    qWarning() << __PRETTY_FUNCTION__ << accepted;
+void SituareEngine::enablePowerSave(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
 
-    if (accepted) {
-        if (m_automaticUpdateIntervalTimer) {
+    m_gps->enablePowerSave(enabled);
 
-            if (enabled && m_gps->isRunning()) {
-//                if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
-//                    m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
-//                else
-                    m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs / 60);
-                m_automaticUpdateIntervalTimer->start();
-            }
-            else
-                m_automaticUpdateIntervalTimer->stop();
-        }
-    }
+    if(m_autoCenteringEnabled)
+        m_mapEngine->setAutoCentering(!enabled);
 }
 
-void SituareEngine::error(const QString &error)
+void SituareEngine::error(const int context, const int error)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    qDebug() << "ERROR MESSAGE: " << error;
 
-    m_ui->buildInformationBox(error, true);
-
-    if(error.compare(SESSION_EXPIRED) == 0) {
+    switch(error)
+    {
+    case SituareError::ERROR_GENERAL:
+        if(context == ErrorContext::SITUARE) {
+            m_ui->toggleProgressIndicator(false);
+            m_ui->buildInformationBox(tr("Unknown server error"), true);
+        }
+        break;
+    case 1: //errors: SituareError::ERROR_MISSING_ARGUMENT and QNetworkReply::ConnectionRefusedError
+        m_ui->toggleProgressIndicator(false);
+        if(context == ErrorContext::SITUARE) {
+            m_ui->buildInformationBox(tr("Missing parameter from request"), true);
+        } else if(context == ErrorContext::NETWORK) {
+            m_ui->buildInformationBox(tr("Connection refused by the server"), true);
+        }
+        break;
+    case QNetworkReply::RemoteHostClosedError:
+        if(context == ErrorContext::NETWORK) {
+            m_ui->toggleProgressIndicator(false);
+            m_ui->buildInformationBox(tr("Connection closed by the server"), true);
+        }
+        break;
+    case QNetworkReply::HostNotFoundError:
+        if(context == ErrorContext::NETWORK) {
+            m_ui->toggleProgressIndicator(false);
+            m_ui->buildInformationBox(tr("Remote server not found"), true);
+        }
+        break;
+    case QNetworkReply::TimeoutError:
+        if(context == ErrorContext::NETWORK) {
+            m_ui->toggleProgressIndicator(false);
+            m_ui->buildInformationBox(tr("Connection timed out"), true);
+        }
+        break;
+    case QNetworkReply::UnknownNetworkError:
+        if(context == ErrorContext::NETWORK) {
+            m_ui->toggleProgressIndicator(false);
+            m_ui->buildInformationBox(tr("No network connection"), 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:
+    case SituareError::ERROR_GEOLOCATION_REQUEST_FAIL:
+    case SituareError::ERROR_GEOLOCATION_LONLAT_INVALID:
+        m_ui->toggleProgressIndicator(false);
+        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:
+        setGPS(false);
+        m_ui->buildInformationBox(tr("GPS initialization failed"), 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;
+    case SituareError::ERROR_GEOLOCATION_SERVER_UNAVAILABLE:
+        m_ui->toggleProgressIndicator(false);
+        m_ui->buildInformationBox(tr("Address server not responding"), true);
+        break;
+    default:
+        m_ui->toggleProgressIndicator(false);
+        if(context == ErrorContext::NETWORK)
+            qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
+        else
+            qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
+
+        break;
     }
 }
 
@@ -223,67 +322,84 @@ void SituareEngine::fetchUsernameFromSettings()
     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
 }
 
+void SituareEngine::imageReady(User *user)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(user->type())
+        emit userLocationReady(user);
+    else
+        emit friendImageReady(user);
+}
+
 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 (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
+    QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
 
-        connect(m_gps, SIGNAL(position(QPointF,qreal)),
-                this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+    if (m_gps->isInitialized()) {
 
-        changeAutoCenteringSetting(true);
-        enableGPS(true);
+        if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
 
-        m_ui->buildInformationBox(tr("GPS enabled"));
-        m_ui->buildInformationBox(tr("Auto centering enabled"));
+            connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+                    this, SLOT(setFirstStartZoomLevel()));
 
-    } else { // Normal start
-        changeAutoCenteringSetting(autoCenteringEnabled.toBool());
-        enableGPS(gpsEnabled.toBool());
+            changeAutoCenteringSetting(true);
+            setGPS(true);
 
-        if (gpsEnabled.toBool())
             m_ui->buildInformationBox(tr("GPS enabled"));
 
-        if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
-            m_ui->buildInformationBox(tr("Auto centering enabled"));
+        } else { // Normal start
+            changeAutoCenteringSetting(autoCenteringEnabled.toBool());
+            setGPS(gpsEnabled.toBool());
+
+            if (gpsEnabled.toBool())
+                m_ui->buildInformationBox(tr("GPS enabled"));
+        }
+    } else {
+        setGPS(false);
     }
 }
 
-bool SituareEngine::isUserMoved()
+void SituareEngine::locationSearch(QString location)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    return m_userMoved;
+    if(!location.isEmpty())
+        m_geocodingService->requestLocation(location);
 }
 
 void SituareEngine::loginActionPressed()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_loggedIn) {
-        logout();
-        m_situareService->clearUserData();
+    if (m_networkAccessManager->isConnected()) {
+        if(m_ui->loginState()) {
+            logout();
+            m_situareService->clearUserData();
+        } else {
+            m_facebookAuthenticator->start();
+        }
     }
     else {
-        m_facebookAuthenticator->start();
+        error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
     }
 }
 
 void SituareEngine::loginOk()
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_loggedIn = true;
-    m_ui->loggedIn(m_loggedIn);
+    m_ui->loggedIn(true);
 
     m_ui->show();
     m_situareService->fetchLocations(); // request user locations
-    m_ui->requestAutomaticLocationUpdateSettings();
+
+    if (m_gps->isRunning())
+        m_ui->readAutomaticLocationUpdateSettings();
 }
 
 void SituareEngine::loginProcessCancelled()
@@ -291,83 +407,170 @@ void SituareEngine::loginProcessCancelled()
     qDebug() << __PRETTY_FUNCTION__;
 
     m_ui->toggleProgressIndicator(false);
-    m_ui->updateItemVisibility(m_loggedIn);
+    m_ui->updateItemVisibility();
 }
 
 void SituareEngine::logout()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_loggedIn = false;
-    m_ui->loggedIn(m_loggedIn);
+    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();
+    if (m_networkAccessManager->isConnected()) {
+        m_ui->toggleProgressIndicator(true);
+        m_situareService->fetchLocations();
+    }
+    else {
+        error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+    }
 }
 
 void SituareEngine::requestAddress()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (m_gps->isRunning())
-        m_situareService->reverseGeo(m_gps->lastPosition());
-    else
-        m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
+    if (m_networkAccessManager->isConnected()) {
+        if (m_gps->isRunning())
+            m_situareService->reverseGeo(m_gps->lastPosition());
+        else
+            m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
+    }
+    else {
+        error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+    }
 }
 
 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_ui->toggleProgressIndicator(true);
+    if (m_networkAccessManager->isConnected()) {
+        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);
+        if (m_gps->isRunning())
+            m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
+        else
+            m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
+    }
+    else {
+        error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+    }
 }
 
-void SituareEngine::saveGPSPosition(QPointF position)
+void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
+    if ((fabs(m_lastUpdatedGPSPosition.longitude() - position.longitude()) >
          USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
-        (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
+        (fabs(m_lastUpdatedGPSPosition.latitude() - position.latitude()) >
          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::routeParsed(Route &route)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    Q_UNUSED(route);
+
+    m_ui->toggleProgressIndicator(false);
 }
 
-void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
+void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    Q_UNUSED(latLonCoordinate);
-    Q_UNUSED(accuracy);
+    m_ui->toggleProgressIndicator(true);
 
-    if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled        
+    if (m_gps->isRunning())
+        m_routingService->requestRoute(m_gps->lastPosition(), endPointCoordinates);
+    else
+        m_routingService->requestRoute(m_mapEngine->centerGeoCoordinate(), endPointCoordinates);
+}
+
+void SituareEngine::setAutoCentering(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
+
+    m_ui->setIndicatorButtonEnabled(enabled);
+    m_mapEngine->setAutoCentering(enabled);
+    m_ui->setOwnLocationCrosshairVisibility(!enabled);
+
+    if (enabled) {
+        setGPS(true);
+        m_gps->requestLastPosition();
+    }
+}
+
+void SituareEngine::setFirstStartZoomLevel()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    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)));
+    disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+               this, SLOT(setFirstStartZoomLevel()));
+}
+
+void SituareEngine::setGPS(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
+
+    if (m_gps->isInitialized()) {
+        m_ui->setGPSButtonEnabled(enabled);
+        m_mapEngine->setGPSEnabled(enabled);
+
+        if (enabled && !m_gps->isRunning()) {
+            m_gps->start();
+            m_gps->requestLastPosition();
+
+            if(m_ui->loginState())
+                m_ui->readAutomaticLocationUpdateSettings();
+        }
+        else if (!enabled && m_gps->isRunning()) {
+            m_gps->stop();
+            changeAutoCenteringSetting(false);
+            enableAutomaticLocationUpdate(false);
+        }
+    }
+    else {
+        if (enabled)
+            m_ui->buildInformationBox(tr("Unable to start GPS"));
+        m_ui->setGPSButtonEnabled(false);
+        m_mapEngine->setGPSEnabled(false);
+    }
 }
 
 void SituareEngine::signalsFromFacebookAuthenticator()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_facebookAuthenticator, SIGNAL(error(QString)),
-            this, SLOT(error(QString)));
+    connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
 
     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
@@ -378,9 +581,6 @@ void SituareEngine::signalsFromFacebookAuthenticator()
     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
             m_ui, SLOT(startLoginProcess()));
 
-    connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
-            m_ui, SLOT(loginFailed()));
-
     connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
             m_ui, SLOT(saveCookies()));
 
@@ -388,26 +588,34 @@ void SituareEngine::signalsFromFacebookAuthenticator()
             m_ui, SLOT(loginUsingCookies()));
 }
 
+void SituareEngine::signalsFromGeocodingService()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_geocodingService, SIGNAL(locationDataParsed(const QList<Location>&)),
+            m_ui, SIGNAL(locationDataParsed(const QList<Location>&)));
+}
+
 void SituareEngine::signalsFromGPS()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_gps, SIGNAL(position(QPointF,qreal)),
-            m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
+    connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+            m_mapEngine, SLOT(gpsPositionUpdate(GeoCoordinate, qreal)));
 
     connect(m_gps, SIGNAL(timeout()),
             m_ui, SLOT(gpsTimeout()));
 
-    connect(m_gps, SIGNAL(error(QString)),
-            this, SLOT(error(QString)));
-
-    connect(m_gps, SIGNAL(position(QPointF,qreal)),
-            this, SLOT(saveGPSPosition(QPointF)));
+    connect(m_gps, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
 }
 
 void SituareEngine::signalsFromMainWindow()
 {
-    qDebug() << __PRETTY_FUNCTION__;    
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_ui, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
 
     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
             this, SLOT(fetchUsernameFromSettings()));
@@ -422,8 +630,8 @@ void SituareEngine::signalsFromMainWindow()
             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
 
     // signals from map view
-    connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
-            m_mapEngine, SLOT(setLocation(QPoint)));
+    connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate)),
+            m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
 
     connect(m_ui, SIGNAL(mapViewResized(QSize)),
             m_mapEngine, SLOT(viewResized(QSize)));
@@ -439,11 +647,8 @@ 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(gpsTriggered(bool)),
-            this, SLOT(enableGPS(bool)));
+            this, SLOT(setGPS(bool)));
 
     //signals from dialogs
     connect(m_ui, SIGNAL(cancelLoginProcess()),
@@ -456,32 +661,45 @@ void SituareEngine::signalsFromMainWindow()
             this, SLOT(requestUpdateLocation(QString,bool)));
 
     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
-            this, SLOT(enableAutomaticLocationUpdate(bool, int)));    
+            this, SLOT(enableAutomaticLocationUpdate(bool, int)));
 
     // signals from user info tab
     connect(m_ui, SIGNAL(refreshUserData()),
             this, SLOT(refreshUserData()));
 
-    connect (m_ui, SIGNAL(notificateUpdateFailing(QString)),
-             this, SLOT(error(QString)));
+    connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
+            m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
+
 
-    connect(m_ui, SIGNAL(findUser(QPointF)),
-            m_mapEngine, SLOT(setViewLocation(QPointF)));
+    // signals from routing tab
+    connect(m_ui,
+            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
+            m_mapEngine,
+            SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
 
-    // signals from friend list tab
-    connect(m_ui, SIGNAL(findFriend(QPointF)),
-            m_mapEngine, SLOT(setViewLocation(QPointF)));
+    // signals from distence indicator button
+    connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
+            this, SLOT(changeAutoCenteringSetting(bool)));
+
+    connect(m_ui, SIGNAL(searchForLocation(QString)),
+            this, SLOT(locationSearch(QString)));
+
+    connect(m_ui, SIGNAL(draggingModeTriggered()),
+            this, SLOT(draggingModeTriggered()));
+
+    connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
+            this, SLOT(routeTo(const GeoCoordinate&)));
 }
 
 void SituareEngine::signalsFromMapEngine()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_mapEngine, SIGNAL(error(QString)),
-            this, SLOT(error(QString)));
+    connect(m_mapEngine, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
 
-    connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
-            m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
+    connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate)),
+            m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate)));
 
     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
             m_ui, SIGNAL(zoomLevelChanged(int)));
@@ -497,14 +715,37 @@ void SituareEngine::signalsFromMapEngine()
 
     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
+
+    connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
+            m_ui, SIGNAL(newMapResolution(qreal)));
+
+    connect(m_mapEngine, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
+            m_ui, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)));
+}
+
+void SituareEngine::signalsFromRoutingService()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            this, SLOT(routeParsed(Route&)));
+
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            m_mapEngine, SLOT(setRoute(Route&)));
+
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            m_ui, SIGNAL(routeParsed(Route&)));
 }
 
 void SituareEngine::signalsFromSituareService()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_situareService, SIGNAL(error(QString)),
-            this, SLOT(error(QString)));
+    connect(m_situareService, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
+
+    connect(m_situareService, SIGNAL(imageReady(User*)),
+            this, SLOT(imageReady(User*)));
 
     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
             m_ui, SIGNAL(reverseGeoReady(QString)));
@@ -516,17 +757,25 @@ void SituareEngine::signalsFromSituareService()
             this, SLOT(updateWasSuccessful()));
 
     connect(m_situareService, SIGNAL(updateWasSuccessful()),
-            m_ui, SIGNAL(updateWasSuccessful()));
+            m_ui, SIGNAL(clearUpdateLocationDialogData()));
+}
 
-    connect(m_situareService, SIGNAL(error(QString)),
-            m_ui, SIGNAL(messageSendingFailed(QString)));
+void SituareEngine::startAutomaticUpdate()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_gps->requestUpdate();
+    m_automaticUpdateRequest = true;
 }
 
 void SituareEngine::updateWasSuccessful()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_situareService->fetchLocations();
+    if (m_networkAccessManager->isConnected())
+        m_situareService->fetchLocations();
+    else
+        error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
 }
 
 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
@@ -534,6 +783,7 @@ void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_ui->toggleProgressIndicator(false);
+    m_ui->showPanels();
 
     emit userLocationReady(user);
     emit friendsLocationsReady(friendsList);