mirror of
https://github.com/saitohirga/WSJT-X.git
synced 2025-03-21 19:48:54 -04:00
initial commit
This commit is contained in:
parent
663ed60013
commit
eebababece
127
Network/FileDownload.cpp
Normal file
127
Network/FileDownload.cpp
Normal file
@ -0,0 +1,127 @@
|
||||
|
||||
#include "FileDownload.hpp"
|
||||
#include <QCoreApplication>
|
||||
#include <QUrl>
|
||||
#include <QNetworkRequest>
|
||||
#include <QFileInfo>
|
||||
#include <QDir>
|
||||
#include <QTemporaryFile>
|
||||
#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;
|
||||
}
|
39
Network/FileDownload.hpp
Normal file
39
Network/FileDownload.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef WSJTX_FILEDOWNLOAD_H
|
||||
#define WSJTX_FILEDOWNLOAD_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
#include <QTemporaryFile>
|
||||
|
||||
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
|
@ -16,6 +16,8 @@
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QDebug>
|
||||
#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 ();
|
||||
|
Loading…
Reference in New Issue
Block a user