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