librime: levers_api_impl.h Source File

| librime 1.2 Rime Input Method Engine, the core library

levers_api_impl.h
Go to the documentation of this file.
1// implementation of levers api
2
3#include <rime_levers_api.h>
4
5#include <rime/service.h>
6#include <rime/lever/custom_settings.h>
7#include <rime/lever/switcher_settings.h>
8#include <rime/lever/user_dict_manager.h>
9
10using namespace rime;
11
12static RimeCustomSettings* rime_levers_custom_settings_init(
13 const char* config_id,
14 const char* generator_id) {
15 return reinterpret_cast<RimeCustomSettings*>(new CustomSettings(
16 &Service::instance().deployer(), config_id, generator_id));
17}
18
19static void rime_levers_custom_settings_destroy(RimeCustomSettings* settings) {
20 delete reinterpret_cast<CustomSettings*>(settings);
21}
22
23static Bool rime_levers_load_settings(RimeCustomSettings* settings) {
24 return Bool(reinterpret_cast<CustomSettings*>(settings)->Load());
25}
26
27static Bool rime_levers_save_settings(RimeCustomSettings* settings) {
28 return Bool(reinterpret_cast<CustomSettings*>(settings)->Save());
29}
30
31static Bool rime_levers_customize_bool(RimeCustomSettings* settings,
32 const char* key,
33 Bool value) {
34 an<ConfigItem> item = New<ConfigValue>(bool(value));
35 auto custom_settings = reinterpret_cast<CustomSettings*>(settings);
36 return custom_settings->Customize(key, item);
37}
38
39static Bool rime_levers_customize_int(RimeCustomSettings* settings,
40 const char* key,
41 int value) {
42 an<ConfigItem> item = New<ConfigValue>(value);
43 auto custom_settings = reinterpret_cast<CustomSettings*>(settings);
44 return custom_settings->Customize(key, item);
45}
46
47static Bool rime_levers_customize_double(RimeCustomSettings* settings,
48 const char* key,
49 double value) {
50 an<ConfigItem> item = New<ConfigValue>(value);
51 auto custom_settings = reinterpret_cast<CustomSettings*>(settings);
52 return custom_settings->Customize(key, item);
53}
54
55static Bool rime_levers_customize_string(RimeCustomSettings* settings,
56 const char* key,
57 const char* value) {
58 an<ConfigItem> item = New<ConfigValue>(value);
59 auto custom_settings = reinterpret_cast<CustomSettings*>(settings);
60 return custom_settings->Customize(key, item);
61}
62
63static Bool rime_levers_customize_item(RimeCustomSettings* settings,
64 const char* key,
65 RimeConfig* value) {
66 an<ConfigItem> item;
67 if (value) {
68 if (Config* v = reinterpret_cast<Config*>(value->ptr)) {
69 item = v->GetItem("");
70 }
71 }
72 auto custom_settings = reinterpret_cast<CustomSettings*>(settings);
73 return custom_settings->Customize(key, item);
74}
75
76static Bool rime_levers_is_first_run(RimeCustomSettings* settings) {
77 return reinterpret_cast<CustomSettings*>(settings)->IsFirstRun();
78}
79
80static Bool rime_levers_settings_is_modified(RimeCustomSettings* settings) {
81 return reinterpret_cast<CustomSettings*>(settings)->modified();
82}
83
84static Bool rime_levers_settings_get_config(RimeCustomSettings* settings,
85 RimeConfig* config) {
86 if (!config)
87 return False;
88 config->ptr = reinterpret_cast<CustomSettings*>(settings)->config();
89 return Bool(!!config->ptr);
90}
91
92static RimeSwitcherSettings* rime_levers_switcher_settings_init() {
93 return reinterpret_cast<RimeSwitcherSettings*>(
94 new SwitcherSettings(&Service::instance().deployer()));
95}
96
97static Bool rime_levers_get_available_schema_list(
98 RimeSwitcherSettings* settings,
99 RimeSchemaList* list) {
100 auto ss = reinterpret_cast<SwitcherSettings*>(settings);
101 list->size = 0;
102 list->list = NULL;
103 if (ss->available().empty()) {
104 return False;
105 }
106 list->list = new RimeSchemaListItem[ss->available().size()];
107 for (const auto& info : ss->available()) {
108 auto& item(list->list[list->size]);
109 item.schema_id = const_cast<char*>(info.schema_id.c_str());
110 item.name = const_cast<char*>(info.name.c_str());
111 item.reserved = const_cast<SchemaInfo*>(&info);
112 ++list->size;
113 }
114 return True;
115}
116
117static Bool rime_levers_get_selected_schema_list(RimeSwitcherSettings* settings,
118 RimeSchemaList* list) {
119 auto ss = reinterpret_cast<SwitcherSettings*>(settings);
120 list->size = 0;
121 list->list = NULL;
122 if (ss->selection().empty()) {
123 return False;
124 }
125 list->list = new RimeSchemaListItem[ss->selection().size()];
126 for (const string& schema_id : ss->selection()) {
127 auto& item(list->list[list->size]);
128 item.schema_id = const_cast<char*>(schema_id.c_str());
129 item.name = NULL;
130 item.reserved = NULL;
131 ++list->size;
132 }
133 return True;
134}
135
136static void rime_levers_schema_list_destroy(RimeSchemaList* list) {
137 delete[] list->list;
138 list->size = 0;
139 list->list = NULL;
140}
141
142static const char* rime_levers_get_schema_id(RimeSchemaInfo* info) {
143 auto si = reinterpret_cast<SchemaInfo*>(info);
144 return si && !si->schema_id.empty() ? si->schema_id.c_str() : NULL;
145}
146
147static const char* rime_levers_get_schema_name(RimeSchemaInfo* info) {
148 auto si = reinterpret_cast<SchemaInfo*>(info);
149 return si && !si->name.empty() ? si->name.c_str() : NULL;
150}
151
152static const char* rime_levers_get_schema_version(RimeSchemaInfo* info) {
153 auto si = reinterpret_cast<SchemaInfo*>(info);
154 return si && !si->version.empty() ? si->version.c_str() : NULL;
155}
156static const char* rime_levers_get_schema_author(RimeSchemaInfo* info) {
157 auto si = reinterpret_cast<SchemaInfo*>(info);
158 return si && !si->author.empty() ? si->author.c_str() : NULL;
159}
160
161static const char* rime_levers_get_schema_description(RimeSchemaInfo* info) {
162 auto si = reinterpret_cast<SchemaInfo*>(info);
163 return si && !si->description.empty() ? si->description.c_str() : NULL;
164}
165
166static const char* rime_levers_get_schema_file_path(RimeSchemaInfo* info) {
167 auto si = reinterpret_cast<SchemaInfo*>(info);
168 return si && !si->file_path.empty() ? si->file_path.c_str() : NULL;
169}
170
171static Bool rime_levers_select_schemas(RimeSwitcherSettings* settings,
172 const char* schema_id_list[],
173 int count) {
174 auto ss = reinterpret_cast<SwitcherSettings*>(settings);
175 SwitcherSettings::Selection selection;
176 for (int i = 0; i < count; ++i) {
177 selection.push_back(schema_id_list[i]);
178 }
179 return ss->Select(selection);
180}
181
182static const char* rime_levers_get_hotkeys(RimeSwitcherSettings* settings) {
183 auto ss = reinterpret_cast<SwitcherSettings*>(settings);
184 return !ss->hotkeys().empty() ? ss->hotkeys().c_str() : NULL;
185}
186
187static Bool rime_levers_set_hotkeys(RimeSwitcherSettings* settings,
188 const char* hotkeys) {
189 auto ss = reinterpret_cast<SwitcherSettings*>(settings);
190 return Bool(ss->SetHotkeys(hotkeys));
191}
192
193static Bool rime_levers_user_dict_iterator_init(RimeUserDictIterator* iter) {
194 UserDictManager mgr(&Service::instance().deployer());
195 UserDictList* list = new UserDictList;
196 mgr.GetUserDictList(list);
197 if (list->empty()) {
198 delete list;
199 return False;
200 }
201 iter->ptr = list;
202 iter->i = 0;
203 return True;
204}
205
206static void rime_levers_user_dict_iterator_destroy(RimeUserDictIterator* iter) {
207 delete (UserDictList*)iter->ptr;
208 iter->ptr = NULL;
209 iter->i = 0;
210}
211
212static const char* rime_levers_next_user_dict(RimeUserDictIterator* iter) {
213 auto list = reinterpret_cast<UserDictList*>(iter->ptr);
214 if (!list || iter->i >= list->size()) {
215 return NULL;
216 }
217 return (*list)[iter->i++].c_str();
218}
219
220static Bool rime_levers_backup_user_dict(const char* dict_name) {
221 UserDictManager mgr(&Service::instance().deployer());
222 return Bool(mgr.Backup(dict_name));
223}
224
225static Bool rime_levers_restore_user_dict(const char* snapshot_file) {
226 UserDictManager mgr(&Service::instance().deployer());
227 return Bool(mgr.Restore(path(snapshot_file)));
228}
229
230static int rime_levers_export_user_dict(const char* dict_name,
231 const char* text_file) {
232 UserDictManager mgr(&Service::instance().deployer());
233 return mgr.Export(dict_name, path(text_file));
234}
235
236static int rime_levers_import_user_dict(const char* dict_name,
237 const char* text_file) {
238 UserDictManager mgr(&Service::instance().deployer());
239 return mgr.Import(dict_name, path(text_file));
240}
241
242static RimeCustomApi* RIME_FLAVORED(rime_levers_get_api)() {
243 static RIME_FLAVORED(RimeLeversApi) s_api = {0};
244 if (!s_api.data_size) {
245 RIME_STRUCT_INIT(RIME_FLAVORED(RimeLeversApi), s_api);
246 s_api.custom_settings_init = rime_levers_custom_settings_init;
247 s_api.custom_settings_destroy = rime_levers_custom_settings_destroy;
248 s_api.load_settings = rime_levers_load_settings;
249 s_api.save_settings = rime_levers_save_settings;
250 s_api.customize_bool = rime_levers_customize_bool;
251 s_api.customize_int = rime_levers_customize_int;
252 s_api.customize_double = rime_levers_customize_double;
253 s_api.customize_string = rime_levers_customize_string;
254 s_api.is_first_run = rime_levers_is_first_run;
255 s_api.settings_is_modified = rime_levers_settings_is_modified;
256 s_api.settings_get_config = rime_levers_settings_get_config;
257 s_api.switcher_settings_init = rime_levers_switcher_settings_init;
258 s_api.get_available_schema_list = rime_levers_get_available_schema_list;
259 s_api.get_selected_schema_list = rime_levers_get_selected_schema_list;
260 s_api.schema_list_destroy = rime_levers_schema_list_destroy;
261 s_api.get_schema_id = rime_levers_get_schema_id;
262 s_api.get_schema_name = rime_levers_get_schema_name;
263 s_api.get_schema_version = rime_levers_get_schema_version;
264 s_api.get_schema_author = rime_levers_get_schema_author;
265 s_api.get_schema_description = rime_levers_get_schema_description;
266 s_api.get_schema_file_path = rime_levers_get_schema_file_path;
267 s_api.select_schemas = rime_levers_select_schemas;
268 s_api.get_hotkeys = rime_levers_get_hotkeys;
269 s_api.set_hotkeys = rime_levers_set_hotkeys;
270 s_api.user_dict_iterator_init = rime_levers_user_dict_iterator_init;
271 s_api.user_dict_iterator_destroy = rime_levers_user_dict_iterator_destroy;
272 s_api.next_user_dict = rime_levers_next_user_dict;
273 s_api.backup_user_dict = rime_levers_backup_user_dict;
274 s_api.restore_user_dict = rime_levers_restore_user_dict;
275 s_api.export_user_dict = rime_levers_export_user_dict;
276 s_api.import_user_dict = rime_levers_import_user_dict;
277 s_api.customize_item = rime_levers_customize_item;
278 }
279 return (RimeCustomApi*)&s_api;
280}
service.h
custom_settings.h
user_dict_manager.h
switcher_settings.h
RimeSchemaListItem
struct rime_schema_list_item_t RimeSchemaListItem
False
#define False
Definition rime_api.h:50
RIME_STRUCT_INIT
#define RIME_STRUCT_INIT(Type, var)
Definition rime_api.h:60
RimeCustomApi
struct rime_custom_api_t RimeCustomApi
Extend the structure to publish custom data/functions in your specific module.
RimeSchemaList
struct rime_schema_list_t RimeSchemaList
RimeConfig
struct rime_config_t RimeConfig
Bool
#define Bool
Definition rime_api.h:46
RIME_FLAVORED
#define RIME_FLAVORED(name)
Definition rime_api.h:42
True
#define True
Definition rime_api.h:54
rime_levers_api.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::UserDictList
vector< string > UserDictList
Definition user_dict_manager.h:17
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::path
Definition common.h:84
rime::path::path
path()
Definition common.h:88
rime::Config
Definition config_component.h:21
rime::CustomSettings
Definition custom_settings.h:16
rime::CustomSettings::CustomSettings
CustomSettings(Deployer *deployer, const string &config_id, const string &generator_id)
Definition custom_settings.cc:25
rime::CustomSettings::Customize
bool Customize(const string &key, const an< ConfigItem > &item)
Definition custom_settings.cc:73
rime::SchemaInfo
Definition switcher_settings.h:15
rime::SchemaInfo::file_path
string file_path
Definition switcher_settings.h:21
rime::SchemaInfo::name
string name
Definition switcher_settings.h:17
rime::SchemaInfo::description
string description
Definition switcher_settings.h:20
rime::SchemaInfo::schema_id
string schema_id
Definition switcher_settings.h:16
rime::SchemaInfo::author
string author
Definition switcher_settings.h:19
rime::SchemaInfo::version
string version
Definition switcher_settings.h:18
rime::SwitcherSettings
Definition switcher_settings.h:24
rime::SwitcherSettings::Select
bool Select(Selection selection)
Definition switcher_settings.cc:33
rime::SwitcherSettings::Selection
vector< string > Selection
Definition switcher_settings.h:28
rime::SwitcherSettings::hotkeys
const string & hotkeys() const
Definition switcher_settings.h:37
rime::SwitcherSettings::SwitcherSettings
SwitcherSettings(Deployer *deployer)
Definition switcher_settings.cc:18
rime::UserDictManager
Definition user_dict_manager.h:19
rime::Service::instance
static Service & instance()
Definition service.cc:196
rime_config_t::ptr
void * ptr
Definition rime_api.h:189
RimeCustomSettings
Definition rime_levers_api.h:16
RimeSwitcherSettings
Definition rime_levers_api.h:20
RimeSchemaInfo
Definition rime_levers_api.h:24
RimeUserDictIterator
Definition rime_levers_api.h:28
RimeUserDictIterator::ptr
void * ptr
Definition rime_levers_api.h:29
RimeUserDictIterator::i
size_t i
Definition rime_levers_api.h:30