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,
24 #include <QStringList>
25 #include "situareservice.h"
26 #include "situarecommon.h"
27 #include "../cookiehandler/cookiehandler.h"
29 SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
30 : QObject(parent), networkManager(manager)
32 qDebug() << __PRETTY_FUNCTION__;
34 connect(&facebookAuthentication, SIGNAL(credentialsReady()), SLOT(credentialsReady()));
36 connect(networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
38 credentials = facebookAuthentication.loginCredentials();
41 SituareService::~SituareService()
43 qDebug() << __PRETTY_FUNCTION__;
45 delete networkManager;
48 void SituareService::updateLocation(QPointF coordinates, QString status, bool publish)
50 qDebug() << __PRETTY_FUNCTION__;
52 CookieHandler cookieHandler;
54 QString cookie = cookieHandler.formCookie(API_KEY, credentials.expires(), credentials.userID(), credentials.sessionKey(),
55 credentials.sessionSecret(), credentials.sig(), EN_LOCALE);
60 publishValue = PUBLISH_TRUE;
63 publishValue = PUBLISH_FALSE;
65 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
66 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
68 sendRequest(url, COOKIE, cookie);
71 void SituareService::reverseGeo(QPointF coordinates)
73 qDebug() << __PRETTY_FUNCTION__;
75 CookieHandler cookieHandler;
77 QString cookie = cookieHandler.formCookie(API_KEY, credentials.expires(), credentials.userID(), credentials.sessionKey(),
78 credentials.sessionSecret(), credentials.sig(), EN_LOCALE);
80 QString urlParameters = formUrlParameters(coordinates);
81 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
83 sendRequest(url, COOKIE, cookie);
86 QUrl SituareService::formUrl(const QString baseUrl, const QString phpScript, QString urlParameters)
88 qDebug() << __PRETTY_FUNCTION__;
92 urlString.append(baseUrl);
93 urlString.append(phpScript);
94 if(urlParameters != NULL) // ToDo: check this better
95 urlString.append(urlParameters);
97 QUrl url = QUrl(urlString);
104 QString SituareService::formUrlParameters(QPointF coordinates, QString status, QString publish)
108 parameters.append(QUESTION_MARK);
109 parameters.append(LATITUDE);
110 parameters.append(EQUAL_MARK);
111 parameters.append(QString::number(coordinates.x()));
112 parameters.append(AMBERSAND_MARK);
113 parameters.append(LONGTITUDE);
114 parameters.append(EQUAL_MARK);
115 parameters.append(QString::number(coordinates.y()));
117 if(publish.compare(PUBLISH_TRUE) == 0) {
118 parameters.append(AMBERSAND_MARK);
119 parameters.append(PUBLISH);
120 parameters.append(EQUAL_MARK);
121 parameters.append(PUBLISH_TRUE);
123 else if(publish.compare(PUBLISH_FALSE) == 0) {
124 parameters.append(AMBERSAND_MARK);
125 parameters.append(PUBLISH);
126 parameters.append(EQUAL_MARK);
127 parameters.append(PUBLISH_FALSE);
131 parameters.append(AMBERSAND_MARK);
132 parameters.append(DATA);
133 parameters.append(EQUAL_MARK);
134 parameters.append(status);
140 void SituareService::sendRequest(QUrl url, const QString cookieType, QString cookie)
142 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
144 QNetworkRequest request;
147 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
149 QNetworkReply *reply = networkManager->get(request);
151 currentRequests.append(reply);
154 void SituareService::requestFinished(QNetworkReply *reply)
156 qDebug() << __PRETTY_FUNCTION__;
158 QUrl url = reply->url();
159 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
160 if (reply->error()) {
161 qDebug() << reply->errorString();
162 emit error(reply->errorString());
163 // ToDo: some general http error handling etc, signal UI?
166 qint64 max = reply->size();
167 QByteArray replyArray = reply->read(max);
168 qDebug() << "Reply from: " << url << "reply " << replyArray;
169 // ToDo: results handling includes Situare's errors
170 // works like situare's error handling i.e. both lat & lon are missing/wrong -> we get only error for wrong lon
171 if(replyArray == ERROR_LAT.toAscii()) {
172 qDebug() << "Error: " << ERROR_LAT;
174 emit error(replyArray);
176 else if(replyArray == ERROR_LON.toAscii()) {
177 qDebug() << "Error: " << ERROR_LON;
179 emit error(replyArray);
181 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
182 qDebug() << "Error: " << ERROR_SESSION;
184 emit error(replyArray);
187 // no error -> update was successful
189 qDebug() << reply->read(max);
193 currentRequests.removeAll(reply);
194 reply->deleteLater();
197 void SituareService::credentialsReady()
199 qDebug() << __PRETTY_FUNCTION__;
200 credentials = facebookAuthentication.loginCredentials();