Skip to content

Commit

Permalink
Merge pull request #1767 from matterhorn103/fix-plugin-downloader
Browse files Browse the repository at this point in the history
Fix plugin downloader on Qt6
  • Loading branch information
ghutchis authored Nov 9, 2024
2 parents ef16055 + aac01fd commit 687e701
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions avogadro/qtplugins/plugindownloader/downloaderwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ void DownloaderWidget::downloadREADME(int row, int col)
QNetworkRequest request;
setRawHeaders(&request);
request.setUrl(url); // Set the url

m_reply = m_NetworkAccessManager->get(request);
connect(m_reply, SIGNAL(finished()), this, SLOT(showREADME()));
}
Expand Down Expand Up @@ -283,7 +282,6 @@ void DownloaderWidget::downloadNext()
QNetworkRequest request;
setRawHeaders(&request);
request.setUrl(url); // Set the url

m_reply = m_NetworkAccessManager->get(request);
connect(m_reply, SIGNAL(finished()), this, SLOT(handleRedirect()));
}
Expand Down Expand Up @@ -332,24 +330,48 @@ bool DownloaderWidget::checkToInstall()
}

// The download url for Github is always a redirect to the actual zip
// Using Qt 6 the redirect gets taken care of automatically, but on Qt 5 we
// have to do it manually
// m_reply is a QNetworkReply
void DownloaderWidget::handleRedirect()
{
int statusCode =
m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (m_reply->error() == QNetworkReply::NoError) {
QVariant statusCode =
m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (statusCode.toInt() == 302) {
if (statusCode == 302) {
// Redirected, have to manually redirect
QVariant possibleRedirectUrl =
m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

QUrl _urlRedirectedTo = possibleRedirectUrl.toUrl();

QNetworkRequest request;
setRawHeaders(&request);
request.setUrl(_urlRedirectedTo); // Set the url
m_reply = m_NetworkAccessManager->get(request);
// Now we have the actual zip and can extract it
connect(m_reply, SIGNAL(finished()), this, SLOT(unzipPlugin()));
} else if (statusCode == 200) {
// Normal success response
unzipPlugin();
} else {
// Something went wrong
QString errorString = m_reply->errorString();
m_ui->readmeBrowser->append(
tr("Failed to download from %1: status code %2, %3\n",
"After an HTTP request; %1 is a URL, %2 is the HTTP status code, %3 "
"is the error message (if any)")
.arg(m_reply->url().toString())
.arg(statusCode)
.arg(errorString));
}
} else {
QString errorString = m_reply->errorString();
m_ui->readmeBrowser->append(
tr("Failed to download from %1: status code %2, %3\n",
"After an HTTP request; %1 is a URL, %2 is the HTTP status code, %3 "
"is the error message (if any)")
.arg(m_reply->url().toString())
.arg(statusCode)
.arg(errorString));
m_reply->deleteLater();
m_downloadList.removeLast();
downloadNext();
Expand Down

0 comments on commit 687e701

Please sign in to comment.