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__;
48 void FacebookAuthentication::clearAccountInformation()
50 qDebug() << __PRETTY_FUNCTION__;
52 m_loginCredentials.clearCredentials();
53 QSettings settings(DIRECTORY_NAME, FILE_NAME);
54 settings.remove(USERNAME);
55 settings.remove(COOKIES);
58 QUrl FacebookAuthentication::formLoginPageUrl(const QStringList &urlParts) const
60 qDebug() << __PRETTY_FUNCTION__;
62 return QUrl(urlParts.join(EMPTY));
65 const QString FacebookAuthentication::loadUsername()
67 qDebug() << __PRETTY_FUNCTION__;
69 QSettings settings(DIRECTORY_NAME, FILE_NAME);
70 return settings.value(USERNAME, EMPTY).toString();
73 FacebookCredentials FacebookAuthentication::loginCredentials() const
75 qDebug() << __PRETTY_FUNCTION__;
76 return m_loginCredentials;
79 void FacebookAuthentication::saveUsername(const QString &username)
81 qDebug() << __PRETTY_FUNCTION__;
83 QSettings settings(DIRECTORY_NAME, FILE_NAME);
84 settings.setValue(USERNAME, username);
87 void FacebookAuthentication::start()
89 qDebug() << __PRETTY_FUNCTION__;
92 list.append(FACEBOOK_LOGINBASE);
93 list.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
94 list.append(INTERVAL1);
95 list.append(SITUARE_LOGIN_SUCCESS);
96 list.append(INTERVAL2);
97 list.append(SITUARE_LOGIN_FAILURE);
98 list.append(FACEBOOK_LOGIN_ENDING);
100 QSettings settings(DIRECTORY_NAME, FILE_NAME);
102 QString cookies = settings.value(COOKIES, EMPTY).toString();
103 if(!cookies.isEmpty()) {
104 emit loginUsingCookies();
107 emit newLoginRequest(formLoginPageUrl(list));
111 bool FacebookAuthentication::updateCredentials(const QUrl &url)
113 qDebug() << __PRETTY_FUNCTION__;
118 qDebug() << "url is valid";
120 QString callbackUrl = url.toString();
121 qDebug() << "callbackUrl: " << callbackUrl.toAscii();
123 if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
124 qDebug() << "login success";
126 // let's find out session credentials
127 if(callbackUrl.contains(SESSION_KEY)) {
129 QJson::Parser parser;
132 // split string into string part and json part
133 QStringList list = url.toString().split("=");
135 for(int i=0;i<list.count();i++) {
136 // if string starts with json item
137 if(list.at(i).startsWith("{")) {
138 QByteArray jsonString = list.at(i).toAscii();
139 QVariantMap result = parser.parse (jsonString, &ok).toMap();
142 qFatal("An error occurred during parsing");
145 qDebug() << "Session Key" << result[SESSION_KEY].toString();
146 m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
148 // // commeted out until qjson parser can handle 64-bit integers
149 // qDebug() << "userID" << result[USER_ID].toString();
150 // m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
152 // dirty fix, get user id from session_key
153 QStringList list = result[SESSION_KEY].toString().split("-");
154 m_loginCredentials.setUserID(list.at(1));
155 qDebug() << m_loginCredentials.userID();
157 qDebug() << "Expires" << result[EXPIRES].toString();
158 m_loginCredentials.setExpires(result[EXPIRES].toString());
160 qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
161 m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
163 qDebug() << "Signature" << result[SIGNATURE].toString();
164 m_loginCredentials.setSig(result[SIGNATURE].toString());
168 emit saveCookiesRequest();
170 emit credentialsReady(m_loginCredentials);
172 else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
173 qWarning() << "login failure" << endl;
174 qDebug() << callbackUrl;
176 /* emit loginFailure for every second login attemps, since webview loads login
177 error page (loadingDone() signal is emitted) and we need to avoid that because
178 at this point we don't have new login parameters */
179 if(m_loginAttempts % 2) {
180 clearAccountInformation();
184 else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
185 qDebug() << "correct loginPage";
188 qDebug() << "totally wrong webPage";
189 // we should not get a wrong page at this point
194 qDebug() << " Loading of page failed invalid URL" << endl;
195 // we should not get a wrong page at this point