Fixed unit test of MapView
[situare] / src / network / networkaccessmanager.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
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.
10
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.
15
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,
19    USA.
20 */
21
22 #include <QNetworkRequest>
23 #include <QNetworkAccessManager>
24 #include <QDebug>
25
26 #include "networkhandler.h"
27 #include "networkaccessmanager.h"
28 #include "networkreply.h"
29
30 NetworkAccessManager *NetworkAccessManager::m_instance = 0;
31
32 NetworkAccessManager::NetworkAccessManager()
33     : m_networkHandler(0),
34       m_networkAccessManagerPrivate(0)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37
38     m_networkHandler = new NetworkHandler(this);
39     m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
40
41     connect(m_networkHandler, SIGNAL(connected()),
42             this, SLOT(connected()));
43
44     connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
45             this, SLOT(downloadFinished(QNetworkReply*)));
46 }
47
48 QAbstractNetworkCache *NetworkAccessManager::cache() const
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     return m_networkAccessManagerPrivate->cache();
53 }
54
55 void NetworkAccessManager::connected()
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
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);
63     }
64
65     m_requestQueue.clear();
66 }
67
68 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71
72     QString key = m_temporaryReplyQueue.key(reply, "");
73
74     //Replace offline reply object's content with server reply's content
75     if (!key.isEmpty()) {
76         QNetworkReply *offlineReply = m_offlineReplyQueue.value(key, 0);
77
78         if (offlineReply) {
79             memmove(offlineReply, reply, sizeof(QNetworkReply));
80             m_offlineReplyQueue.remove(key);
81             m_temporaryReplyQueue.remove(key);
82             emit finished(offlineReply);
83         }
84     }
85     //Forward online request's reply
86     else {
87         emit finished(reply);
88     }
89 }
90
91 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
92 {
93     qDebug() << __PRETTY_FUNCTION__;
94
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);
101         return reply;
102     }
103     //Connected, use normal get method.
104     else {
105         return m_networkAccessManagerPrivate->get(request);
106     }
107 }
108
109
110 NetworkAccessManager *NetworkAccessManager::instance()
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     if (!m_instance)
115         m_instance = new NetworkAccessManager();
116
117     return m_instance;
118 }
119
120 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     m_networkAccessManagerPrivate->setCache(cache);
125 }