File: SettingsManager.cpp

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (270 lines) | stat: -rw-r--r-- 7,367 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*!
	@file
	@author		Albert Semenov
	@date		07/2012
*/

#include "Precompiled.h"
#include "SettingsManager.h"

namespace tools
{
	MYGUI_SINGLETON_DEFINITION(SettingsManager);

	SettingsManager::SettingsManager() :
		mSingletonHolder(this)
	{
		mDocument = new pugi::xml_document();
		pugi::xml_node declaration = mDocument->append_child(pugi::node_declaration);
		declaration.append_attribute("version") = "1.0";
		declaration.append_attribute("encoding") = "utf-8";

		mDocument->append_child("Settings");

		mUserDocument = new pugi::xml_document();
		declaration = mUserDocument->append_child(pugi::node_declaration);
		declaration.append_attribute("version") = "1.0";
		declaration.append_attribute("encoding") = "utf-8";

		mUserDocument->append_child("Settings");
	}

	SettingsManager::~SettingsManager()
	{
		delete mDocument;
		mDocument = nullptr;

		delete mUserDocument;
		mUserDocument = nullptr;
	}

	bool SettingsManager::loadSettingsFile(CString _fileName)
	{
		pugi::xml_document doc;
		pugi::xml_parse_result result = doc.load_file(_fileName.c_str());

		if (result)
		{
			if (std::string_view(doc.first_child().name()) == std::string_view(mDocument->document_element().name()))
				mergeNodes(mDocument->document_element(), doc.first_child());
		}
		else
		{
			// логировать если это ошибка формата xml
		}

		return result;
	}

	void SettingsManager::saveSettingsFile(CString _fileName)
	{
		mDocument->save_file(_fileName.c_str());
	}

	bool SettingsManager::getExistValue(CString _path)
	{
		pugi::xpath_node node = mUserDocument->document_element().select_single_node(_path.c_str());
		if (!node.node().empty())
			return true;

		node = mDocument->document_element().select_single_node(_path.c_str());
		return !node.node().empty();
	}

	std::string SettingsManager::getValue(CString _path)
	{
		pugi::xpath_node node = mUserDocument->document_element().select_single_node(_path.c_str());
		if (!node.node().empty())
			return node.node().child_value();

		node = mDocument->document_element().select_single_node(_path.c_str());
		if (!node.node().empty())
			return node.node().child_value();

		return {};
	}

	void SettingsManager::setValueImpl(std::string_view _path, CString _value)
	{
		const char* path = _path.empty() ? "" : _path.data();
		pugi::xpath_node node = mUserDocument->document_element().select_single_node(path);
		if (!node.node().empty())
		{
			node.node().text().set(_value.c_str());
		}
		else
		{
			std::vector<std::string> names;
			names = MyGUI::utility::split(_path, "/");

			pugi::xml_node currentNode = mUserDocument->document_element();
			for (const auto& name : names)
			{
				pugi::xml_node childNode = currentNode.child(name.data());
				if (childNode.empty())
					childNode = currentNode.append_child(name.data());

				currentNode = childNode;
			}

			currentNode.text().set(_value.c_str());
		}

		eventSettingsChanged(_path);
	}

	SettingsManager::VectorString SettingsManager::getValueList(std::string_view _path)
	{
		SettingsManager::VectorString result;
		std::string path{_path};
		path += "/Value";

		pugi::xpath_node_set nodes = mUserDocument->document_element().select_nodes(path.data());
		if (!nodes.empty())
		{
			for (const auto& node : nodes)
				result.emplace_back(node.node().child_value());
		}
		else
		{
			nodes = mDocument->document_element().select_nodes(path.data());
			for (const auto& node : nodes)
				result.emplace_back(node.node().child_value());
		}

		return result;
	}

	void SettingsManager::mergeNodes(pugi::xml_node _nodeTarget, pugi::xml_node _nodeSource)
	{
		bool listElement = MyGUI::utility::endWith(_nodeTarget.name(), ".List");

		// затираем текст у цели потому что любое значение текста источника является конечным
		pugi::xml_node targetTextNode = _nodeTarget.first_child();
		if (!targetTextNode.empty() && targetTextNode.type() == pugi::node_pcdata)
			targetTextNode.set_value("");

		pugi::xml_node sourceTextNode = _nodeSource.first_child();
		if (!sourceTextNode.empty() && sourceTextNode.type() == pugi::node_pcdata)
		{
			targetTextNode = _nodeTarget.first_child();
			if (targetTextNode.empty())
				targetTextNode = _nodeTarget.append_child(pugi::node_pcdata);
			targetTextNode.set_value(sourceTextNode.value());
		}

		for (auto& child : _nodeSource)
		{
			if (child.type() != pugi::node_element)
				continue;

			pugi::xml_node targetChildNode;

			if (listElement)
			{
				targetChildNode = _nodeTarget.append_child(child.name());
			}
			else
			{
				targetChildNode = _nodeTarget.child(child.name());
				if (targetChildNode.empty())
					targetChildNode = _nodeTarget.append_child(child.name());
			}

			mergeAttributes(targetChildNode, child);
			mergeNodes(targetChildNode, child);
		}
	}

	void SettingsManager::mergeAttributes(pugi::xml_node _nodeTarget, pugi::xml_node _nodeSource)
	{
		for (pugi::xml_node::attribute_iterator attribute = _nodeSource.attributes_begin();
			 attribute != _nodeSource.attributes_end();
			 attribute++)
		{
			pugi::xml_attribute attributeNode = _nodeTarget.attribute((*attribute).name());
			if (attributeNode.empty())
				attributeNode = _nodeTarget.append_attribute((*attribute).name());
			attributeNode.set_value((*attribute).value());
		}
	}

	bool SettingsManager::loadUserSettingsFile(std::string_view _fileName)
	{
		mUserSettingsFileName = _fileName;

		pugi::xml_document doc;
		pugi::xml_parse_result result = doc.load_file(mUserSettingsFileName.data());

		if (result)
		{
			if (std::string_view(doc.first_child().name()) ==
				std::string_view(mUserDocument->document_element().name()))
				mergeNodes(mUserDocument->document_element(), doc.first_child());
		}
		else
		{
			// логировать если это ошибка формата xml
		}

		return result;
	}

	void SettingsManager::saveUserSettingsFile()
	{
		if (!mUserSettingsFileName.empty())
			mUserDocument->save_file(mUserSettingsFileName.data());
	}

	void SettingsManager::setValueListImpl(std::string_view _path, const VectorString& _values)
	{
		if (!MyGUI::utility::endWith(_path, ".List"))
			return;

		pugi::xml_node targetNode;

		pugi::xpath_node node = mUserDocument->document_element().select_single_node(_path.data());
		if (!node.node().empty())
		{
			targetNode = node.node();
			while (!targetNode.first_child().empty())
				targetNode.remove_child(targetNode.first_child());
		}
		else
		{
			std::vector<std::string> names;
			names = MyGUI::utility::split(_path, "/");

			pugi::xml_node currentNode = mUserDocument->document_element();
			for (const auto& name : names)
			{
				pugi::xml_node childNode = currentNode.child(name.data());
				if (childNode.empty())
					childNode = currentNode.append_child(name.data());

				currentNode = childNode;
			}

			targetNode = currentNode;
		}

		for (const auto& _value : _values)
			targetNode.append_child("Value").text().set(_value.data());

		eventSettingsChanged(_path);
	}

	pugi::xpath_node_set SettingsManager::getValueNodeList(std::string_view _path)
	{
		std::string path{_path};
		path += "/Value";

		pugi::xpath_node_set nodes = mUserDocument->document_element().select_nodes(path.data());
		if (!nodes.empty())
			return nodes;

		nodes = mDocument->document_element().select_nodes(path.data());
		return nodes;
	}

}