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"
38 FacebookAuthentication::FacebookAuthentication(QObject *parent)
43 qDebug() << __PRETTY_FUNCTION__;
45 readCredentials(m_loginCredentials);
48 void FacebookAuthentication::start()
50 qDebug() << __PRETTY_FUNCTION__;
52 if (!verifyCredentials(m_loginCredentials)) {
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);
62 emit newLoginRequest(formLoginPageUrl(list));
65 emit credentialsReady(m_loginCredentials);
68 bool FacebookAuthentication::updateCredentials(const QUrl &url)
70 qDebug() << __PRETTY_FUNCTION__;
75 qDebug() << "url is valid";
77 QString callbackUrl = url.toString();
78 qDebug() << "callbackUrl: " << callbackUrl.toAscii();
80 if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
81 qDebug() << "login success";
83 // let's find out session credentials
84 if(callbackUrl.contains(SESSION_KEY)) {
89 // split string into string part and json part
90 QStringList list = url.toString().split("=");
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();
99 qFatal("An error occurred during parsing");
102 qDebug() << "Session Key" << result[SESSION_KEY].toString();
103 m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
105 qDebug() << "userID" << result[USER_ID].toString();
106 m_loginCredentials.setUserID(result[USER_ID].toString());
108 qDebug() << "Expires" << result[EXPIRES].toString();
109 m_loginCredentials.setExpires(result[EXPIRES].toString());
111 qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
112 m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
114 qDebug() << "Signature" << result[SIGNATURE].toString();
115 m_loginCredentials.setSig(result[SIGNATURE].toString());
120 writeCredentials(m_loginCredentials);
121 emit credentialsReady(m_loginCredentials);
123 else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
124 qWarning() << "login failure" << endl;
125 qDebug() << callbackUrl;
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) {
134 else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
135 qDebug() << "correct loginPage";
138 qDebug() << "totally wrong webPage";
139 // we should not get a wrong page at this point
144 qDebug() << " Loading of page failed invalid URL" << endl;
145 // we should not get a wrong page at this point
152 void FacebookAuthentication::writeCredentials(const FacebookCredentials &credentials)
154 qDebug() << __PRETTY_FUNCTION__;
155 QSettings settings(DIRECTORY_NAME, FILE_NAME);
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());
164 void FacebookAuthentication::readCredentials(FacebookCredentials &credentialsFromFile)
166 qDebug() << __PRETTY_FUNCTION__;
168 QSettings settings(DIRECTORY_NAME, FILE_NAME);
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());
177 FacebookCredentials FacebookAuthentication::loginCredentials() const
179 qDebug() << __PRETTY_FUNCTION__;
180 return m_loginCredentials;
183 bool FacebookAuthentication::verifyCredentials(const FacebookCredentials &credentials) const
185 qDebug() << __PRETTY_FUNCTION__;
187 // if expires value is 0, then credentials are valid forever
188 if(credentials.expires() == "0") {
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();
199 QDateTime currentTime;
200 currentTime = QDateTime::currentDateTime();
201 QString currentTimeString = currentTime.toString(dateTimeFormat);
202 qDebug() << currentTimeString.toAscii();
204 return currentTime < expireTime;
208 QUrl FacebookAuthentication::formLoginPageUrl(const QStringList &urlParts) const
210 qDebug() << __PRETTY_FUNCTION__;
212 return QUrl(urlParts.join(EMPTY));