summaryrefslogtreecommitdiffstats
path: root/logic/OneSixRule.cpp
blob: 545cd641564a7c0eed7d3784b81bfa52bc65bd1d (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
#include "OneSixRule.h"
#include <QJsonObject>
#include <QJsonArray>

QList<QSharedPointer<Rule>> rulesFromJsonV4(QJsonObject &objectWithRules)
{
	QList<QSharedPointer<Rule>> rules;
	auto rulesVal = objectWithRules.value("rules");
	if (!rulesVal.isArray())
		return rules;

	QJsonArray ruleList = rulesVal.toArray();
	for (auto ruleVal : ruleList)
	{
		QSharedPointer<Rule> rule;
		if (!ruleVal.isObject())
			continue;
		auto ruleObj = ruleVal.toObject();
		auto actionVal = ruleObj.value("action");
		if (!actionVal.isString())
			continue;
		auto action = RuleAction_fromString(actionVal.toString());
		if (action == Defer)
			continue;

		auto osVal = ruleObj.value("os");
		if (!osVal.isObject())
		{
			// add a new implicit action rule
			rules.append(ImplicitRule::create(action));
			continue;
		}

		auto osObj = osVal.toObject();
		auto osNameVal = osObj.value("name");
		if (!osNameVal.isString())
			continue;
		OpSys requiredOs = OpSys_fromString(osNameVal.toString());
		QString versionRegex = osObj.value("version").toString();
		// add a new OS rule
		rules.append(OsRule::create(action, requiredOs, versionRegex));
	}
}

QJsonObject ImplicitRule::toJson()
{
	QJsonObject ruleObj;
	ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow"));
	return ruleObj;
}

QJsonObject OsRule::toJson()
{
	QJsonObject ruleObj;
	ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow"));
	QJsonObject osObj;
	{
		osObj.insert("name", OpSys_toString(m_system));
		osObj.insert("version", m_version_regexp);
	}
	ruleObj.insert("os", osObj);
	return ruleObj;
}

RuleAction RuleAction_fromString(QString name)
{
	if (name == "allow")
		return Allow;
	if (name == "disallow")
		return Disallow;
	return Defer;
}