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"
31 #include "ui/avatarimage.h"
33 SituareService::SituareService(QObject *parent)
37 qDebug() << __PRETTY_FUNCTION__;
39 m_networkManager = new QNetworkAccessManager;
40 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
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)));
49 SituareService::~SituareService()
51 qDebug() << __PRETTY_FUNCTION__;
53 delete m_networkManager;
59 delete m_imageFetcher;
62 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
63 m_friendsList.clear();
66 void SituareService::fetchLocations()
68 qDebug() << __PRETTY_FUNCTION__;
70 QString cookie = formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
71 m_credentials.sessionKey(), m_credentials.sessionSecret(),
72 m_credentials.sig(), EN_LOCALE);
74 QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
75 sendRequest(url, COOKIE, cookie);
78 void SituareService::reverseGeo(const QPointF &coordinates)
80 qDebug() << __PRETTY_FUNCTION__;
82 QString cookie = formCookie(API_KEY, m_credentials.expires(),m_credentials.userID(),
83 m_credentials.sessionKey(), m_credentials.sessionSecret(),
84 m_credentials.sig(), EN_LOCALE);
86 QString urlParameters = formUrlParameters(coordinates);
87 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
89 sendRequest(url, COOKIE, cookie);
92 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
95 qDebug() << __PRETTY_FUNCTION__;
97 QString cookie = formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
98 m_credentials.sessionKey(), m_credentials.sessionSecret(),
99 m_credentials.sig(), EN_LOCALE);
102 QString publishValue;
104 publishValue = PUBLISH_TRUE;
107 publishValue = PUBLISH_FALSE;
109 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
110 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
112 sendRequest(url, COOKIE, cookie);
115 QString SituareService::formCookie(const QString &apiKeyValue, QString expiresValue,
116 QString userValue, QString sessionKeyValue,
117 QString sessionSecretValue, const QString &signatureValue,
118 const QString &localeValue)
120 qDebug() << __PRETTY_FUNCTION__;
127 QString sessionSecret;
130 QString signature = EQUAL_MARK;
131 QStringList variableList;
133 signature.append(signatureValue);
134 apiKey.append(apiKeyValue);
135 apiKey.append(UNDERLINE_MARK);
138 user.append(EQUAL_MARK);
139 expires.append(EXPIRES);
140 expires.append(EQUAL_MARK);
141 sessionKey.append(SESSION_KEY);
142 sessionKey.append(EQUAL_MARK);
143 sessionSecret.append(SESSION_SECRET);
144 sessionSecret.append(EQUAL_MARK);
145 locale.append(LOCALE);
146 locale.append(EQUAL_MARK);
147 locale.append(localeValue);
149 variableList.append(expires.append(expiresValue.append(BREAK_MARK)));
150 variableList.append(sessionKey.append(sessionKeyValue.append(BREAK_MARK)));
151 variableList.append(user.append(userValue).append(BREAK_MARK));
152 variableList.append(sessionSecret.append(sessionSecretValue.append(BREAK_MARK)));
154 cookie.append(BREAK_MARK);
156 foreach(variable, variableList) {
157 cookie.append(apiKey);
158 cookie.append(variable);
160 apiKey.remove(UNDERLINE_MARK);
161 cookie.append(apiKey);
162 cookie.append(signature);
163 cookie.append(BREAK_MARK);
164 cookie.append(locale);
171 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript,
172 QString urlParameters)
174 qDebug() << __PRETTY_FUNCTION__;
177 urlString.append(baseUrl);
178 urlString.append(phpScript);
179 if(!urlParameters.isEmpty())
180 urlString.append(urlParameters);
182 QUrl url = QUrl(urlString);
189 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status,
194 parameters.append(QUESTION_MARK);
195 parameters.append(LATITUDE);
196 parameters.append(EQUAL_MARK);
197 parameters.append(QString::number(coordinates.y()));
198 parameters.append(AMBERSAND_MARK);
199 parameters.append(LONGTITUDE);
200 parameters.append(EQUAL_MARK);
201 parameters.append(QString::number(coordinates.x()));
203 if(publish.compare(PUBLISH_TRUE) == 0) {
204 parameters.append(AMBERSAND_MARK);
205 parameters.append(PUBLISH);
206 parameters.append(EQUAL_MARK);
207 parameters.append(PUBLISH_TRUE);
209 else if(publish.compare(PUBLISH_FALSE) == 0) {
210 parameters.append(AMBERSAND_MARK);
211 parameters.append(PUBLISH);
212 parameters.append(EQUAL_MARK);
213 parameters.append(PUBLISH_FALSE);
216 if(!status.isEmpty()) {
217 parameters.append(AMBERSAND_MARK);
218 parameters.append(DATA);
219 parameters.append(EQUAL_MARK);
220 parameters.append(status);
226 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
228 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
230 QNetworkRequest request;
233 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
235 QNetworkReply *reply = m_networkManager->get(request);
237 m_currentRequests.append(reply);
240 void SituareService::requestFinished(QNetworkReply *reply)
242 qDebug() << __PRETTY_FUNCTION__;
244 QUrl url = reply->url();
245 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
246 if (reply->error()) {
247 qDebug() << reply->errorString();
248 emit error(reply->errorString());
251 qint64 max = reply->size();
252 QByteArray replyArray = reply->read(max);
253 qDebug() << "Reply from: " << url << "reply " << replyArray;
254 // ToDo: results handling includes Situare's errors
255 // works like situare's error handling i.e. both lat & lon are missing/wrong
256 // -> we get only error for wrong lon
257 if(replyArray == ERROR_LAT.toAscii()) {
258 qDebug() << "Error: " << ERROR_LAT;
259 emit error(replyArray);
261 else if(replyArray == ERROR_LON.toAscii()) {
262 qDebug() << "Error: " << ERROR_LON;
263 emit error(replyArray);
265 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
266 qDebug() << "Error: " << ERROR_SESSION;
267 emit invalidSessionCredentials();
269 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
270 qDebug() << "JSON string";
271 parseUserData(replyArray);
273 else if(replyArray == "") {
274 if(url.toString().contains(UPDATE_LOCATION.toAscii())) {
275 emit updateWasSuccessful();
278 // session credentials are invalid
279 emit invalidSessionCredentials();
283 // Street address ready
284 QString address = QString::fromUtf8(replyArray);
285 emit reverseGeoReady(address);
288 m_currentRequests.removeAll(reply);
289 reply->deleteLater();
292 void SituareService::credentialsReady(const FacebookCredentials &credentials)
294 qDebug() << __PRETTY_FUNCTION__;
296 m_credentials = credentials;
299 void SituareService::parseUserData(const QByteArray &jsonReply)
301 qDebug() << __PRETTY_FUNCTION__;
305 m_defaultImage = false;
306 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
307 m_friendsList.clear();
314 QJson::Parser parser;
317 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
320 qFatal("An error occurred during parsing");
324 QVariant userVariant = result.value("user");
325 QMap<QString, QVariant> userMap = userVariant.toMap();
327 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
329 QUrl imageUrl = userMap["profile_pic"].toUrl();
330 qWarning() << "JOOO1" << imageUrl;
331 QUrl imageUrlBig = userMap["pic"].toUrl();
332 qWarning() << "JOOO2" << imageUrlBig;
334 if(imageUrl.isEmpty()) {
335 // user doesn't have profile image, so we need to get him a silhouette image
336 m_defaultImage = true;
339 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
340 userMap["note"].toString(), imageUrl, imageUrlBig, userMap["timestamp"].toString(),
341 true, userMap["uid"].toString());
343 foreach (QVariant friendsVariant, result["friends"].toList()) {
344 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
345 QVariant distance = friendMap["distance"];
346 QMap<QString, QVariant> distanceMap = distance.toMap();
348 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
350 QUrl imageUrl = friendMap["profile_pic"].toUrl();
351 QUrl imageUrlBig = friendMap["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, imageUrlBig,
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));
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));
388 if(m_user->profileImageUrl() == url) {
389 m_user->setProfileImage(AvatarImage::create(image));
392 if(m_user->profileImageUrlBig() == url) {
393 m_user->setProfileImageBig(AvatarImage::create(image));
394 qWarning() << "OTETTIIN VASTAAN SUURI PROFIILIKUVA";
397 for(int i=0;i<m_friendsList.count();i++) {
398 if(m_friendsList.at(i)->profileImageUrl() == url) {
399 m_friendsList.at(i)->setProfileImage(AvatarImage::create(image));
400 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
404 if(m_nbrOfImages == m_visited) {
405 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
406 qDebug() << "emit userDataChanged";
407 emit userDataChanged(m_user, m_friendsList);
411 void SituareService::addProfileImages()
413 qDebug() << __PRETTY_FUNCTION__;
415 // reduce net traffic by sending only one download request for facebook silhouette image
417 emit fetchImage(QUrl(SILHOUETTE_URL));
420 if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
421 emit fetchImage(m_user->profileImageUrl());
422 qWarning() << "PROFILE IMAGE URL: " << m_user->profileImageUrl();
425 if(!m_user->profileImageUrlBig().isEmpty() && m_user->profileImageUrlBig().isValid()) {
426 emit fetchImage(m_user->profileImageUrlBig());
427 qWarning() << "PROFILE IMAGE URL BIG: " << m_user->profileImageUrlBig();
430 for(int i=0;i<m_friendsList.count();i++) {
431 if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
432 m_friendsList.at(i)->profileImageUrl().isValid()) {
433 m_visited++; // indicates how many friends that have profile image
434 emit fetchImage(m_friendsList.at(i)->profileImageUrl());
439 void SituareService::clearUserData()
441 qDebug() << __PRETTY_FUNCTION__;
443 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
444 m_friendsList.clear();
450 emit userDataChanged(m_user, m_friendsList);