summaryrefslogtreecommitdiffstats
path: root/logic/tasks/StandardTask.h
blob: 6f283dcd0bf5a3995bc952651bb1e5934fc8dca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Licensed under the Apache-2.0 license. See README.md for details.

#pragma once

#include "Task.h"

#include <QPointer>
#include <memory>

#include "QObjectPtr.h"

class QEventLoop;
class QDir;
class NetAction;
class NetJob;
class INetworkValidator;

class StandardTask : public Task
{
	Q_OBJECT
public:
	explicit StandardTask(QObject *parent = nullptr);

protected:
	// TODO: switch to a future-based system
	void runTask(QObjectPtr<Task> other);
	void runTaskNonBlocking(QObjectPtr<Task> other);
	QByteArray networkGet(const QUrl &url);
	QByteArray networkGetCached(const QString &name, const QString &base, const QString &path, const QUrl &url, const bool alwaysRefetch = false,
								INetworkValidator *validator = nullptr);
	QByteArray networkGetCached(const QString &name, const QString &base, const QString &path, const QUrl &url, const QMap<QString, QString> &headers,
								INetworkValidator *validator = nullptr);
	void networkGetCachedNonBlocking(const QString &name, const QString &base, const QString &path, const QUrl &url, const bool alwaysRefetch = false,
									 INetworkValidator *validator = nullptr);
	void waitOnPending();

private:
	QEventLoop *m_loop;
	QList<QPointer<Task>> m_pendingTasks; // only used to check if the object was deleted
	QList<QObjectPtr<Task>> m_pendingTaskPtrs;

	QObjectPtr<NetJob> wrapDownload(const QString &name, std::shared_ptr<NetAction> action);
};