1 /*
2  * SPDX-FileCopyrightText: 2021~2021 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  */
7 #include "rimeservice.h"
8 #include "dbus_public.h"
9 #include "rimestate.h"
10 
11 namespace fcitx {
12 
RimeService(RimeEngine * engine)13 RimeService::RimeService(RimeEngine *engine) : engine_(engine) {
14     auto dbus = engine->dbus();
15     if (!dbus) {
16         return;
17     }
18     auto bus = dbus->call<IDBusModule::bus>();
19     bus->addObjectVTable("/rime", "org.fcitx.Fcitx.Rime1", *this);
20 }
21 
currentState()22 RimeState *RimeService::currentState() {
23     auto ic = engine_->instance()->mostRecentInputContext();
24     if (!ic) {
25         return nullptr;
26     }
27     return ic->propertyFor(&engine_->factory());
28 }
29 
setAsciiMode(bool ascii)30 void RimeService::setAsciiMode(bool ascii) {
31     if (auto state = currentState()) {
32         state->setLatinMode(ascii);
33         if (auto ic = engine_->instance()->mostRecentInputContext();
34             ic && ic->hasFocus()) {
35             engine_->instance()->showInputMethodInformation(ic);
36         }
37     }
38 }
39 
isAsciiMode()40 bool RimeService::isAsciiMode() {
41     if (auto state = currentState()) {
42         RIME_STRUCT(RimeStatus, status);
43         if (state->getStatus(&status)) {
44             return status.is_ascii_mode;
45         }
46     }
47     return false;
48 }
49 
setSchema(const std::string & schema)50 void RimeService::setSchema(const std::string &schema) {
51     if (auto state = currentState()) {
52         state->selectSchema(schema);
53         if (auto ic = engine_->instance()->mostRecentInputContext();
54             ic && ic->hasFocus()) {
55             engine_->instance()->showInputMethodInformation(ic);
56         }
57     }
58 }
59 
currentSchema()60 std::string RimeService::currentSchema() {
61     if (auto state = currentState()) {
62         RIME_STRUCT(RimeStatus, status);
63         if (state->getStatus(&status)) {
64             return status.schema_id ? status.schema_id : "";
65         }
66     }
67     return "";
68 }
69 
listAllSchemas()70 std::vector<std::string> RimeService::listAllSchemas() {
71     std::vector<std::string> schemas;
72     if (auto api = engine_->api()) {
73         RimeSchemaList list;
74         list.size = 0;
75         if (api->get_schema_list(&list)) {
76             for (size_t i = 0; i < list.size; i++) {
77                 schemas.emplace_back(list.list[i].schema_id);
78             }
79             api->free_schema_list(&list);
80         }
81     }
82     return schemas;
83 }
84 
85 } // namespace fcitx
86