File: simpleDownloader.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (285 lines) | stat: -rw-r--r-- 6,954 bytes parent folder | download | duplicates (4)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <BALL/SYSTEM/simpleDownloader.h>

#include <QtCore/QCoreApplication>
#include <QtNetwork/QSslError>
#include <QtCore/QBuffer>

#include <boost/shared_ptr.hpp>

namespace BALL
{
	namespace SimpleDownloaderHelper
	{
		HelperThread::HelperThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent)
			: err_(0), url_(url), result_(result), parent_(parent)
		{
		}

		HelperThread::HelperThread(const QUrl& url, const String& path, SimpleDownloader* parent)
			: err_(0), url_(url), result_(0), path_(path), parent_(parent)
		{
		}

		int HelperThread::getStatus()
		{
			return err_;
		}

		void HelperThread::run()
		{
			QNetworkAccessManager* man = new QNetworkAccessManager();
			QNetworkReply* reply = getReply_(man);

			BasicHelper* dl;
			if(path_ != "") {
				dl = new DLHelper(this, reply, path_);
			} else {
				dl = new DLArrayHelper(this, reply, result_);
			}

			err_ = exec();

			if(reply->error() != QNetworkReply::NoError) {
				err_ = reply->error();
			}

			delete dl;
			delete reply;
			delete man;
		}

		DLThread::DLThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent)
			: HelperThread(url, result, parent)
		{
		}

		DLThread::DLThread(const QUrl& url, const String& path, SimpleDownloader* parent)
			: HelperThread(url, path, parent)
		{
		}

		QNetworkReply* DLThread::getReply_(QNetworkAccessManager* man)
		{
			return man->get(QNetworkRequest(url_));
		}

		UPThread::UPThread(const QUrl& url, const QByteArray* data, QByteArray* result, SimpleDownloader* parent)
			: HelperThread(url, result, parent), data_(data), file_(0)
		{
		}

		UPThread::UPThread(const QUrl& url, const QByteArray* data, const String& path, SimpleDownloader* parent)
			: HelperThread(url, path, parent), data_(data), file_(0)
		{
		}

		UPThread::UPThread(const QUrl& url, QIODevice* file, QByteArray* result, SimpleDownloader* parent)
			: HelperThread(url, result, parent), data_(0), file_(file)
		{
		}

		UPThread::UPThread(const QUrl& url, QIODevice* file, const String& path, SimpleDownloader* parent)
			: HelperThread(url, path, parent), data_(0), file_(file)
		{
		}

		QNetworkReply* UPThread::getReply_(QNetworkAccessManager* man)
		{
			if(data_)
			{
				return man->post(QNetworkRequest(url_), *data_);
			}
			else
			{
				return man->post(QNetworkRequest(url_), file_);
			}
		}

		BasicHelper::BasicHelper(HelperThread* caller, QNetworkReply* reply)
			: caller_(caller), reply_(reply)
		{
			QObject::connect(reply, SIGNAL(finished()),
												this,   SLOT(finished()));
			QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
												this,   SLOT(error(QNetworkReply::NetworkError)));
#ifndef QT_NO_SSL
			QObject::connect(reply, SIGNAL(sslErrors(const QList<QSslError>&)),
												this,   SLOT(sslErrors(const QList<QSslError>&)));
#endif
		}

		void BasicHelper::error(QNetworkReply::NetworkError error)
		{
			Log.error() << "Error while downloading. Error code is: " << error << "\n";
			caller_->exit(error);
		}

#ifndef QT_NO_SSL
		void BasicHelper::sslErrors(const QList<QSslError>& errors)
		{
			Log.error() << "SSL error(s) while downloading. Errors are:\n";

			QSslError ssl_error;
			Q_FOREACH(ssl_error, errors) {
				Log.error() << ssl_error.errorString().toStdString() << "\n";
			}

			caller_->exit(-1);
		}
#endif

		DLHelper::DLHelper(HelperThread* caller, QNetworkReply* reply, const String& path)
			: BasicHelper(caller, reply), file_(path.c_str())
		{
			file_.open(QIODevice::WriteOnly);
			QObject::connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
												this,   SLOT(receivedData()));
		}

		void DLHelper::finished()
		{
			file_.write(reply_->readAll());
			file_.close();
			caller_->quit();
		}

		void DLHelper::receivedData()
		{
			file_.write(reply_->readAll());
		}

		DLArrayHelper::DLArrayHelper(HelperThread* caller, QNetworkReply* reply, QByteArray* result)
			: BasicHelper(caller, reply), result_(result)
		{
		}

		void DLArrayHelper::finished()
		{
			*result_ = reply_->readAll();
			caller_->quit();
		}
	}

	SimpleDownloader::SimpleDownloader(const String& url, unsigned int timeout)
		: url_(url.c_str()), timeout_(timeout)
	{
	}

	SimpleDownloader::SimpleDownloader(const QUrl& url, unsigned int timeout)
		: url_(url), timeout_(timeout)
	{
	}

	int SimpleDownloader::downloadToBuffer(std::vector<char>& array)
	{
		QByteArray tmp_array;

		int result;

		SimpleDownloaderHelper::DLThread th(url_, &tmp_array, this);

		result = download_(th);

		array.resize(tmp_array.count());
		std::copy(tmp_array.data(), tmp_array.data() + tmp_array.count(), &array[0]);

		return result;
	}

	int SimpleDownloader::downloadToFile(const String& path)
	{
		SimpleDownloaderHelper::DLThread th(url_, path, this);
		return download_(th);
	}

	int SimpleDownloader::uploadStringToBuffer(const String& data, std::vector<char>& response)
	{
		QByteArray tmp_array;
		QByteArray data_array(data.c_str());
		SimpleDownloaderHelper::UPThread th(url_, &data_array, &tmp_array, this);

		int result = download_(th);
		response.resize(tmp_array.count());
		std::copy(tmp_array.data(), tmp_array.data() + tmp_array.count(), &response[0]);

		return result;
	}

	int SimpleDownloader::uploadStringToFile(const String& data, const String& path)
	{
		QByteArray data_array(data.c_str());
		SimpleDownloaderHelper::UPThread th(url_, &data_array, path, this);
		return download_(th);
	}

	int SimpleDownloader::uploadFileToBuffer(const String& path, std::vector<char>& response)
	{
		QByteArray tmp_array;
		QFile file(path.c_str());

		if(!file.open(QIODevice::ReadOnly)) {
			return -1;
		}

		SimpleDownloaderHelper::UPThread th(url_, &file, &tmp_array, this);

		int result = download_(th);
		response.resize(tmp_array.count());
		std::copy(tmp_array.data(), tmp_array.data() + tmp_array.count(), &response[0]);

		return result;
	}

	int SimpleDownloader::uploadFileToFile(const String& in_path, const String& out_path)
	{
		QFile file(in_path.c_str());

		if(!file.open(QIODevice::ReadOnly)) {
			return -1;
		}

		SimpleDownloaderHelper::UPThread th(url_, &file, out_path, this);

		return download_(th);
	}

	int SimpleDownloader::download_(SimpleDownloaderHelper::HelperThread& th)
	{
		// TODO: This can be a std::unique_ptr once we depend on C++11
		boost::shared_ptr<QCoreApplication> app;
		if(!QCoreApplication::instance()) {
			int tmp = 0;
			app.reset(new QCoreApplication(tmp, 0));
		}

		th.start();

		if (!th.wait(timeout_))
		{
			Log.error() << "SimpleDownloader::download_: Download request \"" << (String)(url_.toString()) << "\" timed out.\n";
			return -1;
		}

		return th.getStatus();
	}

	void SimpleDownloader::setTimeout(unsigned int timeout)
	{
		timeout_ = timeout;
	}

	void SimpleDownloader::setURL(const String& url)
	{
		url_ = QUrl(url.c_str());
	}

	void SimpleDownloader::setURL(const QUrl& url)
	{
		url_ = url;
	}

	const QUrl& SimpleDownloader::getURL() const
	{
		return url_;
	}
}