Implemented custom login and created functional tests for it
[situare] / src / facebookservice / facebookauthentication.h
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Ville Tiensuu - ville.tiensuu@ixonos.com
6        Kaj Wallin - kaj.wallin@ixonos.com
7        Henri Lampela - henri.lampela@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #ifndef FACEBOOKAUTHENTICATION_H
25 #define FACEBOOKAUTHENTICATION_H
26
27 #include <QtGui>
28 #include <QtWebKit>
29 #include <QString>
30 #include <QLayout>
31
32 #include "facebookcredentials.h"
33 #include "ui/logindialog.h"
34
35 /**
36 * @brief FacebookAuthentication class takes care of transmitting username and password to facebook.
37 *        And it also receives credentials from Facebook. Other components of Situare application
38 *        needs credentials to communicate with facebook.
39 *
40 * @author Ville Tiensuu
41 * @class FacebookAuthentication facebookauthentication.h "facebookauthentication.h"
42 */
43 class FacebookAuthentication : public QMainWindow
44 {
45     Q_OBJECT
46
47 public:
48     /**
49     * @brief FacebookAuthentication constructor
50     *
51     * -Composes Loginpage from pieces of strings.
52     * -Checks if there is valid credentials stored on the file. If there is emits signal. If not it
53     *  calls start method.
54     * -Connects signal from m_webView to UpdateCredentials() method. With this feature it is
55     *  verified that class tries always update credentials when web page changes.
56     * -Allocates memory for m_webView and m_mainlayout
57     *
58     * @param parent instance of parent
59     */
60     FacebookAuthentication(QWidget *parent = 0);
61
62     /**
63     * @brief Releases allocated memory for m_webView and m_mainlayout
64     *
65     */
66     ~FacebookAuthentication();
67
68     /**
69     * @brief Getter for m_loginCredentials
70     *
71     * @return FacebookCredentials
72     */
73     FacebookCredentials loginCredentials() const;
74
75 public slots:
76
77     /**
78     * @brief Shows the m_webView and loads page that is specified in the m_facebookLoginPage
79     *        variable. Specifies font size for the page.
80     *    
81     */
82     void start();
83
84     /**
85     * @brief Slot to intercept signal when user has pressed connect button from loginDialog
86     *
87     * @param email E-mail
88     * @param password Password
89     */
90     void loginDialogDone(const QString &email, const QString &password);
91
92     /**
93     * @brief Toggle progress indicator.
94     *
95     * @param state true if progress indicator should be shown, false otherwise
96     */
97     void toggleProgressIndicator(bool state);
98
99 private: 
100
101     /**
102     * @brief  Appends given parts to returned string. Method is used to form facebook login page
103               from given parts.
104     *
105     * @param  part1 first part of the formed string
106     * @param  part2 second part of the formed string
107     * @param  part3 third part of the formed string
108     * @param  part4 fouth part of the formed string
109     * @param  part5 fifth part of the formed string
110     * @param  part6 sixth part of the formed string
111     * @param  part7 sevents part of the formed string
112     */
113     QString formLoginPage(const QString & part1, const QString & part2, const QString & part3,
114                           const QString & part4, const QString & part5, const QString & part6,
115                           const QString & part7) const;
116
117     /**
118     * @brief Reads previous stored credentials from file.
119     *
120     * @param credentialsFromFile This dataclass is the place where method stores credentials.
121     *        Corrent parameter here is m_loginCredentials
122     */
123     void readCredentials(FacebookCredentials &credentialsFromFile);
124
125     /**
126     * @brief Checks expiration time of credentials and compares it to current time.
127     *
128     * @param credentials this parameter represents credentials that will be verified.
129     * @return bool returns true if expiration time is after current time. in other cases returns
130     *               false.
131     */
132     bool verifyCredentials(const FacebookCredentials &credentials) const;   
133
134     /**
135     * @brief Writes credentials to File
136     *
137     * @param credentials Contents of this dataclass is stored to file
138     */
139     void writeCredentials(const FacebookCredentials &credentials);
140
141 private slots:
142     /**
143     * @brief  Search credentials from URL that is given as parameter.
144     *         If credentials are found thay are stored to loginCredentials variable.
145     *
146     * @param url URL where this method tries to find credentials.
147     * @return bool if credentials are found returns true,
148     *               if credentials are not found returns false.
149     */
150     bool updateCredentials(const QUrl & url);
151
152     /**
153     * @brief Slot to intercept signal when webview has finished loading webpage
154     *
155     * @param done Status of the loading
156     */
157     void loadDone(bool done);
158
159     /**
160     * @brief Slot to intercept signal when login has failed (loginFailure signal)
161     *
162     */
163     void loginFailed();
164
165 signals:
166
167     /**
168     * @brief This signal is emitted if updateCredentials method finds credentials from URL.
169     *        Signal is also emitted at the beginning of the program if there is valid credentials
170     *        in the file.
171     *
172     * @param credentials New credentials
173     */
174     void credentialsReady(const FacebookCredentials &credentials);
175
176     /**
177     * @brief This signal is emitted if updateCredentials method can't find credentials from URL
178     *
179     */
180     void loginFailure();
181
182     /**
183     * @brief Signal that indicates when user has cancelled login process
184     *
185     */
186     void quitSituare();
187
188 private:
189
190     /**
191     * @brief Dataclass that contains authorization to use facebook. Dataclass is composed of five
192     *        QStrings and setters and getters.
193     *
194     * @var m_loginCredentials
195     */
196     FacebookCredentials m_loginCredentials;
197
198     QString m_email; ///< Placeholder for email
199     QString m_facebookLoginPage; ///< String that contantains URL of facebook loginpage.
200     int m_loginAttempts; ///< Indicates login attempts
201     LoginDialog *m_loginDialog; ///< Login dialog
202     QHBoxLayout *m_mainlayout; ///< Lays out m_webView in window.
203     QString m_password; ///< Placeholder for password
204     bool m_refresh; ///< Indicates when webpage is refreshed
205     QWebView *m_webView; ///< Shows facebook login page.
206 };
207
208 #endif // FACEBOOKAUTHENTICATION_H