1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _TABLE_TABLE_H_
8 #define _TABLE_TABLE_H_
9 
10 #include "ime.h"
11 #include <fcitx-config/configuration.h>
12 #include <fcitx-config/iniparser.h>
13 #include <fcitx/addonfactory.h>
14 #include <fcitx/addonmanager.h>
15 #include <fcitx/inputcontextproperty.h>
16 #include <fcitx/inputmethodengine.h>
17 #include <fcitx/instance.h>
18 #include <libime/pinyin/pinyindictionary.h>
19 #include <memory>
20 
21 namespace fcitx {
22 
23 class TableState;
24 
25 FCITX_CONFIGURATION(TableGlobalConfig,
26                     KeyListOption modifyDictionary{this,
27                                                    "ModifyDictionaryKey",
28                                                    _("Modify dictionary"),
29                                                    {Key("Control+8")},
30                                                    KeyListConstrain()};
31                     KeyListOption forgetWord{this,
32                                              "ForgetWord",
33                                              _("Forget word"),
34                                              {Key("Control+7")},
35                                              KeyListConstrain()};
36                     KeyListOption lookupPinyin{this,
37                                                "LookupPinyinKey",
38                                                _("Lookup pinyin"),
39                                                {Key("Control+Alt+E")},
40                                                KeyListConstrain()};);
41 
42 class TableEngine final : public InputMethodEngine {
43 public:
44     TableEngine(Instance *instance);
45     ~TableEngine();
instance()46     Instance *instance() { return instance_; }
47     void activate(const InputMethodEntry &entry,
48                   InputContextEvent &event) override;
49     void deactivate(const InputMethodEntry &entry,
50                     InputContextEvent &event) override;
51     void keyEvent(const InputMethodEntry &entry, KeyEvent &keyEvent) override;
52     std::string subMode(const InputMethodEntry &entry,
53                         InputContext &ic) override;
54     void reloadConfig() override;
55     void reset(const InputMethodEntry &entry,
56                InputContextEvent &event) override;
57     void save() override;
factory()58     auto &factory() { return factory_; }
59 
ime()60     TableIME *ime() { return ime_.get(); }
config()61     auto &config() { return config_; }
getConfig()62     const Configuration *getConfig() const override { return &config_; }
setConfig(const RawConfig & config)63     void setConfig(const RawConfig &config) override {
64         config_.load(config, true);
65         safeSaveAsIni(config_, "conf/table.conf");
66     }
67 
68     const Configuration *
69     getConfigForInputMethod(const InputMethodEntry &) const override;
70     void setConfigForInputMethod(const InputMethodEntry &,
71                                  const RawConfig &) override;
72 
73     const libime::PinyinDictionary &pinyinDict();
74     const libime::LanguageModel &pinyinModel();
75 
76     FCITX_ADDON_DEPENDENCY_LOADER(fullwidth, instance_->addonManager());
77     FCITX_ADDON_DEPENDENCY_LOADER(punctuation, instance_->addonManager());
78     FCITX_ADDON_DEPENDENCY_LOADER(quickphrase, instance_->addonManager());
79     FCITX_ADDON_DEPENDENCY_LOADER(pinyinhelper, instance_->addonManager());
80     FCITX_ADDON_DEPENDENCY_LOADER(chttrans, instance_->addonManager());
81 
82 private:
83     void cloudTableSelected(InputContext *inputContext,
84                             const std::string &selected,
85                             const std::string &word);
86 
87     Instance *instance_;
88     std::unique_ptr<TableIME> ime_;
89     std::vector<std::unique_ptr<HandlerTableEntry<EventHandler>>> events_;
90     FactoryFor<TableState> factory_;
91 
92     TableGlobalConfig config_;
93     libime::PinyinDictionary pinyinDict_;
94     bool pinyinLoaded_ = false;
95     std::unique_ptr<libime::LanguageModel> pinyinLM_;
96 };
97 
98 class TableEngineFactory : public AddonFactory {
99 public:
create(AddonManager * manager)100     AddonInstance *create(AddonManager *manager) override {
101         registerDomain("fcitx5-chinese-addons", FCITX_INSTALL_LOCALEDIR);
102         return new TableEngine(manager->instance());
103     }
104 };
105 } // namespace fcitx
106 
107 #endif // _TABLE_TABLE_H_
108