Clear user information only when logout initiated from logout button.
[situare] / src / facebookservice / facebookauthentication.cpp
index ca8ac04..431c412 100644 (file)
@@ -36,7 +36,7 @@
 #endif // Q_WS_MAEMO_5
 
 #include "common.h"
-#include "error.h"
+#include "../error.h"
 #include "network/networkcookiejar.h"
 #include "situareservice/situarecommon.h"
 #include "ui/mainwindow.h"
@@ -48,6 +48,7 @@ const QString FB_LOGIN_URL = "https://www.facebook.com/login.php";
 
 FacebookAuthentication::FacebookAuthentication(MainWindow *mainWindow, QObject *parent)
     : QObject(parent),
+      m_loggedIn(false),
       m_browser(0),
       m_mainWindow(mainWindow)
 {
@@ -58,32 +59,39 @@ void FacebookAuthentication::browserDestroyed()
 {
     qWarning() << __PRETTY_FUNCTION__;
 
+    m_mainWindow->toggleProgressIndicator(false);
     m_browser = 0;
 }
 
-void FacebookAuthentication::clearAccountInformation(bool keepUsername)
+void FacebookAuthentication::clearAccountInformation(bool clearUserInformation)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__ << "clearUserInformation:" << clearUserInformation;
 
-    ///< @todo (HIGH) clear session from SituareService
     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
 
-    if(!keepUsername) {
+    settings.remove(USER_UNSEND_MESSAGE);
+    settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
+
+    if (clearUserInformation) {
+        NetworkCookieJar::clearCookiesSetting();
         settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
         settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
     }
+}
 
-    settings.remove(USER_UNSEND_MESSAGE);
-    settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
+void FacebookAuthentication::destroyLogin()
+{
+    qWarning() << __PRETTY_FUNCTION__;
 
-    NetworkCookieJar::clearCookiesSetting();
+    m_mainWindow->destroyLoginDialog();
+    m_browser->deleteLater();
 }
 
-void FacebookAuthentication::loadFinished(bool ok)
+bool FacebookAuthentication::isLoggedIn() const
 {
-    qWarning() << __PRETTY_FUNCTION__ << ok;
+    qWarning() << __PRETTY_FUNCTION__;
 
-    ///< @todo show browsed window if url != redirect url
+    return m_loggedIn;
 }
 
 void FacebookAuthentication::login()
@@ -96,15 +104,16 @@ void FacebookAuthentication::login()
         if (m_browser) {
             m_browser->page()->networkAccessManager()->setCookieJar(new NetworkCookieJar());
 
-            connect(m_browser, SIGNAL(loadFinished(bool)),
-                    this, SLOT(loadFinished(bool)));
-
             connect(m_browser, SIGNAL(urlChanged(QUrl)),
                     this, SLOT(urlChanged(QUrl)));
 
             connect(m_browser, SIGNAL(destroyed(QObject*)),
                     this, SLOT(browserDestroyed()));
 
+            connect(m_browser->page()->networkAccessManager(),
+                    SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)),
+                    this, SLOT(sslErrors(QNetworkReply*, QList<QSslError>)));
+
             connect(m_browser->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
                     this, SLOT(networkReplyHandler(QNetworkReply*)));
         }
@@ -122,16 +131,30 @@ void FacebookAuthentication::login()
         url.append("req_perms=publish_stream");
 
         m_browser->load(QUrl(url));
+
+        m_mainWindow->toggleProgressIndicator(true);
     }
 }
 
+void FacebookAuthentication::logOut(bool clearUserInformation)
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    clearAccountInformation(clearUserInformation);
+    m_loggedIn = false;
+    emit loggedOut();
+}
+
 void FacebookAuthentication::networkReplyHandler(QNetworkReply *reply)
 {
     qWarning() <<__PRETTY_FUNCTION__;
 
-    if (reply->error() != QNetworkReply::NoError) {
+    if ((reply->error() != QNetworkReply::NoError)
+        && (reply->error() != QNetworkReply::OperationCanceledError)) {
+
         qCritical() << __PRETTY_FUNCTION__ << "error:" << reply->error() << reply->errorString();
-        /// @todo Emit error signal
+        emit error(ErrorContext::NETWORK, reply->error());
+        destroyLogin();
     }
 }
 
@@ -153,6 +176,14 @@ QString FacebookAuthentication::parseSession(const QUrl &url)
         return QString();
 }
 
+void FacebookAuthentication::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    Q_UNUSED(errors);
+    reply->ignoreSslErrors();
+}
+
 void FacebookAuthentication::urlChanged(const QUrl &url)
 {
     qWarning() << __PRETTY_FUNCTION__ << url.toString();
@@ -176,8 +207,8 @@ void FacebookAuthentication::urlChanged(const QUrl &url)
         const QString session = parseSession(url);
         qWarning() << __PRETTY_FUNCTION__ << "login finished, parsed session:" << session;
         if (!session.isEmpty()) {
-            m_mainWindow->destroyLoginDialog();
-            m_browser->deleteLater();
+            destroyLogin();
+            m_loggedIn = true;
             emit loggedIn(session);
         }
     }