File: modstatemodel.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (172 lines) | stat: -rw-r--r-- 4,397 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * modstatemodel.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "modstatemodel.h"

#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/modding/ModManager.h"

ModStateModel::ModStateModel()
	: repositoryData(std::make_unique<JsonNode>())
	, modManager(std::make_unique<ModManager>())
{
}

ModStateModel::~ModStateModel() = default;

void ModStateModel::setRepositoryData(const JsonNode & repositoriesList)
{
	*repositoryData = repositoriesList;
	modManager = std::make_unique<ModManager>(*repositoryData);
}

void ModStateModel::reloadLocalState()
{
	CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &){ return true; });
	modManager = std::make_unique<ModManager>(*repositoryData);
}

const JsonNode & ModStateModel::getRepositoryData() const
{
	return *repositoryData;
}

ModState ModStateModel::getMod(QString modName) const
{
	assert(modName.toLower() == modName);
	return ModState(modManager->getModDescription(modName.toStdString()));
}

template<typename Container>
QStringList stringListStdToQt(const Container & container)
{
	QStringList result;
	for (const auto & str : container)
		result.push_back(QString::fromStdString(str));
	return result;
}

QStringList ModStateModel::getAllMods() const
{
	return stringListStdToQt(modManager->getAllMods());
}

bool ModStateModel::isModExists(QString modName) const
{
	return vstd::contains(modManager->getAllMods(), modName.toStdString());
}

bool ModStateModel::isModInstalled(QString modName) const
{
	return isModExists(modName) && getMod(modName).isInstalled();
}

bool ModStateModel::isModSettingEnabled(QString rootModName, QString modSettingName) const
{
	return modManager->isModSettingActive(rootModName.toStdString(), modSettingName.toStdString());
}

bool ModStateModel::isModEnabled(QString modName) const
{
	return modManager->isModActive(modName.toStdString());
}

bool ModStateModel::isModUpdateAvailable(QString modName) const
{
	return getMod(modName).isUpdateAvailable();
}

bool ModStateModel::isModVisible(QString modName) const
{
	return getMod(modName).isVisible();
}

QString ModStateModel::getInstalledModSizeFormatted(QString modName) const
{
	return QCoreApplication::translate("File size", "%1 MiB").arg(QString::number(getInstalledModSizeMegabytes(modName), 'f', 1));
}

double ModStateModel::getInstalledModSizeMegabytes(QString modName) const
{
	return modManager->getInstalledModSizeMegabytes(modName.toStdString());
}

void ModStateModel::doEnableMods(QStringList modList)
{
	std::vector<std::string> stdList;

	for (const auto & entry : modList)
		stdList.push_back(entry.toStdString());

	modManager->tryEnableMods(stdList);
}

void ModStateModel::doDisableMod(QString modname)
{
	modManager->tryDisableMod(modname.toStdString());
}

bool ModStateModel::isSubmod(QString modname)
{
	return modname.contains('.');
}

QString ModStateModel::getTopParent(QString modname) const
{
	QStringList components = modname.split('.');
	if (components.size() > 1)
		return components.front();
	else
		return "";
}

void ModStateModel::createNewPreset(const QString & presetName)
{
	modManager->createNewPreset(presetName.toStdString());
}

void ModStateModel::deletePreset(const QString & presetName)
{
	modManager->deletePreset(presetName.toStdString());
}

void ModStateModel::activatePreset(const QString & presetName)
{
	modManager->activatePreset(presetName.toStdString());
}

void ModStateModel::renamePreset(const QString & oldPresetName, const QString & newPresetName)
{
	modManager->renamePreset(oldPresetName.toStdString(), newPresetName.toStdString());
}

QStringList ModStateModel::getAllPresets() const
{
	auto result = modManager->getAllPresets();
	return stringListStdToQt(result);
}

QString ModStateModel::getActivePreset() const
{
	return QString::fromStdString(modManager->getActivePreset());
}

JsonNode ModStateModel::exportCurrentPreset() const
{
	return modManager->exportCurrentPreset();
}

std::tuple<QString, QStringList> ModStateModel::importPreset(const JsonNode & data)
{
	std::tuple<QString, QStringList> result;
	const auto & [presetName, modList] = modManager->importPreset(data);

	return {QString::fromStdString(presetName), stringListStdToQt(modList)};
}