forked from linuxdeepin/deepin-fcitx5configtool-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcitx5configproxy.cpp
More file actions
402 lines (370 loc) · 14.5 KB
/
fcitx5configproxy.cpp
File metadata and controls
402 lines (370 loc) · 14.5 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "fcitx5configproxy.h"
#include "private/fcitx5configproxy_p.h"
#include <dbusprovider.h>
#include <varianthelper.h>
#include <QDBusPendingCallWatcher>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(proxy, "fcitx5.configtool.proxy")
using namespace deepin::fcitx5configtool;
Fcitx5ConfigProxyPrivate::Fcitx5ConfigProxyPrivate(Fcitx5ConfigProxy *parent,
fcitx::kcm::DBusProvider *dbus,
const QString &path)
: q(parent), dbusprovider(dbus), path(path)
{
qCDebug(proxy) << "Entering Fcitx5ConfigProxyPrivate constructor for path:" << path;
timer = new QTimer(q);
timer->setInterval(1000);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, q, &Fcitx5ConfigProxy::save);
qCDebug(proxy) << "Exiting Fcitx5ConfigProxyPrivate constructor";
}
QStringList Fcitx5ConfigProxyPrivate::formatKey(const QString &shortcut) {
qCDebug(proxy) << "Formatting key from shortcut:" << shortcut;
QStringList list;
for (const auto &key : shortcut.split("+")) {
if (key.trimmed().toLower() == "control")
list << "Ctrl";
else if (key.trimmed().toLower() == "super")
list << "Meta";
else if (key.trimmed().toLower() == "backspace")
list << "Backspace";
else if (key.trimmed().toLower() == "space")
list << "Space";
else
list << key.trimmed();
}
if (list.size() == 3 && list.contains("Ctrl") && list.contains("Meta")) {
if (list.contains("Control_L")) {
list.removeAll("Control_L");
} else {
list.removeAll("Control_R");
}
} else if (list.size() == 3 && list.contains("Shift") && list.contains("Meta")) {
if (list.contains("Shift_L")) {
list.removeAll("Shift_L");
} else {
list.removeAll("Shift_R");
}
} else if (list.size() == 3 && list.contains("Alt") && list.contains("Meta")) {
if (list.contains("Alt_L")) {
list.removeAll("Alt_L");
} else {
list.removeAll("Shift_R");
}
}
qCDebug(proxy) << "Formatted key list:" << list;
return list;
}
QString Fcitx5ConfigProxyPrivate::formatKeys(const QStringList &keys) {
qCDebug(proxy) << "Formatting keys from list:" << keys;
QStringList list;
for (const auto &key : keys) {
if (key.trimmed().toLower() == "ctrl")
list << "Control";
else if (key.trimmed().toLower() == "meta")
list << "Super";
else if (key.trimmed().toLower() == "backspace")
list << "BackSpace";
else if (key.trimmed().toLower() == "space")
list << "space";
else
list << key.trimmed();
}
if (list.size() == 2 && list.contains("Shift") && list.contains("Super")) {
list.append("Shift_L");
} else if (list.size() == 2 && list.contains("Control") && list.contains("Super")) {
list.append("Control_L");
} else if (list.size() == 2 && list.contains("Alt") && list.contains("Super")) {
list.append("Alt_L");
}
return list.join("+");
}
QVariant Fcitx5ConfigProxyPrivate::readDBusValue(const QVariant &value) {
if (value.canConvert<QDBusArgument>()) {
auto argument = qvariant_cast<QDBusArgument>(value);
QVariantMap map;
argument >> map;
QVariantMap resultMap;
for (auto iter = map.begin(); iter != map.end(); ++iter) {
resultMap[iter.key()] = readDBusValue(iter.value());
}
return resultMap;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (value.typeId() == QMetaType::QVariantMap) {
#else
if (value.type() == QVariant::Map) {
#endif
QVariantMap resultMap;
const auto map = value.toMap();
for (auto iter = map.begin(); iter != map.end(); ++iter) {
resultMap[iter.key()] = readDBusValue(iter.value());
}
return resultMap;
}
return value;
}
Fcitx5ConfigProxy::Fcitx5ConfigProxy(fcitx::kcm::DBusProvider *dbus, const QString &path, QObject *parent)
: QObject(parent), d(new Fcitx5ConfigProxyPrivate(this, dbus, path))
{
qCDebug(proxy) << "Entering Fcitx5ConfigProxy constructor for path:" << path;
}
Fcitx5ConfigProxy::~Fcitx5ConfigProxy() = default;
void Fcitx5ConfigProxy::clear()
{
qCDebug(proxy) << "Entering clear";
d->configValue.clear();
d->configTypes.clear();
Q_EMIT requestConfigFinished();
qCDebug(proxy) << "Exiting clear";
}
QVariantList Fcitx5ConfigProxy::globalConfigTypes() const
{
qCDebug(proxy) << "Entering globalConfigTypes";
QVariantList list;
for (const auto &type : d->configTypes) {
if (type.name() == "GlobalConfig") {
for (const auto &option : type.options()) {
QVariantMap item;
item["name"] = option.name();
item["description"] = option.description();
list.append(item);
}
break;
}
}
qCDebug(proxy) << "Exiting globalConfigTypes with" << list.size() << "items";
return list;
}
QVariantList Fcitx5ConfigProxy::globalConfigOptions(const QString &type, bool all) const
{
qCDebug(proxy) << "Entering globalConfigOptions for type" << type << "all:" << all;
QVariantList list;
QString currentType = type+"$"+type+"Config";
for (const auto &configType : d->configTypes) {
if (configType.name() != currentType)
continue;
for (const auto &option : configType.options()) {
// Don't display TriggerKeys and EnumerateForwardKeys
if (!all && (option.name() == "TriggerKeys" || option.name() == "EnumerateForwardKeys")) {
continue;
}
QVariantMap item;
item["name"] = option.name();
item["type"] = option.type();
item["description"] = option.description();
auto variant = value(type+"/"+option.name());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (variant.typeId() == QMetaType::QVariantMap) {
#else
if (variant.type() == QVariant::Map) {
#endif
QVariantMap map = variant.toMap();
if (map.contains("0")) {
item["value"] = d->formatKey(map["0"].toString());
}
} else {
item["value"] = variant;
}
QVariantMap properties = option.properties();
if (!properties.isEmpty()) {
auto iterator = properties.begin();
while (iterator != properties.constEnd()) {
if (iterator.key() == "Enum") {
auto argument = qvariant_cast<QDBusArgument>(iterator.value());
QVariantMap map;
argument >> map;
QVariantList enumStrings = map.values().toList();
if (!enumStrings.isEmpty()) {
item["properties"] = enumStrings;
}
break;
}
++iterator;
}
iterator = properties.begin();
while (iterator != properties.constEnd()) {
if (iterator.key() == "EnumI18n") {
auto argument = qvariant_cast<QDBusArgument>(iterator.value());
QVariantMap map;
argument >> map;
QVariantList enumStrings = map.values().toList();
if (!enumStrings.isEmpty()) {
item["propertiesI18n"] = enumStrings;
}
break;
}
++iterator;
}
if (option.type() == "Integer") {
bool ok = false;
int maxVal = properties.contains("IntMax") ? properties["IntMax"].toInt(&ok) : 9999;
item["intMax"] = ok ? maxVal : 9999;
int minVal = properties.contains("IntMin") ? properties["IntMin"].toInt(&ok) : 0;
item["intMin"] = ok ? minVal : 0;
}
}
list.append(item);
}
break;
}
qCDebug(proxy) << "Exiting globalConfigOptions with" << list.size() << "items";
return list;
}
QVariant Fcitx5ConfigProxy::globalConfigOption(const QString &type, const QString &optionName) const
{
qCDebug(proxy) << "Entering globalConfigOption for type" << type << "option:" << optionName;
QVariantMap item;
QString currentType = type+"$"+type+"Config";
for (const auto &configType : d->configTypes) {
if (configType.name() != currentType)
continue;
for (const auto &option : configType.options()) {
// Don't display TriggerKeys and EnumerateForwardKeys
if (option.name() != optionName) {
continue;
}
item["name"] = option.name();
item["type"] = option.type();
item["description"] = option.description();
auto variant = value(type+"/"+option.name());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (variant.typeId() == QMetaType::QVariantMap) {
#else
if (variant.type() == QVariant::Map) {
#endif
QVariantMap map = variant.toMap();
if (map.contains("0")) {
item["value"] = d->formatKey(map["0"].toString());
}
} else {
item["value"] = variant;
}
QVariantMap properties = option.properties();
if (!properties.isEmpty()) {
auto iterator = properties.begin();
while (iterator != properties.constEnd()) {
if (iterator.key() == "Enum") {
auto argument = qvariant_cast<QDBusArgument>(iterator.value());
QVariantMap map;
argument >> map;
QVariantList enumStrings = map.values().toList();
if (!enumStrings.isEmpty()) {
item["properties"] = enumStrings;
}
break;
}
++iterator;
}
iterator = properties.begin();
while (iterator != properties.constEnd()) {
if (iterator.key() == "EnumI18n") {
auto argument = qvariant_cast<QDBusArgument>(iterator.value());
QVariantMap map;
argument >> map;
QVariantList enumStrings = map.values().toList();
if (!enumStrings.isEmpty()) {
item["propertiesI18n"] = enumStrings;
}
break;
}
++iterator;
}
}
break;
}
break;
}
qCDebug(proxy) << "Exiting globalConfigOption with item" << item;
return item;
}
void Fcitx5ConfigProxy::restoreDefault(const QString &type)
{
qCDebug(proxy) << "Entering restoreDefault for type" << type;
QString currentType = type+"$"+type+"Config";
for (const auto &configType : d->configTypes) {
if (configType.name() != currentType)
continue;
for (const auto &option : configType.options()) {
auto defaultValue = option.defaultValue();
setValue(type+"/"+option.name(), d->readDBusValue(defaultValue.variant()));
}
break;
}
qCDebug(proxy) << "Exiting restoreDefault";
}
void Fcitx5ConfigProxy::requestConfig(bool sync)
{
qCDebug(proxy) << "Entering requestConfig for path" << d->path << "with sync" << sync;
if (!d->dbusprovider->controller()) {
qCWarning(proxy) << "DBus controller not available for requestConfig";
return;
}
auto call = d->dbusprovider->controller()->GetConfig(d->path);
auto watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher,
&QDBusPendingCallWatcher::finished,
this,
&Fcitx5ConfigProxy::onRequestConfigFinished);
if (sync) {
qCDebug(proxy) << "Waiting for requestConfig to finish";
watcher->waitForFinished();
}
qCDebug(proxy) << "Exiting requestConfig";
}
void Fcitx5ConfigProxy::onRequestConfigFinished(QDBusPendingCallWatcher *watcher)
{
qCDebug(proxy) << "Entering onRequestConfigFinished for path" << d->path;
watcher->deleteLater();
QDBusPendingReply<QDBusVariant, fcitx::FcitxQtConfigTypeList> reply = *watcher;
if (reply.isError()) {
qCWarning(proxy) << "Failed to get config for path" << d->path << "Error:" << reply.error();
return;
}
qCInfo(proxy) << "Successfully received config for path:" << d->path;
d->configTypes = reply.argumentAt<1>();
auto value = reply.argumentAt<0>().variant();
QVariantMap allMap;
allMap = d->readDBusValue(value).toMap();
std::swap(d->configValue, allMap);
Q_EMIT requestConfigFinished();
qCDebug(proxy) << "Exiting onRequestConfigFinished";
}
QVariant Fcitx5ConfigProxy::value(const QString &path) const
{
qCDebug(proxy) << "Entering value for path:" << path;
return fcitx::kcm::readVariant(d->configValue, path);
}
void Fcitx5ConfigProxy::setValue(const QString &path, const QVariant &value, bool isKey)
{
qCDebug(proxy) << "Setting value for config path:" << path << "isKey:" << isKey;
if (value == this->value(path)) {
qCDebug(proxy) << "Value unchanged, skipping update for path:" << path;
return;
}
if (isKey) {
qCDebug(proxy) << "Formatting keys for path:" << path;
auto keys = d->formatKeys(value.toStringList());
fcitx::kcm::writeVariant(d->configValue, path, keys);
} else {
qCDebug(proxy) << "Writing value for path:" << path;
fcitx::kcm::writeVariant(d->configValue, path, value);
}
d->timer->start();
qCDebug(proxy) << "Exiting setValue";
}
void Fcitx5ConfigProxy::save()
{
qCDebug(proxy) << "Entering save for path:" << d->path;
if (!d->dbusprovider->controller()) {
qCWarning(proxy) << "DBus controller not available for save";
return;
}
QDBusVariant var(d->configValue);
d->dbusprovider->controller()->SetConfig(d->path, var);
requestConfig(false);
qCDebug(proxy) << "Exiting save";
}