Menu

[b84aab]: / src / platform / qt / httpfile.cpp  Maximize  Restore  History

Download this file

62 lines (52 with data), 1.6 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
// This file is part of SmallBASIC
//
// Copyright(C) 2001-2012 Chris Warren-Smith.
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
#include <QNetworkAccessManager>
#include "httpfile.h"
QNetworkAccessManager manager;
HttpFile::HttpFile(HttpFileListener *listener, const QString path) :
QTemporaryFile() {
this->listener = listener;
this->url = QUrl(path);
requestFile();
}
HttpFile::~HttpFile() {
reply->deleteLater();
}
void HttpFile::requestFile() {
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "SmallBASIC - QT");
open(QIODevice::WriteOnly);
reply = manager.get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(finished()));
}
void HttpFile::finished() {
flush();
close();
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error()) {
listener->loadError(reply->errorString());
deleteLater();
} else if (!redirectionTarget.isNull()) {
url = url.resolved(redirectionTarget.toUrl());
open(QIODevice::WriteOnly);
resize(0);
requestFile();
} else {
// success
listener->loadPath(fileName(), false, true);
deleteLater();
}
}
// Called every time the QNetworkReply has new data. Read all of
// its new data and write it into the This uses less RAM than
// when reading it at the finished() signal of the QNetworkReply
void HttpFile::readyRead() {
write(reply->readAll());
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.