Merge branch 'master' into situare_interact
[situare] / src / engine / engine.cpp
index 666515f..de6692a 100644 (file)
@@ -61,7 +61,7 @@ SituareEngine::SituareEngine()
     qDebug() << __PRETTY_FUNCTION__;
 
     m_ui = new MainWindow;
-    m_ui->updateItemVisibility();
+    m_ui->updateItemVisibility(false);
 
     Application *application = static_cast<Application *>(qApp);
     application->registerWindow(m_ui->winId());
@@ -83,7 +83,7 @@ SituareEngine::SituareEngine()
                                           new ImageFetcher(m_networkAccessManager, this), this);
 
     // build FacebookAuthenticator
-    m_facebookAuthenticator = new FacebookAuthentication(this);
+    m_facebookAuthenticator = new FacebookAuthentication(m_ui, this);
 
     // build routing service
     m_routingService = new RoutingService(this); // create this when needed, not in constructor!
@@ -130,16 +130,16 @@ SituareEngine::SituareEngine()
     m_mapEngine->init();
     m_ui->show();
 
-    m_facebookAuthenticator->start();
-
     m_gps->setMode(GPSPosition::Default);
     initializeGpsAndAutocentering();
 
     m_mce = new MCE(this);
     connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(setPowerSaving(bool)));
-    
+
     m_contactManager = new ContactManager(this);
     m_contactManager->requestContactGuids();
+
+    m_facebookAuthenticator->login();
 }
 
 SituareEngine::~SituareEngine()
@@ -148,7 +148,7 @@ SituareEngine::~SituareEngine()
 
     delete m_ui;
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
 }
@@ -216,12 +216,12 @@ void SituareEngine::error(const int context, const int error)
     {
     case SituareError::ERROR_GENERAL:
         if(context == ErrorContext::SITUARE) {
-            //m_ui->toggleProgressIndicator(false);
+            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);
+        m_ui->toggleProgressIndicator(false);
         if(context == ErrorContext::SITUARE) {
             m_ui->buildInformationBox(tr("Missing parameter from request"), true);
         } else if(context == ErrorContext::NETWORK) {
@@ -230,50 +230,43 @@ void SituareEngine::error(const int context, const int error)
         break;
     case QNetworkReply::RemoteHostClosedError:
         if(context == ErrorContext::NETWORK) {
-            //m_ui->toggleProgressIndicator(false);
+            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->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->toggleProgressIndicator(false);
             m_ui->buildInformationBox(tr("Connection timed out"), true);
         }
         break;
     case QNetworkReply::UnknownNetworkError:
         if(context == ErrorContext::NETWORK) {
-            //m_ui->toggleProgressIndicator(false);
+            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();
+        m_facebookAuthenticator->logOut();
+        m_facebookAuthenticator->login();
         break;
     case SituareError::UPDATE_FAILED:
-        //m_ui->toggleProgressIndicator(false);
+        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->toggleProgressIndicator(false);
         m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
         break;
     case SituareError::ADDRESS_RETRIEVAL_FAILED:
-        //m_ui->toggleProgressIndicator(false);
+        m_ui->toggleProgressIndicator(false);
         m_ui->buildInformationBox(tr("Address retrieval failed"), true);
         break;
     case SituareError::IMAGE_DOWNLOAD_FAILED:
@@ -292,14 +285,14 @@ void SituareEngine::error(const int context, const int error)
         m_facebookAuthenticator->clearAccountInformation(false); // clean all
         break;
     case SituareError::ERROR_ROUTING_FAILED:
-        //m_ui->toggleProgressIndicator(false);
+        m_ui->toggleProgressIndicator(false);
         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);
+        m_ui->toggleProgressIndicator(false);
         if(context == ErrorContext::NETWORK)
             qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
         else
@@ -308,18 +301,11 @@ void SituareEngine::error(const int context, const int error)
     }
 }
 
-void SituareEngine::fetchUsernameFromSettings()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ui->setUsername(m_facebookAuthenticator->loadUsername());
-}
-
 void SituareEngine::imageReady(const QString &id, const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_facebookAuthenticator->loginCredentials().userID() == id)
+    if (id.isNull())
         emit userImageReady(id, image);
     else
         emit friendImageReady(id, image);
@@ -329,7 +315,7 @@ void SituareEngine::initializeGpsAndAutocentering()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
 
@@ -369,52 +355,32 @@ void SituareEngine::loginActionPressed()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (m_networkAccessManager->isConnected()) {
-        if(m_ui->loginState()) {
-            logout();
-            m_situareService->clearUserData();
-        } else {
-            m_facebookAuthenticator->start();
-        }
-    }
-    else {
+    if (m_facebookAuthenticator->isLoggedIn())
+        m_facebookAuthenticator->logOut(true);
+    else if (m_networkAccessManager->isConnected())
+        m_facebookAuthenticator->login();
+    else
         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
-    }
 }
 
-void SituareEngine::loginOk()
+void SituareEngine::onLogin()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     m_ui->loggedIn(true);
 
-    m_ui->show();
-    m_situareService->fetchLocations(); // request user locations
+    m_situareService->fetchLocations();
 
     if (m_gps->isRunning())
         m_ui->readAutomaticLocationUpdateSettings();
 }
 
-void SituareEngine::loginProcessCancelled()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ui->toggleProgressIndicator(false);
-    m_ui->updateItemVisibility();
-}
-
-void SituareEngine::logout()
+void SituareEngine::onLogout()
 {
     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_situareService->updateSession(""); // empty session string means logged out
     m_automaticUpdateFirstStart = true;
 }
 
@@ -504,7 +470,6 @@ void SituareEngine::requestSendMessage(const QString &receiverId, const QString
         m_situareService->sendMessage(receiverId, message);
 }
 
-
 void SituareEngine::routeParsed(Route &route)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -566,7 +531,7 @@ void SituareEngine::setGPS(bool enabled)
             m_gps->start();
             m_gps->requestLastPosition();
 
-            if(m_ui->loginState())
+            if(m_facebookAuthenticator->isLoggedIn())
                 m_ui->readAutomaticLocationUpdateSettings();
         }
         else if (!enabled && m_gps->isRunning()) {
@@ -630,20 +595,13 @@ void SituareEngine::signalsFromFacebookAuthenticator()
     connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
             this, SLOT(error(int, 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(loggedIn(QString, bool)),
+            m_situareService, SLOT(updateSession(QString)));
 
-    connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
-            m_ui, SLOT(saveCookies()));
+    connect(m_facebookAuthenticator, SIGNAL(loggedIn(QString, bool)),
+            this, SLOT(onLogin()));
 
-    connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
-            m_ui, SLOT(loginUsingCookies()));
+    connect(m_facebookAuthenticator, SIGNAL(loggedOut()), this, SLOT(onLogout()));
 }
 
 void SituareEngine::signalsFromGeocodingService()
@@ -678,18 +636,9 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(error(int, int)),
             this, SLOT(error(int, 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(SceneCoordinate)),
             m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
@@ -711,17 +660,13 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(gpsTriggered(bool)),
             this, SLOT(setGPS(bool)));
 
-    //signals from dialogs
-    connect(m_ui, SIGNAL(cancelLoginProcess()),
-            this, SLOT(loginProcessCancelled()));
-
     connect(m_ui, SIGNAL(requestReverseGeo()),
             this, SLOT(requestAddress()));
 
-    connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
+    connect(m_ui, SIGNAL(locationUpdate(QString,bool)),
             this, SLOT(setProgressIndicatorEnabled()));
 
-    connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
+    connect(m_ui, SIGNAL(locationUpdate(QString,bool)),
             this, SLOT(requestUpdateLocation(QString,bool)));
 
     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),