2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Ville Tiensuu - ville.tiensuu@ixonos.com
6 Kaj Wallin - kaj.wallin@ixonos.com
7 Henri Lampela - henri.lampela@ixonos.com
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.
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.
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,
27 #include <QStringList>
28 #include <QVariantMap>
31 #include <QMaemo5InformationBox>
32 #endif // Q_WS_MAEMO_5
34 #include "facebookauthentication.h"
35 #include "facebookcommon.h"
36 #include "../common.h"
39 FacebookAuthentication::FacebookAuthentication(QObject *parent)
44 qDebug() << __PRETTY_FUNCTION__;
46 readCredentials(m_loginCredentials);
49 void FacebookAuthentication::start()
51 qDebug() << __PRETTY_FUNCTION__;
53 if(!verifyCredentials(m_loginCredentials)) {
55 list.append(FACEBOOK_LOGINBASE);
56 list.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
57 list.append(INTERVAL1);
58 list.append(SITUARE_LOGIN_SUCCESS);
59 list.append(INTERVAL2);
60 list.append(SITUARE_LOGIN_FAILURE);
61 list.append(FACEBOOK_LOGIN_ENDING);
63 QSettings settings(DIRECTORY_NAME, FILE_NAME);
65 QString cookies = settings.value(COOKIES, EMPTY).toString();
66 if(!cookies.isEmpty()) {
67 emit loginUsingCookies();
70 emit newLoginRequest(formLoginPageUrl(list));
74 emit credentialsReady(false, m_loginCredentials);
79 bool FacebookAuthentication::updateCredentials(const QUrl &url)
81 qDebug() << __PRETTY_FUNCTION__;
86 qDebug() << "url is valid";
88 QString callbackUrl = url.toString();
89 qDebug() << "callbackUrl: " << callbackUrl.toAscii();
91 if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
92 qDebug() << "login success";
94 // let's find out session credentials
95 if(callbackUrl.contains(SESSION_KEY)) {
100 // split string into string part and json part
101 QStringList list = url.toString().split("=");
103 for(int i=0;i<list.count();i++) {
104 // if string starts with json item
105 if(list.at(i).startsWith("{")) {
106 QByteArray jsonString = list.at(i).toAscii();
107 QVariantMap result = parser.parse (jsonString, &ok).toMap();
110 qFatal("An error occurred during parsing");
113 qDebug() << "Session Key" << result[SESSION_KEY].toString();
114 m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
116 // // commeted out until qjson parser can handle 64-bit integers
117 // qDebug() << "userID" << result[USER_ID].toString();
118 // m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
120 // dirty fix, get user id from session_key
121 QStringList list = result[SESSION_KEY].toString().split("-");
122 m_loginCredentials.setUserID(list.at(1));
123 qDebug() << m_loginCredentials.userID();
125 qDebug() << "Expires" << result[EXPIRES].toString();
126 m_loginCredentials.setExpires(result[EXPIRES].toString());
128 qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
129 m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
131 qDebug() << "Signature" << result[SIGNATURE].toString();
132 m_loginCredentials.setSig(result[SIGNATURE].toString());
136 emit saveCookiesRequest();
138 writeCredentials(m_loginCredentials);
139 emit credentialsReady(true, m_loginCredentials);
141 else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
142 qWarning() << "login failure" << endl;
143 qDebug() << callbackUrl;
145 /* emit loginFailure for every second login attemps, since webview loads login
146 error page (loadingDone() signal is emitted) and we need to avoid that because
147 at this point we don't have new login parameters */
148 if(m_loginAttempts % 2) {
152 else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
153 qDebug() << "correct loginPage";
156 qDebug() << "totally wrong webPage";
157 // we should not get a wrong page at this point
162 qDebug() << " Loading of page failed invalid URL" << endl;
163 // we should not get a wrong page at this point
170 void FacebookAuthentication::writeCredentials(const FacebookCredentials &credentials)
172 qDebug() << __PRETTY_FUNCTION__;
173 QSettings settings(DIRECTORY_NAME, FILE_NAME);
175 settings.setValue(SESSION_KEY, credentials.sessionKey());
176 settings.setValue(USER_ID, credentials.userID());
177 settings.setValue(EXPIRES, credentials.expires());
178 settings.setValue(SESSION_SECRET, credentials.sessionSecret());
179 settings.setValue(SIGNATURE, credentials.sig());
182 void FacebookAuthentication::readCredentials(FacebookCredentials &credentialsFromFile)
184 qDebug() << __PRETTY_FUNCTION__;
186 QSettings settings(DIRECTORY_NAME, FILE_NAME);
188 credentialsFromFile.setSessionKey(settings.value(SESSION_KEY, ERROR).toString());
189 credentialsFromFile.setUserID(settings.value(USER_ID, ERROR).toString());
190 credentialsFromFile.setExpires(settings.value(EXPIRES, ERROR).toString());
191 credentialsFromFile.setSessionSecret(settings.value(SESSION_SECRET, ERROR).toString());
192 credentialsFromFile.setSig(settings.value(SIGNATURE, ERROR).toString());
195 FacebookCredentials FacebookAuthentication::loginCredentials() const
197 qDebug() << __PRETTY_FUNCTION__;
198 return m_loginCredentials;
201 bool FacebookAuthentication::verifyCredentials(const FacebookCredentials &credentials) const
203 qDebug() << __PRETTY_FUNCTION__;
205 // if expires value is 0, then credentials are valid forever
206 if(credentials.expires() == "0") {
210 const QString dateTimeFormat = "dd.MM.yyyy hh:mm:ss";
211 QString expires = credentials.expires();
212 QDateTime expireTime;
213 expireTime.setTime_t(expires.toInt());
214 QString expiresString = expireTime.toString(dateTimeFormat);
215 qDebug() << expiresString.toAscii();
217 QDateTime currentTime;
218 currentTime = QDateTime::currentDateTime();
219 QString currentTimeString = currentTime.toString(dateTimeFormat);
220 qDebug() << currentTimeString.toAscii();
222 return currentTime < expireTime;
226 QUrl FacebookAuthentication::formLoginPageUrl(const QStringList &urlParts) const
228 qDebug() << __PRETTY_FUNCTION__;
230 return QUrl(urlParts.join(EMPTY));
233 void FacebookAuthentication::saveUsername(const QString &username)
235 qDebug() << __PRETTY_FUNCTION__;
237 QSettings settings(DIRECTORY_NAME, FILE_NAME);
238 settings.setValue(USERNAME, username);
241 const QString FacebookAuthentication::loadUsername()
243 qDebug() << __PRETTY_FUNCTION__;
245 QSettings settings(DIRECTORY_NAME, FILE_NAME);
246 return settings.value(USERNAME, EMPTY).toString();
249 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
251 qDebug() << __PRETTY_FUNCTION__;
253 m_loginCredentials.clearCredentials();
254 QSettings settings(DIRECTORY_NAME, FILE_NAME);
256 settings.remove(USERNAME);
257 settings.remove(COOKIES);
259 settings.remove(USER_ID);
260 settings.remove(SESSION_KEY);
261 settings.remove(SESSION_SECRET);
262 settings.remove(EXPIRES);
263 settings.remove(SIGNATURE);
265 emit credentialsChanged(m_loginCredentials);