optimize download manager
This commit is contained in:
parent
23f39cdf2d
commit
9ef81d7d56
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -77,7 +77,6 @@ public Q_SLOTS:
|
|||
private Q_SLOTS:
|
||||
void save() const;
|
||||
void updateRow();
|
||||
void replyFinished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
void addItem(DownloadItem *item);
|
||||
|
|
Loading…
Reference in New Issue
Block a user