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,
22 #include <QNetworkRequest>
23 #include <QNetworkAccessManager>
25 #include "networkhandler.h"
26 #include "networkaccessmanager.h"
27 #include "networkreply.h"
29 NetworkAccessManager::NetworkAccessManager(QObject *parent)
32 m_networkAccessManagerPrivate(0)
34 m_networkHandler = new NetworkHandler(this);
35 m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
37 connect(m_networkHandler, SIGNAL(connected()),
38 this, SLOT(connected()));
40 connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
41 this, SLOT(downloadFinished(QNetworkReply*)));
44 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
46 qDebug() << __PRETTY_FUNCTION__;
48 m_networkAccessManagerPrivate->setCache(cache);
51 QAbstractNetworkCache *NetworkAccessManager::cache() const
53 qDebug() << __PRETTY_FUNCTION__;
55 return m_networkAccessManagerPrivate->cache();
58 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
60 qWarning() << __PRETTY_FUNCTION__ << request.url().toString();
62 //Disconnected from network, queue request and return empty reply.
63 if (!m_networkHandler->isConnected()) {
64 qWarning() << __PRETTY_FUNCTION__ << "not connected";
65 m_requestQueue.append(request);
66 m_networkHandler->connect();
67 QNetworkReply *reply = new NetworkReply(request, this);
68 m_offlineReplyQueue.insert(request.url().toString(), reply);
71 //Connected, use normal get method.
73 qWarning() << __PRETTY_FUNCTION__ << "connected";
74 return m_networkAccessManagerPrivate->get(request);
78 void NetworkAccessManager::connected()
80 qWarning() << __PRETTY_FUNCTION__;
82 //Loop through all requests and calls get method.
83 foreach (const QNetworkRequest &request, m_requestQueue) {
84 qWarning() << __PRETTY_FUNCTION__ << "get():" << request.url().toString();
85 QNetworkReply *reply = m_networkAccessManagerPrivate->get(request);
86 m_temporaryReplyQueue.insert(request.url().toString(), reply);
89 m_requestQueue.clear();
92 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
94 qWarning() << __PRETTY_FUNCTION__ << reply->url().toString();
96 QString key = m_temporaryReplyQueue.key(reply);
98 //Replace offline reply object's content with server reply's content
100 qWarning() << "Sizeof reply: " << sizeof(QNetworkReply);
102 QNetworkReply *offlineReply = m_offlineReplyQueue.value(key);
105 memmove(offlineReply, reply, sizeof(QNetworkReply));
106 m_offlineReplyQueue.remove(key);
107 m_temporaryReplyQueue.remove(key);
108 emit finished(offlineReply);
111 //Forward online request's reply
113 emit finished(reply);