Merge branch 'master' into contact_dialog
[situare] / src / engine / engine.cpp
index 39f0650..d94ebfd 100644 (file)
 #include <QMessageBox>
 #include <QNetworkReply>
 
-#ifdef Q_WS_MAEMO_5
 #include "application.h"
-#endif
-
 #include "common.h"
+#include "contactmanager.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"
@@ -63,13 +63,11 @@ SituareEngine::SituareEngine()
     m_ui = new MainWindow;
     m_ui->updateItemVisibility();
 
-#ifdef Q_WS_MAEMO_5
-    m_app = static_cast<Application *>(qApp);
-    m_app->registerWindow(m_ui->winId());
+    Application *application = static_cast<Application *>(qApp);
+    application->registerWindow(m_ui->winId());
 
-    connect(m_app, SIGNAL(topmostChanged(bool)),
-            this, SLOT(enablePowerSave(bool)));
-#endif
+    connect(application, SIGNAL(topmostWindowChanged(bool)),
+            this, SLOT(topmostWindowChanged(bool)));
 
     m_networkAccessManager = new NetworkAccessManager(this);
 
@@ -89,8 +87,12 @@ SituareEngine::SituareEngine()
     // 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();
@@ -130,7 +132,10 @@ SituareEngine::SituareEngine()
     initializeGpsAndAutocentering();
 
     m_mce = new MCE(this);
-    connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(enablePowerSave(bool)));
+    connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(setPowerSaving(bool)));
+    
+    m_contactManager = new ContactManager(this);
+    m_contactManager->requestContactGuids();
 }
 
 SituareEngine::~SituareEngine()
@@ -146,10 +151,10 @@ SituareEngine::~SituareEngine()
 
 void SituareEngine::changeAutoCenteringSetting(bool enabled)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
 
     m_autoCenteringEnabled = enabled;
-    enableAutoCentering(enabled);
+    setAutoCentering(enabled);
 }
 
 void SituareEngine::disableAutoCentering()
@@ -159,47 +164,12 @@ void SituareEngine::disableAutoCentering()
     changeAutoCenteringSetting(false);
 }
 
-void SituareEngine::enableAutoCentering(bool enabled)
+void SituareEngine::draggingModeTriggered()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_ui->setIndicatorButtonEnabled(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);
-    }
+    if (m_mce)
+        m_mce->vibrationFeedback();
 }
 
 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
@@ -234,16 +204,6 @@ void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateInterv
     }
 }
 
-void SituareEngine::enablePowerSave(bool enabled)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_gps->enablePowerSave(enabled);
-
-    if(m_autoCenteringEnabled)
-        m_mapEngine->setAutoCentering(!enabled);
-}
-
 void SituareEngine::error(const int context, const int error)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -309,8 +269,6 @@ void SituareEngine::error(const int context, const int error)
         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;
@@ -321,7 +279,7 @@ void SituareEngine::error(const int context, const int error)
         m_ui->buildInformationBox(tr("Map image download failed"), true);
         break;
     case SituareError::GPS_INITIALIZATION_FAILED:
-        enableGPS(false);
+        setGPS(false);
         m_ui->buildInformationBox(tr("GPS initialization failed"), true);
         break;
     case SituareError::INVALID_JSON:
@@ -329,17 +287,19 @@ void SituareEngine::error(const int context, const int error)
         m_ui->loggedIn(false);
         m_facebookAuthenticator->clearAccountInformation(false); // clean all
         break;
-    case SituareError::ERROR_GEOLOCATION_SERVER_UNAVAILABLE:
+    case SituareError::ERROR_ROUTING_FAILED:
         m_ui->toggleProgressIndicator(false);
-        m_ui->buildInformationBox(tr("Address server not responding"), true);
+        m_ui->buildInformationBox(tr("Routing failed"), true);
+        break;
+    case SituareError::ERROR_LOCATION_SEARCH_FAILED:
+        m_ui->buildInformationBox(tr("No results found"), true);
         break;
     default:
         m_ui->toggleProgressIndicator(false);
         if(context == ErrorContext::NETWORK)
-            qCritical() << "QNetworkReply::NetworkError: " << error;
+            qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
         else
-            qCritical() << "Unknown error: " << error;
-
+            qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
         break;
     }
 }
@@ -377,35 +337,28 @@ void SituareEngine::initializeGpsAndAutocentering()
                     this, SLOT(setFirstStartZoomLevel()));
 
             changeAutoCenteringSetting(true);
-            enableGPS(true);
+            setGPS(true);
 
             m_ui->buildInformationBox(tr("GPS enabled"));
 
         } else { // Normal start
             changeAutoCenteringSetting(autoCenteringEnabled.toBool());
-            enableGPS(gpsEnabled.toBool());
+            setGPS(gpsEnabled.toBool());
 
             if (gpsEnabled.toBool())
                 m_ui->buildInformationBox(tr("GPS enabled"));
         }
     } else {
-        enableGPS(false);
+        setGPS(false);
     }
 }
 
-void SituareEngine::locationDataReady(QList<Location> &result)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    Q_UNUSED(result) /// @todo remove Q_UNUSED
-}
-
 void SituareEngine::locationSearch(QString location)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     if(!location.isEmpty())
-        m_routingService->requestLocation(location);
+        m_geocodingService->requestLocation(location);
 }
 
 void SituareEngine::loginActionPressed()
@@ -526,6 +479,48 @@ void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
     }
 }
 
+void SituareEngine::routeParsed(Route &route)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    Q_UNUSED(route);
+
+    m_ui->toggleProgressIndicator(false);
+}
+
+void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->toggleProgressIndicator(true);
+
+    if (m_gps->isRunning())
+        m_routingService->requestRoute(m_gps->lastPosition(), endPointCoordinates);
+    else
+        m_routingService->requestRoute(m_mapEngine->centerGeoCoordinate(), endPointCoordinates);
+}
+
+void SituareEngine::routeToCursor()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    routeTo(m_mapEngine->centerGeoCoordinate());
+}
+
+void SituareEngine::setAutoCentering(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
+
+    m_ui->setIndicatorButtonEnabled(enabled);
+    m_mapEngine->setAutoCentering(enabled);
+    m_ui->setCrosshairVisibility(!enabled);
+
+    if (enabled) {
+        setGPS(true);
+        m_gps->requestLastPosition();
+    }
+}
+
 void SituareEngine::setFirstStartZoomLevel()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -537,6 +532,58 @@ void SituareEngine::setFirstStartZoomLevel()
                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::setPowerSaving(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
+
+    m_gps->enablePowerSave(enabled);
+
+    if(m_autoCenteringEnabled)
+        m_mapEngine->setAutoCentering(!enabled);
+}
+
+void SituareEngine::showContactDialog(const QString &facebookId)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QString guid = m_contactManager->contactGuid(facebookId);
+
+    if (!guid.isEmpty())
+        m_ui->showContactDialog(guid);
+    else
+        m_ui->buildInformationBox(tr("Unable to find contact.\nYou must have Facebook "
+                                     "chat account to be able to open contact dialog."), true);
+}
+
 void SituareEngine::signalsFromFacebookAuthenticator()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -560,6 +607,17 @@ 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>&)));
+
+    connect(m_geocodingService, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
+}
+
 void SituareEngine::signalsFromGPS()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -612,7 +670,7 @@ void SituareEngine::signalsFromMainWindow()
 
     // signals from menu buttons
     connect(m_ui, SIGNAL(gpsTriggered(bool)),
-            this, SLOT(enableGPS(bool)));
+            this, SLOT(setGPS(bool)));
 
     //signals from dialogs
     connect(m_ui, SIGNAL(cancelLoginProcess()),
@@ -631,22 +689,43 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(refreshUserData()),
             this, SLOT(refreshUserData()));
 
-    connect(m_ui, SIGNAL(findUser(GeoCoordinate)),
+    connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
             m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
 
-    // signals from friend list tab
-    connect(m_ui, SIGNAL(findFriend(GeoCoordinate)),
-            m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
+    // routing signal from friend list tab & search location tab
+    connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
+            this, SLOT(routeTo(const GeoCoordinate&)));
+
+    // signals from location search panel
+    connect(m_ui,
+            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
+            m_mapEngine,
+            SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
+
+    connect(m_ui, SIGNAL(searchHistoryItemClicked(QString)),
+            this, SLOT(locationSearch(QString)));
+
+    // signals from routing tab
+    connect(m_ui, SIGNAL(clearRoute()),
+            m_mapEngine, SLOT(clearRoute()));
 
-    connect(m_ui, SIGNAL(locationItemClicked(GeoCoordinate&,GeoCoordinate&)),
-            m_mapEngine, SLOT(locationItemClicked(GeoCoordinate&,GeoCoordinate&)));
+    connect(m_ui, SIGNAL(routeToCursor()),
+            this, SLOT(routeToCursor()));
 
-    // signals from distence indicator button
+    // signals from distance indicator button
     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
             this, SLOT(changeAutoCenteringSetting(bool)));
 
+    connect(m_ui, SIGNAL(draggingModeTriggered()),
+            this, SLOT(draggingModeTriggered()));
+
+    // signal from search location dialog
     connect(m_ui, SIGNAL(searchForLocation(QString)),
             this, SLOT(locationSearch(QString)));
+
+    // signal from friend list panel
+    connect(m_ui, SIGNAL(requestContactDialog(const QString &)),
+            this, SLOT(showContactDialog(const QString &)));
 }
 
 void SituareEngine::signalsFromMapEngine()
@@ -676,20 +755,26 @@ void SituareEngine::signalsFromMapEngine()
 
     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(locationDataParsed(QList<Location>&)),
-            this, SLOT(locationDataReady(QList<Location>&)));
+    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(locationDataParsed(QList<Location>&)),
-            m_ui, SIGNAL(locationDataParsed(QList<Location>&)));
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            m_ui, SIGNAL(routeParsed(Route&)));
+
+    connect(m_routingService, SIGNAL(error(int, int)),
+            this, SLOT(error(int, int)));
 }
 
 void SituareEngine::signalsFromSituareService()
@@ -723,6 +808,13 @@ void SituareEngine::startAutomaticUpdate()
     m_automaticUpdateRequest = true;
 }
 
+void SituareEngine::topmostWindowChanged(bool isMainWindow)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setPowerSaving(!isMainWindow);
+}
+
 void SituareEngine::updateWasSuccessful()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -738,9 +830,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);
 }
-