From 5236d1a186661d7373063dfd606bd822b29754d7 Mon Sep 17 00:00:00 2001 From: kh1 Date: Thu, 26 Apr 2012 17:31:02 +0200 Subject: Implememt a way to ask for and save user authentication settings. Change-Id: If1fa6bbcb8cd0d3da08f33f96b52589251f9cffc Reviewed-by: Niels Weber Reviewed-by: Tim Jenssen --- src/libs/installer/getrepositorymetainfojob.cpp | 47 +++++++++ src/libs/installer/getrepositorymetainfojob.h | 5 + src/libs/kdtools/authenticationdialog.ui | 133 ++++++++++++++++++++++++ src/libs/kdtools/kdtools.pri | 2 + src/libs/kdtools/kdupdaterfiledownloader.cpp | 49 +++++++-- src/libs/kdtools/kdupdaterfiledownloader.h | 1 + src/sdk/settingsdialog.cpp | 14 +++ src/sdk/settingsdialog.h | 3 + 8 files changed, 245 insertions(+), 9 deletions(-) create mode 100644 src/libs/kdtools/authenticationdialog.ui (limited to 'src') diff --git a/src/libs/installer/getrepositorymetainfojob.cpp b/src/libs/installer/getrepositorymetainfojob.cpp index ac575ab18..c86ab9309 100644 --- a/src/libs/installer/getrepositorymetainfojob.cpp +++ b/src/libs/installer/getrepositorymetainfojob.cpp @@ -226,6 +226,8 @@ void GetRepositoryMetaInfoJob::startUpdatesXmlDownload() connect(m_downloader, SIGNAL(downloadCanceled()), this, SLOT(updatesXmlDownloadCanceled())); connect(m_downloader, SIGNAL(downloadAborted(QString)), this, SLOT(updatesXmlDownloadError(QString)), Qt::QueuedConnection); + connect(m_downloader, SIGNAL(authenticatorChanged(QAuthenticator)), this, + SLOT(onAuthenticatorChanged(QAuthenticator))); m_downloader->download(); } @@ -440,6 +442,8 @@ void GetRepositoryMetaInfoJob::fetchNextMetaInfo() connect(m_downloader, SIGNAL(downloadCompleted()), this, SLOT(metaDownloadFinished())); connect(m_downloader, SIGNAL(downloadAborted(QString)), this, SLOT(metaDownloadError(QString)), Qt::QueuedConnection); + connect(m_downloader, SIGNAL(authenticatorChanged(QAuthenticator)), this, + SLOT(onAuthenticatorChanged(QAuthenticator))); m_downloader->download(); } @@ -512,5 +516,48 @@ void GetRepositoryMetaInfoJob::unzipFinished(bool ok, const QString &error) finished(QInstaller::ExtractionError, error); } +bool GetRepositoryMetaInfoJob::updateRepositories(QSet *repositories, const QString &username, + const QString &password) +{ + if (!repositories->contains(m_repository)) + return false; + + repositories->remove(m_repository); + m_repository.setUsername(username); + m_repository.setPassword(password); + repositories->insert(m_repository); + return true; +} + +void GetRepositoryMetaInfoJob::onAuthenticatorChanged(const QAuthenticator &authenticator) +{ + const QString username = authenticator.user(); + const QString password = authenticator.password(); + if (username != m_repository.username() || password != m_repository.password()) { + QSet repositories = m_corePrivate->m_settings.defaultRepositories(); + bool reposChanged = updateRepositories(&repositories, username, password); + if (reposChanged) + m_corePrivate->m_settings.setDefaultRepositories(repositories); + + repositories = m_corePrivate->m_settings.temporaryRepositories(); + reposChanged |= updateRepositories(&repositories, username, password); + if (reposChanged) { + m_corePrivate->m_settings.setTemporaryRepositories(repositories, + m_corePrivate->m_settings.hasReplacementRepos()); + } + + repositories = m_corePrivate->m_settings.userRepositories(); + reposChanged |= updateRepositories(&repositories, username, password); + if (reposChanged) + m_corePrivate->m_settings.setUserRepositories(repositories); + + if (reposChanged) { + if (m_corePrivate->isUpdater() || m_corePrivate->isPackageManager()) + m_corePrivate->writeMaintenanceConfigFiles(); + finished(QInstaller::RepositoryUpdatesReceived, tr("Repository updates received.")); + } + } +} + #include "getrepositorymetainfojob.moc" #include "moc_getrepositorymetainfojob.cpp" diff --git a/src/libs/installer/getrepositorymetainfojob.h b/src/libs/installer/getrepositorymetainfojob.h index aec94603d..59acb7499 100644 --- a/src/libs/installer/getrepositorymetainfojob.h +++ b/src/libs/installer/getrepositorymetainfojob.h @@ -43,6 +43,8 @@ #include #include +#include + namespace KDUpdater { class FileDownloader; } @@ -75,6 +77,8 @@ private: /* reimp */ void doStart(); /* reimp */ void doCancel(); void finished(int error, const QString &errorString = QString()); + bool updateRepositories(QSet *repositories, const QString &username, + const QString &password); private Q_SLOTS: void startUpdatesXmlDownload(); @@ -88,6 +92,7 @@ private Q_SLOTS: void metaDownloadError(const QString &error); void unzipFinished(bool status, const QString &error); + void onAuthenticatorChanged(const QAuthenticator &authenticator); private: bool m_canceled; diff --git a/src/libs/kdtools/authenticationdialog.ui b/src/libs/kdtools/authenticationdialog.ui new file mode 100644 index 000000000..290963b5e --- /dev/null +++ b/src/libs/kdtools/authenticationdialog.ui @@ -0,0 +1,133 @@ + + Dialog + + + + 0 + 0 + 389 + 243 + + + + Http authentication required + + + + + + You need to supply a Username and Password to access this site. + + + false + + + + + + + Username: + + + + + + + + + + Password: + + + + + + + QLineEdit::Password + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + + + + + 75 + true + + + + %1 at %2 + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/libs/kdtools/kdtools.pri b/src/libs/kdtools/kdtools.pri index 8b14def9a..a83243502 100644 --- a/src/libs/kdtools/kdtools.pri +++ b/src/libs/kdtools/kdtools.pri @@ -5,6 +5,8 @@ CONFIG(shared, static|shared) { DEFINES += BUILD_SHARED_KDTOOLS } +FORMS += authenticationdialog.ui + HEADERS += $$PWD/kdtoolsglobal.h \ $$PWD/kdjob.h \ $$PWD/kdgenericfactory.h \ diff --git a/src/libs/kdtools/kdupdaterfiledownloader.cpp b/src/libs/kdtools/kdupdaterfiledownloader.cpp index 67639dda6..818c8ac70 100644 --- a/src/libs/kdtools/kdupdaterfiledownloader.cpp +++ b/src/libs/kdtools/kdupdaterfiledownloader.cpp @@ -22,9 +22,11 @@ #include "kdupdaterfiledownloader_p.h" #include "kdupdaterfiledownloaderfactory.h" +#include "ui_authenticationdialog.h" #include +#include #include #include #include @@ -507,10 +509,14 @@ QAuthenticator KDUpdater::FileDownloader::authenticator() const /*! Sets the authenticator object for this class to be \a authenticator. A authenticator is used to pass on the required authentication information. This might only be of use for http or ftp requests. + Emits the authenticator changed signal with the new authenticator in use. */ void KDUpdater::FileDownloader::setAuthenticator(const QAuthenticator &authenticator) { - d->m_authenticator = authenticator; + if (d->m_authenticator.isNull() || (d->m_authenticator != authenticator)) { + d->m_authenticator = authenticator; + emit authenticatorChanged(authenticator); + } } // -- KDUpdater::LocalFileDownloader @@ -1057,8 +1063,7 @@ struct KDUpdater::HttpDownloader::Private , destination(0) , downloaded(false) , aborted(false) - , retrying(false) - , m_authenticationDone(false) + , m_authenticationCount(0) {} HttpDownloader *const q; @@ -1068,8 +1073,7 @@ struct KDUpdater::HttpDownloader::Private QString destFileName; bool downloaded; bool aborted; - bool retrying; - bool m_authenticationDone; + int m_authenticationCount; void shutDown() { @@ -1256,7 +1260,7 @@ void KDUpdater::HttpDownloader::timerEvent(QTimerEvent *event) void KDUpdater::HttpDownloader::startDownload(const QUrl &url) { - d->m_authenticationDone = false; + d->m_authenticationCount = 0; d->manager.setProxyFactory(proxyFactory()); d->http = d->manager.get(QNetworkRequest(url)); @@ -1285,11 +1289,38 @@ void KDUpdater::HttpDownloader::startDownload(const QUrl &url) void KDUpdater::HttpDownloader::onAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator) { - qDebug() << reply->readAll(); - if (!d->m_authenticationDone) { - d->m_authenticationDone = true; + Q_UNUSED(reply) + // first try with the information we have already + if (d->m_authenticationCount == 0) { + d->m_authenticationCount++; authenticator->setUser(this->authenticator().user()); authenticator->setPassword(this->authenticator().password()); + } else if (d->m_authenticationCount == 1) { + // we failed to authenticate, ask for new credentials + QDialog dlg; + Ui::Dialog ui; + ui.setupUi(&dlg); + dlg.adjustSize(); + ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url().host())); + + ui.userEdit->setText(this->authenticator().user()); + ui.passwordEdit->setText(this->authenticator().password()); + + if (dlg.exec() == QDialog::Accepted) { + authenticator->setUser(ui.userEdit->text()); + authenticator->setPassword(ui.passwordEdit->text()); + + // update the authenticator we used initially + QAuthenticator auth; + auth.setUser(ui.userEdit->text()); + auth.setPassword(ui.passwordEdit->text()); + emit authenticatorChanged(auth); + } else { + d->shutDown(); + setDownloadAborted(tr("Authentication request canceled.")); + emit downloadCanceled(); + } + d->m_authenticationCount++; } } diff --git a/src/libs/kdtools/kdupdaterfiledownloader.h b/src/libs/kdtools/kdupdaterfiledownloader.h index 67467bff4..6408b7216 100644 --- a/src/libs/kdtools/kdupdaterfiledownloader.h +++ b/src/libs/kdtools/kdupdaterfiledownloader.h @@ -99,6 +99,7 @@ Q_SIGNALS: void downloadSpeed(qint64 bytesPerSecond); void downloadStatus(const QString &status); void downloadProgress(qint64 bytesReceived, qint64 bytesToReceive); + void authenticatorChanged(const QAuthenticator &authenticator); #ifndef Q_MOC_RUN private: diff --git a/src/sdk/settingsdialog.cpp b/src/sdk/settingsdialog.cpp index b3508e169..9f705b41a 100644 --- a/src/sdk/settingsdialog.cpp +++ b/src/sdk/settingsdialog.cpp @@ -63,6 +63,11 @@ TestRepository::~TestRepository() m_downloader->deleteLater(); } +QInstaller::Repository TestRepository::repository() const +{ + return m_repository; +} + void TestRepository::setRepository(const QInstaller::Repository &repository) { cancel(); @@ -98,6 +103,8 @@ void TestRepository::doStart() connect(m_downloader, SIGNAL(downloadCompleted()), this, SLOT(downloadCompleted())); connect(m_downloader, SIGNAL(downloadAborted(QString)), this, SLOT(downloadAborted(QString)), Qt::QueuedConnection); + connect(m_downloader, SIGNAL(authenticatorChanged(QAuthenticator)), this, + SLOT(onAuthenticatorChanged(QAuthenticator))); m_downloader->setAutoRemoveDownloadedFile(true); m_downloader->setUrl(QUrl(url.toString() + QString::fromLatin1("/Updates.xml"))); @@ -150,6 +157,12 @@ void TestRepository::downloadAborted(const QString &reason) emitFinishedWithError(QInstaller::DownloadError, reason); } +void TestRepository::onAuthenticatorChanged(const QAuthenticator &authenticator) +{ + m_repository.setUsername(authenticator.user()); + m_repository.setPassword(authenticator.password()); +} + // -- PasswordDelegate @@ -431,6 +444,7 @@ void SettingsDialog::testRepository() m_testRepository.setRepository(current->repository()); m_testRepository.start(); m_testRepository.waitForFinished(); + current->setRepository(m_testRepository.repository()); if (m_testRepository.error() > KDJob::NoError) { QMessageBox msgBox(this); diff --git a/src/sdk/settingsdialog.h b/src/sdk/settingsdialog.h index a222ecf87..85fa2f26a 100644 --- a/src/sdk/settingsdialog.h +++ b/src/sdk/settingsdialog.h @@ -42,6 +42,7 @@ #include QT_BEGIN_NAMESPACE +class QAuthenticator; class QLocale; class QVariant; QT_END_NAMESPACE @@ -70,6 +71,7 @@ public: TestRepository(QObject *parent = 0); ~TestRepository(); + QInstaller::Repository repository() const; void setRepository(const QInstaller::Repository &repository); private: @@ -79,6 +81,7 @@ private: private Q_SLOTS: void downloadCompleted(); void downloadAborted(const QString &reason); + void onAuthenticatorChanged(const QAuthenticator &authenticator); private: QInstaller::Repository m_repository; -- cgit v1.2.3