summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/FtbPackDownloader.cpp
blob: a3951bfdb2eae1b87e45289bb437befb76b29fef (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "FtbPackDownloader.h"
#include "PackHelpers.h"
#include "FtbPackFetchTask.h"
#include "Env.h"

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

FtbPackDownloader::~FtbPackDownloader(){
	delete netJobContainer.get();
	netJobContainer.reset(nullptr);
}

bool FtbPackDownloader::isValidPackSelected(){
	FtbModpack dummy;
	dummy.name = "__INVALID__";

	FtbModpack other = fetchedPacks.value(selected.name, dummy);
	if(other.name == "__INVALID__") {
		return false;
	}

	return other.oldVersions.contains(selectedVersion);
}

QString FtbPackDownloader::getSuggestedInstanceName() {
	return selected.name;
}

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();
}

void FtbPackDownloader::selectPack(FtbModpack modpack, QString version) {
	selected = modpack;
	selectedVersion = version;
}

FtbModpack FtbPackDownloader::getSelectedPack() {
	return selected;
}

void FtbPackDownloader::downloadSelected(MetaEntryPtr cache) {
	NetJob *job = new NetJob("Downlad FTB Pack");

	cache->setStale(true);
	QString url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1/%2/%3").arg(selected.dir, selectedVersion.replace(".", "_"), selected.file);
	job->addNetAction(Net::Download::makeCached(url, cache));
	downloadPath = cache->getFullPath();

	netJobContainer.reset(job);

	connect(job, &NetJob::succeeded, this, &FtbPackDownloader::_downloadSucceeded);
	connect(job, &NetJob::failed, this, &FtbPackDownloader::_downloadFailed);
	connect(job, &NetJob::progress, this, &FtbPackDownloader::_downloadProgress);
	job->start();
}

void FtbPackDownloader::_downloadSucceeded() {
	netJobContainer.reset();
	emit downloadSucceded(downloadPath);
}

void FtbPackDownloader::_downloadProgress(qint64 current, qint64 total) {
	emit downloadProgress(current, total);
}

void FtbPackDownloader::_downloadFailed(QString reason) {
	netJobContainer.reset();
	emit downloadFailed(reason);
}