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), m_networkManager(manager)
32 qDebug() << __PRETTY_FUNCTION__;
34 connect(&m_facebookAuthentication, SIGNAL(credentialsReady()), SLOT(credentialsReady()));
36 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
38 m_credentials = m_facebookAuthentication.loginCredentials();
41 SituareService::~SituareService()
43 qDebug() << __PRETTY_FUNCTION__;
45 delete m_networkManager;
48 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
51 qDebug() << __PRETTY_FUNCTION__;
53 CookieHandler cookieHandler;
55 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
56 m_credentials.sessionKey(), m_credentials.sessionSecret(),
57 m_credentials.sig(), EN_LOCALE);
62 publishValue = PUBLISH_TRUE;
65 publishValue = PUBLISH_FALSE;
67 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
68 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
70 sendRequest(url, COOKIE, cookie);
73 void SituareService::reverseGeo(const QPointF &coordinates)
75 qDebug() << __PRETTY_FUNCTION__;
77 CookieHandler cookieHandler;
79 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
80 m_credentials.sessionKey(), m_credentials.sessionSecret(),
81 m_credentials.sig(), EN_LOCALE);
83 QString urlParameters = formUrlParameters(coordinates);
84 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
86 sendRequest(url, COOKIE, cookie);
89 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
91 qDebug() << __PRETTY_FUNCTION__;
94 urlString.append(baseUrl);
95 urlString.append(phpScript);
96 if(urlParameters != NULL)
97 urlString.append(urlParameters);
99 QUrl url = QUrl(urlString);
106 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
110 parameters.append(QUESTION_MARK);
111 parameters.append(LATITUDE);
112 parameters.append(EQUAL_MARK);
113 parameters.append(QString::number(coordinates.x()));
114 parameters.append(AMBERSAND_MARK);
115 parameters.append(LONGTITUDE);
116 parameters.append(EQUAL_MARK);
117 parameters.append(QString::number(coordinates.y()));
119 if(publish.compare(PUBLISH_TRUE) == 0) {
120 parameters.append(AMBERSAND_MARK);
121 parameters.append(PUBLISH);
122 parameters.append(EQUAL_MARK);
123 parameters.append(PUBLISH_TRUE);
125 else if(publish.compare(PUBLISH_FALSE) == 0) {
126 parameters.append(AMBERSAND_MARK);
127 parameters.append(PUBLISH);
128 parameters.append(EQUAL_MARK);
129 parameters.append(PUBLISH_FALSE);
133 parameters.append(AMBERSAND_MARK);
134 parameters.append(DATA);
135 parameters.append(EQUAL_MARK);
136 parameters.append(status);
142 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
144 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
146 QNetworkRequest request;
149 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
151 QNetworkReply *reply = m_networkManager->get(request);
153 m_currentRequests.append(reply);
156 void SituareService::requestFinished(QNetworkReply *reply)
158 qDebug() << __PRETTY_FUNCTION__;
160 QUrl url = reply->url();
161 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
162 if (reply->error()) {
163 qDebug() << reply->errorString();
164 emit error(reply->errorString());
165 // ToDo: some general http error handling etc, signal UI?
168 qint64 max = reply->size();
169 QByteArray replyArray = reply->read(max);
170 qDebug() << "Reply from: " << url << "reply " << replyArray;
171 // ToDo: results handling includes Situare's errors
172 // works like situare's error handling i.e. both lat & lon are missing/wrong
173 // -> we get only error for wrong lon
174 if(replyArray == ERROR_LAT.toAscii()) {
175 qDebug() << "Error: " << ERROR_LAT;
177 emit error(replyArray);
179 else if(replyArray == ERROR_LON.toAscii()) {
180 qDebug() << "Error: " << ERROR_LON;
182 emit error(replyArray);
184 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
185 qDebug() << "Error: " << ERROR_SESSION;
187 emit error(replyArray);
190 // no error -> update was successful
192 qDebug() << reply->read(max);
196 m_currentRequests.removeAll(reply);
197 reply->deleteLater();
200 void SituareService::credentialsReady()
202 qDebug() << __PRETTY_FUNCTION__;
203 m_credentials = m_facebookAuthentication.loginCredentials();