Removed one un-needed signal connect from facebookauthentication
[situare] / src / facebookservice / facebookauthentication.cpp
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 #include <QtDebug>
25 #include <QDateTime>
26 #include <QSettings>
27 #include <QStringList>
28 #include <QVariantMap>
29
30 #ifdef Q_WS_MAEMO_5
31 #include <QMaemo5InformationBox>
32 #endif // Q_WS_MAEMO_5
33
34 #include "facebookauthentication.h"
35 #include "facebookcommon.h"
36 #include "parser.h"
37
38 FacebookAuthentication::FacebookAuthentication(QObject *parent)
39     : QObject(parent),
40     m_loginAttempts(0)
41
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     readCredentials(m_loginCredentials);
46 }
47
48 void FacebookAuthentication::start()
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     if (!verifyCredentials(m_loginCredentials)) {
53         QStringList list;
54         list.append(FACEBOOK_LOGINBASE);
55         list.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
56         list.append(INTERVAL1);
57         list.append(SITUARE_LOGIN_SUCCESS);
58         list.append(INTERVAL2);
59         list.append(SITUARE_LOGIN_FAILURE);
60         list.append(FACEBOOK_LOGIN_ENDING);
61
62         emit newLoginRequest(formLoginPageUrl(list));
63     }
64     else
65         emit credentialsReady(m_loginCredentials);
66 }
67
68 bool FacebookAuthentication::updateCredentials(const QUrl &url)
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71
72     bool found = false;
73
74     if (url.isValid()){
75          qDebug() << "url is valid";
76
77         QString callbackUrl = url.toString();
78         qDebug() << "callbackUrl:  " << callbackUrl.toAscii();
79
80         if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
81             qDebug() << "login success";
82
83             // let's find out session credentials
84             if(callbackUrl.contains(SESSION_KEY)) {
85
86                 QJson::Parser parser;
87                 bool ok;
88
89                 // split string into string part and json part
90                 QStringList list = url.toString().split("=");
91
92                 for(int i=0;i<list.count();i++) {
93                     // if string starts with json item
94                     if(list.at(i).startsWith("{")) {
95                         QByteArray jsonString = list.at(i).toAscii();
96                         QVariantMap result = parser.parse (jsonString, &ok).toMap();
97                         if (!ok) {
98
99                             qFatal("An error occurred during parsing");
100                             exit (1);
101                         }
102                         qDebug() << "Session Key" << result[SESSION_KEY].toString();
103                         m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
104
105                         qDebug() << "userID" << result[USER_ID].toString();
106                         m_loginCredentials.setUserID(result[USER_ID].toString());
107
108                         qDebug() << "Expires" << result[EXPIRES].toString();
109                         m_loginCredentials.setExpires(result[EXPIRES].toString());
110
111                         qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
112                         m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
113
114                         qDebug() << "Signature" << result[SIGNATURE].toString();
115                         m_loginCredentials.setSig(result[SIGNATURE].toString());
116                     }
117                 }
118                 found = true;
119             }
120             writeCredentials(m_loginCredentials);
121             emit credentialsReady(m_loginCredentials);
122         }
123         else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
124             qWarning() << "login failure" << endl;
125             qDebug() << callbackUrl;
126             ++m_loginAttempts;
127             /* emit loginFailure for every second login attemps, since webview loads login
128                error page (loadingDone() signal is emitted) and we need to avoid that because
129                at this point we don't have new login parameters */
130             if(m_loginAttempts % 2) {
131                 emit loginFailure();
132             }
133         }
134         else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
135             qDebug() << "correct loginPage";
136         }
137         else {
138             qDebug() << "totally wrong webPage";
139             // we should not get a wrong page at this point
140             emit loginFailure();
141         }
142     }
143     else {
144         qDebug() << " Loading of page failed invalid URL" << endl;
145         // we should not get a wrong page at this point
146         emit loginFailure();
147         return false;
148     }
149     return found;
150 }
151
152 void FacebookAuthentication::writeCredentials(const FacebookCredentials &credentials)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155     QSettings settings(DIRECTORY_NAME, FILE_NAME);
156
157     settings.setValue(SESSION_KEY, credentials.sessionKey());
158     settings.setValue(USER_ID, credentials.userID());
159     settings.setValue(EXPIRES, credentials.expires());
160     settings.setValue(SESSION_SECRET, credentials.sessionSecret());
161     settings.setValue(SIGNATURE, credentials.sig());
162 }
163
164 void FacebookAuthentication::readCredentials(FacebookCredentials &credentialsFromFile)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     QSettings settings(DIRECTORY_NAME, FILE_NAME);
169
170     credentialsFromFile.setSessionKey(settings.value(SESSION_KEY, ERROR).toString());
171     credentialsFromFile.setUserID(settings.value(USER_ID, ERROR).toString());
172     credentialsFromFile.setExpires(settings.value(EXPIRES, ERROR).toString());
173     credentialsFromFile.setSessionSecret(settings.value(SESSION_SECRET, ERROR).toString());
174     credentialsFromFile.setSig(settings.value(SIGNATURE, ERROR).toString());
175 }
176
177  FacebookCredentials FacebookAuthentication::loginCredentials() const
178  {
179      qDebug() << __PRETTY_FUNCTION__;
180      return m_loginCredentials;
181  }
182
183  bool FacebookAuthentication::verifyCredentials(const FacebookCredentials &credentials) const
184  {
185      qDebug() << __PRETTY_FUNCTION__;
186
187      // if expires value is 0, then credentials are valid forever
188      if(credentials.expires() == "0") {
189          return true;
190      }
191      else {
192          const QString dateTimeFormat = "dd.MM.yyyy  hh:mm:ss";
193          QString expires = credentials.expires();
194          QDateTime expireTime;
195          expireTime.setTime_t(expires.toInt());
196          QString expiresString = expireTime.toString(dateTimeFormat);
197          qDebug() << expiresString.toAscii();
198
199          QDateTime currentTime;
200          currentTime = QDateTime::currentDateTime();
201          QString currentTimeString = currentTime.toString(dateTimeFormat);
202          qDebug() << currentTimeString.toAscii();
203
204          return currentTime < expireTime;
205      }
206  }
207
208  QUrl FacebookAuthentication::formLoginPageUrl(const QStringList &urlParts) const
209  {
210     qDebug() << __PRETTY_FUNCTION__;
211
212     return QUrl(urlParts.join(EMPTY));
213  }