Skip to content

Commit

Permalink
Slightly revamp the http example
Browse files Browse the repository at this point in the history
It was already revamped a fair bit 2 years ago

Replaced Q_NULLPTR with nullptr.
Added a minimum size to the progressbar dialog.
Update the label if a redirect is rejected.
Improve the overwrite dialog message.
Replaced the documentation image.

Task-number: QTBUG-60628
Change-Id: I0fb70d90e1d6ca84a8307bd6ea4ea1ce220feeaf
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Timur Pocheptsov <[email protected]>
  • Loading branch information
Morten242 committed Sep 22, 2017
1 parent ffbe848 commit 390b28b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Binary file modified examples/network/doc/images/http-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 28 additions & 18 deletions examples/network/http/httpwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
#include "httpwindow.h"
#include "ui_authenticationdialog.h"

#ifndef QT_NO_SSL
static const char defaultUrl[] = "https://www.qt.io/";
#if QT_CONFIG(ssl)
const char defaultUrl[] = "https://www.qt.io/";
#else
static const char defaultUrl[] = "http://www.qt.io/";
const char defaultUrl[] = "http://www.qt.io/";
#endif
static const char defaultFileName[] = "index.html";
const char defaultFileName[] = "index.html";

ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent)
: QProgressDialog(parent)
Expand All @@ -71,6 +71,7 @@ ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent)
setMinimum(0);
setValue(0);
setMinimumDuration(0);
setMinimumSize(QSize(400, 75));
}

void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes)
Expand All @@ -87,8 +88,8 @@ HttpWindow::HttpWindow(QWidget *parent)
, launchCheckBox(new QCheckBox("Launch file"))
, defaultFileLineEdit(new QLineEdit(defaultFileName))
, downloadDirectoryLineEdit(new QLineEdit)
, reply(Q_NULLPTR)
, file(Q_NULLPTR)
, reply(nullptr)
, file(nullptr)
, httpRequestAborted(false)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
Expand Down Expand Up @@ -174,15 +175,22 @@ void HttpWindow::downloadFile()
if (fileName.isEmpty())
fileName = defaultFileName;
QString downloadDirectory = QDir::cleanPath(downloadDirectoryLineEdit->text().trimmed());
if (!downloadDirectory.isEmpty() && QFileInfo(downloadDirectory).isDir())
bool useDirectory = !downloadDirectory.isEmpty() && QFileInfo(downloadDirectory).isDir();
if (useDirectory)
fileName.prepend(downloadDirectory + '/');
if (QFile::exists(fileName)) {
if (QMessageBox::question(this, tr("Overwrite Existing File"),
tr("There already exists a file called %1 in "
"the current directory. Overwrite?").arg(fileName),
QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
== QMessageBox::No)
tr("There already exists a file called %1%2."
" Overwrite?")
.arg(fileName,
useDirectory
? QString()
: QStringLiteral(" in the current directory")),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No)
== QMessageBox::No) {
return;
}
QFile::remove(fileName);
}

Expand All @@ -204,7 +212,7 @@ QFile *HttpWindow::openFileForWrite(const QString &fileName)
tr("Unable to save the file %1: %2.")
.arg(QDir::toNativeSeparators(fileName),
file->errorString()));
return Q_NULLPTR;
return nullptr;
}
return file.take();
}
Expand All @@ -224,12 +232,12 @@ void HttpWindow::httpFinished()
fi.setFile(file->fileName());
file->close();
delete file;
file = Q_NULLPTR;
file = nullptr;
}

if (httpRequestAborted) {
reply->deleteLater();
reply = Q_NULLPTR;
reply = nullptr;
return;
}

Expand All @@ -238,21 +246,23 @@ void HttpWindow::httpFinished()
statusLabel->setText(tr("Download failed:\n%1.").arg(reply->errorString()));
downloadButton->setEnabled(true);
reply->deleteLater();
reply = Q_NULLPTR;
reply = nullptr;
return;
}

const QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

reply->deleteLater();
reply = Q_NULLPTR;
reply = nullptr;

if (!redirectionTarget.isNull()) {
const QUrl redirectedUrl = url.resolved(redirectionTarget.toUrl());
if (QMessageBox::question(this, tr("Redirect"),
tr("Redirect to %1 ?").arg(redirectedUrl.toString()),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
QFile::remove(fi.absoluteFilePath());
downloadButton->setEnabled(true);
statusLabel->setText(tr("Download failed:\nRedirect rejected."));
return;
}
file = openFileForWrite(fi.absoluteFilePath());
Expand Down Expand Up @@ -286,7 +296,7 @@ void HttpWindow::enableDownloadButton()
downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
}

void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator)
void HttpWindow::slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator)
{
QDialog authenticationDialog;
Ui::Dialog ui;
Expand All @@ -306,7 +316,7 @@ void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authe
}

#ifndef QT_NO_SSL
void HttpWindow::sslErrors(QNetworkReply*,const QList<QSslError> &errors)
void HttpWindow::sslErrors(QNetworkReply *, const QList<QSslError> &errors)
{
QString errorString;
foreach (const QSslError &error, errors) {
Expand Down
8 changes: 4 additions & 4 deletions examples/network/http/httpwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ProgressDialog : public QProgressDialog {
Q_OBJECT

public:
explicit ProgressDialog(const QUrl &url, QWidget *parent = Q_NULLPTR);
explicit ProgressDialog(const QUrl &url, QWidget *parent = nullptr);

public slots:
void networkReplyProgress(qint64 bytesRead, qint64 totalBytes);
Expand All @@ -82,7 +82,7 @@ class HttpWindow : public QDialog
Q_OBJECT

public:
explicit HttpWindow(QWidget *parent = Q_NULLPTR);
explicit HttpWindow(QWidget *parent = nullptr);

void startRequest(const QUrl &requestedUrl);

Expand All @@ -92,9 +92,9 @@ private slots:
void httpFinished();
void httpReadyRead();
void enableDownloadButton();
void slotAuthenticationRequired(QNetworkReply*,QAuthenticator *);
void slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator);
#ifndef QT_NO_SSL
void sslErrors(QNetworkReply*,const QList<QSslError> &errors);
void sslErrors(QNetworkReply *, const QList<QSslError> &errors);
#endif

private:
Expand Down

0 comments on commit 390b28b

Please sign in to comment.