summaryrefslogtreecommitdiffstats
path: root/logic/GoUpdate.cpp
blob: 6ac53d19e0950f9ae459e676b2b91696f9a50c38 (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

#include "GoUpdate.h"

#include "config.h"
#include "logger/QsLog.h"

GoUpdate::GoUpdate()
{
	currentBuildIndex = VERSION_BUILD;
	builderName = VERSION_BUILD_TYPE;
	repoUrlBase = VERSION_REPO;
}

void GoUpdate::updateCheckFailed()
{
	// TODO: log errors better
	QLOG_ERROR() << "Update check failed for reasons unknown.";
}

void GoUpdate::updateCheckFinished()
{
	QJsonParseError jsonError;
	QByteArray data;
	{
		ByteArrayDownloadPtr dl =
			std::dynamic_pointer_cast<ByteArrayDownload>(index_job->first());
		data = dl->m_data;
		index_job.reset();
	}

	QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
	if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject())
	{
		return;
	}

	QVariant doc = jsonDoc.toVariant();
	auto stuff = doc.toMap();

	// check api version (or later, branch?)
	int ApiVersion = stuff["ApiVersion"].toInt();
	if (ApiVersion != 0)
		return;

	// parse and store the channel list
	auto parsedChannels = stuff["Channels"].toList();
	for (auto channel : parsedChannels)
	{
		auto chanMap = channel.toMap();
		channels.append({chanMap["Id"].toString(), chanMap["Name"].toString(),
						 chanMap["CurrentVersion"].toInt()});
	}

	// parse and store the version list
	auto parsedVersions = stuff["Versions"].toList();
	for (auto version : parsedVersions)
	{
		auto verMap = version.toMap();
		int versionId = verMap["Id"].toInt();
		versions.append({versionId, verMap["Name"].toString()});
		if (currentBuildIndex < versionId)
		{
			newBuildIndex = versionId;
		}
	}

	if (newBuildIndex != -1)
	{
		QLOG_INFO() << "Update is available.";
		emit updateAvailable();
	}
	else
	{
		QLOG_INFO() << "Update check finished.";
	}
}

void GoUpdate::checkForUpdate()
{
	if (repoUrlBase == "invalid")
	{
		return;
	}

	auto job = new NetJob("Assets index");
	job->addNetAction(
		ByteArrayDownload::make(QUrl(repoUrlBase + "/" + VERSION_BRANCH + "/index.json")));
	connect(job, SIGNAL(succeeded()), SLOT(updateCheckFinished()));
	connect(job, SIGNAL(failed()), SLOT(updateCheckFailed()));
	index_job.reset(job);
	job->start();
}

/*
<Forkk> files.multimc.org/lin64/
<manmaed> Hi Forkkie
<Forkk> files.multimc.org/win32/
<Forkk> files.multimc.org/lin32/
*/