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"
30 #include "../cookiehandler/cookiehandler.h"
33 SituareService::SituareService(QObject *parent)
36 qDebug() << __PRETTY_FUNCTION__;
38 m_networkManager = new QNetworkAccessManager;
39 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
41 m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
42 connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
43 connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
44 SLOT(imageReceived(QUrl, QPixmap)));
45 connect(m_imageFetcher, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
50 SituareService::~SituareService()
52 qDebug() << __PRETTY_FUNCTION__;
54 delete m_networkManager;
60 delete m_imageFetcher;
63 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
64 m_friendsList.clear();
67 void SituareService::fetchLocations()
69 qDebug() << __PRETTY_FUNCTION__;
71 CookieHandler cookieHandler;
73 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
74 m_credentials.sessionKey(), m_credentials.sessionSecret(),
75 m_credentials.sig(), EN_LOCALE);
77 QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
78 sendRequest(url, COOKIE, cookie);
81 void SituareService::reverseGeo(const QPointF &coordinates)
83 qDebug() << __PRETTY_FUNCTION__;
85 CookieHandler cookieHandler;
87 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
88 m_credentials.sessionKey(), m_credentials.sessionSecret(),
89 m_credentials.sig(), EN_LOCALE);
91 QString urlParameters = formUrlParameters(coordinates);
92 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
94 sendRequest(url, COOKIE, cookie);
97 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
100 qDebug() << __PRETTY_FUNCTION__;
102 CookieHandler cookieHandler;
104 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
105 m_credentials.sessionKey(), m_credentials.sessionSecret(),
106 m_credentials.sig(), EN_LOCALE);
109 QString publishValue;
111 publishValue = PUBLISH_TRUE;
114 publishValue = PUBLISH_FALSE;
116 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
117 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
119 sendRequest(url, COOKIE, cookie);
122 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
124 qDebug() << __PRETTY_FUNCTION__;
127 urlString.append(baseUrl);
128 urlString.append(phpScript);
129 if(!urlParameters.isEmpty())
130 urlString.append(urlParameters);
132 QUrl url = QUrl(urlString);
139 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
143 parameters.append(QUESTION_MARK);
144 parameters.append(LATITUDE);
145 parameters.append(EQUAL_MARK);
146 parameters.append(QString::number(coordinates.x()));
147 parameters.append(AMBERSAND_MARK);
148 parameters.append(LONGTITUDE);
149 parameters.append(EQUAL_MARK);
150 parameters.append(QString::number(coordinates.y()));
152 if(publish.compare(PUBLISH_TRUE) == 0) {
153 parameters.append(AMBERSAND_MARK);
154 parameters.append(PUBLISH);
155 parameters.append(EQUAL_MARK);
156 parameters.append(PUBLISH_TRUE);
158 else if(publish.compare(PUBLISH_FALSE) == 0) {
159 parameters.append(AMBERSAND_MARK);
160 parameters.append(PUBLISH);
161 parameters.append(EQUAL_MARK);
162 parameters.append(PUBLISH_FALSE);
165 if(!status.isEmpty()) {
166 parameters.append(AMBERSAND_MARK);
167 parameters.append(DATA);
168 parameters.append(EQUAL_MARK);
169 parameters.append(status);
175 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
177 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
179 QNetworkRequest request;
182 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
184 QNetworkReply *reply = m_networkManager->get(request);
186 m_currentRequests.append(reply);
189 void SituareService::requestFinished(QNetworkReply *reply)
191 qDebug() << __PRETTY_FUNCTION__;
193 QUrl url = reply->url();
194 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
195 if (reply->error()) {
196 qDebug() << reply->errorString();
197 emit error(reply->errorString());
200 qint64 max = reply->size();
201 QByteArray replyArray = reply->read(max);
202 qDebug() << "Reply from: " << url << "reply " << replyArray;
203 // ToDo: results handling includes Situare's errors
204 // works like situare's error handling i.e. both lat & lon are missing/wrong
205 // -> we get only error for wrong lon
206 if(replyArray == ERROR_LAT.toAscii()) {
207 qDebug() << "Error: " << ERROR_LAT;
208 emit error(replyArray);
210 else if(replyArray == ERROR_LON.toAscii()) {
211 qDebug() << "Error: " << ERROR_LON;
212 emit error(replyArray);
214 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
215 qDebug() << "Error: " << ERROR_SESSION;
216 emit error(replyArray);
218 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
219 qDebug() << "JSON string";
220 parseUserData(replyArray);
222 else if(replyArray == "") {
223 qDebug() << "No error, update was successful";
224 emit updateWasSuccessful();
227 // Street address ready
228 QString address(replyArray);
229 emit reverseGeoReady(address);
232 m_currentRequests.removeAll(reply);
233 reply->deleteLater();
236 void SituareService::credentialsReady(const FacebookCredentials &credentials)
238 qDebug() << __PRETTY_FUNCTION__;
239 m_credentials = credentials;
243 void SituareService::parseUserData(const QByteArray &jsonReply)
245 qDebug() << __PRETTY_FUNCTION__;
249 m_defaultImage = false;
250 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
251 m_friendsList.clear();
258 QJson::Parser parser;
261 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
264 qFatal("An error occurred during parsing");
268 QVariant userVariant = result.value("user");
269 QMap<QString, QVariant> userMap = userVariant.toMap();
271 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
273 QUrl imageUrl = userMap["profile_pic"].toUrl();
275 if(imageUrl.isEmpty()) {
276 // user doesn't have profile image, so we need to get him a silhouette image
277 m_defaultImage = true;
280 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
281 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
282 true, userMap["uid"].toString());
284 foreach (QVariant friendsVariant, result["friends"].toList()) {
285 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
286 QVariant distance = friendMap["distance"];
287 QMap<QString, QVariant> distanceMap = distance.toMap();
289 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
291 QUrl imageUrl = friendMap["profile_pic"].toUrl();
293 if(imageUrl.isEmpty()) {
294 // friend doesn't have profile image, so we need to get him a silhouette image
295 m_defaultImage = true;
298 User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
299 friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
300 false, friendMap["uid"].toString(), distanceMap["units"].toString(),
301 distanceMap["value"].toDouble());
303 m_friendsList.append(user);
308 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
310 qDebug() << __PRETTY_FUNCTION__;
311 qDebug() << "Image URL: " << url << " size :" << image.size();
313 // assign facebook silhouette image to all who doesn't have a profile image
314 if(url == QUrl(SILHOUETTE_URL)) {
315 if(m_user->profileImageUrl().isEmpty()) {
316 m_user->setProfileImage(image);
318 for(int i=0;i < m_friendsList.count();i++) {
319 if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
320 m_friendsList.at(i)->setProfileImage(image);
325 if(m_user->profileImageUrl() == url) {
326 m_user->setProfileImage(image);
329 for(int i=0;i<m_friendsList.count();i++) {
330 if(m_friendsList.at(i)->profileImageUrl() == url) {
331 m_friendsList.at(i)->setProfileImage(image);
332 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
336 if(m_nbrOfImages == m_visited) {
337 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
338 qDebug() << "emit userDataChanged";
339 emit userDataChanged(m_user, m_friendsList);
343 void SituareService::addProfileImages()
345 qDebug() << __PRETTY_FUNCTION__;
347 // reduce net traffic by sending only one download request for facebook silhouette image
349 emit fetchImage(QUrl(SILHOUETTE_URL));
352 if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
353 emit fetchImage(m_user->profileImageUrl());
356 for(int i=0;i<m_friendsList.count();i++) {
357 if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
358 m_friendsList.at(i)->profileImageUrl().isValid()) {
359 m_visited++; // indicates how many friends that have profile image
360 emit fetchImage(m_friendsList.at(i)->profileImageUrl());