2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@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,
23 #include <QNetworkAccessManager>
24 #include <QNetworkRequest>
27 #include "networkhandler.h"
28 #include "networkreply.h"
30 #include "networkaccessmanager.h"
32 NetworkAccessManager::NetworkAccessManager(QObject *parent)
36 m_networkAccessManagerPrivate(0)
38 qDebug() << __PRETTY_FUNCTION__;
40 m_networkHandler = NetworkHandler::instance();
41 m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
43 connect(m_networkHandler, SIGNAL(connected()),
44 this, SLOT(connected()));
45 connect(m_networkHandler, SIGNAL(disconnected()),
46 this, SLOT(disconnected()));
48 connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
49 this, SLOT(downloadFinished(QNetworkReply*)));
52 QAbstractNetworkCache *NetworkAccessManager::cache() const
54 qDebug() << __PRETTY_FUNCTION__;
56 return m_networkAccessManagerPrivate->cache();
59 void NetworkAccessManager::connected()
61 qDebug() << __PRETTY_FUNCTION__;
65 //Loop through all requests and calls get method.
66 foreach (const QNetworkRequest &request, m_requestQueue) {
67 QNetworkReply *reply = m_networkAccessManagerPrivate->get(request);
68 m_temporaryReplyQueue.insert(request.url().toString(), reply);
71 m_requestQueue.clear();
74 void NetworkAccessManager::disconnected()
76 qDebug() << __PRETTY_FUNCTION__;
81 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
83 qDebug() << __PRETTY_FUNCTION__;
85 QString key = m_temporaryReplyQueue.key(reply, "");
87 //Replace offline reply object's content with server reply's content
89 QNetworkReply *offlineReply = m_offlineReplyQueue.value(key, 0);
92 memmove(offlineReply, reply, sizeof(QNetworkReply));
93 m_offlineReplyQueue.remove(key);
94 m_temporaryReplyQueue.remove(key);
95 emit finished(offlineReply);
98 //Forward online request's reply
100 emit finished(reply);
104 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request, bool onlineRequestsOnly)
106 qDebug() << __PRETTY_FUNCTION__;
108 //Disconnected from network, queue request and return empty reply.
109 if (!m_networkHandler->isConnected()) {
110 m_requestQueue.append(request);
111 m_networkHandler->connect();
112 QNetworkReply *reply = new NetworkReply(request, this);
114 if (onlineRequestsOnly) {
115 ((NetworkReply*)reply)->setErrorCode(QNetworkReply::UnknownNetworkError);
116 emit finished(reply);
119 m_offlineReplyQueue.insert(request.url().toString(), reply);
124 //Connected, use normal get method.
126 return m_networkAccessManagerPrivate->get(request);
130 QNetworkReply *NetworkAccessManager::post(const QNetworkRequest &request, QByteArray &data,
131 bool onlineRequestsOnly)
133 qDebug() << __PRETTY_FUNCTION__;
135 //Disconnected from network, queue request and return empty reply.
136 if (!m_networkHandler->isConnected()) {
137 m_requestQueue.append(request);
138 m_networkHandler->connect();
139 QNetworkReply *reply = new NetworkReply(request, this);
141 if (onlineRequestsOnly) {
142 ((NetworkReply*)reply)->setErrorCode(QNetworkReply::UnknownNetworkError);
143 emit finished(reply);
146 m_offlineReplyQueue.insert(request.url().toString(), reply);
151 //Connected, use normal post method.
153 return m_networkAccessManagerPrivate->post(request, data);
157 bool NetworkAccessManager::isConnected()
159 qDebug() << __PRETTY_FUNCTION__;
164 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
166 qDebug() << __PRETTY_FUNCTION__;
168 m_networkAccessManagerPrivate->setCache(cache);