Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
optimize download manager
  • Loading branch information
wwmayer committed Aug 31, 2016
1 parent 23f39cd commit 9ef81d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
39 changes: 32 additions & 7 deletions src/Gui/DownloadItem.cpp
Expand Up @@ -477,13 +477,38 @@ void DownloadItem::metaDataChanged()
}
}

QVariant statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (!statusCode.isValid())
return;
int status = statusCode.toInt();
if (status != 200) {
QString reason = m_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
qDebug() << reason;
QUrl url = m_reply->url();

// If this is a redirected url use this instead
QUrl redirectUrl = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (!redirectUrl.isEmpty()) {
QString s = redirectUrl.toString();
std::cout << "Redirected to " << s.toStdString() << std::endl;

QVariant header = m_reply->header(QNetworkRequest::LocationHeader);
QString loc = header.toString();
Q_UNUSED(loc);

if (url != redirectUrl) {
url = redirectUrl;

if (m_reply) {
disconnect(m_reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
disconnect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(error(QNetworkReply::NetworkError)));
disconnect(m_reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(downloadProgress(qint64, qint64)));
disconnect(m_reply, SIGNAL(metaDataChanged()),
this, SLOT(metaDataChanged()));
disconnect(m_reply, SIGNAL(finished()),
this, SLOT(finished()));
m_reply->close();
m_reply->deleteLater();
}

m_reply = DownloadManager::getInstance()->networkAccessManager()->get(QNetworkRequest(url));
init();
}
}
}

Expand Down
35 changes: 2 additions & 33 deletions src/Gui/DownloadManager.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/Gui/DownloadManager.h
Expand Up @@ -77,7 +77,6 @@ public Q_SLOTS:
private Q_SLOTS:
void save() const;
void updateRow();
void replyFinished(QNetworkReply* reply);

private:
void addItem(DownloadItem *item);
Expand Down

0 comments on commit 9ef81d7

Please sign in to comment.