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,
29 #include <QStringList>
30 #include <QVariantMap>
33 #include <QMaemo5InformationBox>
34 #endif // Q_WS_MAEMO_5
38 #include "facebookcommon.h"
40 #include "facebookauthentication.h"
42 const QString REDIRECT_URI = "http://www.facebook.com/connect/login_success.html";
44 FacebookAuthentication::FacebookAuthentication(QObject *parent)
48 qDebug() << __PRETTY_FUNCTION__;
52 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
54 qDebug() << __PRETTY_FUNCTION__;
56 m_loginCredentials.clearCredentials();
57 QSettings settings(DIRECTORY_NAME, FILE_NAME);
60 settings.remove(USERNAME);
61 settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
62 settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
65 settings.remove(COOKIES);
66 settings.remove(USER_UNSEND_MESSAGE);
67 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
70 void FacebookAuthentication::loadFinished(bool ok)
72 qWarning() << __PRETTY_FUNCTION__ << ok;
74 ///< @todo show browsed window if url != redirect url
77 const QString FacebookAuthentication::loadUsername()
79 qDebug() << __PRETTY_FUNCTION__;
81 QSettings settings(DIRECTORY_NAME, FILE_NAME);
82 return settings.value(USERNAME, EMPTY).toString();
85 FacebookCredentials FacebookAuthentication::loginCredentials() const
87 qDebug() << __PRETTY_FUNCTION__;
88 return m_loginCredentials;
91 QString FacebookAuthentication::parseAccessToken(const QUrl &url)
93 qWarning() << __PRETTY_FUNCTION__;
95 // const QString ACCESS_TOKEN_PARAMETER("#access_token=");
96 // const QString EXPIRATION_PARAMETER("&expires_in=");
98 // QString urlString = url.toString();
100 // int begin = urlString.indexOf(ACCESS_TOKEN_PARAMETER) + ACCESS_TOKEN_PARAMETER.length();
101 // int end = urlString.indexOf(EXPIRATION_PARAMETER);
103 // if ((begin > -1) && (end > begin))
104 // return urlString.mid(begin, end - begin);
108 const QString BEGIN("session={");
109 const QString END("}");
111 QString urlString = url.toString();
113 int begin = urlString.indexOf(BEGIN);
114 int end = urlString.indexOf(END, begin);
116 if ((begin > -1) && (end > -1))
117 return urlString.mid(begin, end - begin +1);
122 void FacebookAuthentication::saveUsername(const QString &username)
124 qDebug() << __PRETTY_FUNCTION__;
126 QSettings settings(DIRECTORY_NAME, FILE_NAME);
127 settings.setValue(USERNAME, username);
130 void FacebookAuthentication::start()
132 qDebug() << __PRETTY_FUNCTION__;
134 QSettings settings(DIRECTORY_NAME, FILE_NAME);
136 QStringList cookies = settings.value(COOKIES).toStringList();
137 if(!cookies.isEmpty()) {
138 emit loginUsingCookies();
142 emit newLoginRequest();
146 bool FacebookAuthentication::updateCredentials(const QUrl &url)
148 qDebug() << __PRETTY_FUNCTION__ << url.toString();
153 qDebug() << "url is valid";
155 QString callbackUrl = url.toString();
156 qDebug() << "callbackUrl: " << callbackUrl.toAscii();
158 if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
159 qDebug() << "login success";
161 // let's find out session credentials
162 if(callbackUrl.contains(SESSION_KEY)) {
164 QJson::Parser parser;
167 // split string into string part and json part
168 QStringList list = url.toString().split("=");
170 for(int i=0;i<list.count();i++) {
171 // if string starts with json item
172 if(list.at(i).startsWith("{")) {
173 QByteArray jsonString = list.at(i).toAscii();
174 QVariantMap result = parser.parse (jsonString, &ok).toMap();
177 emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
180 qDebug() << "Session Key" << result[SESSION_KEY].toString();
181 m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
183 // // commeted out until qjson parser can handle 64-bit integers
184 // qDebug() << "userID" << result[USER_ID].toString();
185 // m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
187 // dirty fix, get user id from session_key
188 QStringList list = result[SESSION_KEY].toString().split("-");
189 m_loginCredentials.setUserID(list.at(1));
190 qDebug() << m_loginCredentials.userID();
192 qDebug() << "Expires" << result[EXPIRES].toString();
193 m_loginCredentials.setExpires(result[EXPIRES].toString());
195 qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
196 m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
198 qDebug() << "Signature" << result[SIGNATURE].toString();
199 m_loginCredentials.setSig(result[SIGNATURE].toString());
202 m_freshLogin = false;
203 emit saveCookiesRequest();
204 emit credentialsReady(m_loginCredentials);
209 } else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0) {
210 qDebug() << "login failure";
211 qDebug() << callbackUrl;
212 clearAccountInformation(true);
214 emit error(ErrorContext::SITUARE, SituareError::LOGIN_FAILED);
217 emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
219 } else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
220 qDebug() << "correct loginPage";
222 qDebug() << "totally wrong webPage";
223 // we should not get a wrong page at this point
224 emit error(ErrorContext::SITUARE, SituareError::LOGIN_FAILED);
227 qDebug() << " Loading of page failed invalid URL" << endl;
228 // we should not get a wrong page at this point
229 emit error(ErrorContext::SITUARE, SituareError::LOGIN_FAILED);
234 void FacebookAuthentication::urlChanged(const QUrl &url)
236 qWarning() << __PRETTY_FUNCTION__ << url.toString();
238 // if login succeeded
239 if (url.toString().startsWith(REDIRECT_URI)) {
240 const QString accessToken = parseAccessToken(url);
241 qWarning() << __PRETTY_FUNCTION__ << "parsed access token:" << accessToken;
242 if (!accessToken.isEmpty())
243 emit loggedIn(accessToken);