use different temp file class; use VERYYYYMMMDD version from file

This commit is contained in:
Brian Moran 2023-03-16 19:13:59 -07:00
parent b812ac2786
commit 88b1e46ee4
4 changed files with 32 additions and 23 deletions

View File

@ -25,7 +25,8 @@ void FileDownload::errorOccurred(QNetworkReply::NetworkError code)
{
LOG_INFO(QString{"FileDownload [%1]: errorOccurred %2 -> %3"}.arg(user_agent_).arg(code).arg(reply_->errorString()));
Q_EMIT error (reply_->errorString ());
delete tmpfile_;
destfile_.cancelWriting ();
destfile_.commit ();
}
void FileDownload::configure(QNetworkAccessManager *network_manager, const QString &source_url, const QString &destination_path, const QString &user_agent)
@ -38,17 +39,16 @@ void FileDownload::configure(QNetworkAccessManager *network_manager, const QStri
void FileDownload::store()
{
if (tmpfile_->isOpen())
tmpfile_->write (reply_->read (reply_->bytesAvailable ()));
if (destfile_.isOpen())
destfile_.write (reply_->read (reply_->bytesAvailable ()));
else
LOG_INFO(QString{ "FileDownload [%1]: tmpfile is not open"}.arg(user_agent_));
LOG_INFO(QString{ "FileDownload [%1]: file is not open."}.arg(user_agent_));
}
void FileDownload::replyComplete()
{
QFileInfo destination_file(destination_filename_);
QString const tmpfile_path = destination_file.absolutePath();
QDir tmpdir_(tmpfile_path);
QDir tmpdir_(destination_file.absoluteFilePath());
LOG_DEBUG(QString{ "FileDownload [%1]: replyComplete"}.arg(user_agent_));
if (!reply_)
@ -83,8 +83,8 @@ void FileDownload::replyComplete()
}
else if (reply_->error () != QNetworkReply::NoError)
{
tmpfile_->close();
delete tmpfile_;
destfile_.cancelWriting();
destfile_.commit();
url_valid_ = false; // reset
// report errors that are not due to abort
if (QNetworkReply::OperationCanceledError != reply_->error ())
@ -107,14 +107,8 @@ void FileDownload::replyComplete()
url_valid_ = false; // reset
// load the database asynchronously
// future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_.fileName ());
LOG_INFO(QString{ "FileDownload [%1]: complete. tempfile path is %2"}.arg(user_agent_).arg(tmpfile_->fileName()));
// move the file to the destination
tmpdir_.remove(destination_filename_+".old"); // get rid of previous version
tmpdir_.rename(destination_filename_, destination_filename_+".old");
tmpdir_.rename(tmpfile_->fileName(), destination_filename_);
LOG_INFO(QString{ "FileDownload [%1]: moved tempfile %2 to %3"}.arg(user_agent_).arg(tmpfile_->fileName()).arg(destination_filename_));
tmpfile_->close();
delete tmpfile_;
LOG_INFO(QString{ "FileDownload [%1]: complete. File path is %2"}.arg(user_agent_).arg(destfile_.fileName()));
destfile_.commit();
emit complete(destination_filename_);
}
}
@ -184,15 +178,18 @@ void FileDownload::download(QUrl qurl)
QObject::connect(manager_, &QNetworkAccessManager::finished, this, &FileDownload::downloadComplete, Qt::UniqueConnection);
QObject::connect(reply_, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress, Qt::UniqueConnection);
QObject::connect(reply_, &QNetworkReply::finished, this,&FileDownload::replyComplete, Qt::UniqueConnection);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QObject::connect(reply_, &QNetworkReply::errorOccurred,this,&FileDownload::errorOccurred, Qt::UniqueConnection);
#else
QObject::connect(reply_, &QNetworkReply::error, this, &FileDownload::errorOccurred, Qt::UniqueConnection);
#endif
QObject::connect (reply_, &QNetworkReply::readyRead, this, &FileDownload::store, Qt::UniqueConnection);
QFileInfo destination_file(destination_filename_);
QString const tmpfile_base = destination_file.fileName();
QString const tmpfile_path = destination_file.absolutePath();
tmpfile_ = new QTemporaryFile(tmpfile_path+QDir::separator()+tmpfile_base+".XXXXXX");
if (!tmpfile_->open())
destfile_.setFileName(destination_file.absoluteFilePath());
if (!destfile_.open(QSaveFile::WriteOnly))
{
LOG_INFO(QString{"FileDownload [%1]: Unable to open the temporary file based on %2"}.arg(user_agent_).arg(tmpfile_path));
return;

View File

@ -7,6 +7,7 @@
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QTemporaryFile>
#include <QSaveFile>
class FileDownload : public QObject {
Q_OBJECT
@ -24,7 +25,7 @@ private:
QString user_agent_;
QPointer<QNetworkReply> reply_;
QNetworkRequest request_;
QPointer<QTemporaryFile> tmpfile_;
QSaveFile destfile_;
bool url_valid_;
int redirect_count_;
signals:

View File

@ -16,6 +16,7 @@
#include <QTextStream>
#include <QDebug>
#include <QDebugStateSaver>
#include <QRegularExpression>
#include "Configuration.hpp"
#include "Radio.hpp"
#include "pimpl_impl.hpp"
@ -233,6 +234,8 @@ public:
Configuration const * configuration_;
QString path_;
QString cty_version_;
QString cty_version_date_;
entities_type entities_;
prefixes_type prefixes_;
};
@ -330,11 +333,14 @@ QString AD1CCty::impl::get_cty_path(Configuration const * configuration)
void AD1CCty::impl::load_cty(QFile &file)
{
QRegularExpression version_pattern{R"(VER\d{8})"};
int entity_id = 0;
int line_number{0};
entities_.clear();
prefixes_.clear();
cty_version_ = QString{};
cty_version_date_ = QString{};
QTextStream in{&file};
while (!in.atEnd())
@ -377,6 +383,11 @@ void AD1CCty::impl::load_cty(QFile &file)
{
prefix = prefix.mid(1);
exact = true;
// match version pattern to prefix
if (version_pattern.match(prefix).hasMatch())
{
cty_version_date_ = prefix;
}
}
prefixes_.emplace(prefix, exact, entity_id);
}
@ -407,7 +418,7 @@ void AD1CCty::reload(Configuration const * configuration)
m_->impl::load_cty(file);
m_->cty_version_ = AD1CCty::lookup("VERSION").entity_name;
Q_EMIT cty_loaded(m_->cty_version_);
LOG_INFO(QString{"Loaded CTY.DAT version %1"}.arg (m_->cty_version_));
LOG_INFO(QString{"Loaded CTY.DAT version %1, %2"}.arg (m_->cty_version_date_).arg (m_->cty_version_));
}
}
@ -448,5 +459,5 @@ auto AD1CCty::lookup (QString const& call) const -> Record
}
auto AD1CCty::version () const -> QString
{
return m_->cty_version_;
return m_->cty_version_date_;
}

View File

@ -530,7 +530,7 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple,
else
{
m_config.set_CTY_DAT_version(cty_version);
showStatusMessage (tr ("Scanned ADIF log, %1 worked-before records created. CTY: %2. CTY: %2").arg (record_count).arg (cty_version));
showStatusMessage (tr ("Scanned ADIF log, %1 worked-before records created. CTY: %2").arg (record_count).arg (cty_version));
}
});