-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathinputmethodmanager.cpp
More file actions
459 lines (416 loc) · 16 KB
/
Copy pathinputmethodmanager.cpp
File metadata and controls
459 lines (416 loc) · 16 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#include "inputmethodmanager.h"
#include <fcntl.h>
#include <unistd.h>
#include <cassert>
#include <list>
#include <unordered_map>
#include "fcitx-config/iniparser.h"
#include "fcitx-config/rawconfig.h"
#include "fcitx-utils/i18n.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/standardpath.h"
#include "addonmanager.h"
#include "inputmethodconfig_p.h"
#include "inputmethodengine.h"
#include "instance.h"
#include "misc_p.h"
namespace fcitx {
class InputMethodManagerPrivate : QPtrHolder<InputMethodManager> {
public:
InputMethodManagerPrivate(AddonManager *addonManager_,
InputMethodManager *q)
: QPtrHolder(q), addonManager_(addonManager_) {}
void loadConfig(const std::function<void(InputMethodManager &)>
&buildDefaultGroupCallback);
void buildDefaultGroup(const std::function<void(InputMethodManager &)>
&buildDefaultGroupCallback);
void setGroupOrder(const std::vector<std::string> &groupOrder);
// Read entries from inputmethod/*.conf
void loadStaticEntries(const std::unordered_set<std::string> &addonNames);
// Read entries from addon->listInputMethods();
void loadDynamicEntries(const std::unordered_set<std::string> &addonNames);
FCITX_DEFINE_SIGNAL_PRIVATE(InputMethodManager, CurrentGroupAboutToChange);
FCITX_DEFINE_SIGNAL_PRIVATE(InputMethodManager, CurrentGroupChanged);
FCITX_DEFINE_SIGNAL_PRIVATE(InputMethodManager, GroupAdded);
FCITX_DEFINE_SIGNAL_PRIVATE(InputMethodManager, GroupRemoved);
AddonManager *addonManager_;
std::list<std::string> groupOrder_;
bool buildingGroup_ = false;
std::unordered_map<std::string, InputMethodGroup> groups_;
std::unordered_map<std::string, InputMethodEntry> entries_;
Instance *instance_ = nullptr;
std::unique_ptr<HandlerTableEntry<EventHandler>> eventWatcher_;
int64_t timestamp_ = 0;
};
bool checkEntry(const InputMethodEntry &entry,
const std::unordered_set<std::string> &inputMethods) {
return !(entry.name().empty() || entry.uniqueName().empty() ||
entry.addon().empty() || inputMethods.count(entry.addon()) == 0);
}
void InputMethodManagerPrivate::loadConfig(
const std::function<void(InputMethodManager &)>
&buildDefaultGroupCallback) {
const auto &path = StandardPath::global();
auto file = path.open(StandardPath::Type::PkgConfig, "profile", O_RDONLY);
RawConfig config;
if (file.fd() >= 0) {
readFromIni(config, file.fd());
}
InputMethodConfig imConfig;
imConfig.load(config);
groups_.clear();
std::vector<std::string> tempOrder;
if (!imConfig.groups.value().empty()) {
const auto &groupsConfig = imConfig.groups.value();
for (const auto &groupConfig : groupsConfig) {
// group must have a name
if (groupConfig.name.value().empty() ||
groupConfig.defaultLayout.value().empty()) {
continue;
}
auto result =
groups_.emplace(groupConfig.name.value(),
InputMethodGroup(groupConfig.name.value()));
tempOrder.push_back(groupConfig.name.value());
auto &group = result.first->second;
group.setDefaultLayout(groupConfig.defaultLayout.value());
group.setDefaultLayoutOptions(
groupConfig.defaultLayoutOptions.value());
const auto &items = groupConfig.items.value();
for (const auto &item : items) {
if (!entries_.count(item.name.value())) {
FCITX_WARN() << "Group Item " << item.name.value()
<< " in group " << groupConfig.name.value()
<< " is not valid. Removed.";
continue;
}
group.inputMethodList().emplace_back(
std::move(InputMethodGroupItem(item.name.value())
.setLayout(item.layout.value())));
}
if (group.inputMethodList().empty()) {
FCITX_WARN() << "Group " << groupConfig.name.value()
<< " is empty. Removed.";
groups_.erase(groupConfig.name.value());
continue;
}
group.setDefaultInputMethod(groupConfig.defaultInputMethod.value());
}
}
if (groups_.empty()) {
FCITX_INFO() << "No valid input method group in configuration. "
<< "Building a default one";
buildDefaultGroup(buildDefaultGroupCallback);
} else {
if (!imConfig.groupOrder.value().empty()) {
setGroupOrder(imConfig.groupOrder.value());
} else {
setGroupOrder(tempOrder);
}
}
}
void InputMethodManagerPrivate::buildDefaultGroup(
const std::function<void(InputMethodManager &)>
&buildDefaultGroupCallback) {
groups_.clear();
if (buildDefaultGroupCallback) {
buildingGroup_ = true;
buildDefaultGroupCallback(*q_func());
buildingGroup_ = false;
} else {
std::string name = _("Default");
auto result = groups_.emplace(name, InputMethodGroup(name));
auto &group = result.first->second;
group.inputMethodList().emplace_back(
InputMethodGroupItem("keyboard-us"));
group.setDefaultInputMethod("");
group.setDefaultLayout("us");
setGroupOrder({name});
}
assert(!groups_.empty());
assert(!groupOrder_.empty());
}
void InputMethodManagerPrivate::loadStaticEntries(
const std::unordered_set<std::string> &addonNames) {
const auto &path = StandardPath::global();
timestamp_ = path.timestamp(StandardPath::Type::PkgData, "inputmethod");
auto filesMap =
path.multiOpenAll(StandardPath::Type::PkgData, "inputmethod", O_RDONLY,
filter::Suffix(".conf"));
for (const auto &file : filesMap) {
const auto name = file.first.substr(0, file.first.size() - 5);
if (entries_.count(name) != 0) {
continue;
}
const auto &files = file.second;
RawConfig config;
// reverse the order, so we end up parse user file at last.
for (auto iter = files.rbegin(), end = files.rend(); iter != end;
iter++) {
auto fd = iter->fd();
readFromIni(config, fd);
}
InputMethodInfo imInfo;
imInfo.load(config);
// Remove ".conf"
InputMethodEntry entry = toInputMethodEntry(name, imInfo);
if (checkEntry(entry, addonNames) &&
stringutils::isConcatOf(file.first, entry.uniqueName(), ".conf")) {
entries_.emplace(std::string(entry.uniqueName()), std::move(entry));
}
}
}
void InputMethodManagerPrivate::loadDynamicEntries(
const std::unordered_set<std::string> &addonNames) {
for (const auto &addonName : addonNames) {
const auto *addonInfo = addonManager_->addonInfo(addonName);
// on request input method should always provides entry with config file
if (!addonInfo || addonInfo->onDemand()) {
continue;
}
auto *engine =
static_cast<InputMethodEngine *>(addonManager_->addon(addonName));
if (!engine) {
FCITX_WARN() << "Failed to load input method addon: " << addonName;
continue;
}
auto newEntries = engine->listInputMethods();
FCITX_INFO() << "Found " << newEntries.size() << " input method(s) "
<< "in addon " << addonName;
for (auto &newEntry : newEntries) {
// ok we can't let you register something werid.
if (checkEntry(newEntry, addonNames) &&
newEntry.addon() == addonName &&
entries_.count(newEntry.uniqueName()) == 0) {
entries_.emplace(std::string(newEntry.uniqueName()),
std::move(newEntry));
}
}
}
}
InputMethodManager::InputMethodManager(AddonManager *addonManager)
: d_ptr(std::make_unique<InputMethodManagerPrivate>(addonManager, this)) {}
InputMethodManager::~InputMethodManager() {}
void InputMethodManager::load(const std::function<void(InputMethodManager &)>
&buildDefaultGroupCallback) {
FCITX_D();
emit<InputMethodManager::CurrentGroupAboutToChange>(
d->groupOrder_.empty() ? "" : d->groupOrder_.front());
auto addonNames = d->addonManager_->addonNames(AddonCategory::InputMethod);
d->loadStaticEntries(addonNames);
d->loadDynamicEntries(addonNames);
d->loadConfig(buildDefaultGroupCallback);
// groupOrder guarantee to be non-empty at this point.
emit<InputMethodManager::CurrentGroupChanged>(d->groupOrder_.front());
}
void InputMethodManager::reset(const std::function<void(InputMethodManager &)>
&buildDefaultGroupCallback) {
FCITX_D();
emit<InputMethodManager::CurrentGroupAboutToChange>(
d->groupOrder_.empty() ? "" : d->groupOrder_.front());
d->buildDefaultGroup(buildDefaultGroupCallback);
// groupOrder guarantee to be non-empty at this point.
emit<InputMethodManager::CurrentGroupChanged>(d->groupOrder_.front());
}
void InputMethodManager::refresh() {
FCITX_D();
auto addonNames = d->addonManager_->addonNames(AddonCategory::InputMethod);
d->loadStaticEntries(addonNames);
d->loadDynamicEntries(addonNames);
}
std::vector<std::string> InputMethodManager::groups() const {
FCITX_D();
return {d->groupOrder_.begin(), d->groupOrder_.end()};
}
int InputMethodManager::groupCount() const {
FCITX_D();
return d->groups_.size();
}
void InputMethodManager::setCurrentGroup(const std::string &groupName) {
FCITX_D();
if (groupName == d->groupOrder_.front()) {
return;
}
auto iter =
std::find(d->groupOrder_.begin(), d->groupOrder_.end(), groupName);
if (iter != d->groupOrder_.end()) {
emit<InputMethodManager::CurrentGroupAboutToChange>(
d->groupOrder_.front());
d->groupOrder_.splice(d->groupOrder_.begin(), d->groupOrder_, iter);
emit<InputMethodManager::CurrentGroupChanged>(groupName);
}
}
const InputMethodGroup &InputMethodManager::currentGroup() const {
FCITX_D();
return d->groups_.find(d->groupOrder_.front())->second;
}
void InputMethodManager::enumerateGroup(bool forward) {
FCITX_D();
if (groupCount() < 2) {
return;
}
emit<InputMethodManager::CurrentGroupAboutToChange>(d->groupOrder_.front());
if (forward) {
d->groupOrder_.splice(d->groupOrder_.end(), d->groupOrder_,
d->groupOrder_.begin());
} else {
d->groupOrder_.splice(d->groupOrder_.begin(), d->groupOrder_,
std::prev(d->groupOrder_.end()));
}
emit<InputMethodManager::CurrentGroupChanged>(d->groupOrder_.front());
}
void InputMethodManager::setDefaultInputMethod(const std::string &name) {
FCITX_D();
auto ¤tGroup = d->groups_.find(d->groupOrder_.front())->second;
currentGroup.setDefaultInputMethod(name);
}
const InputMethodGroup *
InputMethodManager::group(const std::string &name) const {
FCITX_D();
return findValue(d->groups_, name);
}
void InputMethodManager::setGroup(InputMethodGroup newGroupInfo) {
FCITX_D();
auto *group = findValue(d->groups_, newGroupInfo.name());
if (!group) {
return;
}
bool isCurrent = false;
if (!d->buildingGroup_) {
isCurrent = (group == ¤tGroup());
if (isCurrent) {
emit<InputMethodManager::CurrentGroupAboutToChange>(
d->groupOrder_.front());
}
}
auto &list = newGroupInfo.inputMethodList();
auto iter = std::remove_if(list.begin(), list.end(),
[d](const InputMethodGroupItem &item) {
return !d->entries_.count(item.name());
});
list.erase(iter, list.end());
newGroupInfo.setDefaultInputMethod(newGroupInfo.defaultInputMethod());
*group = std::move(newGroupInfo);
if (!d->buildingGroup_ && isCurrent) {
emit<InputMethodManager::CurrentGroupChanged>(d->groupOrder_.front());
}
}
void InputMethodManagerPrivate::setGroupOrder(
const std::vector<std::string> &groupOrder) {
groupOrder_.clear();
std::unordered_set<std::string> added;
for (const auto &groupName : groupOrder) {
if (groups_.count(groupName)) {
groupOrder_.push_back(groupName);
added.insert(groupName);
}
}
for (auto &p : groups_) {
if (!added.count(p.first)) {
groupOrder_.push_back(p.first);
}
}
assert(groupOrder_.size() == groups_.size());
}
void InputMethodManager::addEmptyGroup(const std::string &name) {
if (group(name)) {
return;
}
FCITX_D();
InputMethodGroup newGroup(name);
if (groupCount()) {
newGroup.setDefaultLayout(currentGroup().defaultLayout());
}
if (newGroup.defaultLayout().empty()) {
newGroup.setDefaultLayout("us");
}
d->groups_.emplace(name, std::move(newGroup));
d->groupOrder_.push_back(name);
if (!d->buildingGroup_) {
emit<InputMethodManager::GroupAdded>(name);
}
}
void InputMethodManager::removeGroup(const std::string &name) {
if (groupCount() == 1) {
return;
}
FCITX_D();
bool isCurrent = d->groupOrder_.front() == name;
auto iter = d->groups_.find(name);
if (iter != d->groups_.end()) {
if (isCurrent) {
emit<InputMethodManager::CurrentGroupAboutToChange>(
d->groupOrder_.front());
}
d->groups_.erase(iter);
d->groupOrder_.remove(name);
if (isCurrent) {
emit<InputMethodManager::CurrentGroupChanged>(
d->groupOrder_.front());
}
if (!d->buildingGroup_) {
emit<InputMethodManager::GroupRemoved>(name);
}
}
}
void InputMethodManager::save() {
FCITX_D();
InputMethodConfig config;
std::vector<InputMethodGroupConfig> groups;
config.groupOrder.setValue(
std::vector<std::string>{d->groupOrder_.begin(), d->groupOrder_.end()});
for (auto &p : d->groups_) {
auto &group = p.second;
groups.emplace_back();
auto &groupConfig = groups.back();
groupConfig.name.setValue(group.name());
groupConfig.defaultLayout.setValue(group.defaultLayout());
groupConfig.defaultLayoutOptions.setValue(group.defaultLayoutOptions());
groupConfig.defaultInputMethod.setValue(group.defaultInputMethod());
std::vector<InputMethodGroupItemConfig> itemsConfig;
for (auto &item : group.inputMethodList()) {
itemsConfig.emplace_back();
auto &itemConfig = itemsConfig.back();
itemConfig.name.setValue(item.name());
itemConfig.layout.setValue(item.layout());
}
groupConfig.items.setValue(std::move(itemsConfig));
}
config.groups.setValue(std::move(groups));
safeSaveAsIni(config, "profile");
}
const InputMethodEntry *
InputMethodManager::entry(const std::string &name) const {
FCITX_D();
return findValue(d->entries_, name);
}
bool InputMethodManager::foreachEntries(
const std::function<bool(const InputMethodEntry &entry)> &callback) {
FCITX_D();
for (auto &p : d->entries_) {
if (!callback(p.second)) {
return false;
}
}
return true;
}
void InputMethodManager::setGroupOrder(const std::vector<std::string> &groups) {
FCITX_D();
if (!d->buildingGroup_) {
throw std::runtime_error("Called not within building group");
}
d->setGroupOrder(groups);
}
bool InputMethodManager::checkUpdate() const {
FCITX_D();
auto timestamp = StandardPath::global().timestamp(
StandardPath::Type::PkgData, "inputmethod");
return timestamp > d->timestamp_;
}
} // namespace fcitx