diff options
author | kh1 <karsten.heimrich@digia.com> | 2014-01-28 15:08:57 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@digia.com> | 2014-02-03 13:29:31 +0100 |
commit | 7fe93a0bd045060fa0736931a1981bf7ef3c72be (patch) | |
tree | 182c37a9891bff2f22601307b907852904891675 /src/libs/installer/downloadfiletask.cpp | |
parent | 858e16567539b793d31fe918f9728d68307df321 (diff) |
Introduce TaskItem and Authenticator role.
Make sure every reply can use its own authenticator and
pass back the initial task item to the caller(task tracking).
Change-Id: I8b57dbf9d70f3061a91e436a578ec7e0c3138beb
Reviewed-by: Niels Weber <niels.weber@digia.com>
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
Diffstat (limited to 'src/libs/installer/downloadfiletask.cpp')
-rw-r--r-- | src/libs/installer/downloadfiletask.cpp | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/src/libs/installer/downloadfiletask.cpp b/src/libs/installer/downloadfiletask.cpp index d3f5f98f7..8d15a835d 100644 --- a/src/libs/installer/downloadfiletask.cpp +++ b/src/libs/installer/downloadfiletask.cpp @@ -75,11 +75,10 @@ Downloader::~Downloader() } void Downloader::download(QFutureInterface<FileTaskResult> &fi, const QList<FileTaskItem> &items, - const QAuthenticator &authenticator, QNetworkProxyFactory *networkProxyFactory) + QNetworkProxyFactory *networkProxyFactory) { m_items = items; m_futureInterface = &fi; - m_authenticator = authenticator; fi.reportStarted(); fi.setExpectedResultCount(items.count()); @@ -173,8 +172,8 @@ void Downloader::onFinished(QNetworkReply *reply) delete data.file; delete data.observer; - FileTaskItem taskItem(url.toString(), filename); - taskItem.insert(TaskRole::Checksum, data.expectedCheckSum); + FileTaskItem taskItem = data.taskItem; + taskItem.insert(TaskRole::SourceFile, url.toString()); QNetworkReply *const redirectReply = startDownload(taskItem); foreach (const QUrl &redirect, redirects) @@ -199,14 +198,15 @@ void Downloader::onFinished(QNetworkReply *reply) data.observer->addBytesTransfered(ba.size()); data.observer->addCheckSumData(ba.data(), ba.size()); } - m_futureInterface->reportResult(FileTaskResult(filename, data.observer->checkSum())); - if (!data.expectedCheckSum.isEmpty()) { - if (data.expectedCheckSum != data.observer->checkSum().toHex()) { + const QByteArray expectedCheckSum = data.taskItem.value(TaskRole::Checksum).toByteArray(); + if (!expectedCheckSum.isEmpty()) { + if (expectedCheckSum != data.observer->checkSum().toHex()) { m_futureInterface->reportException(FileTaskException(QString::fromLatin1("Checksum" " mismatch detected '%1'.").arg(reply->url().toString()))); } } + m_futureInterface->reportResult(FileTaskResult(filename, data.observer->checkSum(), data.taskItem)); data.file->close(); delete data.file; @@ -259,14 +259,15 @@ void Downloader::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) void Downloader::onAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator) { - Q_UNUSED(reply) - if (!authenticator) + if (!authenticator || !reply || !m_downloads.contains(reply)) return; - if (!m_authenticator.user().isEmpty()) { - authenticator->setUser(m_authenticator.user()); - authenticator->setPassword(m_authenticator.password()); - m_authenticator = QAuthenticator(); // clear so we fail on next call + FileTaskItem *item = &m_downloads[reply].taskItem; + const QAuthenticator auth = item->value(TaskRole::Authenticator).value<QAuthenticator>(); + if (!auth.user().isEmpty()) { + authenticator->setUser(auth.user()); + authenticator->setPassword(auth.password()); + item->insert(TaskRole::Authenticator, QVariant()); // clear so we fail on next call } else { m_futureInterface->reportException(FileTaskException(QString::fromLatin1("Could not " "authenticate using the provided credentials. Source: '%1'.").arg(reply->url() @@ -314,8 +315,7 @@ QNetworkReply *Downloader::startDownload(const FileTaskItem &item) } QNetworkReply *reply = m_nam.get(QNetworkRequest(source)); - m_downloads.insert(reply, Data(file.take(), new FileTaskObserver(QCryptographicHash::Sha1), - item.value(TaskRole::Checksum).toByteArray())); + m_downloads.insert(reply, Data(file.take(), new FileTaskObserver(QCryptographicHash::Sha1), item)); connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead())); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, @@ -373,9 +373,16 @@ void DownloadFileTask::doTask(QFutureInterface<FileTaskResult> &fi) QEventLoop el; Downloader downloader; connect(&downloader, SIGNAL(finished()), &el, SLOT(quit())); - downloader.download(fi, taskItems(), m_authenticator, (m_proxyFactory.isNull() ? 0 - : m_proxyFactory->clone())); // Downloader takes ownership of this copy. - el.exec(); // That's tricky here, we run our own event loop to keep QNAM working + + QList<FileTaskItem> items = taskItems(); + if (!m_authenticator.isNull()) { + for (int i = 0; i < items.count(); ++i) { + if (items.at(i).value(TaskRole::Authenticator).isNull()) + items[i].insert(TaskRole::Authenticator, QVariant::fromValue(m_authenticator)); + } + } + downloader.download(fi, items, (m_proxyFactory.isNull() ? 0 : m_proxyFactory->clone())); + el.exec(); // That's tricky here, we need to run our own event loop to keep QNAM working. } } // namespace QInstaller |