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,
24 #include <qjson/parser.h>
28 #include <QNetworkReply>
30 #include <QStringList>
31 #include <QVariantMap>
35 #include <QMaemo5InformationBox>
36 #endif // Q_WS_MAEMO_5
40 #include "network/networkcookiejar.h"
41 #include "situareservice/situarecommon.h"
42 #include "ui/mainwindow.h"
44 #include "facebookauthentication.h"
46 const QString FB_LOGIN_SUCCESS_URL = "http://www.facebook.com/connect/login_success.html";
47 const QString FB_LOGIN_URL = "https://www.facebook.com/login.php";
49 FacebookAuthentication::FacebookAuthentication(MainWindow *mainWindow, QObject *parent)
53 m_mainWindow(mainWindow)
55 qDebug() << __PRETTY_FUNCTION__;
58 void FacebookAuthentication::browserDestroyed()
60 qWarning() << __PRETTY_FUNCTION__;
62 m_mainWindow->toggleProgressIndicator(false);
66 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
68 qDebug() << __PRETTY_FUNCTION__;
70 QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
73 settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
74 settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
77 settings.remove(USER_UNSEND_MESSAGE);
78 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
80 NetworkCookieJar::clearCookiesSetting();
83 void FacebookAuthentication::destroyLogin()
85 qWarning() << __PRETTY_FUNCTION__;
87 m_mainWindow->destroyLoginDialog();
88 m_browser->deleteLater();
91 bool FacebookAuthentication::isLoggedIn()
93 qWarning() << __PRETTY_FUNCTION__;
98 void FacebookAuthentication::login()
100 qWarning() << __PRETTY_FUNCTION__;
103 m_browser = new QWebView(m_mainWindow);
106 m_browser->page()->networkAccessManager()->setCookieJar(new NetworkCookieJar());
108 connect(m_browser, SIGNAL(urlChanged(QUrl)),
109 this, SLOT(urlChanged(QUrl)));
111 connect(m_browser, SIGNAL(destroyed(QObject*)),
112 this, SLOT(browserDestroyed()));
114 connect(m_browser->page()->networkAccessManager(),
115 SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)),
116 this, SLOT(sslErrors(QNetworkReply*, QList<QSslError>)));
118 connect(m_browser->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
119 this, SLOT(networkReplyHandler(QNetworkReply*)));
124 QString url = FB_LOGIN_URL + "?";
125 url.append("api_key=" + API_KEY +"&");
126 url.append("display=touch&");
127 url.append("fbconnect=1&");
128 url.append("next=" + FB_LOGIN_SUCCESS_URL + "&");
129 url.append("return_session=1&");
130 url.append("session_version=3&");
131 url.append("v=1.0&");
132 url.append("req_perms=publish_stream");
134 m_browser->load(QUrl(url));
136 m_mainWindow->toggleProgressIndicator(true);
140 void FacebookAuthentication::logOut()
142 qWarning() << __PRETTY_FUNCTION__;
144 clearAccountInformation();
149 void FacebookAuthentication::networkReplyHandler(QNetworkReply *reply)
151 qWarning() <<__PRETTY_FUNCTION__;
153 if ((reply->error() != QNetworkReply::NoError)
154 && (reply->error() != QNetworkReply::OperationCanceledError)) {
156 qCritical() << __PRETTY_FUNCTION__ << "error:" << reply->error() << reply->errorString();
157 emit error(ErrorContext::NETWORK, reply->error());
162 QString FacebookAuthentication::parseSession(const QUrl &url)
164 qWarning() << __PRETTY_FUNCTION__;
166 const QString BEGIN("session={");
167 const QString END("}");
169 QString urlString = url.toString();
171 int begin = urlString.indexOf(BEGIN);
172 int end = urlString.indexOf(END, begin);
174 if ((begin > -1) && (end > -1))
175 return urlString.mid(begin, end - begin + 1);
180 void FacebookAuthentication::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
182 qWarning() << __PRETTY_FUNCTION__;
185 reply->ignoreSslErrors();
188 void FacebookAuthentication::urlChanged(const QUrl &url)
190 qWarning() << __PRETTY_FUNCTION__ << url.toString();
194 * Login with cookie failed:
195 1) http://m.facebook.com/login.php?api_key=cf77865a5070f2c2ba3b52cbf3371579&cancel_url=http://www.facebook.com/connect/login_failure.html&display=touch&fbconnect=1&next=http://www.facebook.com/connect/uiserver.php?app_id=286811277465&next=http%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&display=touch&cancel_url=http%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_failure.html&perms=publish_stream&return_session=1&session_version=3&fbconnect=1&canvas=0&legacy_return=1&method=permissions.request&return_session=1&session_version=3&v=1.0&req_perms=publish_stream&app_id=286811277465&refsrc=http://www.facebook.com/login.php&fbb=ra985c5e9
197 * Login with cookie succeeded:
198 1) http://www.facebook.com/connect/uiserver.php?app_id=286811277465&next=http://www.facebook.com/connect/login_success.html&display=touch&cancel_url=http://www.facebook.com/connect/login_failure.html&perms=publish_stream&return_session=1&session_version=3&fbconnect=1&canvas=0&legacy_return=1&method=permissions.request&session={"session_key":"2.iHXi5fLKlHktva2R71xSAw__.3600.1289228400-100001006647973","uid":100001006647973,"expires":1289228400,"secret":"q4_Hn5qRdxnVT_qh3ztv5w__","sig":"c9d29ca857bacec48b952e7d2826a3ca"}&fbb=rb28f24e5
199 2) http://www.facebook.com/connect/login_success.html?perms=publish_stream&selected_profiles=100001006647973&session={"session_key":"2.iHXi5fLKlHktva2R71xSAw__.3600.1289228400-100001006647973","uid":"100001006647973","expires":1289228400,"secret":"q4_Hn5qRdxnVT_qh3ztv5w__","access_token":"286811277465|2.iHXi5fLKlHktva2R71xSAw__.3600.1289228400-100001006647973|LVTHGW82A98SGvv6Fl43DlCrFT0","sig":"8edd8d611047bcd162abbe9983b25a56"}
202 if (!url.toString().contains("session={")) {
203 // url parameter doesn't contain session data, so login with cookies failed
204 qWarning() << __PRETTY_FUNCTION__ << "working credentials required";
205 m_mainWindow->buildLoginDialog(m_browser);
206 } else if (url.toString().startsWith(FB_LOGIN_SUCCESS_URL)) {
208 const QString session = parseSession(url);
209 qWarning() << __PRETTY_FUNCTION__ << "login finished, parsed session:" << session;
210 if (!session.isEmpty()) {
213 emit loggedIn(session);
217 qWarning() << __PRETTY_FUNCTION__ << "credentials accepted, getting the access_token";