optimize download manager

This commit is contained in:
wmayer
2016-08-31 16:22:21 +02:00
parent dfc1686539
commit ab61f66716
3 changed files with 34 additions and 41 deletions

View File

@@ -71,8 +71,6 @@ DownloadManager::DownloadManager(QWidget *parent)
m_model = new DownloadModel(this);
ui->downloadsView->setModel(m_model);
connect(ui->cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup()));
connect(m_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
load();
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
@@ -142,42 +140,13 @@ QUrl DownloadManager::redirectUrl(const QUrl& url) const
return redirectUrl;
}
void DownloadManager::replyFinished(QNetworkReply* reply)
{
// The 'requestFileName' is used to store the argument passed by 'download()'
// and to also distinguish between replies created by 'download()' and
// this method.
// Replies from this method shouldn't be further examined because it's not
// assumed to get re-directed urls over several steps.
QVariant var = reply->property("requestFileName");
if (var.isValid()) {
bool requestFileName = reply->property("requestFileName").toBool();
QUrl url = reply->url();
// If this is a redirected url use this instead
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (!redirectUrl.isEmpty()) {
url = redirectUrl;
}
// start the actual download now
handleUnsupportedContent(m_manager->get(QNetworkRequest(url)), requestFileName);
}
reply->deleteLater();
}
void DownloadManager::download(const QNetworkRequest &request, bool requestFileName)
{
if (request.url().isEmpty())
return;
std::cout << request.url().toString().toStdString() << std::endl;
// postpone this reply until we can examine it in replyFinished
QNetworkReply* reply = m_manager->get(request);
reply->setProperty("requestFileName", QVariant(requestFileName));
std::cout << request.url().toString().toStdString() << std::endl;
handleUnsupportedContent(m_manager->get(request), requestFileName);
}
void DownloadManager::handleUnsupportedContent(QNetworkReply *reply, bool requestFileName)