Removing some old code, disabled the api=2.0 parameter
[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 <QSettings>
29 #include <QStringList>
30 #include <QVariantMap>
31
32 #ifdef Q_WS_MAEMO_5
33 #include <QMaemo5InformationBox>
34 #endif // Q_WS_MAEMO_5
35
36 #include "common.h"
37 #include "error.h"
38 #include "facebookcommon.h"
39 #include "ui/facebookloginbrowser.h"
40
41 #include "facebookauthentication.h"
42
43 const QString REDIRECT_URI = "http://www.facebook.com/connect/login_success.html";
44
45 FacebookAuthentication::FacebookAuthentication(QObject *parent)
46     : QObject(parent),
47       m_browser(0)
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51 }
52
53 void FacebookAuthentication::browserDestroyed()
54 {
55     qWarning() << __PRETTY_FUNCTION__;
56
57     ///< @todo (HIGH) Is newer called!
58
59     m_browser = 0;
60 }
61
62 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
63 {
64     qDebug() << __PRETTY_FUNCTION__;
65
66     ///< @todo (HIGH) clear session from SituareService
67     QSettings settings(DIRECTORY_NAME, FILE_NAME);
68
69     if(!keepUsername) {
70         settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
71         settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
72     }
73
74     settings.remove(COOKIES);
75     settings.remove(USER_UNSEND_MESSAGE);
76     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
77 }
78
79 void FacebookAuthentication::loadFinished(bool ok)
80 {
81     qWarning() << __PRETTY_FUNCTION__ << ok;
82
83     ///< @todo show browsed window if url != redirect url
84 }
85
86 void FacebookAuthentication::login()
87 {
88     qWarning() << __PRETTY_FUNCTION__;
89
90     emit buildLoginBrowser();
91 }
92
93 QString FacebookAuthentication::parseSession(const QUrl &url)
94 {
95     qWarning() << __PRETTY_FUNCTION__;
96
97     const QString BEGIN("session={");
98     const QString END("}");
99
100     QString urlString = url.toString();
101
102     int begin = urlString.indexOf(BEGIN);
103     int end = urlString.indexOf(END, begin);
104
105     if ((begin > -1) && (end > -1))
106         return urlString.mid(begin, end - begin + 1);
107     else
108         return QString();
109 }
110
111 void FacebookAuthentication::setBrowser(FacebookLoginBrowser *browser)
112 {
113     qWarning() << __PRETTY_FUNCTION__;
114
115     m_browser = browser;
116
117     if (m_browser) {
118         connect(m_browser, SIGNAL(loadFinished(bool)),
119                 this, SLOT(loadFinished(bool)));
120
121         connect(m_browser, SIGNAL(urlChanged(QUrl)),
122                 this, SLOT(urlChanged(QUrl)));
123
124         connect(m_browser, SIGNAL(destroyed()),
125                 this, SLOT(browserDestroyed()));
126
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"));
128
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");
139
140         m_browser->load(QUrl(url));
141     }
142 }
143
144 void FacebookAuthentication::urlChanged(const QUrl &url)
145 {
146     qWarning() << __PRETTY_FUNCTION__ << url.toString();
147
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);
154     }
155 }