Added method to queue requests if not connected to network.
[situare] / src / engine / 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(QObject *parent)
30     : QObject(parent),
31       m_networkHandler(0),
32       m_networkAccessManagerPrivate(0)
33 {
34     m_networkHandler = new NetworkHandler(this);
35     m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
36
37     connect(m_networkHandler, SIGNAL(connected()),
38             this, SLOT(connected()));
39
40     connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
41             this, SLOT(downloadFinished(QNetworkReply*)));
42 }
43
44 void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     m_networkAccessManagerPrivate->setCache(cache);
49 }
50
51 QAbstractNetworkCache *NetworkAccessManager::cache() const
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     return m_networkAccessManagerPrivate->cache();
56 }
57
58 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
59 {
60     qWarning() << __PRETTY_FUNCTION__ << request.url().toString();
61
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);
69         return reply;
70     }
71     //Connected, use normal get method.
72     else {
73         qWarning() << __PRETTY_FUNCTION__ << "connected";
74         return m_networkAccessManagerPrivate->get(request);
75     }
76 }
77
78 void NetworkAccessManager::connected()
79 {
80     qWarning() << __PRETTY_FUNCTION__;
81
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);
87     }
88
89     m_requestQueue.clear();
90 }
91
92 void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
93 {
94     qWarning() << __PRETTY_FUNCTION__ << reply->url().toString();
95
96     QString key = m_temporaryReplyQueue.key(reply);
97
98     //Replace offline reply object's content with server reply's content
99     if (!key.isNull()) {
100         qWarning() << "Sizeof reply: " << sizeof(QNetworkReply);
101
102         QNetworkReply *offlineReply = m_offlineReplyQueue.value(key);
103
104         if (offlineReply) {
105             memmove(offlineReply, reply, sizeof(QNetworkReply));
106             m_offlineReplyQueue.remove(key);
107             m_temporaryReplyQueue.remove(key);
108             emit finished(offlineReply);
109         }
110     }
111     //Forward online request's reply
112     else {
113         emit finished(reply);
114     }
115 }