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>
29 #include <QStringList>
30 #include <QVariantMap>
33 #include <QMaemo5InformationBox>
34 #endif // Q_WS_MAEMO_5
38 #include "facebookcommon.h"
39 #include "ui/facebookloginbrowser.h"
41 #include "facebookauthentication.h"
43 const QString REDIRECT_URI = "http://www.facebook.com/connect/login_success.html";
45 FacebookAuthentication::FacebookAuthentication(QObject *parent)
49 qDebug() << __PRETTY_FUNCTION__;
53 void FacebookAuthentication::browserDestroyed()
55 qWarning() << __PRETTY_FUNCTION__;
57 ///< @todo (HIGH) Is newer called!
62 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
64 qDebug() << __PRETTY_FUNCTION__;
66 ///< @todo (HIGH) clear session from SituareService
67 QSettings settings(DIRECTORY_NAME, FILE_NAME);
70 settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
71 settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
74 settings.remove(COOKIES);
75 settings.remove(USER_UNSEND_MESSAGE);
76 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
79 void FacebookAuthentication::loadFinished(bool ok)
81 qWarning() << __PRETTY_FUNCTION__ << ok;
83 ///< @todo show browsed window if url != redirect url
86 void FacebookAuthentication::login()
88 qWarning() << __PRETTY_FUNCTION__;
90 emit buildLoginBrowser();
93 QString FacebookAuthentication::parseSession(const QUrl &url)
95 qWarning() << __PRETTY_FUNCTION__;
97 const QString BEGIN("session={");
98 const QString END("}");
100 QString urlString = url.toString();
102 int begin = urlString.indexOf(BEGIN);
103 int end = urlString.indexOf(END, begin);
105 if ((begin > -1) && (end > -1))
106 return urlString.mid(begin, end - begin + 1);
111 void FacebookAuthentication::setBrowser(FacebookLoginBrowser *browser)
113 qWarning() << __PRETTY_FUNCTION__;
118 connect(m_browser, SIGNAL(loadFinished(bool)),
119 this, SLOT(loadFinished(bool)));
121 connect(m_browser, SIGNAL(urlChanged(QUrl)),
122 this, SLOT(urlChanged(QUrl)));
124 connect(m_browser, SIGNAL(destroyed()),
125 this, SLOT(browserDestroyed()));
127 // browser->load(QUrl("https://graph.facebook.com/oauth/authorize?client_id=4197c64da2fb6b927236feaea32d7d81&redirect_uri=http://www.facebook.com/connect/login_success.html&display=touch&type=user_agent"));
129 QString url = "https://www.facebook.com/login.php?";
130 url.append("api_key=cf77865a5070f2c2ba3b52cbf3371579&"); ///< @todo hard coded test server api key
131 url.append("cancel_url=http://www.facebook.com/connect/login_failure.html&");
132 url.append("display=touch&");
133 url.append("fbconnect=1&");
134 url.append("next=http://www.facebook.com/connect/login_success.html&");
135 url.append("return_session=1&");
136 url.append("session_version=3&");
137 url.append("v=1.0&");
138 url.append("req_perms=publish_stream");
140 m_browser->load(QUrl(url));
144 void FacebookAuthentication::urlChanged(const QUrl &url)
146 qWarning() << __PRETTY_FUNCTION__ << url.toString();
148 // if login succeeded
149 if (url.toString().startsWith(REDIRECT_URI)) {
150 const QString session = parseSession(url);
151 qWarning() << __PRETTY_FUNCTION__ << "parsed session:" << session;
152 if (!session.isEmpty())
153 emit loggedIn(session);