-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalconfig.cpp
More file actions
219 lines (175 loc) · 7.07 KB
/
globalconfig.cpp
File metadata and controls
219 lines (175 loc) · 7.07 KB
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
#include "globalconfig.h"
//at main.cpp
extern void loadTranslation(const QString& langCode);
GlobalConfig::GlobalConfig(QObject* parent) : QObject(parent) {
// 确定配置文件路径
QString appDataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << appDataDir << "\n";
QDir dir(appDataDir);
if (!dir.exists())
dir.mkpath(".");
m_configPath = dir.filePath("config.json");
// 加载或初始化配置
if (!loadConfig()) {
initDefaultConfig();
saveConfig();
}
}
bool GlobalConfig::loadConfigFromText(QString configText)
{
configText.replace(QStringLiteral(":"), QStringLiteral(": "));
configText.replace(QStringLiteral(","), QStringLiteral(", "));
// 解析JSON文本
QJsonParseError parseError;
QJsonDocument tempDoc = QJsonDocument::fromJson(configText.toUtf8(), &parseError);
if (tempDoc.isNull() || !tempDoc.isObject()) {
qDebug() << tr("JSON解析错误:") << parseError.errorString();
return false;
}
// 定义允许的字段及其默认值
const QStringList allowedFields = {"adbPath", "deviceSerial", "sndcpyApkPath", "sndcpyPort", "note", "wndInfoOfAdvancedKeyboard", "language"};
const QStringList allowedKeyboardFields = {"buttons", "height", "width"};
QJsonObject defaultLayout{{"width", 800}, {"height", 80}};
QJsonObject defaultObj{
{"adbPath", "adb.exe"},
{"sndcpyApkPath", "sndcpy.apk"},
{"sndcpyPort", 28200},
{"deviceSerial", ""},
{"note", tr("此配置不会实时刷新。请尽量从GUI更改配置,不合适的修改可能导致错误。/This configuration will not be refreshed in real-time. Please try to make configuration changes through the GUI, as inappropriate modifications may lead to errors.")},
{"wndInfoOfAdvancedKeyboard", defaultLayout},
{"language", "zh_CN"}
};
// 过滤并处理配置
QJsonObject originalObj = tempDoc.object();
QJsonObject filteredObj;
for (const auto& key : allowedFields) {
if (!originalObj.contains(key)) {
filteredObj[key] = defaultObj[key];
continue;
}
if (key != "wndInfoOfAdvancedKeyboard") {
filteredObj[key] = originalObj[key];
continue;
}
// 特殊处理键盘布局配置
QJsonObject keyboardObj = originalObj[key].toObject();
QJsonObject filteredKeyboardObj = defaultLayout;
for (const auto& keyboardKey : allowedKeyboardFields) {
if (!keyboardObj.contains(keyboardKey)) continue;
if (keyboardKey == "buttons") {
// 过滤buttons对象,只保留简单键值对
QJsonObject buttonsObj = keyboardObj[keyboardKey].toObject();
QJsonObject filteredButtonsObj;
for (const auto& buttonKey : buttonsObj.keys()) {
auto value = buttonsObj[buttonKey];
if (value.isString() || value.isDouble() || value.isBool()) {
filteredButtonsObj[buttonKey] = value;
}
}
filteredKeyboardObj[keyboardKey] = filteredButtonsObj;
} else {
filteredKeyboardObj[keyboardKey] = keyboardObj[keyboardKey];
}
}
filteredObj[key] = filteredKeyboardObj;
}
m_jsonDoc.setObject(filteredObj);
normalizePath();
return true;
}
bool GlobalConfig::loadConfig()
{
QFile file(m_configPath);
if (!file.open(QIODevice::ReadOnly))
return false;
QByteArray data = file.readAll();
file.close();
if (!loadConfigFromText(data)) {
return false;
}
// 保留原有的保存逻辑
QSaveFile saveFile(m_configPath);
if (!saveFile.open(QIODevice::WriteOnly))
return false;
saveFile.write(m_jsonDoc.toJson());
return saveFile.commit();
}
bool GlobalConfig::saveConfig() {
QFile file(m_configPath);
if (!file.open(QIODevice::WriteOnly))
return false;
normalizePath();
file.write(m_jsonDoc.toJson());
qDebug()<<m_jsonDoc<<'\n';
loadTranslation(m_jsonDoc["language"].toString());
return true;
}
void GlobalConfig::normalizePath() {
QJsonObject obj = m_jsonDoc.object();
auto normalizeHelper = [](const QString& path) -> QString {
if (path.isEmpty()) return path; // 保持空值不变
// 转换为本地分隔符并清理路径
QString normalized = QDir::toNativeSeparators(QDir::cleanPath(path));
return normalized;
};
if (obj.contains("adbPath") && obj["adbPath"].isString())
obj["adbPath"] = normalizeHelper(obj["adbPath"].toString());
if (obj.contains("sndcpyApkPath") && obj["sndcpyApkPath"].isString())
obj["sndcpyApkPath"] = normalizeHelper(obj["sndcpyApkPath"].toString());
m_jsonDoc.setObject(obj);
}
QString GlobalConfig::adbPath() const {
return m_jsonDoc.object()["adbPath"].toString();
}
void GlobalConfig::setAdbPath(const QString& path) {
QJsonObject obj = m_jsonDoc.object();
obj["adbPath"] = path;
m_jsonDoc.setObject(obj);
}
QString GlobalConfig::sndcpyApkPath() const {
return m_jsonDoc.object()["sndcpyApkPath"].toString();
}
void GlobalConfig::setSndcpyApkPath(const QString& path) {
QJsonObject obj = m_jsonDoc.object();
obj["sndcpyApkPath"] = path;
m_jsonDoc.setObject(obj);
}
int GlobalConfig::sndcpyPort() const {
return m_jsonDoc.object()["sndcpyPort"].toInt();
}
void GlobalConfig::setSndcpyPort(int port) {
QJsonObject obj = m_jsonDoc.object();
obj["sndcpyPort"] = port;
m_jsonDoc.setObject(obj);
}
QString GlobalConfig::deviceSerial() const {
return m_jsonDoc.object()["deviceSerial"].toString();
}
void GlobalConfig::setDeviceSerial(const QString& ser) {
QJsonObject obj = m_jsonDoc.object();
obj["deviceSerial"] = ser;
m_jsonDoc.setObject(obj);
}
void GlobalConfig::initDefaultConfig() {
QJsonObject obj, layout;
layout["width"] = 800;
layout["height"] = 80;
obj["adbPath"] = "adb.exe";
obj["sndcpyApkPath"] = "sndcpy.apk";
obj["sndcpyPort"] = 28200;
obj["deviceSerial"] = "";
obj["note"] = tr("此配置不会实时刷新。请尽量从GUI更改配置,不合适的修改可能导致错误。/This configuration will not be refreshed in real-time. Please try to make configuration changes through the GUI, as inappropriate modifications may lead to errors.");
obj["wndInfoOfAdvancedKeyboard"] = layout;
m_jsonDoc.setObject(obj);
}
QJsonObject GlobalConfig::advancedKeyboardLayout() {
return m_jsonDoc["wndInfoOfAdvancedKeyboard"].toObject();
}
void GlobalConfig::setAdvancedKeyboardLayout(const QJsonObject& layout){
QJsonObject obj = m_jsonDoc.object();
obj["wndInfoOfAdvancedKeyboard"] = layout;
m_jsonDoc.setObject(obj);
}
QString GlobalConfig::language() const{
return m_jsonDoc["language"].toString();
}