2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Henri Lampela - henri.lampela@ixonos.com
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.
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.
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,
22 #include <QtAlgorithms>
25 #include <QStringList>
27 #include <QNetworkReply>
28 #include "situareservice.h"
29 #include "situarecommon.h"
32 #include "ui/avatarimage.h"
33 #include "network/networkaccessmanager.h"
35 SituareService::SituareService(QObject *parent)
39 qDebug() << __PRETTY_FUNCTION__;
41 m_networkManager = new NetworkAccessManager(this);
42 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
43 this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
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)));
54 SituareService::~SituareService()
56 qDebug() << __PRETTY_FUNCTION__;
63 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
64 m_friendsList.clear();
67 void SituareService::fetchLocations()
69 qDebug() << __PRETTY_FUNCTION__;
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);
75 QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
76 sendRequest(url, COOKIE, cookie);
79 void SituareService::reverseGeo(const GeoCoordinate &coordinates)
81 qDebug() << __PRETTY_FUNCTION__;
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);
87 QString urlParameters = formUrlParameters(coordinates);
88 urlParameters.append(JSON_FORMAT);
89 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
91 sendRequest(url, COOKIE, cookie);
94 void SituareService::updateLocation(const GeoCoordinate &coordinates, const QString &status,
97 qDebug() << __PRETTY_FUNCTION__;
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);
104 QString publishValue;
106 publishValue = PUBLISH_TRUE;
109 publishValue = PUBLISH_FALSE;
111 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
112 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
114 sendRequest(url, COOKIE, cookie);
117 QString SituareService::formCookie(const QString &apiKeyValue, QString expiresValue,
118 QString userValue, QString sessionKeyValue,
119 QString sessionSecretValue, const QString &signatureValue,
120 const QString &localeValue)
122 qDebug() << __PRETTY_FUNCTION__;
129 QString sessionSecret;
132 QString signature = EQUAL_MARK;
133 QStringList variableList;
135 signature.append(signatureValue);
136 apiKey.append(apiKeyValue);
137 apiKey.append(UNDERLINE_MARK);
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);
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)));
156 cookie.append(BREAK_MARK);
158 foreach(variable, variableList) {
159 cookie.append(apiKey);
160 cookie.append(variable);
162 apiKey.remove(UNDERLINE_MARK);
163 cookie.append(apiKey);
164 cookie.append(signature);
165 cookie.append(BREAK_MARK);
166 cookie.append(locale);
173 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript,
174 QString urlParameters)
176 qDebug() << __PRETTY_FUNCTION__;
179 urlString.append(baseUrl);
180 urlString.append(phpScript);
181 if(!urlParameters.isEmpty())
182 urlString.append(urlParameters);
184 QUrl url = QUrl(urlString);
191 QString SituareService::formUrlParameters(const GeoCoordinate &coordinates, QString status,
196 parameters.append(QUESTION_MARK);
197 parameters.append(LATITUDE);
198 parameters.append(EQUAL_MARK);
199 parameters.append(QString::number(coordinates.latitude()));
200 parameters.append(AMBERSAND_MARK);
201 parameters.append(LONGTITUDE);
202 parameters.append(EQUAL_MARK);
203 parameters.append(QString::number(coordinates.longitude()));
205 if(publish.compare(PUBLISH_TRUE) == 0) {
206 parameters.append(AMBERSAND_MARK);
207 parameters.append(PUBLISH);
208 parameters.append(EQUAL_MARK);
209 parameters.append(PUBLISH_TRUE);
210 } else if(publish.compare(PUBLISH_FALSE) == 0) {
211 parameters.append(AMBERSAND_MARK);
212 parameters.append(PUBLISH);
213 parameters.append(EQUAL_MARK);
214 parameters.append(PUBLISH_FALSE);
217 if(!status.isEmpty()) {
218 parameters.append(AMBERSAND_MARK);
219 parameters.append(DATA);
220 parameters.append(EQUAL_MARK);
221 parameters.append(status);
227 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
229 qDebug() << __PRETTY_FUNCTION__;
231 QNetworkRequest request;
234 request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
235 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
237 QNetworkReply *reply = m_networkManager->get(request, true);
239 m_currentRequests.append(reply);
242 void SituareService::requestFinished(QNetworkReply *reply)
244 qDebug() << __PRETTY_FUNCTION__;
247 if (m_currentRequests.contains(reply)) {
249 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
251 if (reply->error()) {
252 emit error(ErrorContext::NETWORK, reply->error());
254 QByteArray replyArray = reply->readAll();
255 qDebug() << "Reply from: " << reply->url() << "reply " << replyArray;
257 if(replyArray == ERROR_LAT.toAscii()) {
258 qDebug() << "Error: " << ERROR_LAT;
259 emit error(ErrorContext::SITUARE, SituareError::UPDATE_FAILED);
260 } else if(replyArray == ERROR_LON.toAscii()) {
261 qDebug() << "Error: " << ERROR_LON;
262 emit error(ErrorContext::SITUARE, SituareError::UPDATE_FAILED);
263 } else if(replyArray.contains(ERROR_SESSION.toAscii())) {
264 qDebug() << "Error: " << ERROR_SESSION;
265 emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
266 } else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
267 qDebug() << "JSON string";
268 parseUserData(replyArray);
269 } else if(replyArray.isEmpty()) {
270 if(reply->url().toString().contains(UPDATE_LOCATION.toAscii())) {
271 emit updateWasSuccessful();
273 // session credentials are invalid
274 emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
278 emit error(ErrorContext::SITUARE, SituareError::ERROR_GENERAL);
281 m_currentRequests.removeAll(reply);
282 reply->deleteLater();
286 void SituareService::credentialsReady(const FacebookCredentials &credentials)
288 qDebug() << __PRETTY_FUNCTION__;
290 m_credentials = credentials;
293 void SituareService::parseUserData(const QByteArray &jsonReply)
295 qDebug() << __PRETTY_FUNCTION__;
297 m_defaultImage = false;
299 QJson::Parser parser;
302 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
304 emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
308 if(result.contains("ErrorCode")) {
309 QVariant errorVariant = result.value("ErrorCode");
310 emit error(ErrorContext::SITUARE, errorVariant.toInt());
312 } else if(result.contains("user")) {
314 QVariant userVariant = result.value("user");
315 QMap<QString, QVariant> userMap = userVariant.toMap();
317 GeoCoordinate coordinates(userMap["latitude"].toReal(), userMap["longitude"].toReal());
319 QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
321 if(imageUrl.isEmpty()) {
322 // user doesn't have profile image, so we need to get him a silhouette image
323 m_defaultImage = true;
326 QString address = userMap["address"].toString();
327 if(address.isEmpty()) {
328 QStringList location;
329 location.append(QString::number(coordinates.latitude()));
330 location.append(QString::number(coordinates.longitude()));
331 address = location.join(", ");
334 User user = User(address, coordinates, userMap["name"].toString(),
335 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
336 true, userMap["uid"].toString());
338 QList<User> tmpFriendsList;
340 foreach (QVariant friendsVariant, result["friends"].toList()) {
341 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
342 QVariant distance = friendMap["distance"];
343 QMap<QString, QVariant> distanceMap = distance.toMap();
345 GeoCoordinate coordinates(friendMap["latitude"].toReal(),friendMap["longitude"].toReal());
347 QUrl imageUrl = friendMap["profile_pic"].toUrl();
349 if(imageUrl.isEmpty()) {
350 // friend doesn't have profile image, so we need to get him a silhouette image
351 m_defaultImage = true;
354 QString address = friendMap["address"].toString();
355 if(address.isEmpty()) {
356 QStringList location;
357 location.append(QString::number(coordinates.latitude()));
358 location.append(QString::number(coordinates.longitude()));
359 address = location.join(", ");
362 User buddy = User(address, coordinates, friendMap["name"].toString(),
363 friendMap["note"].toString(), imageUrl,
364 friendMap["timestamp"].toString(),
365 false, friendMap["uid"].toString(), distanceMap["units"].toString(),
366 distanceMap["value"].toDouble());
368 tmpFriendsList.append(buddy);
371 QList<QUrl> imageUrlList; // url list for images
373 // set unchanged profile images or add new images to imageUrlList for downloading
375 if(m_user->profileImageUrl() != user.profileImageUrl()) {
376 if(!user.profileImageUrl().isEmpty())
377 imageUrlList.append(user.profileImageUrl());
379 user.setProfileImage(m_user->profileImage());
382 if(!user.profileImageUrl().isEmpty())
383 imageUrlList.append(user.profileImageUrl());
386 // clear old user object
392 // create new user object from temporary user object
393 m_user = new User(user);
395 // set unchanged profile images or add new images to imageUrlList for downloading
396 if(!m_friendsList.isEmpty()) {
397 foreach(User tmpBuddy, tmpFriendsList) {
398 if(!tmpBuddy.profileImageUrl().isEmpty()) {
400 foreach(User *buddy, m_friendsList) {
401 if(tmpBuddy.profileImageUrl() == buddy->profileImageUrl()) {
402 tmpBuddy.setProfileImage(buddy->profileImage());
407 if(!found && !tmpBuddy.profileImageUrl().isEmpty())
408 imageUrlList.append(tmpBuddy.profileImageUrl());
412 foreach(User buddy, tmpFriendsList) {
413 if(!buddy.profileImageUrl().isEmpty())
414 imageUrlList.append(buddy.profileImageUrl());
418 // clear old friendlist
419 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
420 m_friendsList.clear();
422 // populate new friendlist with temporary friendlist's data
423 foreach(User tmpFriendItem, tmpFriendsList) {
424 User *friendItem = new User(tmpFriendItem);
425 m_friendsList.append(friendItem);
427 tmpFriendsList.clear();
429 emit userDataChanged(m_user, m_friendsList);
431 // set silhouette image to imageUrlList for downloading
433 imageUrlList.append(QUrl(SILHOUETTE_URL));
435 addProfileImages(imageUrlList);
436 imageUrlList.clear();
438 QVariant address = result.value("address");
439 if(!address.toString().isEmpty()) {
440 emit reverseGeoReady(address.toString());
442 QStringList coordinates;
443 coordinates.append(result.value("lat").toString());
444 coordinates.append(result.value("lon").toString());
446 emit error(ErrorContext::SITUARE, SituareError::ADDRESS_RETRIEVAL_FAILED);
447 emit reverseGeoReady(coordinates.join(", "));
453 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
455 qDebug() << __PRETTY_FUNCTION__;
456 qDebug() << "Image URL: " << url << " size :" << image.size();
458 // assign facebook silhouette image to all who doesn't have a profile image
459 if(url == QUrl(SILHOUETTE_URL)) {
460 if(m_user->profileImageUrl().isEmpty()) {
461 m_user->setProfileImage(AvatarImage::create(image, AvatarImage::Large));
462 emit imageReady(m_user);
464 foreach(User *friendItem, m_friendsList) {
465 if(friendItem->profileImageUrl().isEmpty()) {
466 friendItem->setProfileImage(AvatarImage::create(image, AvatarImage::Small));
467 emit imageReady(friendItem);
472 if (m_user->profileImageUrl() == url) {
473 m_user->setProfileImage(AvatarImage::create(image, AvatarImage::Large));
474 emit imageReady(m_user);
477 foreach(User *friendItem, m_friendsList) {
478 if(friendItem->profileImageUrl() == url) {
479 friendItem->setProfileImage(AvatarImage::create(image, AvatarImage::Small));
480 emit imageReady(friendItem);
485 void SituareService::addProfileImages(const QList<QUrl> &imageUrlList)
487 qDebug() << __PRETTY_FUNCTION__;
489 foreach(QUrl url, imageUrlList) {
490 emit fetchImage(url);
494 void SituareService::clearUserData()
496 qDebug() << __PRETTY_FUNCTION__;
498 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
499 m_friendsList.clear();
505 emit userDataChanged(m_user, m_friendsList);