Changes to logout prosess and login state storing, logout bug fix
[situare] / src / facebookservice / facebookauthentication.h
index 5d17f5c..28725bd 100644 (file)
 #ifndef FACEBOOKAUTHENTICATION_H
 #define FACEBOOKAUTHENTICATION_H
 
-#include <QtGui>
-#include <QtWebKit>
-#include <QString>
-#include <QLayout>
+#include <QUrl>
 
-#include "facebookcredentials.h"
-#include "ui/logindialog.h"
+class QNetworkReply;
+class QSslError;
+class QWebView;
+
+class MainWindow;
 
 /**
-* @brief FacebookAuthentication class takes care of transmitting username and password to facebook.
-*        And it also receives credentials from Facebook. Other components of Situare application
-*        needs credentials to communicate with facebook.
+* @brief FacebookAuthentication class takes care of Facebook login process. It creates
+         QWebView instance and tries to login with cookies using hidden browser.
+         If failed, then visible login browser dialog is invoked. Class also does parse the
+         accuired credentials.
 *
 * @author Ville Tiensuu
-* @class FacebookAuthentication facebookauthentication.h "facebookauthentication.h"
+* @author Sami Rämö - sami.ramo (at) ixonos.com
 */
-class FacebookAuthentication : public QMainWindow
+class FacebookAuthentication : public QObject
 {
     Q_OBJECT
 
 public:
     /**
-    * @brief FacebookAuthentication constructor
-    *
-    * -Composes Loginpage from pieces of strings.
-    * -Checks if there is valid credentials stored on the file. If there is emits signal. If not it
-    *  calls start method.
-    * -Connects signal from m_webView to UpdateCredentials() method. With this feature it is
-    *  verified that class tries always update credentials when web page changes.
-    * -Allocates memory for m_webView and m_mainlayout
+    * @brief Constructor
     *
-    * @param parent instance of parent
-    */
-    FacebookAuthentication(QWidget *parent = 0);
-
-    /**
-    * @brief Releases allocated memory for m_webView and m_mainlayout
+    * Initiates internal data members.
     *
+    * @param mainWindow MainWindow instance
+    * @param parent Instance of the parent
     */
-    ~FacebookAuthentication();
+    FacebookAuthentication(MainWindow *mainWindow, QObject *parent = 0);
 
 /*******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
-
-    /**
-    * @brief Getter for m_loginCredentials
-    *
-    * @return FacebookCredentials
-    */
-    FacebookCredentials loginCredentials() const;
-
 public slots:
-
     /**
-    * @brief Shows the m_webView and loads page that is specified in the m_facebookLoginPage
-    *        variable. Specifies font size for the page.
-    *    
-    */
-    void start();
-
-    /**
-    * @brief Slot to intercept signal when user has pressed connect button from loginDialog
+    * @brief Clears account information from settings
     *
-    * @param email E-mail
-    * @param password Password
+    * @param keepUsername keep = true, false otherwise
     */
-    void loginDialogDone(const QString &email, const QString &password);
+    void clearAccountInformation(bool keepUsername = false);
 
     /**
-    * @brief Toggle progress indicator.
-    *
-    * @param state true if progress indicator should be shown, false otherwise
-    */
-    void toggleProgressIndicator(bool state);
-
-private: 
+      * @brief Is the user currently logged in
+      *
+      * @returns True if the user is logged in, otherwise false
+      */
+    bool isLoggedIn();
 
     /**
-    * @brief Creates login url with given parameters
-    *
-    * @param urlParts Url parts
-    * @return QUrl Login page url
-    */
-    QUrl formLoginPageUrl(const QStringList & urlParts) const;
+      * @brief Initiate login process
+      *
+      * Builds login browser and starts loading login URL.
+      */
+    void login();
 
     /**
-    * @brief Reads previous stored credentials from file.
-    *
-    * @param credentialsFromFile This dataclass is the place where method stores credentials.
-    *        Corrent parameter here is m_loginCredentials
-    */
-    void readCredentials(FacebookCredentials &credentialsFromFile);
+      * @brief Log out
+      */
+    void logOut();
 
+private:
     /**
-    * @brief Checks expiration time of credentials and compares it to current time.
-    *
-    * @param credentials this parameter represents credentials that will be verified.
-    * @return bool returns true if expiration time is after current time. in other cases returns
-    *               false.
-    */
-    bool verifyCredentials(const FacebookCredentials &credentials) const;   
+      * @brief Destroy login dialog and browser
+      */
+    void destroyLogin();
 
     /**
-    * @brief Writes credentials to File
-    *
-    * @param credentials Contents of this dataclass is stored to file
-    */
-    void writeCredentials(const FacebookCredentials &credentials);
+      * @brief Parses the session information from the URL
+      *
+      * @param url URL
+      * @returns Parsed session, or empty string if parsing failed.
+      */
+    QString parseSession(const QUrl &url);
 
 private slots:
     /**
-    * @brief  Search credentials from URL that is given as parameter.
-    *         If credentials are found thay are stored to loginCredentials variable.
-    *
-    * @param url URL where this method tries to find credentials.
-    * @return bool if credentials are found returns true,
-    *               if credentials are not found returns false.
-    */
-    bool updateCredentials(const QUrl & url);
+      * @brief Cleanup after browser is destructed
+      *
+      * Clears the pointer to the browser and disables the progress indicator.
+      */
+    void browserDestroyed();
 
     /**
-    * @brief Slot to intercept signal when webview has finished loading webpage
-    *
-    * @param done Status of the loading
-    */
-    void loadDone(bool done);
+      * @brief Handler for login page loading errors
+      *
+      * @param reply Network reply
+      */
+    void networkReplyHandler(QNetworkReply *reply);
 
     /**
-    * @brief Slot to intercept signal when login has failed (loginFailure signal)
-    *
-    */
-    void loginFailed();
+      * @brief Handler for SSL errors, ignores the error
+      */
+    void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
+
+    /**
+      * @brief Handler for browser URL changes
+      *
+      * Does check the new URL and based on that invokes the login dialog with visible browser view
+      * or parses the session from the new URL.
+      *
+      * @param url New URL
+      */
+    void urlChanged(const QUrl &url);
 
 /*******************************************************************************
  * SIGNALS
  ******************************************************************************/
-
 signals:
-
     /**
-    * @brief This signal is emitted if updateCredentials method finds credentials from URL.
-    *        Signal is also emitted at the beginning of the program if there is valid credentials
-    *        in the file.
+    * @brief Signals error
     *
-    * @param credentials New credentials
+    * @param context error context
+    * @param error error code
     */
-    void credentialsReady(const FacebookCredentials &credentials);
+    void error(const int context, const int error);
 
     /**
-    * @brief This signal is emitted if updateCredentials method can't find credentials from URL
-    *
-    */
-    void loginFailure();
+      * @brief Emitted when logged in successfully
+      *
+      * All login related actions should be connected to this signal.
+      *
+      * @param session Session data
+      */
+    void loggedIn(const QString session);
 
     /**
-    * @brief Signal that indicates when user has cancelled login process
-    *
-    */
-    void quitSituare();
+      * @brief Emitted when logged out
+      *
+      * All logout related actions should be connected to this signal.
+      */
+    void loggedOut();
 
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
-
 private:
-
-    /**
-    * @brief Dataclass that contains authorization to use facebook. Dataclass is composed of five
-    *        QStrings and setters and getters.
-    *
-    * @var m_loginCredentials
-    */
-    FacebookCredentials m_loginCredentials;
-
-    QString m_email; ///< Placeholder for email
-    int m_loginAttempts; ///< Indicates login attempts
-    LoginDialog *m_loginDialog; ///< Login dialog
-    QHBoxLayout *m_mainlayout; ///< Lays out m_webView in window.
-    QString m_password; ///< Placeholder for password
-    bool m_refresh; ///< Indicates when webpage is refreshed
-    QWebView *m_webView; ///< Shows facebook login page.
+    bool m_loggedIn;                ///< Is the user currently logged in
+    QWebView *m_browser;            ///< Login browser
+    MainWindow *m_mainWindow;       ///< MainWindow
 };
 
 #endif // FACEBOOKAUTHENTICATION_H