Added unit tests.
[situare] / src / engine / engine.cpp
index 3df27a3..d4064c7 100644 (file)
@@ -29,7 +29,8 @@
 
 #include "application.h"
 #include "common.h"
-#include "error.h"
+#include "contactmanager.h"
+#include "../error.h"
 #include "facebookservice/facebookauthentication.h"
 #include "gps/gpsposition.h"
 #include "map/mapengine.h"
@@ -78,7 +79,8 @@ SituareEngine::SituareEngine()
     m_gps = new GPSPosition(this);
 
     // build SituareService
-    m_situareService = new SituareService(this);
+    m_situareService = new SituareService(m_networkAccessManager,
+                                          new ImageFetcher(m_networkAccessManager, this), this);
 
     // build FacebookAuthenticator
     m_facebookAuthenticator = new FacebookAuthentication(this);
@@ -110,11 +112,14 @@ SituareEngine::SituareEngine()
     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(userImageReady(QString,QPixmap)),
+            m_ui, SIGNAL(userImageReady(QString,QPixmap)));
 
-    connect(this, SIGNAL(friendImageReady(User*)),
-            m_mapEngine, SIGNAL(friendImageReady(User*)));
+    connect(this, SIGNAL(friendImageReady(QString,QPixmap)),
+            m_ui, SIGNAL(friendImageReady(QString,QPixmap)));
+
+    connect(this, SIGNAL(friendImageReady(QString,QPixmap)),
+            m_mapEngine, SIGNAL(friendImageReady(QString,QPixmap)));
 
     m_automaticUpdateIntervalTimer = new QTimer(this);
     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
@@ -132,6 +137,9 @@ SituareEngine::SituareEngine()
 
     m_mce = new MCE(this);
     connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(setPowerSaving(bool)));
+    
+    m_contactManager = new ContactManager(this);
+    m_contactManager->requestContactGuids();
 }
 
 SituareEngine::~SituareEngine()
@@ -265,8 +273,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;
@@ -285,9 +291,12 @@ 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);
@@ -295,7 +304,6 @@ void SituareEngine::error(const int context, const int error)
             qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
         else
             qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
-
         break;
     }
 }
@@ -307,14 +315,14 @@ void SituareEngine::fetchUsernameFromSettings()
     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
 }
 
-void SituareEngine::imageReady(User *user)
+void SituareEngine::imageReady(const QString &id, const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(user->type())
-        emit userLocationReady(user);
+    if(m_facebookAuthenticator->loginCredentials().userID() == id)
+        emit userImageReady(id, image);
     else
-        emit friendImageReady(user);
+        emit friendImageReady(id, image);
 }
 
 void SituareEngine::initializeGpsAndAutocentering()
@@ -475,6 +483,30 @@ void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
     }
 }
 
+void SituareEngine::requestInterestingPeople()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QRectF currentSceneRect = m_mapEngine->currentViewSceneRect();
+    SceneCoordinate bottomLeftSceneCoordinate(currentSceneRect.left(), currentSceneRect.bottom());
+    SceneCoordinate topRightSceneCoordinate(currentSceneRect.right(), currentSceneRect.top());
+
+    m_situareService->fetchPeopleWithSimilarInterest(GeoCoordinate(bottomLeftSceneCoordinate),
+                                                     GeoCoordinate(topRightSceneCoordinate));
+}
+
+void SituareEngine::requestSendMessage(const QString &receiverId, const QString &message,
+                                bool addCoordinates)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (addCoordinates)
+        m_situareService->sendMessage(receiverId, message, m_mapEngine->centerGeoCoordinate());
+    else
+        m_situareService->sendMessage(receiverId, message);
+}
+
+
 void SituareEngine::routeParsed(Route &route)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -496,6 +528,13 @@ void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
         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;
@@ -560,6 +599,26 @@ void SituareEngine::setPowerSaving(bool enabled)
         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.\nAdd Facebook IM "
+                                     "account from Conversations to use this feature."), true);
+}
+
+void SituareEngine::showMessageDialog(const QPair<QString, QString> &receiver)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->showMessageDialog(receiver);
+}
+
 void SituareEngine::signalsFromFacebookAuthenticator()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -589,6 +648,9 @@ void SituareEngine::signalsFromGeocodingService()
 
     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()
@@ -625,8 +687,8 @@ void SituareEngine::signalsFromMainWindow()
             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
 
     // signals from map view
-    connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate, bool)),
-            m_mapEngine, SLOT(setCenterPosition(SceneCoordinate, bool)));
+    connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate)),
+            m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
 
     connect(m_ui, SIGNAL(mapViewResized(QSize)),
             m_mapEngine, SLOT(viewResized(QSize)));
@@ -658,6 +720,12 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
             this, SLOT(enableAutomaticLocationUpdate(bool, int)));
 
+    connect(m_ui, SIGNAL(addTags(QStringList)),
+            m_situareService, SLOT(addTags(QStringList)));
+
+    connect(m_ui, SIGNAL(removeTags(QStringList)),
+            m_situareService, SLOT(removeTags(QStringList)));
+
     // signals from user info tab
     connect(m_ui, SIGNAL(refreshUserData()),
             this, SLOT(refreshUserData()));
@@ -665,24 +733,62 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
             m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
 
-    // signals from routing tab
+    connect(m_ui, SIGNAL(requestPopularTags()),
+            m_situareService, SLOT(fetchPopularTags()));
+
+    // routing signal from friend list tab & search location tab
+    connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
+            this, SLOT(routeTo(const GeoCoordinate&)));
+
+    connect(m_ui, SIGNAL(requestContactDialog(const QString &)),
+            this, SLOT(showContactDialog(const QString &)));
+
+    // signals from location search panel
     connect(m_ui,
             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
             m_mapEngine,
             SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
 
-    // signals from distence indicator button
+    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(routeToCursor()),
+            this, SLOT(routeToCursor()));
+
+    // 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 search dialogs
     connect(m_ui, SIGNAL(searchForLocation(QString)),
             this, SLOT(locationSearch(QString)));
 
-    connect(m_ui, SIGNAL(draggingModeTriggered()),
-            this, SLOT(draggingModeTriggered()));
+    connect(m_ui, SIGNAL(requestSearchPeopleByTag(QString)),
+            m_situareService, SLOT(searchPeopleByTag(QString)));
 
-    connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
-            this, SLOT(routeTo(const GeoCoordinate&)));
+    // signals from meet people panel
+    connect(m_ui, SIGNAL(requestInterestingPeople()),
+            this, SLOT(requestInterestingPeople()));
+
+    connect(m_ui, SIGNAL(requestMessageDialog(QPair<QString, QString>)),
+            this, SLOT(showMessageDialog(QPair<QString, QString>)));
+
+    connect(m_ui, SIGNAL(sendMessage(QString,QString,bool)),
+            this, SLOT(requestSendMessage(QString,QString,bool)));
+
+    // signals from message panel
+    connect(m_ui, SIGNAL(requestMessages()),
+            m_situareService, SLOT(fetchMessages()));
+
+    connect(m_ui, SIGNAL(requestRemoveMessage(QString)),
+            m_situareService, SLOT(removeMessage(QString)));
 }
 
 void SituareEngine::signalsFromMapEngine()
@@ -692,8 +798,8 @@ void SituareEngine::signalsFromMapEngine()
     connect(m_mapEngine, SIGNAL(error(int, int)),
             this, SLOT(error(int, int)));
 
-    connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate, bool)),
-            m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate, bool)));
+    connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate)),
+            m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate)));
 
     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
             m_ui, SIGNAL(zoomLevelChanged(int)));
@@ -729,6 +835,9 @@ void SituareEngine::signalsFromRoutingService()
 
     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()
@@ -738,8 +847,8 @@ void SituareEngine::signalsFromSituareService()
     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(imageReady(QString,QPixmap)),
+            this, SLOT(imageReady(QString,QPixmap)));
 
     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
             m_ui, SIGNAL(reverseGeoReady(QString)));
@@ -747,11 +856,20 @@ void SituareEngine::signalsFromSituareService()
     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
             this, SLOT(userDataChanged(User*, QList<User*>&)));
 
-    connect(m_situareService, SIGNAL(updateWasSuccessful()),
-            this, SLOT(updateWasSuccessful()));
+    connect(m_situareService, SIGNAL(updateWasSuccessful(SituareService::SuccessfulMethod)),
+            this, SLOT(updateWasSuccessful(SituareService::SuccessfulMethod)));
 
-    connect(m_situareService, SIGNAL(updateWasSuccessful()),
+    connect(m_situareService, SIGNAL(updateWasSuccessful(SituareService::SuccessfulMethod)),
             m_ui, SIGNAL(clearUpdateLocationDialogData()));
+
+    connect(m_situareService, SIGNAL(interestingPeopleReceived(QList<User>&,QList<User>&)),
+            m_ui, SIGNAL(interestingPeopleReceived(QList<User>&,QList<User>&)));
+
+    connect(m_situareService, SIGNAL(messagesReceived(QList<Message>&, QList<Message> &)),
+            m_ui, SIGNAL(messagesReceived(QList<Message>&, QList<Message>&)));
+
+    connect(m_situareService, SIGNAL(popularTagsReceived(QHash<QString,QString>&)),
+            m_ui, SIGNAL(popularTagsReceived(QHash<QString,QString>&)));
 }
 
 void SituareEngine::startAutomaticUpdate()
@@ -769,14 +887,22 @@ void SituareEngine::topmostWindowChanged(bool isMainWindow)
     setPowerSaving(!isMainWindow);
 }
 
-void SituareEngine::updateWasSuccessful()
+void SituareEngine::updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__;
 
-    if (m_networkAccessManager->isConnected())
-        m_situareService->fetchLocations();
-    else
+    if (m_networkAccessManager->isConnected()) {
+        if (successfulMethod == SituareService::SuccessfulUpdateLocation)
+            m_situareService->fetchLocations();
+        else  if ((successfulMethod == SituareService::SuccessfulRemoveMessage) ||
+                  (successfulMethod == SituareService::SuccessfulSendMessage))
+            m_situareService->fetchMessages();
+        else if ((successfulMethod == SituareService::SuccessfulAddTags) ||
+                 (successfulMethod == SituareService::SuccessfulRemoveTags))
+            m_situareService->fetchLocations();
+    } else {
         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+    }
 }
 
 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
@@ -784,7 +910,6 @@ void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_ui->toggleProgressIndicator(false);
-    m_ui->showPanels();
 
     emit userLocationReady(user);
     emit friendsLocationsReady(friendsList);