forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapnetwork.cpp
More file actions
227 lines (200 loc) · 5.87 KB
/
mapnetwork.cpp
File metadata and controls
227 lines (200 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
*
* This file is part of QMapControl,
* an open-source cross-platform map widget
*
* Copyright (C) 2007 - 2008 Kai Winter
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
*
* Contact e-mail: kaiwinter@gmx.de
* Program URL : http://qmapcontrol.sourceforge.net/
*
*/
#include "mapnetwork.h"
#include <QNetworkRequest>
#include <QUrl>
#include <QMapIterator>
#include <QWaitCondition>
#include <QMutexLocker>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QRegExp>
#endif
namespace qmapcontrol
{
MapNetwork::MapNetwork(ImageManager *parent)
: parent(parent)
, http(new QNetworkAccessManager(this))
, loaded(0)
, networkActive(false)
, cacheEnabled(false)
{
connect(http, SIGNAL(finished(QNetworkReply *)), this,
SLOT(requestFinished(QNetworkReply *)));
}
MapNetwork::~MapNetwork()
{
foreach (QNetworkReply *reply, replyList)
{
if (reply->isRunning())
{
reply->abort();
}
reply->deleteLater();
reply = 0;
}
http->deleteLater();
http = 0;
}
void MapNetwork::loadImage(const QString &host, const QString &url)
{
QString hostName = host;
QString portNumber = QString("80");
QRegExp r(".:.");
if (r.indexIn(host) >= 0)
{
QStringList s = host.split(":");
hostName = s.at(0);
portNumber = s.at(1);
}
QString finalUrl = QString("http://%1:%2%3").arg(hostName).arg(portNumber).arg(url);
QNetworkRequest request = QNetworkRequest(QUrl(finalUrl));
if (cacheEnabled)
{
// prefer our cached version (if enabled) over fresh network query
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::PreferCache);
// Data obtained should be saved to cache for future uses
request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, true);
}
request.setRawHeader(
"User-Agent",
"Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)");
QMutexLocker lock(&vectorMutex);
replyList.append(http->get(request));
loadingMap.insert(finalUrl, url);
}
void MapNetwork::requestFinished(QNetworkReply *reply)
{
if (!reply)
{
// qDebug() << "MapNetwork::requestFinished - reply no longer valid";
return;
}
// qDebug() << "MapNetwork::requestFinished" << reply->url().toString();
if (reply->error() == QNetworkReply::NoError)
{
QString id = reply->url().toString();
// check if id is in map?
bool idInMap = false;
QString url;
{
QMutexLocker lock(&vectorMutex);
idInMap = loadingMap.contains(id);
if (idInMap)
{
url = loadingMap[id];
loadingMap.remove(id);
}
}
if (idInMap)
{
// qDebug() << "request finished for reply: " << reply << ", belongs to: " <<
// url << endl;
QByteArray ax;
if (reply->bytesAvailable() > 0)
{
QPixmap pm;
ax = reply->readAll();
if (pm.loadFromData(ax) && pm.size().width() > 1
&& pm.size().height() > 1)
{
loaded
+= pm.size().width() * pm.size().height() * pm.depth() / 8 / 1024;
// qDebug() << "Network loaded: " << loaded << " width:" <<
// pm.size().width() << " height:" <<pm.size().height();
parent->receivedImage(pm, url);
}
else
{
parent->fetchFailed(url);
}
}
}
}
if (loadQueueSize() == 0)
{
// qDebug () << "all loaded";
parent->loadingQueueEmpty();
}
QMutexLocker lock(&vectorMutex);
replyList.removeAll(reply);
reply->deleteLater();
reply = 0;
}
int MapNetwork::loadQueueSize() const
{
QMutexLocker lock(&vectorMutex);
return loadingMap.size();
}
void MapNetwork::setDiskCache(QNetworkDiskCache *qCache)
{
cacheEnabled = (qCache != 0);
if (http)
{
http->setCache(qCache);
}
}
void MapNetwork::abortLoading()
{
// qDebug() << "MapNetwork::abortLoading";
// be sure that replyList is copied in case it's modified in another thread
QListIterator<QNetworkReply *> iter(replyList);
while (iter.hasNext())
{
QNetworkReply *reply = iter.next();
if (reply)
{
if (reply->isRunning())
{
reply->abort();
}
reply->deleteLater();
reply = 0;
}
}
QMutexLocker lock(&vectorMutex);
replyList.clear();
loadingMap.clear();
}
bool MapNetwork::imageIsLoading(QString url)
{
QMutexLocker lock(&vectorMutex);
return loadingMap.values().contains(url);
}
void MapNetwork::setProxy(const QString host, const int port, const QString username,
const QString password)
{
// do not set proxy on qt/extended
#ifndef Q_WS_QWS
if (http)
{
QNetworkProxy proxy = QNetworkProxy(QNetworkProxy::HttpProxy, host, port);
proxy.setUser(username);
proxy.setPassword(password);
http->setProxy(proxy);
}
#endif
}
}