NOISSUE continue refactoring things to make tests pass

This commit is contained in:
Petr Mrázek
2021-11-21 23:21:12 +01:00
parent c2c56a2f6c
commit 69213b1206
103 changed files with 634 additions and 773 deletions

View File

@ -15,16 +15,17 @@
#include "Download.h"
#include "BuildConfig.h"
#include <QFileInfo>
#include <QDateTime>
#include <QDebug>
#include "Env.h"
#include <FileSystem.h>
#include "FileSystem.h"
#include "ChecksumValidator.h"
#include "MetaCacheSink.h"
#include "ByteArraySink.h"
#include "BuildConfig.h"
namespace Net {
Download::Download():NetAction()
@ -41,7 +42,7 @@ Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry, Options options
auto cachedNode = new MetaCacheSink(entry, md5Node);
dl->m_sink.reset(cachedNode);
dl->m_target_path = entry->getFullPath();
return std::shared_ptr<Download>(dl);
return dl;
}
Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options options)
@ -50,7 +51,7 @@ Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options opti
dl->m_url = url;
dl->m_options = options;
dl->m_sink.reset(new ByteArraySink(output));
return std::shared_ptr<Download>(dl);
return dl;
}
Download::Ptr Download::makeFile(QUrl url, QString path, Options options)
@ -59,7 +60,7 @@ Download::Ptr Download::makeFile(QUrl url, QString path, Options options)
dl->m_url = url;
dl->m_options = options;
dl->m_sink.reset(new FileSink(path));
return std::shared_ptr<Download>(dl);
return dl;
}
void Download::addValidator(Validator * v)
@ -67,7 +68,7 @@ void Download::addValidator(Validator * v)
m_sink->addValidator(v);
}
void Download::start()
void Download::startImpl()
{
if(m_status == Job_Aborted)
{
@ -97,7 +98,7 @@ void Download::start()
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT);
QNetworkReply *rep = ENV->network().get(request);
QNetworkReply *rep = m_network->get(request);
m_reply.reset(rep);
connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));
@ -207,7 +208,7 @@ bool Download::handleRedirect()
m_url = QUrl(redirect.toString());
qDebug() << "Following redirect to " << m_url.toString();
start();
start(m_network);
return true;
}

View File

@ -20,13 +20,15 @@
#include "Validator.h"
#include "Sink.h"
#include "QObjectPtr.h"
namespace Net {
class Download : public NetAction
{
Q_OBJECT
public: /* types */
typedef std::shared_ptr<class Download> Ptr;
typedef shared_qobject_ptr<class Download> Ptr;
enum class Option
{
NoOptions = 0,
@ -62,7 +64,7 @@ protected slots:
void downloadReadyRead() override;
public slots:
void start() override;
void startImpl() override;
private: /* data */
// FIXME: remove this, it has no business being here.

View File

@ -1,7 +1,6 @@
#include "FileSink.h"
#include <QFile>
#include <QFileInfo>
#include "Env.h"
#include "FileSystem.h"
namespace Net {

View File

@ -13,7 +13,6 @@
* limitations under the License.
*/
#include "Env.h"
#include "HttpMetaCache.h"
#include "FileSystem.h"

View File

@ -1,8 +1,8 @@
#include "MetaCacheSink.h"
#include <QFile>
#include <QFileInfo>
#include "Env.h"
#include "FileSystem.h"
#include "Application.h"
namespace Net {
@ -53,7 +53,7 @@ JobStatus MetaCacheSink::finalizeCache(QNetworkReply & reply)
}
m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch());
m_entry->setStale(false);
ENV->metacache()->updateEntry(m_entry);
APPLICATION->metacache()->updateEntry(m_entry);
return Job_Finished;
}

View File

@ -35,14 +35,15 @@ enum JobStatus
Job_Failed_Proceed
};
typedef std::shared_ptr<class NetAction> NetActionPtr;
class NetAction : public QObject
{
Q_OBJECT
protected:
explicit NetAction() : QObject(0) {};
explicit NetAction() : QObject(nullptr) {};
public:
using Ptr = shared_qobject_ptr<NetAction>;
virtual ~NetAction() {};
bool isRunning() const
@ -93,9 +94,17 @@ protected slots:
virtual void downloadReadyRead() = 0;
public slots:
virtual void start() = 0;
void start(shared_qobject_ptr<QNetworkAccessManager> network) {
m_network = network;
startImpl();
}
protected:
virtual void startImpl() = 0;
public:
shared_qobject_ptr<QNetworkAccessManager> m_network;
/// index within the parent job, FIXME: nuke
int m_index_within_job = 0;

View File

@ -144,7 +144,7 @@ void NetJob::startMoreParts()
connect(part.get(), SIGNAL(aborted(int)), SLOT(partAborted(int)));
connect(part.get(), SIGNAL(netActionProgress(int, qint64, qint64)),
SLOT(partProgress(int, qint64, qint64)));
part->start();
part->start(m_network);
}
}
@ -194,7 +194,7 @@ bool NetJob::abort()
return fullyAborted;
}
bool NetJob::addNetAction(NetActionPtr action)
bool NetJob::addNetAction(NetAction::Ptr action)
{
action->m_index_within_job = downloads.size();
downloads.append(action);

View File

@ -22,33 +22,34 @@
#include "QObjectPtr.h"
class NetJob;
typedef shared_qobject_ptr<NetJob> NetJobPtr;
class NetJob : public Task
{
Q_OBJECT
public:
using Ptr = shared_qobject_ptr<NetJob>;
explicit NetJob(QString job_name) : Task()
{
setObjectName(job_name);
}
virtual ~NetJob();
bool addNetAction(NetActionPtr action);
bool addNetAction(NetAction::Ptr action);
NetActionPtr operator[](int index)
NetAction::Ptr operator[](int index)
{
return downloads[index];
}
const NetActionPtr at(const int index)
const NetAction::Ptr at(const int index)
{
return downloads.at(index);
}
NetActionPtr first()
NetAction::Ptr first()
{
if (downloads.size())
return downloads[0];
return NetActionPtr();
return NetAction::Ptr();
}
int size() const
{
@ -64,6 +65,19 @@ private slots:
public slots:
virtual void executeTask() override;
virtual bool abort() override;
virtual void start(shared_qobject_ptr<QNetworkAccessManager> network) {
m_network = network;
start();
}
protected slots:
void start() override {
if(!m_network) {
throw "Missing network while trying to start " + objectName();
return;
}
Task::start();
}
private slots:
void partProgress(int index, qint64 bytesReceived, qint64 bytesTotal);
@ -72,13 +86,15 @@ private slots:
void partAborted(int index);
private:
shared_qobject_ptr<QNetworkAccessManager> m_network;
struct part_info
{
qint64 current_progress = 0;
qint64 total_progress = 1;
int failures = 0;
};
QList<NetActionPtr> downloads;
QList<NetAction::Ptr> downloads;
QList<part_info> parts_progress;
QQueue<int> m_todo;
QSet<int> m_doing;

View File

@ -1,11 +1,12 @@
#include "PasteUpload.h"
#include "Env.h"
#include "BuildConfig.h"
#include "Application.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <BuildConfig.h>
PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window)
{
@ -41,7 +42,7 @@ void PasteUpload::executeTask()
request.setRawHeader("Content-Length", QByteArray::number(m_jsonContent.size()));
request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str());
QNetworkReply *rep = ENV->network().post(request, m_jsonContent);
QNetworkReply *rep = APPLICATION->network()->post(request, m_jsonContent);
m_reply = std::shared_ptr<QNetworkReply>(rep);
setStatus(tr("Uploading to paste.ee"));