summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/FtbPackFetchTask.cpp
blob: 6f578e048e8a09469b37789af29fdfe72daf3db2 (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
#include "FtbPackFetchTask.h"
#include <QDomDocument>

FtbPackFetchTask::FtbPackFetchTask() {

}

FtbPackFetchTask::~FtbPackFetchTask() {

}

void FtbPackFetchTask::fetch() {
	NetJob *netJob = new NetJob("FtbModpackFetch");

	QUrl url = QUrl("https://ftb.cursecdn.com/FTB2/static/modpacks.xml");
	qDebug() << "Downloading version info from " << url.toString();

	netJob->addNetAction(downloadPtr = Net::Download::makeByteArray(url, &modpacksXmlFileData));

	QObject::connect(netJob, &NetJob::succeeded, this, &FtbPackFetchTask::fileDownloadFinished);
	QObject::connect(netJob, &NetJob::failed, this, &FtbPackFetchTask::fileDownloadFailed);

	jobPtr.reset(netJob);
	netJob->start();
}

void FtbPackFetchTask::fileDownloadFinished(){

	jobPtr.reset();

	QDomDocument doc;

	QString errorMsg = "Unknown error.";
	int errorLine = -1;
	int errorCol = -1;

	if(!doc.setContent(modpacksXmlFileData, false, &errorMsg, &errorLine, &errorCol)){
		auto fullErrMsg = QString("Failed to fetch modpack data: %s %d:%d!").arg(errorMsg, errorLine, errorCol);
		qWarning() << fullErrMsg;
		emit failed(fullErrMsg);
		modpacksXmlFileData.clear();
		return;
	}

	modpacksXmlFileData.clear();

	FtbModpackList modpackList;

	QDomNodeList nodes = doc.elementsByTagName("modpack");
	for(int i = 0; i < nodes.length(); i++) {
		QDomElement element = nodes.at(i).toElement();

		FtbModpack modpack;
		modpack.name = element.attribute("name");
		modpack.currentVersion = element.attribute("version");
		modpack.mcVersion = element.attribute("mcVersion");
		modpack.description = element.attribute("description");
		modpack.mods = element.attribute("mods");
		modpack.image = element.attribute("image");
		modpack.oldVersions = element.attribute("oldVersions").split(";");
		modpack.author = element.attribute("author");

		modpack.dir = element.attribute("dir");
		modpack.file = element.attribute("url");

		modpackList.append(modpack);
	}

	emit finished(modpackList);
}

void FtbPackFetchTask::fileDownloadFailed(QString reason){
	qWarning() << "Fetching FtbPacks failed: " << reason;
	emit failed(reason);
}