1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SERVICES_INCLUDE_SYS_CFG_PARSE_H
17 #define SERVICES_INCLUDE_SYS_CFG_PARSE_H
18 
19 #include <map>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "input_method_utils.h"
25 #include "serializable.h"
26 namespace OHOS {
27 namespace MiscServices {
28 struct SystemConfig : public Serializable {
29     std::string systemInputMethodConfigAbility;
30     std::string defaultInputMethod;
31     bool enableInputMethodFeature = false;
32     bool enableFullExperienceFeature = false;
UnmarshalSystemConfig33     bool Unmarshal(cJSON *node) override
34     {
35         GetValue(node, GET_NAME(systemInputMethodConfigAbility), systemInputMethodConfigAbility);
36         GetValue(node, GET_NAME(defaultInputMethod), defaultInputMethod);
37         GetValue(node, GET_NAME(enableInputMethodFeature), enableInputMethodFeature);
38         GetValue(node, GET_NAME(enableFullExperienceFeature), enableFullExperienceFeature);
39         return true;
40     }
41 };
42 struct ImeSystemConfig : public Serializable {
43     SystemConfig systemConfig;
UnmarshalImeSystemConfig44     bool Unmarshal(cJSON *node) override
45     {
46         return GetValue(node, GET_NAME(systemConfig), systemConfig);
47     }
48 };
49 
50 struct InputTypeInfo : public Serializable {
51     InputType type{ InputType::NONE };
52     std::string bundleName;
53     std::string subName;
UnmarshalInputTypeInfo54     bool Unmarshal(cJSON *node) override
55     {
56         int32_t typeTemp = -1;
57         auto ret = GetValue(node, GET_NAME(inputType), typeTemp);
58         if (typeTemp <= static_cast<int32_t>(InputType::NONE) || typeTemp >= static_cast<int32_t>(InputType::END)) {
59             return false;
60         }
61         type = static_cast<InputType>(typeTemp);
62         ret = GetValue(node, GET_NAME(bundleName), bundleName) && ret;
63         ret = GetValue(node, GET_NAME(subtypeId), subName) && ret;
64         return ret;
65     }
66 };
67 struct InputTypeCfg : public Serializable {
68     std::vector<InputTypeInfo> inputType;
UnmarshalInputTypeCfg69     bool Unmarshal(cJSON *node) override
70     {
71         return GetValue(node, GET_NAME(supportedInputTypeList), inputType);
72     }
73 };
74 
75 struct SysPanelAdjust : public Serializable {
76     std::vector<std::string> style;
77     int32_t top = 0;
78     int32_t left = 0;
79     int32_t right = 0;
80     int32_t bottom = 0;
UnmarshalSysPanelAdjust81     bool Unmarshal(cJSON *node) override
82     {
83         auto ret = GetValue(node, GET_NAME(style), style);
84         ret = GetValue(node, GET_NAME(top), top) && ret;
85         ret = GetValue(node, GET_NAME(left), left) && ret;
86         ret = GetValue(node, GET_NAME(right), right) && ret;
87         ret = GetValue(node, GET_NAME(bottom), bottom) && ret;
88         return ret;
89     }
90 };
91 
92 struct SysPanelAdjustCfg : public Serializable {
93     std::vector<SysPanelAdjust> panelAdjust;
UnmarshalSysPanelAdjustCfg94     bool Unmarshal(cJSON *node) override
95     {
96         return GetValue(node, GET_NAME(sysPanelAdjust), panelAdjust);
97     }
98 };
99 
100 struct DefaultFullImeInfo : public Serializable {
101     std::string appId;
102     std::string expirationTime;
103     uint32_t expirationVersionCode{ 0 };
UnmarshalDefaultFullImeInfo104     bool Unmarshal(cJSON *node) override
105     {
106         bool ret = GetValue(node, GET_NAME(appIdentifier), appId);
107         ret &= GetValue(node, GET_NAME(expirationTime), expirationTime);
108         GetValue(node, GET_NAME(expirationVersionCode), expirationVersionCode);
109         return ret;
110     }
111 };
112 
113 struct DefaultFullImeCfg : Serializable {
114     std::vector<DefaultFullImeInfo> defaultFullImeList;
UnmarshalDefaultFullImeCfg115     bool Unmarshal(cJSON *node) override
116     {
117         return GetValue(node, GET_NAME(defaultFullImeList), defaultFullImeList);
118     }
119 };
120 
121 class SysCfgParser {
122 public:
123     static bool ParseSystemConfig(SystemConfig &systemConfig);
124     static bool ParseInputType(std::vector<InputTypeInfo> &inputType);
125     static bool ParsePanelAdjust(std::vector<SysPanelAdjust> &sysPanelAdjust);
126     static bool ParseDefaultFullIme(std::vector<DefaultFullImeInfo> &defaultFullImeList);
127 
128 private:
129     static constexpr const char *SYS_CFG_FILE_PATH = "etc/inputmethod/inputmethod_framework_config.json";
130     static std::string GetSysCfgContent(const std::string &key);
131 };
132 } // namespace MiscServices
133 } // namespace OHOS
134 #endif // SERVICES_INCLUDE_SYS_CFG_PARSE_H
135