Merge branch 'master' into settings_auto_update
[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 <QtDebug>
25 #include <QDateTime>
26 #include <QSettings>
27 #include <QStringList>
28 #include <QVariantMap>
29
30 #ifdef Q_WS_MAEMO_5
31 #include <QMaemo5InformationBox>
32 #endif // Q_WS_MAEMO_5
33
34 #include "facebookauthentication.h"
35 #include "facebookcommon.h"
36 #include "../common.h"
37 #include "parser.h"
38
39 FacebookAuthentication::FacebookAuthentication(QObject *parent)
40     : QObject(parent),
41     m_freshLogin(false)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45 }
46
47 void FacebookAuthentication::clearAccountInformation(bool keepUsername)
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51     m_loginCredentials.clearCredentials();
52     QSettings settings(DIRECTORY_NAME, FILE_NAME);
53
54     if(!keepUsername)
55         settings.remove(USERNAME);
56
57     settings.remove(COOKIES);
58 }
59
60 const QString FacebookAuthentication::loadUsername()
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     QSettings settings(DIRECTORY_NAME, FILE_NAME);
65     return settings.value(USERNAME, EMPTY).toString();
66 }
67
68 FacebookCredentials FacebookAuthentication::loginCredentials() const
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71     return m_loginCredentials;
72 }
73
74 void FacebookAuthentication::saveUsername(const QString &username)
75 {
76     qDebug() << __PRETTY_FUNCTION__;
77
78     QSettings settings(DIRECTORY_NAME, FILE_NAME);
79     settings.setValue(USERNAME, username);
80 }
81
82 void FacebookAuthentication::start()
83 {
84     qDebug() << __PRETTY_FUNCTION__;
85
86     QSettings settings(DIRECTORY_NAME, FILE_NAME);
87
88     QStringList cookies = settings.value(COOKIES).toStringList();
89     if(!cookies.isEmpty()) {
90         emit loginUsingCookies();
91     }
92     else {
93         m_freshLogin = true;
94         emit newLoginRequest();
95     }
96 }
97
98 bool FacebookAuthentication::updateCredentials(const QUrl &url)
99 {
100     qWarning() << __PRETTY_FUNCTION__ << url.toString();
101
102     bool found = false;
103
104     if (url.isValid()){
105          qDebug() << "url is valid";
106
107         QString callbackUrl = url.toString();
108         qDebug() << "callbackUrl:  " << callbackUrl.toAscii();
109
110         if (callbackUrl.indexOf(LOGIN_SUCCESS_REPLY) == 0) {
111             qDebug() << "login success";
112
113             // let's find out session credentials
114             if(callbackUrl.contains(SESSION_KEY)) {
115
116                 QJson::Parser parser;
117                 bool ok;
118
119                 // split string into string part and json part
120                 QStringList list = url.toString().split("=");
121
122                 for(int i=0;i<list.count();i++) {
123                     // if string starts with json item
124                     if(list.at(i).startsWith("{")) {
125                         QByteArray jsonString = list.at(i).toAscii();
126                         QVariantMap result = parser.parse (jsonString, &ok).toMap();
127                         if (!ok) {
128
129                             qFatal("An error occurred during parsing");
130                             exit (1);
131                         }
132                         qDebug() << "Session Key" << result[SESSION_KEY].toString();
133                         m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
134
135 //                        // commeted out until qjson parser can handle 64-bit integers
136 //                        qDebug() << "userID" << result[USER_ID].toString();
137 //                        m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
138
139                         // dirty fix, get user id from session_key
140                         QStringList list = result[SESSION_KEY].toString().split("-");
141                         m_loginCredentials.setUserID(list.at(1));
142                         qDebug() << m_loginCredentials.userID();
143
144                         qDebug() << "Expires" << result[EXPIRES].toString();
145                         m_loginCredentials.setExpires(result[EXPIRES].toString());
146
147                         qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
148                         m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
149
150                         qDebug() << "Signature" << result[SIGNATURE].toString();
151                         m_loginCredentials.setSig(result[SIGNATURE].toString());
152                     }
153                 }
154                 found = true;
155                 m_freshLogin = false;
156                 emit saveCookiesRequest();
157             }
158             emit credentialsReady(m_loginCredentials);
159         }
160         else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
161             qWarning() << "login failure" << endl;
162             qDebug() << callbackUrl;
163             clearAccountInformation(true);
164             if(m_freshLogin) {
165                 emit error(LOGIN_FAILED);
166                 emit loginFailure();
167             }
168             else {
169                 m_freshLogin = true;
170                 emit error(SESSION_EXPIRED);
171             }
172         }
173         else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
174             qDebug() << "correct loginPage";
175         }
176         else {
177             qDebug() << "totally wrong webPage";
178             // we should not get a wrong page at this point
179             emit loginFailure();
180         }
181     }
182     else {
183         qDebug() << " Loading of page failed invalid URL" << endl;
184         // we should not get a wrong page at this point
185         emit loginFailure();
186         return false;
187     }
188     return found;
189 }