Debian Codegraph @

modules/chttrans/chttrans.h

raw
1 /*
2 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7 #ifndef _CHTTRANS_CHTTRANS_H_
8 #define _CHTTRANS_CHTTRANS_H_
9
10 #include "config.h"
11 #include "notifications_public.h"
12 #include <fcitx-config/configuration.h>
13 #include <fcitx-config/enum.h>
14 #include <fcitx-config/iniparser.h>
15 #include <fcitx-utils/i18n.h>
16 #include <fcitx-utils/standardpaths.h>
17 #include <fcitx/action.h>
18 #include <fcitx/addoninstance.h>
19 #include <fcitx/addonmanager.h>
20 #include <fcitx/instance.h>
21 #include <unordered_set>
22
23 #ifdef ENABLE_OPENCC
24 struct OpenCCAnnotation : public fcitx::EnumAnnotation {
25 void
26 setProfiles(std::vector<std::pair<std::string, std::string>> profiles) {
27 profiles_ = std::move(profiles);
28 }
29 void dumpDescription(fcitx::RawConfig &config) const {
30 fcitx::EnumAnnotation::dumpDescription(config);
31 for (size_t i = 0; i < profiles_.size(); i++) {
32 config.setValueByPath("Enum/" + std::to_string(i),
33 profiles_[i].first);
34 config.setValueByPath("EnumI18n/" + std::to_string(i),
35 profiles_[i].second);
36 }
37 }
38
39 private:
40 std::vector<std::pair<std::string, std::string>> profiles_;
41 };
42 #endif
43
44 FCITX_CONFIG_ENUM(ChttransEngine, Native, OpenCC);
45
46 FCITX_CONFIGURATION(
47 ChttransConfig,
48 #ifdef ENABLE_OPENCC
49 fcitx::Option<ChttransEngine> engine{this, "Engine", _("Translate engine"),
50 ChttransEngine::OpenCC};
51 #endif
52 fcitx::Option<fcitx::KeyList> hotkey{
53 this, "Hotkey", _("Toggle key"), {fcitx::Key("Control+Shift+F")}};
54 fcitx::HiddenOption<std::vector<std::string>> enabledIM{
55 this, "EnabledIM", _("Enabled Input Methods")};
56 #ifdef ENABLE_OPENCC
57 fcitx::Option<std::string, fcitx::NoConstrain<std::string>,
58 fcitx::DefaultMarshaller<std::string>, OpenCCAnnotation>
59 openCCS2TProfile{this, "OpenCCS2TProfile",
60 _("OpenCC profile for Simplified to Traditional"),
61 "default"};
62 fcitx::Option<std::string, fcitx::NoConstrain<std::string>,
63 fcitx::DefaultMarshaller<std::string>, OpenCCAnnotation>
64 openCCT2SProfile{this, "OpenCCT2SProfile",
65 _("OpenCC profile for Traditional to Simplified"),
66 "default"};
67 #endif
68 );
69
70 enum class ChttransIMType { Simp, Trad, Other };
71
72 class ChttransBackend {
73 public:
74 virtual ~ChttransBackend() {}
75 bool load(const ChttransConfig &config) {
76 if (!loaded_) {
77 loadResult_ = loadOnce(config);
78 loaded_ = true;
79 }
80 return loadResult_;
81 }
82 virtual std::string convertSimpToTrad(const std::string &) = 0;
83 virtual std::string convertTradToSimp(const std::string &) = 0;
84 bool loaded() { return loaded_ && loadResult_; }
85
86 virtual void updateConfig(const ChttransConfig &) {}
87
88 protected:
89 virtual bool loadOnce(const ChttransConfig &) = 0;
90
91 private:
92 bool loaded_ = false;
93 bool loadResult_ = false;
94 };
95
96 class Chttrans final : public fcitx::AddonInstance {
97 class ToggleAction : public fcitx::Action {
98 public:
99 ToggleAction(Chttrans *parent) : parent_(parent) {}
100
101 std::string shortText(fcitx::InputContext *ic) const override {
102 return parent_->currentType(ic) == ChttransIMType::Trad
103 ? _("Traditional Chinese")
104 : _("Simplified Chinese");
105 }
106 std::string icon(fcitx::InputContext *ic) const override {
107 return parent_->currentType(ic) == ChttransIMType::Trad
108 ? "fcitx-chttrans-active"
109 : "fcitx-chttrans-inactive";
110 }
111
112 void activate(fcitx::InputContext *ic) override {
113 return parent_->toggle(ic);
114 }
115
116 private:
117 Chttrans *parent_;
118 };
119
120 public:
121 Chttrans(fcitx::Instance *instance);
122
123 void reloadConfig() override;
124 void save() override;
125 const fcitx::Configuration *getConfig() const override;
126 void setConfig(const fcitx::RawConfig &config) override {
127 config_.load(config, true);
128 fcitx::safeSaveAsIni(config_, "conf/chttrans.conf");
129 populateConfig();
130 }
131 void populateConfig();
132
133 // The conversion type.
134 ChttransIMType convertType(fcitx::InputContext *inputContext) const;
135
136 // Input method type.
137 ChttransIMType inputMethodType(fcitx::InputContext *inputContext) const;
138
139 // The actual language consider both input method & conversion.
140 ChttransIMType currentType(fcitx::InputContext *inputContext) const;
141 std::string convert(ChttransIMType type, const std::string &str);
142 void toggle(fcitx::InputContext *ic);
143
144 FCITX_ADDON_DEPENDENCY_LOADER(notifications, instance_->addonManager());
145
146 private:
147 void syncToConfig();
148
149 fcitx::Instance *instance_;
150 ChttransConfig config_;
151 std::unique_ptr<fcitx::HandlerTableEntry<fcitx::EventHandler>>
152 eventHandler_;
153 std::unordered_map<ChttransEngine, std::unique_ptr<ChttransBackend>,
154 fcitx::EnumHash>
155 backends_;
156 ChttransBackend *currentBackend_ = nullptr;
157 std::unordered_set<std::string> enabledIM_;
158 fcitx::ScopedConnection outputFilterConn_;
159 fcitx::ScopedConnection commitFilterConn_;
160 ToggleAction toggleAction_{this};
161 };
162
163 const fcitx::StandardPaths &openCCStandardPath();
164
165 #endif // _CHTTRANS_CHTTRANS_H_