Fixed FacebookLoginBrowser::destroyed() signal handling
[situare] / src / facebookservice / facebookauthentication.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Ville Tiensuu - ville.tiensuu@ixonos.com
6        Kaj Wallin - kaj.wallin@ixonos.com
7        Henri Lampela - henri.lampela@ixonos.com
8
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.
12
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.
17
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,
21    USA.
22 */
23
24 #include <qjson/parser.h>
25
26 #include <QtDebug>
27 #include <QDateTime>
28 #include <QNetworkReply>
29 #include <QSettings>
30 #include <QStringList>
31 #include <QVariantMap>
32
33 #ifdef Q_WS_MAEMO_5
34 #include <QMaemo5InformationBox>
35 #endif // Q_WS_MAEMO_5
36
37 #include "common.h"
38 #include "error.h"
39 #include "situareservice/situarecommon.h"
40 #include "ui/facebookloginbrowser.h"
41
42 #include "facebookauthentication.h"
43
44 const QString FB_LOGIN_SUCCESS_URL = "http://www.facebook.com/connect/login_success.html";
45 const QString FB_LOGIN_URL = "https://www.facebook.com/login.php";
46
47 FacebookAuthentication::FacebookAuthentication(QObject *parent)
48     : QObject(parent),
49       m_browser(0)
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52 }
53
54 void FacebookAuthentication::browserDestroyed()
55 {
56     qWarning() << __PRETTY_FUNCTION__;
57
58     ///< @todo (HIGH) Is newer called!
59
60     m_browser = 0;
61 }
62
63 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66
67     ///< @todo (HIGH) clear session from SituareService
68     QSettings settings(DIRECTORY_NAME, FILE_NAME);
69
70     if(!keepUsername) {
71         settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
72         settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
73     }
74
75     settings.remove(COOKIES);
76     settings.remove(USER_UNSEND_MESSAGE);
77     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
78 }
79
80 void FacebookAuthentication::loadFinished(bool ok)
81 {
82     qWarning() << __PRETTY_FUNCTION__ << ok;
83
84     ///< @todo show browsed window if url != redirect url
85 }
86
87 void FacebookAuthentication::login()
88 {
89     qWarning() << __PRETTY_FUNCTION__;
90
91     emit buildLoginBrowser();
92 }
93
94 void FacebookAuthentication::networkReplyHandler(QNetworkReply *reply)
95 {
96     qWarning() <<__PRETTY_FUNCTION__;
97
98     if (reply->error() != QNetworkReply::NoError) {
99         qCritical() << __PRETTY_FUNCTION__ << "error:" << reply->error() << reply->errorString();
100         /// @todo Emit error signal
101     }
102 }
103
104 QString FacebookAuthentication::parseSession(const QUrl &url)
105 {
106     qWarning() << __PRETTY_FUNCTION__;
107
108     const QString BEGIN("session={");
109     const QString END("}");
110
111     QString urlString = url.toString();
112
113     int begin = urlString.indexOf(BEGIN);
114     int end = urlString.indexOf(END, begin);
115
116     if ((begin > -1) && (end > -1))
117         return urlString.mid(begin, end - begin + 1);
118     else
119         return QString();
120 }
121
122 void FacebookAuthentication::setBrowser(FacebookLoginBrowser *browser)
123 {
124     qWarning() << __PRETTY_FUNCTION__;
125
126     m_browser = browser;
127
128     if (m_browser) {
129         connect(m_browser, SIGNAL(loadFinished(bool)),
130                 this, SLOT(loadFinished(bool)));
131
132         connect(m_browser, SIGNAL(urlChanged(QUrl)),
133                 this, SLOT(urlChanged(QUrl)));
134
135         connect(m_browser, SIGNAL(destroyed(QObject*)),
136                 this, SLOT(browserDestroyed()));
137
138         connect(m_browser->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
139                 this, SLOT(networkReplyHandler(QNetworkReply*)));
140
141         QString url = FB_LOGIN_URL + "?";
142         url.append("api_key=" + API_KEY +"&");
143         url.append("display=touch&");
144         url.append("fbconnect=1&");
145         url.append("next=" + FB_LOGIN_SUCCESS_URL + "&");
146         url.append("return_session=1&");
147         url.append("session_version=3&");
148         url.append("v=1.0&");
149         url.append("req_perms=publish_stream");
150
151         m_browser->load(QUrl(url));
152     }
153 }
154
155 void FacebookAuthentication::urlChanged(const QUrl &url)
156 {
157     qWarning() << __PRETTY_FUNCTION__ << url.toString();
158
159     /*
160       Redirects:
161         * Login with cookie failed:
162             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
163
164         * Login with cookie succeeded:
165             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
166             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"}
167      */
168
169     if (!url.toString().contains("session={")) {
170         // url parameter doesn't contain session data, so login with cookies failed
171         qWarning() << __PRETTY_FUNCTION__ << "working credentials required";
172         m_browser->show();
173     } else if (url.toString().startsWith(FB_LOGIN_SUCCESS_URL)) {
174         // login succeeded
175         const QString session = parseSession(url);
176         qWarning() << __PRETTY_FUNCTION__ << "login finished, parsed session:" << session;
177         if (!session.isEmpty())
178             emit loggedIn(session);
179     }
180     else {
181         qWarning() << __PRETTY_FUNCTION__ << "credentials accepted, getting the access_token";
182     }
183 }