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::m_instance = 0;
31 NetworkAccessManager::NetworkAccessManager()
32 : m_networkHandler(0),
33 m_networkAccessManagerPrivate(0)
35 m_networkHandler = new NetworkHandler(this);
36 m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
38 connect(m_networkHandler, SIGNAL(connected()),
39 this, SLOT(connected()));
41 connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
42 this, SLOT(downloadFinished(QNetworkReply*)));
45 QAbstractNetworkCache *NetworkAccessManager::cache() const
47 qDebug() << __PRETTY_FUNCTION__;
49 return m_networkAccessManagerPrivate->cache();
52 void NetworkAccessManager::connected()
54 qDebug() << __PRETTY_FUNCTION__;
56 //Loop through all requests and calls get method.
57 foreach (const QNetworkRequest &request, m_requestQueue) {
58 QNetworkReply *reply = m_networkAccessManagerPrivate->get(request);
59 m_temporaryReplyQueue.insert(request.url().toString(), reply);
62 m_requestQueue.clear();
65 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
67 qDebug() << __PRETTY_FUNCTION__;
69 QString key = m_temporaryReplyQueue.key(reply, "");
71 //Replace offline reply object's content with server reply's content
73 QNetworkReply *offlineReply = m_offlineReplyQueue.value(key, 0);
76 memmove(offlineReply, reply, sizeof(QNetworkReply));
77 m_offlineReplyQueue.remove(key);
78 m_temporaryReplyQueue.remove(key);
79 emit finished(offlineReply);
82 //Forward online request's reply
88 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
90 qDebug() << __PRETTY_FUNCTION__;
92 //Disconnected from network, queue request and return empty reply.
93 if (!m_networkHandler->isConnected()) {
94 m_requestQueue.append(request);
95 m_networkHandler->connect();
96 QNetworkReply *reply = new NetworkReply(request, this);
97 m_offlineReplyQueue.insert(request.url().toString(), reply);
100 //Connected, use normal get method.
102 return m_networkAccessManagerPrivate->get(request);
107 NetworkAccessManager *NetworkAccessManager::instance()
109 qDebug() << __PRETTY_FUNCTION__;
112 m_instance = new NetworkAccessManager();
117 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
119 qDebug() << __PRETTY_FUNCTION__;
121 m_networkAccessManagerPrivate->setCache(cache);