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 SituareService::SituareService(QObject *parent)
35 qDebug() << __PRETTY_FUNCTION__;
37 m_networkManager = new QNetworkAccessManager;
38 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
40 m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
41 connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
42 connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
43 SLOT(imageReceived(QUrl, QPixmap)));
44 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.x()));
198 parameters.append(AMBERSAND_MARK);
199 parameters.append(LONGTITUDE);
200 parameters.append(EQUAL_MARK);
201 parameters.append(QString::number(coordinates.y()));
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 error(replyArray);
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();
282 // Street address ready
283 QString address(replyArray);
284 emit reverseGeoReady(address);
287 m_currentRequests.removeAll(reply);
288 reply->deleteLater();
291 void SituareService::credentialsReady(const FacebookCredentials &credentials)
293 qDebug() << __PRETTY_FUNCTION__;
294 m_credentials = credentials;
298 void SituareService::parseUserData(const QByteArray &jsonReply)
300 qDebug() << __PRETTY_FUNCTION__;
304 m_defaultImage = false;
305 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
306 m_friendsList.clear();
313 QJson::Parser parser;
316 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
319 qFatal("An error occurred during parsing");
323 QVariant userVariant = result.value("user");
324 QMap<QString, QVariant> userMap = userVariant.toMap();
326 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
328 QUrl imageUrl = userMap["profile_pic"].toUrl();
330 if(imageUrl.isEmpty()) {
331 // user doesn't have profile image, so we need to get him a silhouette image
332 m_defaultImage = true;
335 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
336 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
337 true, userMap["uid"].toString());
339 foreach (QVariant friendsVariant, result["friends"].toList()) {
340 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
341 QVariant distance = friendMap["distance"];
342 QMap<QString, QVariant> distanceMap = distance.toMap();
344 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
346 QUrl imageUrl = friendMap["profile_pic"].toUrl();
348 if(imageUrl.isEmpty()) {
349 // friend doesn't have profile image, so we need to get him a silhouette image
350 m_defaultImage = true;
353 User *user = new User(friendMap["address"].toString(), coordinates,
354 friendMap["name"].toString(),
355 friendMap["note"].toString(), imageUrl,
356 friendMap["timestamp"].toString(),
357 false, friendMap["uid"].toString(),
358 distanceMap["units"].toString(),
359 distanceMap["value"].toDouble());
361 m_friendsList.append(user);
366 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
368 qDebug() << __PRETTY_FUNCTION__;
369 qDebug() << "Image URL: " << url << " size :" << image.size();
371 // assign facebook silhouette image to all who doesn't have a profile image
372 if(url == QUrl(SILHOUETTE_URL)) {
373 if(m_user->profileImageUrl().isEmpty()) {
374 m_user->setProfileImage(image);
376 for(int i=0;i < m_friendsList.count();i++) {
377 if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
378 m_friendsList.at(i)->setProfileImage(image);
383 if(m_user->profileImageUrl() == url) {
384 m_user->setProfileImage(image);
387 for(int i=0;i<m_friendsList.count();i++) {
388 if(m_friendsList.at(i)->profileImageUrl() == url) {
389 m_friendsList.at(i)->setProfileImage(image);
390 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
394 if(m_nbrOfImages == m_visited) {
395 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
396 qDebug() << "emit userDataChanged";
397 emit userDataChanged(m_user, m_friendsList);
401 void SituareService::addProfileImages()
403 qDebug() << __PRETTY_FUNCTION__;
405 // reduce net traffic by sending only one download request for facebook silhouette image
407 emit fetchImage(QUrl(SILHOUETTE_URL));
410 if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
411 emit fetchImage(m_user->profileImageUrl());
414 for(int i=0;i<m_friendsList.count();i++) {
415 if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
416 m_friendsList.at(i)->profileImageUrl().isValid()) {
417 m_visited++; // indicates how many friends that have profile image
418 emit fetchImage(m_friendsList.at(i)->profileImageUrl());