summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/ftb/FtbPackDownloader.cpp
blob: 3e274c9e2d71d6cd6a89088fa53209efaf6cc9e3 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "FtbPackDownloader.h"
#include "PackHelpers.h"
#include "FtbPackFetchTask.h"
#include "Env.h"

FtbPackDownloader::FtbPackDownloader()
{
	done = false;
	fetching = false;
}

FtbPackDownloader::~FtbPackDownloader()
{
}

FtbModpackList FtbPackDownloader::getModpacks()
{
	return static_cast<FtbModpackList>(fetchedPacks.values());
}

void FtbPackDownloader::fetchModpacks(bool force = false)
{
	if(fetching || (!force && done))
	{
		qDebug() << "Skipping modpack refetch because done or already fetching [done =>" << done << "| fetching =>" << fetching << "]";
		return;
	}

	fetching = true;

	fetchTask = new FtbPackFetchTask();
	connect(fetchTask, &FtbPackFetchTask::finished, this, &FtbPackDownloader::fetchSuccess);
	connect(fetchTask, &FtbPackFetchTask::failed, this, &FtbPackDownloader::fetchFailed);
	fetchTask->fetch();
}


void FtbPackDownloader::fetchSuccess(FtbModpackList modpacks)
{
	for(int i = 0; i < modpacks.size(); i++)
	{
		fetchedPacks.insert(modpacks.at(i).name, modpacks.at(i));
	}

	fetching = false;
	done = true;
	emit ready();
	fetchTask->deleteLater();
}

void FtbPackDownloader::fetchFailed(QString reason)
{
	qWarning() << "Failed to fetch FtbData" << reason;
	fetching = false;
	emit packFetchFailed();
	fetchTask->deleteLater();
}