From 390b28b240755c2636aa8fef96d5b0197a780a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Thu, 21 Sep 2017 15:55:08 +0200 Subject: Slightly revamp the http example 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 Reviewed-by: Timur Pocheptsov --- examples/network/doc/images/http-example.png | Bin 7006 -> 8099 bytes examples/network/http/httpwindow.cpp | 46 ++++++++++++++++----------- examples/network/http/httpwindow.h | 8 ++--- 3 files changed, 32 insertions(+), 22 deletions(-) (limited to 'examples/network') diff --git a/examples/network/doc/images/http-example.png b/examples/network/doc/images/http-example.png index 16b0539b1b..c5f3ef1649 100644 Binary files a/examples/network/doc/images/http-example.png and b/examples/network/doc/images/http-example.png differ diff --git a/examples/network/http/httpwindow.cpp b/examples/network/http/httpwindow.cpp index fddd2c809a..ec90b8f7fe 100644 --- a/examples/network/http/httpwindow.cpp +++ b/examples/network/http/httpwindow.cpp @@ -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) @@ -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) @@ -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); @@ -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); } @@ -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(); } @@ -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; } @@ -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()); @@ -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; @@ -306,7 +316,7 @@ void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authe } #ifndef QT_NO_SSL -void HttpWindow::sslErrors(QNetworkReply*,const QList &errors) +void HttpWindow::sslErrors(QNetworkReply *, const QList &errors) { QString errorString; foreach (const QSslError &error, errors) { diff --git a/examples/network/http/httpwindow.h b/examples/network/http/httpwindow.h index 3bb43dbf89..20ad2bb4da 100644 --- a/examples/network/http/httpwindow.h +++ b/examples/network/http/httpwindow.h @@ -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); @@ -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); @@ -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 &errors); + void sslErrors(QNetworkReply *, const QList &errors); #endif private: -- cgit v1.2.3