summaryrefslogtreecommitdiffstats
path: root/logic/resources/ResourceHandler.h
blob: f09d890488db962b3e99f4ef36618cd571d69946 (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
#pragma once

#include <QVariant>
#include <memory>

#include "multimc_logic_export.h"

class Resource;

/** Base class for things that can retrieve a resource.
 *
 * Subclass, provide a constructor that takes a single QString as argument, and
 * call Resource::registerHandler<MyResourceHandler>("<id>"), where <id> is the
 * prefix of the resource ("web", "icon", etc.)
 */
class MULTIMC_LOGIC_EXPORT ResourceHandler
{
public:
	virtual ~ResourceHandler() {}

	void setResource(Resource *resource) { m_resource = resource; }
	/// reimplement this if you need to do something after you have been put in a shared pointer
	// we do this instead of inheriting from std::enable_shared_from_this
	virtual void init(std::shared_ptr<ResourceHandler>&) {}

	QVariant result() const { return m_result; }

protected: // use these methods to notify the resource of changes
	void setResult(const QVariant &result);
	void setFailure(const QString &reason);
	void setProgress(const int progress);

private:
	QVariant m_result;
	Resource *m_resource = nullptr;
};