diff --git a/CMakeLists.txt b/CMakeLists.txt index 7db4036..122b899 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,9 @@ find_package(Qt${QT_VERSION_MAJOR} CONFIG REQUIRED COMPONENTS Widgets Network) add_library(QSimpleUpdater STATIC etc/resources/qsimpleupdater.qrc include/QSimpleUpdater.h + src/AuthenticateDialog.cpp + src/AuthenticateDialog.h + src/AuthenticateDialog.ui src/Downloader.cpp src/Downloader.h src/Downloader.ui diff --git a/include/QSimpleUpdater.h b/include/QSimpleUpdater.h index 4b6700b..f1edc6b 100644 --- a/include/QSimpleUpdater.h +++ b/include/QSimpleUpdater.h @@ -97,6 +97,8 @@ public slots: void setUseCustomAppcast(const QString &url, const bool customAppcast); void setUseCustomInstallProcedures(const QString &url, const bool custom); void setMandatoryUpdate(const QString &url, const bool mandatory_update); + void setDownloadUserName(const QString &url, const QString &userName); + void setDownloadPassword(const QString &url, const QString &password); protected: ~QSimpleUpdater(); diff --git a/src/AuthenticateDialog.cpp b/src/AuthenticateDialog.cpp new file mode 100644 index 0000000..4110851 --- /dev/null +++ b/src/AuthenticateDialog.cpp @@ -0,0 +1,34 @@ +#include "AuthenticateDialog.h" +#include "ui_AuthenticateDialog.h" + +AuthenticateDialog::AuthenticateDialog(QWidget *parent) + : QDialog(parent) + , ui(new Ui::AuthenticateDialog) +{ + ui->setupUi(this); +} + +AuthenticateDialog::~AuthenticateDialog() +{ + delete ui; +} + +void AuthenticateDialog::setUserName(const QString &userName) +{ + ui->userLE->setText(userName); +} + +void AuthenticateDialog::setPassword(const QString &password) +{ + ui->passwordLE->setText(password); +} + +QString AuthenticateDialog::userName() const +{ + return ui->userLE->text(); +} + +QString AuthenticateDialog::password() const +{ + return ui->passwordLE->text(); +} diff --git a/src/AuthenticateDialog.h b/src/AuthenticateDialog.h new file mode 100644 index 0000000..a7636ea --- /dev/null +++ b/src/AuthenticateDialog.h @@ -0,0 +1,28 @@ +#ifndef AUTHENTICATEDIALOG_H +#define AUTHENTICATEDIALOG_H + +#include + +namespace Ui +{ +class AuthenticateDialog; +} + +class AuthenticateDialog : public QDialog +{ + Q_OBJECT + +public: + explicit AuthenticateDialog(QWidget *parent = nullptr); + ~AuthenticateDialog(); + + void setUserName(const QString &userName); + void setPassword(const QString &password); + QString userName() const; + QString password() const; + +private: + Ui::AuthenticateDialog *ui; +}; + +#endif // AUTHENTICATEDIALOG_H diff --git a/src/AuthenticateDialog.ui b/src/AuthenticateDialog.ui new file mode 100644 index 0000000..bcd578f --- /dev/null +++ b/src/AuthenticateDialog.ui @@ -0,0 +1,111 @@ + + + AuthenticateDialog + + + + 0 + 0 + 512 + 164 + + + + Dialog + + + + + + + 0 + 0 + + + + Please provide the user name and password for the download location. + + + + + + + + + &User name: + + + userLE + + + + + + + + + + &Password: + + + passwordLE + + + + + + + QLineEdit::Password + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + AuthenticateDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + AuthenticateDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/Downloader.cpp b/src/Downloader.cpp index b49f8e0..af4171a 100644 --- a/src/Downloader.cpp +++ b/src/Downloader.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2014-2021 Alex Spataru * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -30,9 +30,11 @@ #include #include #include +#include #include +#include "AuthenticateDialog.h" #include "Downloader.h" static const QString PARTIAL_DOWN(".part"); @@ -66,6 +68,8 @@ Downloader::Downloader(QWidget *parent) connect(m_ui->stopButton, SIGNAL(clicked()), this, SLOT(cancelDownload())); connect(m_ui->openButton, SIGNAL(clicked()), this, SLOT(installUpdate())); + connect(m_manager, &QNetworkAccessManager::authenticationRequired, this, &Downloader::authenticate); + /* Resize to fit */ setFixedSize(minimumSizeHint()); } @@ -228,7 +232,7 @@ void Downloader::installUpdate() if (m_mandatoryUpdate) text = tr("In order to install the update, you may need to " - "quit the application. This is a mandatory update, exiting now will close the application"); + "quit the application. This is a mandatory update, exiting now will close the application."); box.setText("

" + text + "

"); @@ -356,7 +360,7 @@ void Downloader::metaDataChanged() if (variant.isValid()) { QString contentDisposition = QByteArray::fromPercentEncoding(variant.toByteArray()).constData(); - QRegularExpression regExp("filename=(\S+)"); + QRegularExpression regExp(R"(filename=(\S+))"); QRegularExpressionMatch match = regExp.match(contentDisposition); if (match.hasMatch()) { @@ -446,6 +450,19 @@ void Downloader::calculateTimeRemaining(qint64 received, qint64 total) } } +void Downloader::authenticate(QNetworkReply *reply, QAuthenticator *authenticator) +{ + Q_UNUSED(reply); + AuthenticateDialog dlg(this); + dlg.setUserName(authenticator->user()); + dlg.setPassword(authenticator->password()); + if (dlg.exec()) + { + authenticator->setUser(dlg.userName()); + authenticator->setPassword(dlg.password()); + } +} + /** * Rounds the given \a input to two decimal places */ diff --git a/src/Downloader.h b/src/Downloader.h index 1969f24..8b6f2b2 100644 --- a/src/Downloader.h +++ b/src/Downloader.h @@ -32,6 +32,7 @@ namespace Ui class Downloader; } +class QAuthenticator; class QNetworkReply; class QNetworkAccessManager; @@ -72,6 +73,7 @@ private slots: void calculateSizes(qint64 received, qint64 total); void updateProgress(qint64 received, qint64 total); void calculateTimeRemaining(qint64 received, qint64 total); + void authenticate(QNetworkReply *reply, QAuthenticator *authenticator); private: qreal round(const qreal &input); diff --git a/src/QSimpleUpdater.cpp b/src/QSimpleUpdater.cpp index 169415b..433aff1 100644 --- a/src/QSimpleUpdater.cpp +++ b/src/QSimpleUpdater.cpp @@ -382,6 +382,16 @@ void QSimpleUpdater::setMandatoryUpdate(const QString &url, const bool mandatory getUpdater(url)->setMandatoryUpdate(mandatory_update); } +void QSimpleUpdater::setDownloadUserName(const QString &url, const QString &userName) +{ + getUpdater(url)->setDownloadUserName(userName); +} + +void QSimpleUpdater::setDownloadPassword(const QString &url, const QString &password) +{ + getUpdater(url)->setDownloadPassword(password); +} + /** * Returns the \c Updater instance registered with the given \a url. * diff --git a/src/Updater.cpp b/src/Updater.cpp index 3ddfbe9..3251adf 100644 --- a/src/Updater.cpp +++ b/src/Updater.cpp @@ -360,6 +360,17 @@ void Updater::setMandatoryUpdate(const bool mandatory_update) { m_mandatoryUpdate = mandatory_update; } + +void Updater::setDownloadUserName(const QString &user_name) +{ + m_downloadUserName = user_name; +} + +void Updater::setDownloadPassword(const QString &password) +{ + m_downloadPassword = password; +} + /** * Called when the download of the update definitions file is finished. */ @@ -435,9 +446,12 @@ void Updater::setUpdateAvailable(const bool available) QString text = tr("Would you like to download the update now?"); if (m_mandatoryUpdate) { - text = tr("Would you like to download the update now? This is a mandatory update, exiting now will close the " - "application"); + text = tr("Would you like to download the update now?
This is a mandatory update, exiting now will close " + "the application."); } + text += "

"; + if (!m_changelog.isEmpty()) + text += tr("Change log:
%1").arg(m_changelog); QString title = "

" + tr("Version %1 of %2 has been released!").arg(latestVersion()).arg(moduleName()) + "

"; @@ -457,7 +471,10 @@ void Updater::setUpdateAvailable(const bool available) m_downloader->setUrlId(url()); m_downloader->setFileName(downloadUrl().split("/").last()); m_downloader->setMandatoryUpdate(m_mandatoryUpdate); - m_downloader->startDownload(QUrl(downloadUrl())); + auto url = QUrl(downloadUrl()); + url.setUserName(m_downloadUserName); + url.setPassword(m_downloadPassword); + m_downloader->startDownload(url); } else diff --git a/src/Updater.h b/src/Updater.h index a1e1c94..3af4825 100644 --- a/src/Updater.h +++ b/src/Updater.h @@ -80,6 +80,8 @@ public slots: void setUseCustomAppcast(const bool customAppcast); void setUseCustomInstallProcedures(const bool custom); void setMandatoryUpdate(const bool mandatory_update); + void setDownloadUserName(const QString &user_name); + void setDownloadPassword(const QString &password); private slots: void onReply(QNetworkReply *reply); @@ -106,7 +108,8 @@ private slots: QString m_downloadUrl; QString m_moduleVersion; QString m_latestVersion; - + QString m_downloadUserName; + QString m_downloadPassword; Downloader *m_downloader; QNetworkAccessManager *m_manager; };