f539c62487fe9b93fd9b38f3287e12c65c59fa73
[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
25 #include "networkhandler.h"
26 #include "networkaccessmanager.h"
27 #include "networkreply.h"
28
29 NetworkAccessManager *NetworkAccessManager::m_instance = 0;
30
31 NetworkAccessManager::NetworkAccessManager()
32     : m_networkHandler(0),
33       m_networkAccessManagerPrivate(0)
34 {
35     m_networkHandler = new NetworkHandler(this);
36     m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
37
38     connect(m_networkHandler, SIGNAL(connected()),
39             this, SLOT(connected()));
40
41     connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
42             this, SLOT(downloadFinished(QNetworkReply*)));
43 }
44
45 QAbstractNetworkCache *NetworkAccessManager::cache() const
46 {
47     qDebug() << __PRETTY_FUNCTION__;
48
49     return m_networkAccessManagerPrivate->cache();
50 }
51
52 void NetworkAccessManager::connected()
53 {
54     qDebug() << __PRETTY_FUNCTION__;
55
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);
60     }
61
62     m_requestQueue.clear();
63 }
64
65 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     QString key = m_temporaryReplyQueue.key(reply, "");
70
71     //Replace offline reply object's content with server reply's content
72     if (!key.isEmpty()) {
73         QNetworkReply *offlineReply = m_offlineReplyQueue.value(key, 0);
74
75         if (offlineReply) {
76             memmove(offlineReply, reply, sizeof(QNetworkReply));
77             m_offlineReplyQueue.remove(key);
78             m_temporaryReplyQueue.remove(key);
79             emit finished(offlineReply);
80         }
81     }
82     //Forward online request's reply
83     else {
84         emit finished(reply);
85     }
86 }
87
88 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
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);
98         return reply;
99     }
100     //Connected, use normal get method.
101     else {
102         return m_networkAccessManagerPrivate->get(request);
103     }
104 }
105
106
107 NetworkAccessManager *NetworkAccessManager::instance()
108 {
109     qDebug() << __PRETTY_FUNCTION__;
110
111     if (!m_instance)
112         m_instance = new NetworkAccessManager();
113
114     return m_instance;
115 }
116
117 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
118 {
119     qDebug() << __PRETTY_FUNCTION__;
120
121     m_networkAccessManagerPrivate->setCache(cache);
122 }