2ed9faefb1fe90785df97ad89fc327eca6ab630d
[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 <QStringList>
26 #include <QPixmap>
27 #include <QNetworkReply>
28 #include "situareservice.h"
29 #include "situarecommon.h"
30 #include "common.h"
31 #include "parser.h"
32 #include "ui/avatarimage.h"
33 #include "network/networkaccessmanager.h"
34
35 SituareService::SituareService(QObject *parent)
36         : QObject(parent),
37         m_user(0)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     m_networkManager = new NetworkAccessManager(this);
42     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
43             this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
44
45     m_imageFetcher = new ImageFetcher(new NetworkAccessManager(this), this);
46     connect(this, SIGNAL(fetchImage(QUrl)),
47             m_imageFetcher, SLOT(fetchImage(QUrl)));
48     connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)),
49             this, SLOT(imageReceived(QUrl, QPixmap)));
50     connect(m_imageFetcher, SIGNAL(error(int, int)),
51             this, SIGNAL(error(int, int)));
52 }
53
54 SituareService::~SituareService()
55 {
56     qDebug() << __PRETTY_FUNCTION__;
57
58     if(m_user) {
59         delete m_user;
60         m_user = 0;
61     }
62
63     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
64     m_friendsList.clear();
65 }
66
67 void SituareService::fetchLocations()
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70
71     QString cookie = formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
72                                 m_credentials.sessionKey(), m_credentials.sessionSecret(),
73                                 m_credentials.sig(), EN_LOCALE);
74
75     QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
76     sendRequest(url, COOKIE, cookie);
77 }
78
79 void SituareService::reverseGeo(const GeoCoordinate &coordinates)
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     QString cookie = formCookie(API_KEY, m_credentials.expires(),m_credentials.userID(),
84                                 m_credentials.sessionKey(), m_credentials.sessionSecret(),
85                                 m_credentials.sig(), EN_LOCALE);
86
87     QString urlParameters = formUrlParameters(coordinates);
88     urlParameters.append(JSON_FORMAT);
89     QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
90
91     sendRequest(url, COOKIE, cookie);
92 }
93
94 void SituareService::updateLocation(const GeoCoordinate &coordinates, const QString &status,
95                                     const bool &publish)
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     QString cookie = formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
100                                 m_credentials.sessionKey(), m_credentials.sessionSecret(),
101                                 m_credentials.sig(), EN_LOCALE);
102
103
104     QString publishValue;
105     if(publish) {
106         publishValue = PUBLISH_TRUE;
107     }
108     else {
109         publishValue = PUBLISH_FALSE;
110     }
111     QString urlParameters = formUrlParameters(coordinates, status, publishValue);
112     QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
113
114     sendRequest(url, COOKIE, cookie);
115 }
116
117 QString SituareService::formCookie(const QString &apiKeyValue, QString expiresValue,
118                                    QString userValue, QString sessionKeyValue,
119                                    QString sessionSecretValue, const QString &signatureValue,
120                                    const QString &localeValue)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     QString cookie;
125     QString apiKey;
126     QString user;
127     QString expires;
128     QString sessionKey;
129     QString sessionSecret;
130     QString locale;
131     QString variable;
132     QString signature = EQUAL_MARK;
133     QStringList variableList;
134
135     signature.append(signatureValue);
136     apiKey.append(apiKeyValue);
137     apiKey.append(UNDERLINE_MARK);
138
139     user.append(USER);
140     user.append(EQUAL_MARK);
141     expires.append(EXPIRES);
142     expires.append(EQUAL_MARK);
143     sessionKey.append(SESSION_KEY);
144     sessionKey.append(EQUAL_MARK);
145     sessionSecret.append(SESSION_SECRET);
146     sessionSecret.append(EQUAL_MARK);
147     locale.append(LOCALE);
148     locale.append(EQUAL_MARK);
149     locale.append(localeValue);
150
151     variableList.append(expires.append(expiresValue.append(BREAK_MARK)));
152     variableList.append(sessionKey.append(sessionKeyValue.append(BREAK_MARK)));
153     variableList.append(user.append(userValue).append(BREAK_MARK));
154     variableList.append(sessionSecret.append(sessionSecretValue.append(BREAK_MARK)));
155
156     cookie.append(BREAK_MARK);
157
158     foreach(variable, variableList) {
159         cookie.append(apiKey);
160         cookie.append(variable);
161     }
162     apiKey.remove(UNDERLINE_MARK);
163     cookie.append(apiKey);
164     cookie.append(signature);
165     cookie.append(BREAK_MARK);
166     cookie.append(locale);
167
168     qDebug() << cookie;
169
170     return cookie;
171 }
172
173 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript,
174                              QString urlParameters)
175 {
176     qDebug() << __PRETTY_FUNCTION__;
177     QString urlString;
178
179     urlString.append(baseUrl);
180     urlString.append(phpScript);
181     if(!urlParameters.isEmpty())
182         urlString.append(urlParameters);
183
184     QUrl url = QUrl(urlString);
185
186     qDebug() << url;
187
188     return url;
189 }
190
191 QString SituareService::formUrlParameters(const GeoCoordinate &coordinates, QString status,
192                                           QString publish)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     // one scene pixel is about 5.4e-6 degrees, the integer part is max three digits and one
197     // additional digit is added for maximum precision
198     const int COORDINATE_PRECISION = 10;
199
200     QString parameters;
201
202     parameters.append(QUESTION_MARK);
203     parameters.append(LATITUDE);
204     parameters.append(EQUAL_MARK);
205     parameters.append(QString::number(coordinates.latitude(), 'f', COORDINATE_PRECISION));
206     parameters.append(AMBERSAND_MARK);
207     parameters.append(LONGTITUDE);
208     parameters.append(EQUAL_MARK);
209     parameters.append(QString::number(coordinates.longitude(), 'f', COORDINATE_PRECISION));
210
211     if(publish.compare(PUBLISH_TRUE) == 0) {
212         parameters.append(AMBERSAND_MARK);
213         parameters.append(PUBLISH);
214         parameters.append(EQUAL_MARK);
215         parameters.append(PUBLISH_TRUE);
216     } else if(publish.compare(PUBLISH_FALSE) == 0) {
217         parameters.append(AMBERSAND_MARK);
218         parameters.append(PUBLISH);
219         parameters.append(EQUAL_MARK);
220         parameters.append(PUBLISH_FALSE);
221     }
222
223     if(!status.isEmpty()) {
224         parameters.append(AMBERSAND_MARK);
225         parameters.append(DATA);
226         parameters.append(EQUAL_MARK);
227         parameters.append(status);
228     }
229
230     return parameters;
231 }
232
233 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237     QNetworkRequest request;
238
239     request.setUrl(url);
240     request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
241     request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
242
243     QNetworkReply *reply = m_networkManager->get(request, true);
244
245     m_currentRequests.append(reply);
246 }
247
248 void SituareService::requestFinished(QNetworkReply *reply)
249 {
250     qDebug() << __PRETTY_FUNCTION__;
251
252     //Reply from situare
253     if (m_currentRequests.contains(reply)) {
254
255         qDebug() << "BytesAvailable: " << reply->bytesAvailable();
256
257         if (reply->error()) {
258             emit error(ErrorContext::NETWORK, reply->error());
259         } else {
260             QByteArray replyArray = reply->readAll();
261             qDebug() << "Reply from: " << reply->url() << "reply " << replyArray;
262
263             if(replyArray == ERROR_LAT.toAscii()) {
264                 qDebug() << "Error: " << ERROR_LAT;
265                 emit error(ErrorContext::SITUARE, SituareError::UPDATE_FAILED);
266             } else if(replyArray == ERROR_LON.toAscii()) {
267                 qDebug() << "Error: " << ERROR_LON;
268                 emit error(ErrorContext::SITUARE, SituareError::UPDATE_FAILED);
269             } else if(replyArray.contains(ERROR_SESSION.toAscii())) {
270                 qDebug() << "Error: " << ERROR_SESSION;
271                 emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
272             } else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
273                 qDebug() << "JSON string";
274                 parseUserData(replyArray);
275             } else if(replyArray.isEmpty()) {
276                 if(reply->url().toString().contains(UPDATE_LOCATION.toAscii())) {
277                     emit updateWasSuccessful();
278                 } else {
279                     // session credentials are invalid
280                     emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
281                 }
282             } else {
283                 // unknown reply
284                 emit error(ErrorContext::SITUARE, SituareError::ERROR_GENERAL);
285             }
286         }
287         m_currentRequests.removeAll(reply);
288         reply->deleteLater();
289     }
290 }
291
292 void SituareService::credentialsReady(const FacebookCredentials &credentials)
293 {
294     qDebug() << __PRETTY_FUNCTION__;
295
296     m_credentials = credentials;
297 }
298
299 void SituareService::parseUserData(const QByteArray &jsonReply)
300 {
301     qDebug() << __PRETTY_FUNCTION__;
302
303     m_defaultImage = false;
304
305     QJson::Parser parser;
306     bool ok;
307
308     QVariantMap result = parser.parse (jsonReply, &ok).toMap();
309     if (!ok) {
310         emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
311         return;
312     } else {
313
314         if(result.contains("ErrorCode")) {
315             QVariant errorVariant = result.value("ErrorCode");
316             emit error(ErrorContext::SITUARE, errorVariant.toInt());
317             return;
318         } else if(result.contains("user")) {
319
320             QVariant userVariant = result.value("user");
321             QMap<QString, QVariant> userMap = userVariant.toMap();
322
323             GeoCoordinate coordinates(userMap["latitude"].toReal(), userMap["longitude"].toReal());
324
325             QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
326
327             if(imageUrl.isEmpty()) {
328                 // user doesn't have profile image, so we need to get him a silhouette image
329                 m_defaultImage = true;
330             }
331
332             QString address = userMap["address"].toString();
333             if(address.isEmpty()) {
334                 QStringList location;
335                 location.append(QString::number(coordinates.latitude()));
336                 location.append(QString::number(coordinates.longitude()));
337                 address = location.join(", ");
338             }
339
340             User user = User(address, coordinates, userMap["name"].toString(),
341                           userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
342                           true, userMap["uid"].toString());
343
344             QList<User> tmpFriendsList;
345
346             foreach (QVariant friendsVariant, result["friends"].toList()) {
347               QMap<QString, QVariant> friendMap = friendsVariant.toMap();
348               QVariant distance = friendMap["distance"];
349               QMap<QString, QVariant> distanceMap = distance.toMap();
350
351               GeoCoordinate coordinates(friendMap["latitude"].toReal(),friendMap["longitude"].toReal());
352
353               QUrl imageUrl = friendMap["profile_pic"].toUrl();
354
355               if(imageUrl.isEmpty()) {
356                   // friend doesn't have profile image, so we need to get him a silhouette image
357                   m_defaultImage = true;
358               }
359
360               QString address = friendMap["address"].toString();
361               if(address.isEmpty()) {
362                   QStringList location;
363                   location.append(QString::number(coordinates.latitude()));
364                   location.append(QString::number(coordinates.longitude()));
365                   address = location.join(", ");
366               }
367
368               User buddy = User(address, coordinates, friendMap["name"].toString(),
369                                friendMap["note"].toString(), imageUrl,
370                                friendMap["timestamp"].toString(),
371                                false, friendMap["uid"].toString(), distanceMap["units"].toString(),
372                                distanceMap["value"].toDouble());
373
374               tmpFriendsList.append(buddy);
375             }
376
377             QList<QUrl> imageUrlList; // url list for images
378
379             // set unchanged profile images or add new images to imageUrlList for downloading
380             if(m_user) {
381                 if(m_user->profileImageUrl() != user.profileImageUrl()) {
382                     if(!user.profileImageUrl().isEmpty())
383                         imageUrlList.append(user.profileImageUrl());
384                 } else {
385                     user.setProfileImage(m_user->profileImage());
386                 }
387             } else {
388                 if(!user.profileImageUrl().isEmpty())
389                     imageUrlList.append(user.profileImageUrl());
390             }
391
392             // clear old user object
393             if(m_user) {
394                 delete m_user;
395                 m_user = 0;
396             }
397
398             // create new user object from temporary user object
399             m_user = new User(user);
400
401             // set unchanged profile images or add new images to imageUrlList for downloading
402             if(!m_friendsList.isEmpty()) {
403                 foreach(User tmpBuddy, tmpFriendsList) {
404                     if(!tmpBuddy.profileImageUrl().isEmpty()) {
405                         bool found = false;
406                         foreach(User *buddy, m_friendsList) {
407                             if(tmpBuddy.profileImageUrl() == buddy->profileImageUrl()) {
408                                 tmpBuddy.setProfileImage(buddy->profileImage());
409                                 found = true;
410                                 break;
411                             }
412                         }
413                         if(!found && !tmpBuddy.profileImageUrl().isEmpty())
414                             imageUrlList.append(tmpBuddy.profileImageUrl());
415                     }
416                 }
417             } else {
418                 foreach(User buddy, tmpFriendsList) {
419                     if(!buddy.profileImageUrl().isEmpty())
420                         imageUrlList.append(buddy.profileImageUrl());
421                 }
422             }
423
424             // clear old friendlist
425             qDeleteAll(m_friendsList.begin(), m_friendsList.end());
426             m_friendsList.clear();
427
428             // populate new friendlist with temporary friendlist's data
429             foreach(User tmpFriendItem, tmpFriendsList) {
430                 User *friendItem = new User(tmpFriendItem);
431                 m_friendsList.append(friendItem);
432             }
433             tmpFriendsList.clear();
434
435             emit userDataChanged(m_user, m_friendsList);
436
437             // set silhouette image to imageUrlList for downloading
438             if(m_defaultImage)
439                 imageUrlList.append(QUrl(SILHOUETTE_URL));
440
441             addProfileImages(imageUrlList);
442             imageUrlList.clear();
443         } else {
444             QVariant address = result.value("address");
445             if(!address.toString().isEmpty()) {
446                 emit reverseGeoReady(address.toString());
447             } else {
448                 QStringList coordinates;
449                 coordinates.append(result.value("lat").toString());
450                 coordinates.append(result.value("lon").toString());
451
452                 emit error(ErrorContext::SITUARE, SituareError::ADDRESS_RETRIEVAL_FAILED);
453                 emit reverseGeoReady(coordinates.join(", "));
454             }
455         }
456     }
457 }
458
459 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
460 {
461     qDebug() << __PRETTY_FUNCTION__;
462     qDebug() << "Image URL: " << url << " size :" << image.size();
463
464     // assign facebook silhouette image to all who doesn't have a profile image
465     if(url == QUrl(SILHOUETTE_URL)) {
466         if(m_user->profileImageUrl().isEmpty()) {
467             m_user->setProfileImage(AvatarImage::create(image, AvatarImage::Large));
468             emit imageReady(m_user);
469         }
470         foreach(User *friendItem, m_friendsList) {
471             if(friendItem->profileImageUrl().isEmpty()) {
472                 friendItem->setProfileImage(AvatarImage::create(image, AvatarImage::Small));
473                 emit imageReady(friendItem);
474             }
475         }
476     }
477
478     if (m_user->profileImageUrl() == url) {
479         m_user->setProfileImage(AvatarImage::create(image, AvatarImage::Large));
480         emit imageReady(m_user);
481     }
482
483     foreach(User *friendItem, m_friendsList) {
484         if(friendItem->profileImageUrl() == url) {
485             friendItem->setProfileImage(AvatarImage::create(image, AvatarImage::Small));
486             emit imageReady(friendItem);
487         }
488     }
489 }
490
491 void SituareService::addProfileImages(const QList<QUrl> &imageUrlList)
492 {
493     qDebug() << __PRETTY_FUNCTION__;
494
495     foreach(QUrl url, imageUrlList) {
496         emit fetchImage(url);
497     }
498 }
499
500 void SituareService::clearUserData()
501 {
502     qDebug() << __PRETTY_FUNCTION__;
503
504     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
505     m_friendsList.clear();
506
507     if(m_user) {
508         delete m_user;
509         m_user = 0;
510     }
511     emit userDataChanged(m_user, m_friendsList);
512 }