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>
26 #include "networkhandler.h"
27 #include "networkaccessmanager.h"
28 #include "networkreply.h"
30 NetworkAccessManager *NetworkAccessManager::m_instance = 0;
32 NetworkAccessManager::NetworkAccessManager()
33 : m_networkHandler(0),
34 m_networkAccessManagerPrivate(0)
36 qDebug() << __PRETTY_FUNCTION__;
38 m_networkHandler = new NetworkHandler(this);
39 m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
41 connect(m_networkHandler, SIGNAL(connected()),
42 this, SLOT(connected()));
44 connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
45 this, SLOT(downloadFinished(QNetworkReply*)));
48 QAbstractNetworkCache *NetworkAccessManager::cache() const
50 qDebug() << __PRETTY_FUNCTION__;
52 return m_networkAccessManagerPrivate->cache();
55 void NetworkAccessManager::connected()
57 qDebug() << __PRETTY_FUNCTION__;
59 //Loop through all requests and calls get method.
60 foreach (const QNetworkRequest &request, m_requestQueue) {
61 QNetworkReply *reply = m_networkAccessManagerPrivate->get(request);
62 m_temporaryReplyQueue.insert(request.url().toString(), reply);
65 m_requestQueue.clear();
68 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
70 qDebug() << __PRETTY_FUNCTION__;
72 QString key = m_temporaryReplyQueue.key(reply, "");
74 //Replace offline reply object's content with server reply's content
76 QNetworkReply *offlineReply = m_offlineReplyQueue.value(key, 0);
79 memmove(offlineReply, reply, sizeof(QNetworkReply));
80 m_offlineReplyQueue.remove(key);
81 m_temporaryReplyQueue.remove(key);
82 emit finished(offlineReply);
85 //Forward online request's reply
91 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
93 qDebug() << __PRETTY_FUNCTION__;
95 //Disconnected from network, queue request and return empty reply.
96 if (!m_networkHandler->isConnected()) {
97 m_requestQueue.append(request);
98 m_networkHandler->connect();
99 QNetworkReply *reply = new NetworkReply(request, this);
100 m_offlineReplyQueue.insert(request.url().toString(), reply);
103 //Connected, use normal get method.
105 return m_networkAccessManagerPrivate->get(request);
110 NetworkAccessManager *NetworkAccessManager::instance()
112 qDebug() << __PRETTY_FUNCTION__;
115 m_instance = new NetworkAccessManager();
120 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
122 qDebug() << __PRETTY_FUNCTION__;
124 m_networkAccessManagerPrivate->setCache(cache);