Parsing the access token from the url
[situare] / src / facebookservice / facebookauthentication.cpp
index 716b054..398e55d 100644 (file)
@@ -21,6 +21,8 @@
    USA.
 */
 
+#include "parser.h"
+
 #include <QtDebug>
 #include <QDateTime>
 #include <QSettings>
 #include <QMaemo5InformationBox>
 #endif // Q_WS_MAEMO_5
 
-#include "facebookauthentication.h"
-#include "facebookcommon.h"
 #include "common.h"
-#include "parser.h"
+#include "error.h"
+#include "facebookcommon.h"
+
+#include "facebookauthentication.h"
+
+const QString REDIRECT_URI = "http://www.facebook.com/connect/login_success.html";
 
 FacebookAuthentication::FacebookAuthentication(QObject *parent)
     : QObject(parent),
@@ -62,6 +67,13 @@ void FacebookAuthentication::clearAccountInformation(bool keepUsername)
     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
 }
 
+void FacebookAuthentication::loadFinished(bool ok)
+{
+    qWarning() << __PRETTY_FUNCTION__ << ok;
+
+    ///< @todo show browsed window if url != redirect url
+}
+
 const QString FacebookAuthentication::loadUsername()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -76,6 +88,24 @@ FacebookCredentials FacebookAuthentication::loginCredentials() const
     return m_loginCredentials;
 }
 
+QString FacebookAuthentication::parseAccessToken(const QUrl &url)
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    const QString ACCESS_TOKEN_PARAMETER("#access_token=");
+    const QString EXPIRATION_PARAMETER("&expires_in=");
+
+    QString urlString = url.toString();
+
+    int begin = urlString.indexOf(ACCESS_TOKEN_PARAMETER) + ACCESS_TOKEN_PARAMETER.length();
+    int end = urlString.indexOf(EXPIRATION_PARAMETER);
+
+    if ((begin > -1) && (end > begin))
+        return urlString.mid(begin, end - begin);
+    else
+        return QString();
+}
+
 void FacebookAuthentication::saveUsername(const QString &username)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -131,7 +161,7 @@ bool FacebookAuthentication::updateCredentials(const QUrl &url)
                         QVariantMap result = parser.parse (jsonString, &ok).toMap();
 
                         if (!ok) {
-                            emit error(SituareError::INVALID_JSON);
+                            emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
                             found = false;
                         } else {
                             qDebug() << "Session Key" << result[SESSION_KEY].toString();
@@ -161,29 +191,40 @@ bool FacebookAuthentication::updateCredentials(const QUrl &url)
                             emit credentialsReady(m_loginCredentials);
                         }
                     }
-                }       
-            }   
+                }
+            }
         } else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0) {
             qDebug() << "login failure";
             qDebug() << callbackUrl;
             clearAccountInformation(true);
             if(m_freshLogin) {
-                emit error(SituareError::LOGIN_FAILED);
+                emit error(ErrorContext::SITUARE, SituareError::LOGIN_FAILED);
             } else {
                 m_freshLogin = true;
-                emit error(SituareError::SESSION_EXPIRED);
+                emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
             }
         } else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
             qDebug() << "correct loginPage";
         } else {
             qDebug() << "totally wrong webPage";
             // we should not get a wrong page at this point
-            emit error(SituareError::LOGIN_FAILED);
+            emit error(ErrorContext::SITUARE, SituareError::LOGIN_FAILED);
         }
     } else {
         qDebug() << " Loading of page failed invalid URL" << endl;
         // we should not get a wrong page at this point
-        emit error(SituareError::LOGIN_FAILED);
+        emit error(ErrorContext::SITUARE, SituareError::LOGIN_FAILED);
     }
     return found;
 }
+
+void FacebookAuthentication::urlChanged(const QUrl &url)
+{
+    qWarning() << __PRETTY_FUNCTION__ << url.toString();
+
+    // if login succeeded
+    if (url.toString().startsWith(REDIRECT_URI)) {
+        ///< @todo hide browser dialog
+        qWarning() << __PRETTY_FUNCTION__ << "access_token:" << parseAccessToken(url);
+    }
+}