From 6aa294384993423d09c4755d0557cd76785cfa9f Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Tue, 10 Nov 2020 14:07:15 +0000 Subject: [PATCH] Confirm redownload of files if less than 100 days old --- plugins/channelrx/demodadsb/adsbdemodgui.cpp | 73 +++++++++++++++----- plugins/channelrx/demodadsb/adsbdemodgui.h | 2 + 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/plugins/channelrx/demodadsb/adsbdemodgui.cpp b/plugins/channelrx/demodadsb/adsbdemodgui.cpp index 07cc002c7..f19599682 100644 --- a/plugins/channelrx/demodadsb/adsbdemodgui.cpp +++ b/plugins/channelrx/demodadsb/adsbdemodgui.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "ui_adsbdemodgui.h" @@ -1215,14 +1216,18 @@ void ADSBDemodGUI::on_getOSNDB_clicked(bool checked) // Don't try to download while already in progress if (m_progressDialog == nullptr) { - // Download Opensky network database to a file - QUrl dbURL(QString(OSNDB_URL)); - m_progressDialog = new QProgressDialog(this); - m_progressDialog->setAttribute(Qt::WA_DeleteOnClose); - m_progressDialog->setCancelButton(nullptr); - m_progressDialog->setLabelText(QString("Downloading %1.").arg(OSNDB_URL)); - QNetworkReply *reply = m_dlm.download(dbURL, getOSNDBFilename()); - connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); + QString osnDBFilename = getOSNDBFilename(); + if (confirmDownload(osnDBFilename)) + { + // Download Opensky network database to a file + QUrl dbURL(QString(OSNDB_URL)); + m_progressDialog = new QProgressDialog(this); + m_progressDialog->setAttribute(Qt::WA_DeleteOnClose); + m_progressDialog->setCancelButton(nullptr); + m_progressDialog->setLabelText(QString("Downloading %1.").arg(OSNDB_URL)); + QNetworkReply *reply = m_dlm.download(dbURL, osnDBFilename); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); + } } } @@ -1231,14 +1236,18 @@ void ADSBDemodGUI::on_getAirportDB_clicked(bool checked) // Don't try to download while already in progress if (m_progressDialog == nullptr) { - // Download Opensky network database to a file - QUrl dbURL(QString(AIRPORTS_URL)); - m_progressDialog = new QProgressDialog(this); - m_progressDialog->setAttribute(Qt::WA_DeleteOnClose); - m_progressDialog->setCancelButton(nullptr); - m_progressDialog->setLabelText(QString("Downloading %1.").arg(AIRPORTS_URL)); - QNetworkReply *reply = m_dlm.download(dbURL, getAirportDBFilename()); - connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); + QString airportDBFile = getAirportDBFilename(); + if (confirmDownload(airportDBFile)) + { + // Download Opensky network database to a file + QUrl dbURL(QString(AIRPORTS_URL)); + m_progressDialog = new QProgressDialog(this); + m_progressDialog->setAttribute(Qt::WA_DeleteOnClose); + m_progressDialog->setCancelButton(nullptr); + m_progressDialog->setLabelText(QString("Downloading %1.").arg(AIRPORTS_URL)); + QNetworkReply *reply = m_dlm.download(dbURL, airportDBFile); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); + } } } @@ -1276,6 +1285,38 @@ QString ADSBDemodGUI::getFastDBFilename() return getDataDir() + "/aircraftDatabaseFast.csv"; } +qint64 ADSBDemodGUI::fileAgeInDays(QString filename) +{ + QFile file(filename); + if (file.exists()) + { + QDateTime modified = file.fileTime(QFileDevice::FileModificationTime); + if (modified.isValid()) + return modified.daysTo(QDateTime::currentDateTime()); + else + return -1; + } + return -1; +} + +bool ADSBDemodGUI::confirmDownload(QString filename) +{ + qint64 age = fileAgeInDays(filename); + if ((age == -1) || (age > 100)) + return true; + else + { + QMessageBox::StandardButton reply; + if (age == 0) + reply = QMessageBox::question(this, "Confirm download", "This file was last downloaded today. Are you sure you wish to redownload it?", QMessageBox::Yes|QMessageBox::No); + else if (age == 1) + reply = QMessageBox::question(this, "Confirm download", "This file was last downloaded yesterday. Are you sure you wish to redownload it?", QMessageBox::Yes|QMessageBox::No); + else + reply = QMessageBox::question(this, "Confirm download", QString("This file was last downloaded %1 days ago. Are you sure you wish to redownload this file?").arg(age), QMessageBox::Yes|QMessageBox::No); + return reply == QMessageBox::Yes; + } +} + bool ADSBDemodGUI::readOSNDB(const QString& filename) { m_aircraftInfo = AircraftInformation::readOSNDB(filename); diff --git a/plugins/channelrx/demodadsb/adsbdemodgui.h b/plugins/channelrx/demodadsb/adsbdemodgui.h index 99d210c76..9f80b833b 100644 --- a/plugins/channelrx/demodadsb/adsbdemodgui.h +++ b/plugins/channelrx/demodadsb/adsbdemodgui.h @@ -508,6 +508,8 @@ private: QString getAirportFrequenciesDBFilename(); QString getOSNDBFilename(); QString getFastDBFilename(); + qint64 ADSBDemodGUI::fileAgeInDays(QString filename); + bool ADSBDemodGUI::confirmDownload(QString filename); void readAirportDB(const QString& filename); void readAirportFrequenciesDB(const QString& filename); bool readOSNDB(const QString& filename);