Cleaning out old login code ongoing
[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 "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
40 #include "facebookauthentication.h"
41
42 const QString REDIRECT_URI = "http://www.facebook.com/connect/login_success.html";
43
44 FacebookAuthentication::FacebookAuthentication(QObject *parent)
45     : QObject(parent)
46 {
47     qDebug() << __PRETTY_FUNCTION__;
48
49 }
50
51 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     ///< @todo (HIGH) clear session from SituareService
56     QSettings settings(DIRECTORY_NAME, FILE_NAME);
57
58     if(!keepUsername) {
59         settings.remove(SETTINGS_AUTOMATIC_UPDATE_ENABLED);
60         settings.remove(SETTINGS_AUTOMATIC_UPDATE_INTERVAL);
61     }
62
63     settings.remove(COOKIES);
64     settings.remove(USER_UNSEND_MESSAGE);
65     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
66 }
67
68 void FacebookAuthentication::loadFinished(bool ok)
69 {
70     qWarning() << __PRETTY_FUNCTION__ << ok;
71
72     ///< @todo show browsed window if url != redirect url
73 }
74
75 QString FacebookAuthentication::parseSession(const QUrl &url)
76 {
77     qWarning() << __PRETTY_FUNCTION__;
78
79     const QString BEGIN("session={");
80     const QString END("}");
81
82     QString urlString = url.toString();
83
84     int begin = urlString.indexOf(BEGIN);
85     int end = urlString.indexOf(END, begin);
86
87     if ((begin > -1) && (end > -1))
88         return urlString.mid(begin, end - begin +1);
89     else
90         return QString();
91 }
92
93 void FacebookAuthentication::urlChanged(const QUrl &url)
94 {
95     qWarning() << __PRETTY_FUNCTION__ << url.toString();
96
97     // if login succeeded
98     if (url.toString().startsWith(REDIRECT_URI)) {
99         const QString session = parseSession(url);
100         qWarning() << __PRETTY_FUNCTION__ << "parsed session:" << session;
101         if (!session.isEmpty())
102             emit loggedIn(session);
103     }
104 }