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