-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpdownload.cpp
237 lines (207 loc) · 7.59 KB
/
httpdownload.cpp
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
#include "httpdownload.h"
#include "ui_httpdownload.h"
#include "mainwindow.h"
//#include "inputdialog.h"
HttpDownload::HttpDownload(QWidget *parent) :
QDialog(parent),
ui(new Ui::HttpDownload)
{
QString content;
ui->setupUi(this);
//ui->urlEdit->setText("https://nchc.dl.sourceforge.net/project/hplip/hplip/3.20.3/hplip-3.20.3.run");
QFile sources;
sources.setFileName("newssources.txt");
if (sources.open(QIODevice::ReadOnly)) {
while(!sources.atEnd()) {
QString line = sources.readLine();
//QStringList fields = line.split(",");
line = line.trimmed();
int pos1 = line.indexOf("|");
if (pos1 != -1) ui->urlList->addItem(line.mid(0,pos1));
else ui->urlList->addItem(line);
}
sources.close();
ui->urlList->addItem("");
//qDebug() << infile.size() << in.readAll();
}
ui->urlList->itemData(ui->urlList->currentIndex());
ui->statusLabel->setWordWrap(true);
ui->downloadButton->setDefault(true);
ui->quitButton->setAutoDefault(false);
//connect(ui->urlList, SIGNAL(textChanged(QString)),
// this, SLOT(enableDownloadButton()));
}
HttpDownload::~HttpDownload()
{
delete ui;
}
void HttpDownload::on_downloadButton_clicked()
{
QString data = ui->urlList->itemText(ui->urlList->currentIndex());
if (data == "") {
tmpstring = "";
labeltext = "Enter URL :";
inputDialog addurl;
addurl.setModal(true); // if nomodal is needed then create pointer inputdialog *datesearch; in mainwindow.h private section, then here use inputdialog = new datesearch(this); datesearch.show();
addurl.exec();
data = tmpstring;
}
if (data != "") {
manager = new QNetworkAccessManager(this);
progressDialog = new QProgressDialog(this);
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
// get url
//url = (ui->urlEdit->text());
tmpstring = data;
data.remove(QRegularExpression("[\\n\\t\\r]"));
//qDebug() << data;
//url = (ui->urlList->itemText(ui->urlList->currentIndex()));
url = (data);
//qDebug() << url;
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
fileName.remove(QRegularExpression("[\\n\\t\\r]"));
if (fileName.isEmpty())
fileName = "headnews.html";
filesource = fileName;
if (QFile::exists(fileName)) {
//if (QMessageBox::question(this, tr("HTTP"),
// tr("There already exists a file called %1 in "
// "the current directory. Overwrite?").arg(fileName),
// QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
//== QMessageBox::No)
//return;
QFile::remove(fileName);
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("HTTP"),
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
return;
}
// used for progressDialog
// This will be set true when canceled from progress dialog
httpRequestAborted = false;
progressDialog->setWindowTitle(tr("HTTP"));
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
// download button disabled after requesting download
ui->downloadButton->setEnabled(false);
startRequest(url);
}
}
void HttpDownload::httpReadyRead()
{
// this slot gets called every time the QNetworkReply has new data.
// We read all of its new data and write it into the file.
// That way we use less RAM than when reading it at the finished()
// signal of the QNetworkReply
if (file)
file->write(reply->readAll());
}
void HttpDownload::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes)
{
if (httpRequestAborted)
return;
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(bytesRead);
}
void HttpDownload::on_quitButton_clicked()
{
this->close();
}
/*void HttpDownload::on_urlList_returnPressed()
{
on_downloadButton_clicked();
}*/
void HttpDownload::enableDownloadButton()
{
//ui->downloadButton->setEnabled(!(ui->urlEdit->text()).isEmpty());
ui->downloadButton->setEnabled((ui->urlList->itemText(ui->urlList->currentIndex())).isEmpty());
}
// During the download progress, it can be canceled
void HttpDownload::cancelDownload()
{
ui->statusLabel->setText(tr("Download canceled."));
httpRequestAborted = true;
reply->abort();
ui->downloadButton->setEnabled(true);
}
// When download finished or canceled, this will be called
void HttpDownload::httpDownloadFinished()
{
// when canceled
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
progressDialog->hide();
return;
}
// download finished normally
progressDialog->hide();
file->flush();
file->close();
// get redirection url
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error()) {
file->remove();
QMessageBox::information(this, tr("HTTP"),
tr("Download failed: %1.")
.arg(reply->errorString()));
ui->downloadButton->setEnabled(true);
} else if (!redirectionTarget.isNull()) {
QUrl newUrl = url.resolved(redirectionTarget.toUrl());
if (QMessageBox::question(this, tr("HTTP"),
tr("Redirect to %1 ?").arg(newUrl.toString()),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
url = newUrl;
reply->deleteLater();
file->open(QIODevice::WriteOnly);
file->resize(0);
startRequest(url);
return;
}
} else {
//QString fileName = QFileInfo(QUrl(ui->urlEdit->text()).path()).fileName();
QString fileName = QFileInfo(QUrl(ui->urlList->itemText(ui->urlList->currentIndex())).path()).fileName();
//qDebug() << fileName;
//ui->downloadButton->setEnabled((ui->urlList->itemText(ui->urlList->currentIndex())));
ui->statusLabel->setText(tr("Downloaded %1 to %2.").arg(fileName).arg(QDir::currentPath()));
ui->downloadButton->setEnabled(true);
}
reply->deleteLater();
reply = 0;
delete file;
file = 0;
manager = 0;
emit dl_ready(ui->urlList->itemText(ui->urlList->currentIndex()));
}
// This will be called when download button is clicked
void HttpDownload::startRequest(QUrl url)
{
// get() method posts a request
// to obtain the contents of the target request
// and returns a new QNetworkReply object
// opened for reading which emits
// the readyRead() signal whenever new data arrives.
reply = manager->get(QNetworkRequest(url));
// Whenever more data is received from the network,
// this readyRead() signal is emitted
connect(reply, SIGNAL(readyRead()),
this, SLOT(httpReadyRead()));
// Also, downloadProgress() signal is emitted when data is received
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(updateDownloadProgress(qint64,qint64)));
// This signal is emitted when the reply has finished processing.
// After this signal is emitted,
// there will be no more updates to the reply's data or metadata.
connect(reply, SIGNAL(finished()),
this, SLOT(httpDownloadFinished()));
}