Implemented NetworkCookieJar::clearCookiesSetting()
[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 "network/networkcookiejar.h"
40 #include "situareservice/situarecommon.h"
41 #include "ui/facebookloginbrowser.h"
42
43 #include "facebookauthentication.h"
44
45 const QString FB_LOGIN_SUCCESS_URL = "http://www.facebook.com/connect/login_success.html";
46 const QString FB_LOGIN_URL = "https://www.facebook.com/login.php";
47
48 FacebookAuthentication::FacebookAuthentication(QObject *parent)
49     : QObject(parent),
50       m_browser(0)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53 }
54
55 void FacebookAuthentication::browserDestroyed()
56 {
57     qWarning() << __PRETTY_FUNCTION__;
58
59     ///< @todo (HIGH) Is newer called!
60
61     m_browser = 0;
62 }
63
64 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
65 {
66     qDebug() << __PRETTY_FUNCTION__;
67
68     ///< @todo (HIGH) clear session from SituareService
69     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
70
71     if(!keepUsername) {
72         settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
73         settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
74     }
75
76     settings.remove(USER_UNSEND_MESSAGE);
77     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
78
79     NetworkCookieJar::clearCookiesSetting();
80 }
81
82 void FacebookAuthentication::loadFinished(bool ok)
83 {
84     qWarning() << __PRETTY_FUNCTION__ << ok;
85
86     ///< @todo show browsed window if url != redirect url
87 }
88
89 void FacebookAuthentication::login()
90 {
91     qWarning() << __PRETTY_FUNCTION__;
92
93     emit buildLoginBrowser();
94 }
95
96 void FacebookAuthentication::networkReplyHandler(QNetworkReply *reply)
97 {
98     qWarning() <<__PRETTY_FUNCTION__;
99
100     if (reply->error() != QNetworkReply::NoError) {
101         qCritical() << __PRETTY_FUNCTION__ << "error:" << reply->error() << reply->errorString();
102         /// @todo Emit error signal
103     }
104 }
105
106 QString FacebookAuthentication::parseSession(const QUrl &url)
107 {
108     qWarning() << __PRETTY_FUNCTION__;
109
110     const QString BEGIN("session={");
111     const QString END("}");
112
113     QString urlString = url.toString();
114
115     int begin = urlString.indexOf(BEGIN);
116     int end = urlString.indexOf(END, begin);
117
118     if ((begin > -1) && (end > -1))
119         return urlString.mid(begin, end - begin + 1);
120     else
121         return QString();
122 }
123
124 void FacebookAuthentication::setBrowser(FacebookLoginBrowser *browser)
125 {
126     qWarning() << __PRETTY_FUNCTION__;
127
128     m_browser = browser;
129
130     if (m_browser) {
131         connect(m_browser, SIGNAL(loadFinished(bool)),
132                 this, SLOT(loadFinished(bool)));
133
134         connect(m_browser, SIGNAL(urlChanged(QUrl)),
135                 this, SLOT(urlChanged(QUrl)));
136
137         connect(m_browser, SIGNAL(destroyed(QObject*)),
138                 this, SLOT(browserDestroyed()));
139
140         connect(m_browser->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
141                 this, SLOT(networkReplyHandler(QNetworkReply*)));
142
143         QString url = FB_LOGIN_URL + "?";
144         url.append("api_key=" + API_KEY +"&");
145         url.append("display=touch&");
146         url.append("fbconnect=1&");
147         url.append("next=" + FB_LOGIN_SUCCESS_URL + "&");
148         url.append("return_session=1&");
149         url.append("session_version=3&");
150         url.append("v=1.0&");
151         url.append("req_perms=publish_stream");
152
153         m_browser->load(QUrl(url));
154     }
155 }
156
157 void FacebookAuthentication::urlChanged(const QUrl &url)
158 {
159     qWarning() << __PRETTY_FUNCTION__ << url.toString();
160
161     /*
162       Redirects:
163         * Login with cookie failed:
164             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
165
166         * Login with cookie succeeded:
167             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
168             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"}
169      */
170
171     if (!url.toString().contains("session={")) {
172         // url parameter doesn't contain session data, so login with cookies failed
173         qWarning() << __PRETTY_FUNCTION__ << "working credentials required";
174         m_browser->show();
175     } else if (url.toString().startsWith(FB_LOGIN_SUCCESS_URL)) {
176         // login succeeded
177         const QString session = parseSession(url);
178         qWarning() << __PRETTY_FUNCTION__ << "login finished, parsed session:" << session;
179         if (!session.isEmpty())
180             emit loggedIn(session);
181     }
182     else {
183         qWarning() << __PRETTY_FUNCTION__ << "credentials accepted, getting the access_token";
184     }
185 }