1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "httpwindow.h"
52
53#include "ui_authenticationdialog.h"
54
55#include <QtWidgets>
56#include <QtNetwork>
57#include <QUrl>
58
59#if QT_CONFIG(ssl)
60const char defaultUrl[] = "https://www.qt.io/";
61#else
62const char defaultUrl[] = "http://www.qt.io/";
63#endif
64const char defaultFileName[] = "index.html";
65
66ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent)
67 : QProgressDialog(parent)
68{
69 setWindowTitle(tr(s: "Download Progress"));
70 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
71 setLabelText(tr(s: "Downloading %1.").arg(a: url.toDisplayString()));
72 setMinimum(0);
73 setValue(0);
74 setMinimumDuration(0);
75 setMinimumSize(QSize(400, 75));
76}
77
78ProgressDialog::~ProgressDialog()
79{
80}
81
82void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes)
83{
84 setMaximum(totalBytes);
85 setValue(bytesRead);
86}
87
88HttpWindow::HttpWindow(QWidget *parent)
89 : QDialog(parent)
90 , statusLabel(new QLabel(tr(s: "Please enter the URL of a file you want to download.\n\n"), this))
91 , urlLineEdit(new QLineEdit(defaultUrl))
92 , downloadButton(new QPushButton(tr(s: "Download")))
93 , launchCheckBox(new QCheckBox("Launch file"))
94 , defaultFileLineEdit(new QLineEdit(defaultFileName))
95 , downloadDirectoryLineEdit(new QLineEdit)
96 , reply(nullptr)
97 , file(nullptr)
98 , httpRequestAborted(false)
99{
100 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
101 setWindowTitle(tr(s: "HTTP"));
102
103 connect(sender: &qnam, signal: &QNetworkAccessManager::authenticationRequired,
104 receiver: this, slot: &HttpWindow::slotAuthenticationRequired);
105#ifndef QT_NO_SSL
106 connect(sender: &qnam, signal: &QNetworkAccessManager::sslErrors,
107 receiver: this, slot: &HttpWindow::sslErrors);
108#endif
109
110 QFormLayout *formLayout = new QFormLayout;
111 urlLineEdit->setClearButtonEnabled(true);
112 connect(sender: urlLineEdit, signal: &QLineEdit::textChanged,
113 receiver: this, slot: &HttpWindow::enableDownloadButton);
114 formLayout->addRow(labelText: tr(s: "&URL:"), field: urlLineEdit);
115 QString downloadDirectory = QStandardPaths::writableLocation(type: QStandardPaths::TempLocation);
116 if (downloadDirectory.isEmpty() || !QFileInfo(downloadDirectory).isDir())
117 downloadDirectory = QDir::currentPath();
118 downloadDirectoryLineEdit->setText(QDir::toNativeSeparators(pathName: downloadDirectory));
119 formLayout->addRow(labelText: tr(s: "&Download directory:"), field: downloadDirectoryLineEdit);
120 formLayout->addRow(labelText: tr(s: "Default &file:"), field: defaultFileLineEdit);
121 launchCheckBox->setChecked(true);
122 formLayout->addRow(widget: launchCheckBox);
123
124 QVBoxLayout *mainLayout = new QVBoxLayout(this);
125 mainLayout->addLayout(layout: formLayout);
126
127 mainLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
128
129 statusLabel->setWordWrap(true);
130 mainLayout->addWidget(statusLabel);
131
132 downloadButton->setDefault(true);
133 connect(sender: downloadButton, signal: &QAbstractButton::clicked, receiver: this, slot: &HttpWindow::downloadFile);
134 QPushButton *quitButton = new QPushButton(tr(s: "Quit"));
135 quitButton->setAutoDefault(false);
136 connect(sender: quitButton, signal: &QAbstractButton::clicked, receiver: this, slot: &QWidget::close);
137 QDialogButtonBox *buttonBox = new QDialogButtonBox;
138 buttonBox->addButton(button: downloadButton, role: QDialogButtonBox::ActionRole);
139 buttonBox->addButton(button: quitButton, role: QDialogButtonBox::RejectRole);
140 mainLayout->addWidget(buttonBox);
141
142 urlLineEdit->setFocus();
143}
144
145HttpWindow::~HttpWindow()
146{
147}
148
149void HttpWindow::startRequest(const QUrl &requestedUrl)
150{
151 url = requestedUrl;
152 httpRequestAborted = false;
153
154 reply = qnam.get(request: QNetworkRequest(url));
155 connect(sender: reply, signal: &QNetworkReply::finished, receiver: this, slot: &HttpWindow::httpFinished);
156 connect(sender: reply, signal: &QIODevice::readyRead, receiver: this, slot: &HttpWindow::httpReadyRead);
157
158 ProgressDialog *progressDialog = new ProgressDialog(url, this);
159 progressDialog->setAttribute(Qt::WA_DeleteOnClose);
160 connect(sender: progressDialog, signal: &QProgressDialog::canceled, receiver: this, slot: &HttpWindow::cancelDownload);
161 connect(sender: reply, signal: &QNetworkReply::downloadProgress, receiver: progressDialog, slot: &ProgressDialog::networkReplyProgress);
162 connect(sender: reply, signal: &QNetworkReply::finished, receiver: progressDialog, slot: &ProgressDialog::hide);
163 progressDialog->show();
164
165 statusLabel->setText(tr(s: "Downloading %1...").arg(a: url.toString()));
166}
167
168void HttpWindow::downloadFile()
169{
170 const QString urlSpec = urlLineEdit->text().trimmed();
171 if (urlSpec.isEmpty())
172 return;
173
174 const QUrl newUrl = QUrl::fromUserInput(userInput: urlSpec);
175 if (!newUrl.isValid()) {
176 QMessageBox::information(parent: this, title: tr(s: "Error"),
177 text: tr(s: "Invalid URL: %1: %2").arg(args: urlSpec, args: newUrl.errorString()));
178 return;
179 }
180
181 QString fileName = newUrl.fileName();
182 if (fileName.isEmpty())
183 fileName = defaultFileLineEdit->text().trimmed();
184 if (fileName.isEmpty())
185 fileName = defaultFileName;
186 QString downloadDirectory = QDir::cleanPath(path: downloadDirectoryLineEdit->text().trimmed());
187 bool useDirectory = !downloadDirectory.isEmpty() && QFileInfo(downloadDirectory).isDir();
188 if (useDirectory)
189 fileName.prepend(s: downloadDirectory + '/');
190 if (QFile::exists(fileName)) {
191 if (QMessageBox::question(parent: this, title: tr(s: "Overwrite Existing File"),
192 text: tr(s: "There already exists a file called %1%2."
193 " Overwrite?")
194 .arg(args&: fileName,
195 args: useDirectory
196 ? QString()
197 : QStringLiteral(" in the current directory")),
198 buttons: QMessageBox::Yes | QMessageBox::No,
199 defaultButton: QMessageBox::No)
200 == QMessageBox::No) {
201 return;
202 }
203 QFile::remove(fileName);
204 }
205
206 file = openFileForWrite(fileName);
207 if (!file)
208 return;
209
210 downloadButton->setEnabled(false);
211
212 // schedule the request
213 startRequest(requestedUrl: newUrl);
214}
215
216std::unique_ptr<QFile> HttpWindow::openFileForWrite(const QString &fileName)
217{
218 std::unique_ptr<QFile> file(new QFile(fileName));
219 if (!file->open(flags: QIODevice::WriteOnly)) {
220 QMessageBox::information(parent: this, title: tr(s: "Error"),
221 text: tr(s: "Unable to save the file %1: %2.")
222 .arg(args: QDir::toNativeSeparators(pathName: fileName),
223 args: file->errorString()));
224 return nullptr;
225 }
226 return file;
227}
228
229void HttpWindow::cancelDownload()
230{
231 statusLabel->setText(tr(s: "Download canceled."));
232 httpRequestAborted = true;
233 reply->abort();
234 downloadButton->setEnabled(true);
235}
236
237void HttpWindow::httpFinished()
238{
239 QFileInfo fi;
240 if (file) {
241 fi.setFile(file->fileName());
242 file->close();
243 file.reset();
244 }
245
246 if (httpRequestAborted) {
247 reply->deleteLater();
248 reply = nullptr;
249 return;
250 }
251
252 if (reply->error()) {
253 QFile::remove(fileName: fi.absoluteFilePath());
254 statusLabel->setText(tr(s: "Download failed:\n%1.").arg(a: reply->errorString()));
255 downloadButton->setEnabled(true);
256 reply->deleteLater();
257 reply = nullptr;
258 return;
259 }
260
261 const QVariant redirectionTarget = reply->attribute(code: QNetworkRequest::RedirectionTargetAttribute);
262
263 reply->deleteLater();
264 reply = nullptr;
265
266 if (!redirectionTarget.isNull()) {
267 const QUrl redirectedUrl = url.resolved(relative: redirectionTarget.toUrl());
268 if (QMessageBox::question(parent: this, title: tr(s: "Redirect"),
269 text: tr(s: "Redirect to %1 ?").arg(a: redirectedUrl.toString()),
270 buttons: QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
271 QFile::remove(fileName: fi.absoluteFilePath());
272 downloadButton->setEnabled(true);
273 statusLabel->setText(tr(s: "Download failed:\nRedirect rejected."));
274 return;
275 }
276 file = openFileForWrite(fileName: fi.absoluteFilePath());
277 if (!file) {
278 downloadButton->setEnabled(true);
279 return;
280 }
281 startRequest(requestedUrl: redirectedUrl);
282 return;
283 }
284
285 statusLabel->setText(tr(s: "Downloaded %1 bytes to %2\nin\n%3")
286 .arg(a: fi.size()).arg(args: fi.fileName(), args: QDir::toNativeSeparators(pathName: fi.absolutePath())));
287 if (launchCheckBox->isChecked())
288 QDesktopServices::openUrl(url: QUrl::fromLocalFile(localfile: fi.absoluteFilePath()));
289 downloadButton->setEnabled(true);
290}
291
292void HttpWindow::httpReadyRead()
293{
294 // this slot gets called every time the QNetworkReply has new data.
295 // We read all of its new data and write it into the file.
296 // That way we use less RAM than when reading it at the finished()
297 // signal of the QNetworkReply
298 if (file)
299 file->write(data: reply->readAll());
300}
301
302void HttpWindow::enableDownloadButton()
303{
304 downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
305}
306
307void HttpWindow::slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator)
308{
309 QDialog authenticationDialog;
310 Ui::Dialog ui;
311 ui.setupUi(&authenticationDialog);
312 authenticationDialog.adjustSize();
313 ui.siteDescription->setText(tr(s: "%1 at %2").arg(args: authenticator->realm(), args: url.host()));
314
315 // Did the URL have information? Fill the UI
316 // This is only relevant if the URL-supplied credentials were wrong
317 ui.userEdit->setText(url.userName());
318 ui.passwordEdit->setText(url.password());
319
320 if (authenticationDialog.exec() == QDialog::Accepted) {
321 authenticator->setUser(ui.userEdit->text());
322 authenticator->setPassword(ui.passwordEdit->text());
323 }
324}
325
326#ifndef QT_NO_SSL
327void HttpWindow::sslErrors(QNetworkReply *, const QList<QSslError> &errors)
328{
329 QString errorString;
330 for (const QSslError &error : errors) {
331 if (!errorString.isEmpty())
332 errorString += '\n';
333 errorString += error.errorString();
334 }
335
336 if (QMessageBox::warning(parent: this, title: tr(s: "SSL Errors"),
337 text: tr(s: "One or more SSL errors has occurred:\n%1").arg(a: errorString),
338 buttons: QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {
339 reply->ignoreSslErrors();
340 }
341}
342#endif
343

source code of qtbase/examples/network/http/httpwindow.cpp