Final implementation of logout
[situare] / src / facebookservice / facebookauthentication.h
index dc0f947..dc449ec 100644 (file)
@@ -4,6 +4,7 @@
 
        Ville Tiensuu - ville.tiensuu@ixonos.com
        Kaj Wallin - kaj.wallin@ixonos.com
+       Henri Lampela - henri.lampela@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
 #ifndef FACEBOOKAUTHENTICATION_H
 #define FACEBOOKAUTHENTICATION_H
 
-#include <QtGui>
-#include <QtWebKit>
-#include <QString>
-#include <QLayout>
+#include <QUrl>
 #include "facebookcredentials.h"
 
 /**
-* @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 parsing and handling of  credentials for
+*        Facebook. Other components of Situare application needs credentials to communicate with
+*        facebook.
 *
 * @author Ville Tiensuu
-* @class FacebookAuthentication facebookauthentication.h "facebookauthentication.h"
 */
-class FacebookAuthentication : public QMainWindow
+class FacebookAuthentication : public QObject
 {
     Q_OBJECT
 
@@ -45,22 +42,15 @@ 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
+    * -Checks if there is valid credentials stored on the file. If there is emits signal.
     *
     * @param parent instance of parent
     */
-    FacebookAuthentication(QWidget *parent = 0);
+    FacebookAuthentication(QObject *parent = 0);
 
-    /**
-    * @brief Releases allocated memory for m_webView and m_mainlayout
-    *
-    */
-    ~FacebookAuthentication();
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
 
     /**
     * @brief Getter for m_loginCredentials
@@ -70,72 +60,52 @@ public:
     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();
 
-private slots:
     /**
-    * @brief  Search credentials from URL that is given as parameter.
-    *         If credentials are found thay are stored to loginCredentials variable.
+    * @brief Loads username from settings
     *
-    * @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.
+    * @return QString Loaded username
     */
-    bool updateCredentials(const QUrl & url);
-
-signals:    
+    const QString loadUsername();
 
     /**
-    * @brief This signal is emitted if user exits logging in by pressing X
+    * @brief Saves username to settings
     *
+    * @param username Username to be saved
     */
-    void userExit();
+    void saveUsername(const QString &username);
 
     /**
-    * @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 Shows the m_webView and loads page that is specified in the m_facebookLoginPage
+    *        variable. Specifies font size for the page.
+    *    
     */
-    void credentialsReady();
+    void start();
 
     /**
-    * @brief This signal is emitted if updateCredentials method can't find credentials from URL
+    * @brief Clears account iformation from settings
     *
+    * @param keepUsername true = keep username
     */
-    void loginFailure();
+    void clearAccountInformation(bool keepUsername = false);
 
-private:
+private: 
 
     /**
-    * @brief  Appends given parts to returned string. Method is used to form facebook login page
-              from given parts.
+    * @brief Creates login url with given parameters
     *
-    * @param  part1 first part of the formed string
-    * @param  part2 second part of the formed string
-    * @param  part3 third part of the formed string
-    * @param  part4 fouth part of the formed string
-    * @param  part5 fifth part of the formed string
-    * @param  part6 sixth part of the formed string
-    * @param  part7 sevents part of the formed string
+    * @param urlParts Url parts
+    * @return QUrl Login page url
     */
-    QString formLoginPage(const QString & part1, const QString & part2, const QString & part3,
-                          const QString & part4, const QString & part5, const QString & part6,
-                          const QString & part7) const;
+    QUrl formLoginPageUrl(const QStringList & urlParts) const;
 
     /**
-    * @brief Program cames to this method when user closes login screen by pressing X.
-    *        method sends userExit() signal in this function
+    * @brief Reads previous stored credentials from file.
     *
-    * @param event without parameter program does not come to this function when user exits by
-    *               pressing X.
+    * @param credentialsFromFile This dataclass is the place where method stores credentials.
+    *        Corrent parameter here is m_loginCredentials
     */
-    void closeEvent(QCloseEvent *event);
+    void readCredentials(FacebookCredentials &credentialsFromFile);
 
     /**
     * @brief Checks expiration time of credentials and compares it to current time.
@@ -144,43 +114,70 @@ private:
     * @return bool returns true if expiration time is after current time. in other cases returns
     *               false.
     */
-    bool verifyCredentials(const FacebookCredentials &credentials) const;
+    bool verifyCredentials(const FacebookCredentials &credentials) const;   
 
     /**
-    * @brief Reads previous stored credentials from file.
+    * @brief Writes credentials to File
     *
-    * @param credentialsFromFile This dataclass is the place where method stores credentials.
-    *        Corrent parameter here is m_loginCredentials
+    * @param credentials Contents of this dataclass is stored to file
     */
-    void readCredentials(FacebookCredentials &credentialsFromFile);
+    void writeCredentials(const FacebookCredentials &credentials);
+
+private slots:
 
     /**
-    * @brief Writes credentials to File
+    * @brief  Search credentials from URL that is given as parameter.
+    *         If credentials are found thay are stored to loginCredentials variable.
     *
-    * @param credentials Contents of this dataclass is stored to file
+    * @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.
     */
-    void writeCredentials(const FacebookCredentials &credentials);
+    bool updateCredentials(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.
+    *
+    * @param freshLogin Was login done via login dialog
+    * @param credentials New credentials
+    */
+    void credentialsReady(bool freshLogin, const FacebookCredentials &credentials);
 
     /**
-    * @brief Shows facebook login page.
+    * @brief Signals changed credentials
     *
-    * @var m_webView
+    * @param credentials New credentials
     */
-    QWebView *m_webView;
+    void credentialsChanged(const FacebookCredentials &credentials);
 
     /**
-    * @brief Lays out m_webView in window.
+    * @brief This signal is emitted if updateCredentials method can't find credentials from URL
     *
-    * @var m_mainlayout
     */
-    QHBoxLayout *m_mainlayout;
+    void loginFailure();
 
     /**
-    * @brief String that contantains URL of facebook loginpage.
+    * @brief Signals when credentials are invalid new login is needed
     *
-    * @var m_facebookLoginPage
+    * @param url Login page url
     */
-    QString m_facebookLoginPage;
+    void newLoginRequest(const QUrl &url);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+
+private:
+
+    int m_loginAttempts; ///< Indicates login attempts
 
     /**
     * @brief Dataclass that contains authorization to use facebook. Dataclass is composed of five