From eebababece37c94a90932d7e65f945b24d123af7 Mon Sep 17 00:00:00 2001 From: Brian Moran Date: Thu, 9 Mar 2023 12:10:20 -0800 Subject: [PATCH] initial commit --- Network/FileDownload.cpp | 127 +++++++++++++++++++++++++++++++++++++++ Network/FileDownload.hpp | 39 ++++++++++++ Network/LotWUsers.cpp | 5 +- 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 Network/FileDownload.cpp create mode 100644 Network/FileDownload.hpp diff --git a/Network/FileDownload.cpp b/Network/FileDownload.cpp new file mode 100644 index 000000000..63f51d98b --- /dev/null +++ b/Network/FileDownload.cpp @@ -0,0 +1,127 @@ + +#include "FileDownload.hpp" +#include +#include +#include +#include +#include +#include +#include "qt_helpers.hpp" +#include "Logger.hpp" + +FileDownload::FileDownload() : QObject(nullptr) +{ + +} + +FileDownload::~FileDownload() +{ +} + +void FileDownload::errorOccurred(QNetworkReply::NetworkError code) +{ + LOG_INFO(QString{"DOWNLOAD: errorOccurred %1 -> %2"}.arg(code).arg(reply_->errorString())); + //LOG_INFO(QString{ "DOWNLOAD: server returned %1"}.arg(reply_->)) +} + +void FileDownload::configure(const QString &source_url, const QString &destination_path) +{ + source_url_ = source_url; + destination_filename_ = destination_path; +} + +void FileDownload::store() +{ + if (tmpfile_->isOpen()) + tmpfile_->write (reply_->read (reply_->bytesAvailable ())); + else + LOG_INFO(QString{ "DOWNLOAD: tmpfile is not open"}); +} + +void FileDownload::replyComplete() +{ + auto is_error = reply_->error (); + LOG_INFO(QString{"DOWNLOAD: reply complete %1"}.arg(is_error)); + if (reply_ && reply_->isFinished ()) + { + reply_->deleteLater (); + } +} + +void FileDownload::downloadComplete(QNetworkReply *data) +{ + // make a temp file in the same place as the file we're downloading. Needs to be on the same + // filesystem as where we eventually want to 'mv' it. + + QUrl r = request_->url(); + LOG_INFO(QString{"DOWNLOAD: finished download %1 -> %2 (%3)"}.arg(source_url_).arg(destination_filename_).arg(r.url())); + + LOG_INFO(QString{ "DOWNLOAD: tempfile path is %1"}.arg(tmpfile_->fileName())); + + tmpfile_->close(); + + LOG_INFO(QString{"DOWNLOAD: moving file to %2"}.arg(destination_filename_)); + + LOG_INFO("Request Headers:"); + Q_FOREACH (const QByteArray& hdr, request_->rawHeaderList()) { + LOG_INFO(QString{ "%1 -> %2"}.arg(QString(hdr)).arg(QString(request_->rawHeader(hdr)))); + } + + LOG_INFO("Response Headers:"); + Q_FOREACH (const QByteArray& hdr, reply_->rawHeaderList()) { + LOG_INFO(QString{ "%1 -> %2"}.arg(QString(hdr)).arg(QString(reply_->rawHeader(hdr)))); + } + // 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_); + emit complete(destination_filename_); + data->deleteLater(); +} + +void FileDownload::download() +{ + //QUrl url = QUrl(source_url_); + + manager_ = new QNetworkAccessManager(this); + + // request_ = new QNetworkRequest("https://www.country-files.com/bigcty/cty.dat"); + request_ = new QNetworkRequest(QUrl(source_url_)); + + LOG_INFO(QString{"DOWNLOAD: starting download %1 -> %2"}.arg(source_url_).arg(destination_filename_)); + + request_->setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); + //request_->setHeader( QNetworkRequest::ContentTypeHeader, "some/type" ); + request_->setRawHeader("Accept", "*/*"); + request_->setRawHeader ("User-Agent", "WSJT-X CTY Downloader"); + + reply_ = manager_->get(*request_); + + reply_->setReadBufferSize(0); + + int http_code = reply_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + QObject::connect(manager_, &QNetworkAccessManager::finished, this, &FileDownload::downloadComplete); + QObject::connect(reply_, &QNetworkReply::downloadProgress, this, &FileDownload::downloadProgress); + QObject::connect(reply_, &QNetworkReply::finished, this,&FileDownload::replyComplete); + QObject::connect(reply_, &QNetworkReply::errorOccurred,this,&FileDownload::errorOccurred); + QObject::connect (reply_, &QNetworkReply::finished, this, &FileDownload::replyComplete); + QObject::connect (reply_, &QNetworkReply::readyRead, this, &FileDownload::store); + + QFileInfo tmpfi(destination_filename_); + QString const tmpfile_path = tmpfi.absolutePath(); + tmpdir_ = new QDir(tmpfile_path); + tmpfile_ = new QTemporaryFile(tmpfile_path+"/big.cty.XXXXXX"); + if (!tmpfile_->open()) + { + LOG_INFO(QString{"DOWNLOAD: Unable to open the temporary file based on %1"}.arg(tmpfile_path)); + return; + } + LOG_INFO(QString{"DOWNLOAD: let's go %1"}.arg(http_code)); +} + +void FileDownload::downloadProgress(qint64 received, qint64 total) +{ + LOG_INFO(QString{"DOWNLOAD: Progress %1 from %2, total %3, so far %4"}.arg(destination_filename_).arg(source_url_).arg(total).arg(received)); + //qDebug() << received << total; +} diff --git a/Network/FileDownload.hpp b/Network/FileDownload.hpp new file mode 100644 index 000000000..91409e8fc --- /dev/null +++ b/Network/FileDownload.hpp @@ -0,0 +1,39 @@ +#ifndef WSJTX_FILEDOWNLOAD_H +#define WSJTX_FILEDOWNLOAD_H + +#include +#include +#include +#include +#include + +class FileDownload : public QObject { + Q_OBJECT + +public: + explicit FileDownload(); + ~FileDownload(); + + void configure(const QString& source_url, const QString& destination_filename); + +private: + QNetworkAccessManager *manager_; + QString source_url_; + QString destination_filename_; + QNetworkReply *reply_; + QNetworkRequest *request_; + QTemporaryFile *tmpfile_; + QDir *tmpdir_; +signals: + void complete(QString filename); + +public slots: + void download(); + void store(); + void downloadComplete(QNetworkReply* data); + void downloadProgress(qint64 recieved, qint64 total); + void errorOccurred(QNetworkReply::NetworkError code); + void replyComplete(); +}; + +#endif //WSJTX_FILEDOWNLOAD_H diff --git a/Network/LotWUsers.cpp b/Network/LotWUsers.cpp index 4e8024010..189b0ca9d 100644 --- a/Network/LotWUsers.cpp +++ b/Network/LotWUsers.cpp @@ -16,6 +16,8 @@ #include #include #include +#include "qt_helpers.hpp" +#include "Logger.hpp" #include "pimpl_impl.hpp" @@ -76,7 +78,7 @@ public: network_manager_->setNetworkAccessible (QNetworkAccessManager::Accessible); } #endif - + LOG_INFO(QString("Download...")); QNetworkRequest request {url}; request.setRawHeader ("User-Agent", "WSJT LotW User Downloader"); request.setOriginatingObject (this); @@ -98,6 +100,7 @@ public: void reply_finished () { + LOG_INFO(QString("Finished...")); if (!reply_) { Q_EMIT self_->load_finished ();