1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "optionutils.h" |
6 | #include "common/util/custompaths.h" |
7 | |
8 | #include <QFile> |
9 | #include <QDir> |
10 | #include <QDebug> |
11 | #include <QJsonObject> |
12 | #include <QJsonParseError> |
13 | #include <QJsonArray> |
14 | |
15 | OptionUtils::OptionUtils(QObject *parent) |
16 | : QObject(parent) |
17 | { |
18 | |
19 | } |
20 | |
21 | OptionUtils::~OptionUtils() |
22 | { |
23 | |
24 | } |
25 | |
26 | QString OptionUtils::getJsonFilePath() |
27 | { |
28 | return CustomPaths::user(CustomPaths::Flags::Configures) \ |
29 | + QDir::separator() \ |
30 | + QString("optionparam.support" ); |
31 | } |
32 | |
33 | bool OptionUtils::readJsonRootObject(const QString &fileName, QJsonObject &rootObject) |
34 | { |
35 | QFile file(fileName); |
36 | if (!file.open(QIODevice::ReadOnly)) |
37 | return false; |
38 | |
39 | QByteArray data = file.readAll(); |
40 | file.close(); |
41 | |
42 | QJsonParseError parseError; |
43 | QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); |
44 | if (QJsonParseError::NoError != parseError.error) { |
45 | return false; |
46 | } |
47 | |
48 | if (!doc.isObject()) |
49 | return false; |
50 | |
51 | rootObject = doc.object(); |
52 | return true; |
53 | } |
54 | |
55 | bool OptionUtils::convertMapToJson(const QMap<QString, QVariant> &map, QJsonObject &jsonObject) |
56 | { |
57 | foreach (QString key, map.keys()) { |
58 | QVariant value = map.value(key); |
59 | convert(key, value, jsonObject); |
60 | } |
61 | return true; |
62 | } |
63 | |
64 | void OptionUtils::convert(const QString &key, const QVariant &variant, QJsonObject &jsonObject) |
65 | { |
66 | switch (variant.type()) { |
67 | case QVariant::String: |
68 | { |
69 | jsonObject.insert(key, variant.toString()); |
70 | break; |
71 | } |
72 | case QVariant::Map: |
73 | { |
74 | QJsonObject object; |
75 | const QVariantMap varMap = variant.toMap(); |
76 | const QVariantMap::const_iterator cend = varMap.constEnd(); |
77 | for (QVariantMap::const_iterator iter = varMap.constBegin(); iter != cend; ++iter) { |
78 | convert(iter.key(), iter.value(), object); |
79 | } |
80 | jsonObject.insert(key, object); |
81 | break; |
82 | } |
83 | case QVariant::List: |
84 | { |
85 | QJsonObject object; |
86 | foreach (const QVariant &var, variant.toList()) { |
87 | Q_UNUSED(var) |
88 | //TODO |
89 | } |
90 | jsonObject.insert(key, object); |
91 | break; |
92 | } |
93 | default: |
94 | break; |
95 | } |
96 | } |
97 | |
98 | bool OptionUtils::convertJsonToMap(const QJsonObject &jsonObject, QMap<QString, QVariant> &map) |
99 | { |
100 | foreach (QString key, jsonObject.keys()) { |
101 | QJsonValue value = jsonObject.value(key); |
102 | convert(key, value, map); |
103 | } |
104 | return true; |
105 | } |
106 | |
107 | void OptionUtils::convert(const QString &key, const QJsonValue &variant, QMap<QString, QVariant> &map) |
108 | { |
109 | switch (variant.type()) { |
110 | case QJsonValue::String: |
111 | { |
112 | map.insert(key, variant.toString()); |
113 | break; |
114 | } |
115 | case QJsonValue::Object: |
116 | { |
117 | QMap<QString, QVariant> varMap; |
118 | const QJsonObject varObject = variant.toObject(); |
119 | foreach (QString varKey, varObject.keys()) { |
120 | QJsonValue value = varObject.value(varKey); |
121 | convert(varKey, value, varMap); |
122 | } |
123 | map.insert(key, varMap); |
124 | break; |
125 | } |
126 | case QJsonValue::Array: |
127 | { |
128 | QMap<QString, QVariant> varMap; |
129 | const QJsonArray array = variant.toArray(); |
130 | //TODO |
131 | map.insert(key, varMap); |
132 | break; |
133 | } |
134 | default: |
135 | break; |
136 | } |
137 | } |
138 | |
139 | bool OptionUtils::writeJsonSection(const QString &fileName, const QString &parentNode, const QString &itemNode, |
140 | const QMap<QString, QVariant> &map) |
141 | { |
142 | QJsonObject jsonObject; |
143 | bool ret = convertMapToJson(map, jsonObject); |
144 | if (!ret) { |
145 | return false; |
146 | } |
147 | |
148 | QJsonObject rootObject; |
149 | QJsonObject parentObject; |
150 | if (readJsonRootObject(fileName, rootObject)) { |
151 | if (rootObject.find(parentNode)->toObject().isEmpty()) { |
152 | rootObject.insert(parentNode, parentObject); |
153 | } else { |
154 | parentObject = rootObject.value(parentNode).toObject(); |
155 | } |
156 | } |
157 | |
158 | if (!parentObject.find(itemNode)->toObject().isEmpty()) { |
159 | parentObject.remove(itemNode); |
160 | } |
161 | |
162 | parentObject.insert(itemNode, jsonObject); |
163 | rootObject.insert(parentNode, parentObject); |
164 | |
165 | QJsonDocument doc; |
166 | doc.setObject(rootObject); |
167 | QString jsonStr(doc.toJson(QJsonDocument::Indented)); |
168 | |
169 | QFile file(fileName); |
170 | if (!file.open(QIODevice::WriteOnly)) |
171 | return false; |
172 | |
173 | file.write(jsonStr.toUtf8()); |
174 | file.close(); |
175 | |
176 | return true; |
177 | } |
178 | |
179 | |
180 | bool OptionUtils::readJsonSection(const QString &fileName, const QString &parentNode, const QString &itemNode, |
181 | QMap<QString, QVariant> &map) |
182 | { |
183 | QJsonObject rootObject; |
184 | if (!readJsonRootObject(fileName, rootObject)) |
185 | return false; |
186 | |
187 | QJsonObject parentObject = rootObject.value(parentNode).toObject(); |
188 | if (parentObject.isEmpty()) |
189 | return false; |
190 | |
191 | QJsonObject nodeObject = parentObject.value(itemNode).toObject(); |
192 | if (nodeObject.isEmpty()) |
193 | return false; |
194 | |
195 | bool ret = convertJsonToMap(nodeObject, map); |
196 | if (!ret) { |
197 | return false; |
198 | } |
199 | |
200 | return true; |
201 | } |
202 | |
203 | bool OptionUtils::readAll(const QString &fileName, QMap<QString, QVariant> &map) |
204 | { |
205 | QJsonObject rootObject; |
206 | if (!readJsonRootObject(fileName, rootObject)) |
207 | return false; |
208 | |
209 | bool ret = convertJsonToMap(rootObject, map); |
210 | if (!ret) { |
211 | return false; |
212 | } |
213 | |
214 | return true; |
215 | } |
216 | |
217 | |
218 | bool OptionUtils::writeAll(const QString &fileName, const QMap<QString, QVariant> &map) |
219 | { |
220 | QJsonObject rootObject; |
221 | bool ret = convertMapToJson(map, rootObject); |
222 | if (!ret) { |
223 | return false; |
224 | } |
225 | |
226 | QJsonDocument doc; |
227 | doc.setObject(rootObject); |
228 | QString jsonStr(doc.toJson(QJsonDocument::Indented)); |
229 | |
230 | QFile file(fileName); |
231 | if (!file.open(QIODevice::WriteOnly)) |
232 | return false; |
233 | |
234 | file.write(jsonStr.toUtf8()); |
235 | file.close(); |
236 | |
237 | return true; |
238 | } |
239 | |