1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-26 17:55:19 +03:00

[Companion] Delete QNetworkReply after update checks and release notes d-load (prevents leftover open handles, see QNetworkAccessManager docs); Use only one QNetworkAccessManager in MainWindow and properly dispose of it. (#6010)

This commit is contained in:
Max Paperno 2018-07-05 13:01:55 -04:00 committed by Bertrand Songis
parent 47cfd7da98
commit d253c3163b
2 changed files with 32 additions and 17 deletions

View file

@ -80,8 +80,9 @@ const char * const OPENTX_COMPANION_DOWNLOAD_URL[] = {
}; };
MainWindow::MainWindow(): MainWindow::MainWindow():
downloadDialog_forWait(NULL), downloadDialog_forWait(nullptr),
checkForUpdatesState(0), checkForUpdatesState(0),
networkManager(nullptr),
windowsListActions(new QActionGroup(this)) windowsListActions(new QActionGroup(this))
{ {
// setUnifiedTitleAndToolBarOnMac(true); // setUnifiedTitleAndToolBarOnMac(true);
@ -252,39 +253,50 @@ QString MainWindow::getCompanionUpdateBaseUrl()
void MainWindow::checkForUpdates() void MainWindow::checkForUpdates()
{ {
if (!checkForUpdatesState) {
closeUpdatesWaitDialog();
if (networkManager) {
networkManager->deleteLater();
networkManager = nullptr;
}
return;
}
if (checkForUpdatesState & SHOW_DIALOG_WAIT) { if (checkForUpdatesState & SHOW_DIALOG_WAIT) {
checkForUpdatesState -= SHOW_DIALOG_WAIT; checkForUpdatesState -= SHOW_DIALOG_WAIT;
downloadDialog_forWait = new downloadDialog(NULL, tr("Checking for updates")); downloadDialog_forWait = new downloadDialog(NULL, tr("Checking for updates"));
downloadDialog_forWait->show(); downloadDialog_forWait->show();
} }
if (networkManager)
disconnect(networkManager, 0, this, 0);
else
networkManager = new QNetworkAccessManager(this);
QUrl url;
if (checkForUpdatesState & CHECK_COMPANION) { if (checkForUpdatesState & CHECK_COMPANION) {
checkForUpdatesState -= CHECK_COMPANION; checkForUpdatesState -= CHECK_COMPANION;
// TODO why create each time a network manager? url.setUrl(QString("%1/%2").arg(getCompanionUpdateBaseUrl()).arg(COMPANION_STAMP));
networkManager = new QNetworkAccessManager(this); connect(networkManager, &QNetworkAccessManager::finished, this, &MainWindow::checkForCompanionUpdateFinished);
connect(networkManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(checkForCompanionUpdateFinished(QNetworkReply*)));
QUrl url(QString("%1/%2").arg(getCompanionUpdateBaseUrl()).arg(COMPANION_STAMP));
qDebug() << "Checking for Companion update " << url.url(); qDebug() << "Checking for Companion update " << url.url();
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
networkManager->get(request);
} }
else if (checkForUpdatesState & CHECK_FIRMWARE) { else if (checkForUpdatesState & CHECK_FIRMWARE) {
checkForUpdatesState -= CHECK_FIRMWARE; checkForUpdatesState -= CHECK_FIRMWARE;
QString stamp = getCurrentFirmware()->getStampUrl(); const QString stamp = getCurrentFirmware()->getStampUrl();
if (!stamp.isEmpty()) { if (!stamp.isEmpty()) {
networkManager = new QNetworkAccessManager(this); url.setUrl(stamp);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkForFirmwareUpdateFinished(QNetworkReply*))); connect(networkManager, &QNetworkAccessManager::finished, this, &MainWindow::checkForFirmwareUpdateFinished);
QUrl url(stamp);
qDebug() << "Checking for firmware update " << url.url(); qDebug() << "Checking for firmware update " << url.url();
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
networkManager->get(request);
} }
} }
else if (checkForUpdatesState==0) { if (!url.isValid()) {
closeUpdatesWaitDialog(); checkForUpdates();
return;
} }
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
networkManager->get(request);
} }
void MainWindow::onUpdatesError() void MainWindow::onUpdatesError()
@ -320,6 +332,7 @@ QString MainWindow::seekCodeString(const QByteArray & qba, const QString & label
void MainWindow::checkForCompanionUpdateFinished(QNetworkReply * reply) void MainWindow::checkForCompanionUpdateFinished(QNetworkReply * reply)
{ {
QByteArray qba = reply->readAll(); QByteArray qba = reply->readAll();
reply->deleteLater();
QString version = seekCodeString(qba, "VERSION"); QString version = seekCodeString(qba, "VERSION");
if (version.isNull()) if (version.isNull())
return onUpdatesError(); return onUpdatesError();
@ -431,6 +444,7 @@ void MainWindow::checkForFirmwareUpdateFinished(QNetworkReply * reply)
bool ignore = false; bool ignore = false;
QByteArray qba = reply->readAll(); QByteArray qba = reply->readAll();
reply->deleteLater();
QString versionString = seekCodeString(qba, "VERSION"); QString versionString = seekCodeString(qba, "VERSION");
QString dateString = seekCodeString(qba, "DATE"); QString dateString = seekCodeString(qba, "DATE");
if (versionString.isNull() || dateString.isNull()) if (versionString.isNull() || dateString.isNull())

View file

@ -48,5 +48,6 @@ void ReleaseNotesFirmwareDialog::replyFinished(QNetworkReply * reply)
{ {
ui->textEditor->setHtml(reply->readAll()); ui->textEditor->setHtml(reply->readAll());
ui->textEditor->setOpenExternalLinks(true); ui->textEditor->setOpenExternalLinks(true);
reply->deleteLater();
} }