Added FriendGroupItem and FriendItemsHandler classes
[situare] / src / situareservice / situareservice.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QtAlgorithms>
23 #include <QDebug>
24 #include <QtGlobal>
25 #include <QString>
26 #include <QStringList>
27 #include <QPixmap>
28 #include <QNetworkReply>
29 #include "situareservice.h"
30 #include "situarecommon.h"
31 #include "../cookiehandler/cookiehandler.h"
32 #include "parser.h"
33
34 SituareService::SituareService(QObject *parent)
35         : QObject(parent)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     m_networkManager = new QNetworkAccessManager;
40     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
41
42     m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
43     connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
44     connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
45             SLOT(imageReceived(QUrl, QPixmap)));
46     connect(m_imageFetcher, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
47
48     m_user = 0;
49 }
50
51 SituareService::~SituareService()
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     delete m_networkManager;
56     m_networkManager = 0;
57     if(m_user) {
58         delete m_user;
59         m_user = 0;
60     }
61     delete m_imageFetcher;
62     m_imageFetcher = 0;
63
64     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
65     m_friendsList.clear();
66 }
67
68 void SituareService::debugData()
69 {
70     qWarning() << __PRETTY_FUNCTION__;
71     QFile file("/home/ramosam-local/situare_debug_data.txt");
72     qWarning() << __PRETTY_FUNCTION__ << "file size: " << file.size();
73     qWarning() << __PRETTY_FUNCTION__ << "file open success?" << file.open(QIODevice::ReadOnly);
74     QByteArray debugReplyArray = file.readAll();
75     qWarning() << __PRETTY_FUNCTION__ << "did read" << debugReplyArray.size();
76     parseUserData(debugReplyArray);
77 }
78
79 void SituareService::fetchLocations()
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     CookieHandler cookieHandler;
84
85     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
86                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
87                                               m_credentials.sig(), EN_LOCALE);
88
89     QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
90     sendRequest(url, COOKIE, cookie);
91 }
92
93 void SituareService::reverseGeo(const QPointF &coordinates)
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     CookieHandler cookieHandler;
98
99     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
100                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
101                                               m_credentials.sig(), EN_LOCALE);
102
103     QString urlParameters = formUrlParameters(coordinates);
104     QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
105
106     sendRequest(url, COOKIE, cookie);
107 }
108
109 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
110                                     const bool &publish)
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     CookieHandler cookieHandler;
115
116     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
117                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
118                                               m_credentials.sig(), EN_LOCALE);
119
120
121     QString publishValue;
122     if(publish) {
123         publishValue = PUBLISH_TRUE;
124     }
125     else {
126         publishValue = PUBLISH_FALSE;
127     }
128     QString urlParameters = formUrlParameters(coordinates, status, publishValue);
129     QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
130
131     sendRequest(url, COOKIE, cookie);
132 }
133
134 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137     QString urlString;
138
139     urlString.append(baseUrl);
140     urlString.append(phpScript);
141     if(!urlParameters.isEmpty())
142         urlString.append(urlParameters);
143
144     QUrl url = QUrl(urlString);
145
146     qDebug() << url;
147
148     return url;
149 }
150
151 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
152 {
153     QString parameters;
154
155     parameters.append(QUESTION_MARK);
156     parameters.append(LATITUDE);
157     parameters.append(EQUAL_MARK);
158     parameters.append(QString::number(coordinates.x()));
159     parameters.append(AMBERSAND_MARK);
160     parameters.append(LONGTITUDE);
161     parameters.append(EQUAL_MARK);
162     parameters.append(QString::number(coordinates.y()));
163
164     if(publish.compare(PUBLISH_TRUE) == 0) {
165         parameters.append(AMBERSAND_MARK);
166         parameters.append(PUBLISH);
167         parameters.append(EQUAL_MARK);
168         parameters.append(PUBLISH_TRUE);
169     }
170     else if(publish.compare(PUBLISH_FALSE) == 0) {
171         parameters.append(AMBERSAND_MARK);
172         parameters.append(PUBLISH);
173         parameters.append(EQUAL_MARK);
174         parameters.append(PUBLISH_FALSE);
175     }
176
177     if(!status.isEmpty()) {
178         parameters.append(AMBERSAND_MARK);
179         parameters.append(DATA);
180         parameters.append(EQUAL_MARK);
181         parameters.append(status);
182     }
183
184     return parameters;
185 }
186
187 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
188 {
189     qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
190
191     QNetworkRequest request;
192
193     request.setUrl(url);
194     request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
195
196     QNetworkReply *reply = m_networkManager->get(request);
197
198     m_currentRequests.append(reply);
199 }
200
201 void SituareService::requestFinished(QNetworkReply *reply)
202 {
203     qDebug() << __PRETTY_FUNCTION__;
204
205     QUrl url = reply->url();
206     qDebug() << "BytesAvailable: " << reply->bytesAvailable();
207     if (reply->error()) {
208         qDebug() << reply->errorString();
209         emit error(reply->errorString());
210     }
211     else {
212         qint64 max = reply->size();
213         QByteArray replyArray = reply->read(max);
214         qDebug() << "Reply from: " << url << "reply " << replyArray;
215         // ToDo: results handling includes Situare's errors
216         // works like situare's error handling i.e. both lat & lon are missing/wrong
217         // -> we get only error for wrong lon
218         if(replyArray == ERROR_LAT.toAscii()) {
219             qDebug() << "Error: " << ERROR_LAT;
220             emit error(replyArray);
221         }
222         else if(replyArray == ERROR_LON.toAscii()) {
223             qDebug() << "Error: " << ERROR_LON;
224             emit error(replyArray);
225         }
226         else if(replyArray.contains(ERROR_SESSION.toAscii())) {
227             qDebug() << "Error: " << ERROR_SESSION;
228             emit error(replyArray);
229         }
230         else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
231             qDebug() << "JSON string";
232             parseUserData(replyArray);
233         }
234         else if(replyArray == "") {
235             qDebug() << "No error, update was successful";
236             emit updateWasSuccessful();
237         }
238         else {
239             // Street address ready
240             QString address(replyArray);
241             emit reverseGeoReady(address);
242         }
243     }
244     m_currentRequests.removeAll(reply);
245     reply->deleteLater();
246 }
247
248 void SituareService::credentialsReady(const FacebookCredentials &credentials)
249 {
250     qDebug() << __PRETTY_FUNCTION__;
251     m_credentials = credentials;
252     
253 }
254
255 void SituareService::parseUserData(const QByteArray &jsonReply)
256 {
257     qDebug() << __PRETTY_FUNCTION__;
258
259     m_visited = 0;
260     m_nbrOfImages = 0;
261     m_defaultImage = false;
262     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
263     m_friendsList.clear();
264
265     if(m_user) {
266         delete m_user;
267         m_user = 0;
268     }
269
270     QJson::Parser parser;
271     bool ok;
272
273     QVariantMap result = parser.parse (jsonReply, &ok).toMap();
274     if (!ok) {
275
276         qFatal("An error occurred during parsing");
277         exit (1);
278     }
279
280     QVariant userVariant = result.value("user");
281     QMap<QString, QVariant> userMap = userVariant.toMap();
282
283     QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
284
285     QUrl imageUrl = userMap["profile_pic"].toUrl();
286
287     if(imageUrl.isEmpty()) {
288         // user doesn't have profile image, so we need to get him a silhouette image
289         m_defaultImage = true;
290     }
291
292     m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
293                   userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
294                   true, userMap["uid"].toString());
295
296     foreach (QVariant friendsVariant, result["friends"].toList()) {
297       QMap<QString, QVariant> friendMap = friendsVariant.toMap();
298       QVariant distance = friendMap["distance"];
299       QMap<QString, QVariant> distanceMap = distance.toMap();
300
301       QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
302
303       QUrl imageUrl = friendMap["profile_pic"].toUrl();
304
305       if(imageUrl.isEmpty()) {
306           // friend doesn't have profile image, so we need to get him a silhouette image
307           m_defaultImage = true;
308       }
309
310       User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
311                             friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
312                             false, friendMap["uid"].toString(), distanceMap["units"].toString(),
313                             distanceMap["value"].toDouble());
314
315       m_friendsList.append(user);
316     }
317     addProfileImages();
318 }
319
320 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
321 {
322     qDebug() << __PRETTY_FUNCTION__;
323     qDebug() << "Image URL: " << url << " size :" << image.size();
324
325     // assign facebook silhouette image to all who doesn't have a profile image
326     if(url == QUrl(SILHOUETTE_URL)) {
327         if(m_user->profileImageUrl().isEmpty()) {
328             m_user->setProfileImage(image);
329         }
330         for(int i=0;i < m_friendsList.count();i++) {
331             if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
332                 m_friendsList.at(i)->setProfileImage(image);
333             }
334         }
335     }
336
337     if(m_user->profileImageUrl() == url) {
338         m_user->setProfileImage(image);
339     }
340
341     for(int i=0;i<m_friendsList.count();i++) {
342         if(m_friendsList.at(i)->profileImageUrl() == url) {
343             m_friendsList.at(i)->setProfileImage(image);
344             m_nbrOfImages++; // indicates how many friend profile images has been downloaded
345         }
346     }
347
348     if(m_nbrOfImages == m_visited) {
349         qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
350         qDebug() << "emit userDataChanged";
351         emit userDataChanged(m_user, m_friendsList);
352     }
353 }
354
355 void SituareService::addProfileImages()
356 {
357     qDebug() << __PRETTY_FUNCTION__;
358
359     // reduce net traffic by sending only one download request for facebook silhouette image
360     if(m_defaultImage) {
361         emit fetchImage(QUrl(SILHOUETTE_URL));
362     }
363
364     if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
365         emit fetchImage(m_user->profileImageUrl());
366     }
367
368     for(int i=0;i<m_friendsList.count();i++) {
369         if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
370            m_friendsList.at(i)->profileImageUrl().isValid()) {
371             m_visited++; // indicates how many friends that have profile image
372             emit fetchImage(m_friendsList.at(i)->profileImageUrl());
373         }
374     }
375 }