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("Download Progress"));
70 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
71 setLabelText(tr("Downloading %1.").arg(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("Please enter the URL of a file you want to download.\n\n"), this))
91 , urlLineEdit(new QLineEdit(defaultUrl))
92 , downloadButton(new QPushButton(tr("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("HTTP"));
102
103 connect(&qnam, &QNetworkAccessManager::authenticationRequired,
104 this, &HttpWindow::slotAuthenticationRequired);
105#ifndef QT_NO_SSL
106 connect(&qnam, &QNetworkAccessManager::sslErrors,
107 this, &HttpWindow::sslErrors);
108#endif
109
110 QFormLayout *formLayout = new QFormLayout;
111 urlLineEdit->setClearButtonEnabled(true);
112 connect(urlLineEdit, &QLineEdit::textChanged,
113 this, &HttpWindow::enableDownloadButton);
114 formLayout->addRow(tr("&URL:"), urlLineEdit);
115 QString downloadDirectory = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
116 if (downloadDirectory.isEmpty() || !QFileInfo(downloadDirectory).isDir())
117 downloadDirectory = QDir::currentPath();
118 downloadDirectoryLineEdit->setText(QDir::toNativeSeparators(downloadDirectory));
119 formLayout->addRow(tr("&Download directory:"), downloadDirectoryLineEdit);
120 formLayout->addRow(tr("Default &file:"), defaultFileLineEdit);
121 launchCheckBox->setChecked(true);
122 formLayout->addRow(launchCheckBox);
123
124 QVBoxLayout *mainLayout = new QVBoxLayout(this);
125 mainLayout->addLayout(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(downloadButton, &QAbstractButton::clicked, this, &HttpWindow::downloadFile);
134 QPushButton *quitButton = new QPushButton(tr("Quit"));
135 quitButton->setAutoDefault(false);
136 connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
137 QDialogButtonBox *buttonBox = new QDialogButtonBox;
138 buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
139 buttonBox->addButton(quitButton, 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(QNetworkRequest(url));
155 connect(reply, &QNetworkReply::finished, this, &HttpWindow::httpFinished);
156 connect(reply, &QIODevice::readyRead, this, &HttpWindow::httpReadyRead);
157
158 ProgressDialog *progressDialog = new ProgressDialog(url, this);
159 progressDialog->setAttribute(Qt::WA_DeleteOnClose);
160 connect(progressDialog, &QProgressDialog::canceled, this, &HttpWindow::cancelDownload);
161 connect(reply, &QNetworkReply::downloadProgress, progressDialog, &ProgressDialog::networkReplyProgress);
162 connect(reply, &QNetworkReply::finished, progressDialog, &ProgressDialog::hide);
163 progressDialog->show();
164
165 statusLabel->setText(tr("Downloading %1...").arg(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(urlSpec);
175 if (!newUrl.isValid()) {
176 QMessageBox::information(this, tr("Error"),
177 tr("Invalid URL: %1: %2").arg(urlSpec, 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(downloadDirectoryLineEdit->text().trimmed());
187 bool useDirectory = !downloadDirectory.isEmpty() && QFileInfo(downloadDirectory).isDir();
188 if (useDirectory)
189 fileName.prepend(downloadDirectory + '/');
190 if (QFile::exists(fileName)) {
191 if (QMessageBox::question(this, tr("Overwrite Existing File"),
192 tr("There already exists a file called %1%2."
193 " Overwrite?")
194 .arg(fileName,
195 useDirectory
196 ? QString()
197 : QStringLiteral(" in the current directory")),
198 QMessageBox::Yes | QMessageBox::No,
199 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(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(QIODevice::WriteOnly)) {
220 QMessageBox::information(this, tr("Error"),
221 tr("Unable to save the file %1: %2.")
222 .arg(QDir::toNativeSeparators(fileName),
223 file->errorString()));
224 return nullptr;
225 }
226 return file;
227}
228
229void HttpWindow::cancelDownload()
230{
231 statusLabel->setText(tr("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(fi.absoluteFilePath());
254 statusLabel->setText(tr("Download failed:\n%1.").arg(reply->errorString()));
255 downloadButton->setEnabled(true);
256 reply->deleteLater();
257 reply = nullptr;
258 return;
259 }
260
261 const QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
262
263 reply->deleteLater();
264 reply = nullptr;
265
266 if (!redirectionTarget.isNull()) {
267 const QUrl redirectedUrl = url.resolved(redirectionTarget.toUrl());
268 if (QMessageBox::question(this, tr("Redirect"),
269 tr("Redirect to %1 ?").arg(redirectedUrl.toString()),
270 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
271 QFile::remove(fi.absoluteFilePath());
272 downloadButton->setEnabled(true);
273 statusLabel->setText(tr("Download failed:\nRedirect rejected."));
274 return;
275 }
276 file = openFileForWrite(fi.absoluteFilePath());
277 if (!file) {
278 downloadButton->setEnabled(true);
279 return;
280 }
281 startRequest(redirectedUrl);
282 return;
283 }
284
285 statusLabel->setText(tr("Downloaded %1 bytes to %2\nin\n%3")
286 .arg(fi.size()).arg(fi.fileName(), QDir::toNativeSeparators(fi.absolutePath())));
287 if (launchCheckBox->isChecked())
288 QDesktopServices::openUrl(QUrl::fromLocalFile(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(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("%1 at %2").arg(authenticator->realm(), 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(this, tr("SSL Errors"),
337 tr("One or more SSL errors has occurred:\n%1").arg(errorString),
338 QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {
339 reply->ignoreSslErrors();
340 }
341}
342#endif
343