Removed FacebookLoginBrowser class, replaced with QWebView.
[situare] / src / facebookservice / facebookauthentication.h
index c7d1a10..5d12838 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 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
 
@@ -48,161 +48,60 @@ 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 mainWindow MainWindow instance
     * @param parent instance of parent
     */
-    FacebookAuthentication(QWidget *parent = 0);
+    FacebookAuthentication(MainWindow *mainWindow, QObject *parent = 0);
 
-    /**
-    * @brief Releases allocated memory for m_webView and m_mainlayout
-    *
-    */
-    ~FacebookAuthentication();
-
-    /**
-    * @brief Getter for m_loginCredentials
-    *
-    * @return FacebookCredentials
-    */
-    FacebookCredentials loginCredentials() const;
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+public:
+    void login();
 
 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
-    *
-    * @param email E-mail
-    * @param password Password
-    */
-    void loginDialogDone(const QString &email, const QString &password);
-
-    /**
-    * @brief Toggle progress indicator.
-    *
-    * @param state true if progress indicator should be shown, false otherwise
-    */
-    void toggleProgressIndicator(bool state);
-
-private: 
-
-    /**
-    * @brief  Appends given parts to returned string. Method is used to form facebook login page
-              from given parts.
+    * @brief Clears account iformation from settings
     *
-    * @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 keepUsername keep = true, false otherwise
     */
-    QString formLoginPage(const QString & part1, const QString & part2, const QString & part3,
-                          const QString & part4, const QString & part5, const QString & part6,
-                          const QString & part7) const;
+    void clearAccountInformation(bool keepUsername = false);
 
-    /**
-    * @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 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 Writes credentials to File
-    *
-    * @param credentials Contents of this dataclass is stored to file
-    */
-    void writeCredentials(const FacebookCredentials &credentials);
+private:
+    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);
+    void browserDestroyed();
 
-    /**
-    * @brief Slot to intercept signal when webview has finished loading webpage
-    *
-    * @param done Status of the loading
-    */
-    void loadDone(bool done);
+    void loadFinished(bool ok);
 
-    /**
-    * @brief Slot to intercept signal when login has failed (loginFailure signal)
-    *
-    */
-    void loginFailed();
+    void networkReplyHandler(QNetworkReply *reply);
 
-signals:
+    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 Signal that indicates when user has cancelled login process
-    *
-    */
-    void quitSituare();
+    void loggedIn(const QString session);
 
+/*******************************************************************************
+ * 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
-    QString m_facebookLoginPage; ///< String that contantains URL of facebook loginpage.
-    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.
+    QWebView *m_browser;
+    MainWindow *m_mainWindow;
 };
 
 #endif // FACEBOOKAUTHENTICATION_H