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 = NetworkAccessManager::instance();
42 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
43 this, SLOT(requestFinished(QNetworkReply*)));
45 m_imageFetcher = new ImageFetcher(NetworkAccessManager::instance(), 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(QString)),
51 this, SIGNAL(error(QString)));
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 QPointF &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 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
90 sendRequest(url, COOKIE, cookie);
93 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
96 qDebug() << __PRETTY_FUNCTION__;
98 QString cookie = formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
99 m_credentials.sessionKey(), m_credentials.sessionSecret(),
100 m_credentials.sig(), EN_LOCALE);
103 QString publishValue;
105 publishValue = PUBLISH_TRUE;
108 publishValue = PUBLISH_FALSE;
110 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
111 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
113 sendRequest(url, COOKIE, cookie);
116 QString SituareService::formCookie(const QString &apiKeyValue, QString expiresValue,
117 QString userValue, QString sessionKeyValue,
118 QString sessionSecretValue, const QString &signatureValue,
119 const QString &localeValue)
121 qDebug() << __PRETTY_FUNCTION__;
128 QString sessionSecret;
131 QString signature = EQUAL_MARK;
132 QStringList variableList;
134 signature.append(signatureValue);
135 apiKey.append(apiKeyValue);
136 apiKey.append(UNDERLINE_MARK);
139 user.append(EQUAL_MARK);
140 expires.append(EXPIRES);
141 expires.append(EQUAL_MARK);
142 sessionKey.append(SESSION_KEY);
143 sessionKey.append(EQUAL_MARK);
144 sessionSecret.append(SESSION_SECRET);
145 sessionSecret.append(EQUAL_MARK);
146 locale.append(LOCALE);
147 locale.append(EQUAL_MARK);
148 locale.append(localeValue);
150 variableList.append(expires.append(expiresValue.append(BREAK_MARK)));
151 variableList.append(sessionKey.append(sessionKeyValue.append(BREAK_MARK)));
152 variableList.append(user.append(userValue).append(BREAK_MARK));
153 variableList.append(sessionSecret.append(sessionSecretValue.append(BREAK_MARK)));
155 cookie.append(BREAK_MARK);
157 foreach(variable, variableList) {
158 cookie.append(apiKey);
159 cookie.append(variable);
161 apiKey.remove(UNDERLINE_MARK);
162 cookie.append(apiKey);
163 cookie.append(signature);
164 cookie.append(BREAK_MARK);
165 cookie.append(locale);
172 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript,
173 QString urlParameters)
175 qDebug() << __PRETTY_FUNCTION__;
178 urlString.append(baseUrl);
179 urlString.append(phpScript);
180 if(!urlParameters.isEmpty())
181 urlString.append(urlParameters);
183 QUrl url = QUrl(urlString);
190 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status,
195 parameters.append(QUESTION_MARK);
196 parameters.append(LATITUDE);
197 parameters.append(EQUAL_MARK);
198 parameters.append(QString::number(coordinates.y()));
199 parameters.append(AMBERSAND_MARK);
200 parameters.append(LONGTITUDE);
201 parameters.append(EQUAL_MARK);
202 parameters.append(QString::number(coordinates.x()));
204 if(publish.compare(PUBLISH_TRUE) == 0) {
205 parameters.append(AMBERSAND_MARK);
206 parameters.append(PUBLISH);
207 parameters.append(EQUAL_MARK);
208 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);
239 m_currentRequests.append(reply);
242 void SituareService::requestFinished(QNetworkReply *reply)
244 qDebug() << __PRETTY_FUNCTION__;
247 if (m_currentRequests.contains(reply)) {
249 QUrl url = reply->url();
250 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
252 if (reply->error()) {
253 emit error(reply->errorString());
256 qint64 max = reply->size();
257 QByteArray replyArray = reply->read(max);
258 qDebug() << "Reply from: " << url << "reply " << replyArray;
260 if(replyArray == ERROR_LAT.toAscii()) {
261 qDebug() << "Error: " << ERROR_LAT;
262 emit error(UPDATE_FAILED);
264 else if(replyArray == ERROR_LON.toAscii()) {
265 qDebug() << "Error: " << ERROR_LON;
266 emit error(UPDATE_FAILED);
268 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
269 qDebug() << "Error: " << ERROR_SESSION;
270 emit error(SESSION_EXPIRED);
272 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
273 qDebug() << "JSON string";
274 parseUserData(replyArray);
276 else if(replyArray.isEmpty()) {
277 if(url.toString().contains(UPDATE_LOCATION.toAscii())) {
278 emit updateWasSuccessful();
281 // session credentials are invalid
282 emit error(SESSION_EXPIRED);
286 // Street address ready
287 QString address = QString::fromUtf8(replyArray);
288 emit reverseGeoReady(address);
291 m_currentRequests.removeAll(reply);
292 reply->deleteLater();
296 void SituareService::credentialsReady(const FacebookCredentials &credentials)
298 qDebug() << __PRETTY_FUNCTION__;
300 m_credentials = credentials;
303 void SituareService::parseUserData(const QByteArray &jsonReply)
305 qDebug() << __PRETTY_FUNCTION__;
309 m_defaultImage = false;
310 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
311 m_friendsList.clear();
318 QJson::Parser parser;
321 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
324 qFatal("An error occurred during parsing");
328 QVariant userVariant = result.value("user");
329 QMap<QString, QVariant> userMap = userVariant.toMap();
331 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
333 QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
335 if(imageUrl.isEmpty()) {
336 // user doesn't have profile image, so we need to get him a silhouette image
337 m_defaultImage = true;
340 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
341 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
342 true, userMap["uid"].toString());
344 foreach (QVariant friendsVariant, result["friends"].toList()) {
345 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
346 QVariant distance = friendMap["distance"];
347 QMap<QString, QVariant> distanceMap = distance.toMap();
349 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
351 QUrl imageUrl = friendMap["profile_pic"].toUrl();
353 if(imageUrl.isEmpty()) {
354 // friend doesn't have profile image, so we need to get him a silhouette image
355 m_defaultImage = true;
358 User *user = new User(friendMap["address"].toString(), coordinates,
359 friendMap["name"].toString(),
360 friendMap["note"].toString(), imageUrl,
361 friendMap["timestamp"].toString(),
362 false, friendMap["uid"].toString(),
363 distanceMap["units"].toString(),
364 distanceMap["value"].toDouble());
366 m_friendsList.append(user);
371 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
373 qDebug() << __PRETTY_FUNCTION__;
374 qDebug() << "Image URL: " << url << " size :" << image.size();
376 // assign facebook silhouette image to all who doesn't have a profile image
377 if(url == QUrl(SILHOUETTE_URL)) {
378 if(m_user->profileImageUrl().isEmpty()) {
379 m_user->setProfileImage(AvatarImage::create(image, AvatarImage::Large));
381 for(int i=0;i < m_friendsList.count();i++) {
382 if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
383 m_friendsList.at(i)->setProfileImage(AvatarImage::create(image,
384 AvatarImage::Small));
389 if (m_user->profileImageUrl() == url) {
390 m_user->setProfileImage(AvatarImage::create(image, AvatarImage::Large));
393 for(int i=0;i<m_friendsList.count();i++) {
394 if(m_friendsList.at(i)->profileImageUrl() == url) {
395 m_friendsList.at(i)->setProfileImage(AvatarImage::create(image, AvatarImage::Small));
396 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
400 if(m_nbrOfImages == m_visited) {
401 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
402 qDebug() << "emit userDataChanged";
403 emit userDataChanged(m_user, m_friendsList);
407 void SituareService::addProfileImages()
409 qDebug() << __PRETTY_FUNCTION__;
411 // reduce net traffic by sending only one download request for facebook silhouette image
413 emit fetchImage(QUrl(SILHOUETTE_URL));
416 if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid())
417 emit fetchImage(m_user->profileImageUrl());
419 for(int i=0;i<m_friendsList.count();i++) {
420 if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
421 m_friendsList.at(i)->profileImageUrl().isValid()) {
422 m_visited++; // indicates how many friends that have profile image
423 emit fetchImage(m_friendsList.at(i)->profileImageUrl());
428 void SituareService::clearUserData()
430 qDebug() << __PRETTY_FUNCTION__;
432 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
433 m_friendsList.clear();
439 emit userDataChanged(m_user, m_friendsList);