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, QNetworkAccessManager *manager)
34 : QObject(parent), m_networkManager(manager)
36 qDebug() << __PRETTY_FUNCTION__;
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)));
48 SituareService::~SituareService()
50 qDebug() << __PRETTY_FUNCTION__;
52 delete m_networkManager;
53 m_networkManager = NULL;
58 delete m_imageFetcher;
59 m_imageFetcher = NULL;
61 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
62 m_friendsList.clear();
65 void SituareService::fetchLocations()
67 qDebug() << __PRETTY_FUNCTION__;
69 CookieHandler cookieHandler;
71 QString cookie = cookieHandler.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);
77 sendRequest(url, COOKIE, cookie);
80 void SituareService::reverseGeo(const QPointF &coordinates)
82 qDebug() << __PRETTY_FUNCTION__;
84 CookieHandler cookieHandler;
86 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
87 m_credentials.sessionKey(), m_credentials.sessionSecret(),
88 m_credentials.sig(), EN_LOCALE);
90 QString urlParameters = formUrlParameters(coordinates);
91 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
93 sendRequest(url, COOKIE, cookie);
96 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
99 qDebug() << __PRETTY_FUNCTION__;
101 CookieHandler cookieHandler;
103 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
104 m_credentials.sessionKey(), m_credentials.sessionSecret(),
105 m_credentials.sig(), EN_LOCALE);
108 QString publishValue;
110 publishValue = PUBLISH_TRUE;
113 publishValue = PUBLISH_FALSE;
115 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
116 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
118 sendRequest(url, COOKIE, cookie);
121 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
123 qDebug() << __PRETTY_FUNCTION__;
126 urlString.append(baseUrl);
127 urlString.append(phpScript);
128 if(urlParameters != NULL)
129 urlString.append(urlParameters);
131 QUrl url = QUrl(urlString);
138 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
142 parameters.append(QUESTION_MARK);
143 parameters.append(LATITUDE);
144 parameters.append(EQUAL_MARK);
145 parameters.append(QString::number(coordinates.x()));
146 parameters.append(AMBERSAND_MARK);
147 parameters.append(LONGTITUDE);
148 parameters.append(EQUAL_MARK);
149 parameters.append(QString::number(coordinates.y()));
151 if(publish.compare(PUBLISH_TRUE) == 0) {
152 parameters.append(AMBERSAND_MARK);
153 parameters.append(PUBLISH);
154 parameters.append(EQUAL_MARK);
155 parameters.append(PUBLISH_TRUE);
157 else if(publish.compare(PUBLISH_FALSE) == 0) {
158 parameters.append(AMBERSAND_MARK);
159 parameters.append(PUBLISH);
160 parameters.append(EQUAL_MARK);
161 parameters.append(PUBLISH_FALSE);
165 parameters.append(AMBERSAND_MARK);
166 parameters.append(DATA);
167 parameters.append(EQUAL_MARK);
168 parameters.append(status);
174 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
176 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
178 QNetworkRequest request;
181 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
183 QNetworkReply *reply = m_networkManager->get(request);
185 m_currentRequests.append(reply);
188 void SituareService::requestFinished(QNetworkReply *reply)
190 qDebug() << __PRETTY_FUNCTION__;
192 QUrl url = reply->url();
193 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
194 if (reply->error()) {
195 qDebug() << reply->errorString();
196 emit error(reply->errorString());
197 // ToDo: some general http error handling etc, signal UI?
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;
209 emit error(replyArray);
211 else if(replyArray == ERROR_LON.toAscii()) {
212 qDebug() << "Error: " << ERROR_LON;
214 emit error(replyArray);
216 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
217 qDebug() << "Error: " << ERROR_SESSION;
219 emit error(replyArray);
221 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
222 qDebug() << "JSON string";
223 parseUserData(replyArray);
225 else if(replyArray == "") {
226 qDebug() << "No error, update was successful";
229 // Street address ready
230 QString address(replyArray);
231 emit reverseGeoReady(address);
234 m_currentRequests.removeAll(reply);
235 reply->deleteLater();
238 void SituareService::credentialsReady(const FacebookCredentials &credentials)
240 qDebug() << __PRETTY_FUNCTION__;
241 m_credentials = credentials;
245 void SituareService::parseUserData(const QByteArray &jsonReply)
247 qDebug() << __PRETTY_FUNCTION__;
251 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
252 m_friendsList.clear();
254 QJson::Parser parser;
257 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
260 qFatal("An error occurred during parsing");
264 QVariant userVariant = result.value("user");
265 QMap<QString, QVariant> userMap = userVariant.toMap();
267 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
269 QUrl imageUrl = userMap["profile_pic"].toUrl();
271 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
272 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
273 true, userMap["uid"].toString());
275 foreach (QVariant friendsVariant, result["friends"].toList()) {
276 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
277 QVariant distance = friendMap["distance"];
278 QMap<QString, QVariant> distanceMap = distance.toMap();
280 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
282 QUrl imageUrl = friendMap["profile_pic"].toUrl();
284 // if (imageUrl.isEmpty())
285 // imageUrl = QUrl("http://static.ak.fbcdn.net/pics/q_silhouette.gif");
287 User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
288 friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
289 false, friendMap["uid"].toString(), distanceMap["units"].toString(),
290 distanceMap["value"].toDouble());
292 m_friendsList.append(user);
297 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
299 qDebug() << __PRETTY_FUNCTION__;
300 qDebug() << "Image URL: " << url << " size :" << image.size();
302 if(m_user->profileImageUrl() == url) {
303 m_user->setProfileImage(image);
306 for(int i=0;i<m_friendsList.count();i++) {
307 if(m_friendsList.at(i)->profileImageUrl() == url) {
308 m_friendsList.at(i)->setProfileImage(image);
309 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
313 if(m_nbrOfImages == m_visited) {
314 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
315 qDebug() << "emit userDataChanged";
316 emit userDataChanged(m_user, m_friendsList);
320 void SituareService::addProfileImages()
322 qDebug() << __PRETTY_FUNCTION__;
324 if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
325 emit fetchImage(m_user->profileImageUrl());
328 for(int i=0;i<m_friendsList.count();i++) {
329 if(!m_friendsList.at(i)->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
330 m_visited++; // indicates how many friends that have profile image
331 emit fetchImage(m_friendsList.at(i)->profileImageUrl());