• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "input_method_setting.h"
17 #include "utils.h"
18 
19 namespace OHOS {
20 namespace MiscServices {
21     const std::u16string InputMethodSetting::CURRENT_INPUT_METHOD_TAG = u"imms_current_input_method";
22     const std::u16string InputMethodSetting::ENABLED_INPUT_METHODS_TAG = u"imms_enabled_input_methods";
23     const std::u16string InputMethodSetting::CURRENT_KEYBOARD_TYPE_TAG = u"imms_current_keyboard_type";
24     const std::u16string InputMethodSetting::CURRENT_SYS_KEYBOARD_TYPE_TAG = u"imms_current_sys_keyboard_type";
25     const std::u16string InputMethodSetting::SYSTEM_LOCALE_TAG = u"system_locales";
26 
27     /*! Constructor
28     */
InputMethodSetting()29     InputMethodSetting::InputMethodSetting()
30     {
31     }
32 
33     /*! Destructor
34     */
~InputMethodSetting()35     InputMethodSetting::~InputMethodSetting()
36     {
37         setting.clear();
38     }
39 
40     /*! Constructor
41     \param inputMethodSetting the source InputMethodSetting copied to this instance
42     */
InputMethodSetting(const InputMethodSetting & inputMethodSetting)43     InputMethodSetting::InputMethodSetting(const InputMethodSetting& inputMethodSetting)
44     {
45         setting = inputMethodSetting.setting;
46     }
47 
48     /*! operator=
49     \param inputMethodSetting the source InputMethodSetting copied to this instance
50     \return return this instance
51     */
operator =(const InputMethodSetting & inputMethodSetting)52     InputMethodSetting& InputMethodSetting::operator =(const InputMethodSetting& inputMethodSetting)
53     {
54         if (this == &inputMethodSetting) {
55             return *this;
56         }
57         setting = inputMethodSetting.setting;
58         return *this;
59     }
60 
61     /*! Set setting data for an item
62     \param key the name of setting item
63     \param value the value of setting item
64     */
SetValue(const std::u16string & key,const std::u16string & value)65     void InputMethodSetting::SetValue(const std::u16string& key, const std::u16string& value)
66     {
67         setting.insert_or_assign(key, value);
68     }
69 
70     /*! Get setting data for an item
71     \param key the name of setting item
72     \return the value of setting item if key is found
73     \return an empty string if key is not found
74     */
GetValue(const std::u16string & key) const75     std::u16string InputMethodSetting::GetValue(const std::u16string& key) const
76     {
77         std::map<std::u16string, std::u16string>::const_iterator it = setting.find(key);
78         if (it == setting.end()) {
79             return u"";
80         }
81         return it->second;
82     }
83 
84     /*! Get the default input method engine
85     \return the imeId of the default input method engine
86     */
GetCurrentInputMethod() const87     std::u16string InputMethodSetting::GetCurrentInputMethod() const
88     {
89         return GetValue(CURRENT_INPUT_METHOD_TAG);
90     }
91 
92     /*! Set the default input method engine
93     \param imeId the ime Id of the given input method engine
94     */
SetCurrentInputMethod(const std::u16string & imeId)95     void InputMethodSetting::SetCurrentInputMethod(const std::u16string& imeId)
96     {
97         SetValue(CURRENT_INPUT_METHOD_TAG, imeId);
98     }
99 
100     /*! Get enabled input method engine list
101     \return a vector of ImeId
102     */
GetEnabledInputMethodList()103     std::vector<std::u16string> InputMethodSetting::GetEnabledInputMethodList()
104     {
105         std::u16string value = GetValue(ENABLED_INPUT_METHODS_TAG);
106         std::vector<std::u16string> tmp1 = Split(value, DELIM_IME);
107         std::vector<std::u16string> imeList;
108         for (int i = 0; i < (int)tmp1.size(); i++) {
109             std::vector<std::u16string> tmp2 = Split(tmp1[i], DELIM_KBD_TYPE);
110             imeList.push_back(tmp2[0]);
111             tmp2.clear();
112         }
113         return imeList;
114     }
115 
116     /*! Add an input method engine to enabled ime list
117     \param imeId the ime id of the added input method engine
118     \param types a vector of hashCode of keyboard types which are supported by the added input method engine
119     \return true - added successfully.
120     \return false - the given input method engine is already enabled.
121     */
AddEnabledInputMethod(const std::u16string & imeId,const std::vector<int32_t> & types)122     bool InputMethodSetting::AddEnabledInputMethod(const std::u16string& imeId, const std::vector<int32_t>& types)
123     {
124         std::u16string str = GetValue(ENABLED_INPUT_METHODS_TAG);
125         std::vector<std::u16string> imeList = Split(str, DELIM_IME);
126 
127         std::u16string typeStr;
128         for (int i = 0; i < (int)types.size(); i++) {
129             typeStr = typeStr + u";" + Utils::to_utf16(std::to_string(types[i]));
130         }
131         std::u16string imeStr = imeId + typeStr;
132         bool flag = false;
133         for (int i = 0; i < (int)imeList.size(); i++) {
134             if (imeList[i] == imeStr) {
135                 return false;
136             }
137             if (imeList[i].find_first_of(imeId)) {
138                 imeList[i] = imeStr;
139                 flag = true;
140                 break;
141             }
142         }
143         if (flag == false) {
144             imeList.push_back(imeStr);
145         }
146 
147         std::u16string value = BuildString(imeList, DELIM_IME);
148         SetValue(ENABLED_INPUT_METHODS_TAG, value);
149         imeList.clear();
150         return true;
151     }
152 
153     /*! Remove an input method engine from enabled ime list.
154     \param imeId the ime id of the given input method engine
155     \return true - removed successfully.
156     \return false - ime id is not found in enabled ime list.
157     */
RemoveEnabledInputMethod(const std::u16string & imeId)158     bool InputMethodSetting::RemoveEnabledInputMethod(const std::u16string& imeId)
159     {
160         std::u16string str = GetValue(ENABLED_INPUT_METHODS_TAG);
161         std::vector<std::u16string> imeList = Split(str, DELIM_IME);
162         bool flag = false;
163         std::vector<std::u16string>::iterator it;
164         for (it = imeList.begin(); it < imeList.end(); ++it) {
165             if (it->find_first_of(imeId)) {
166                 imeList.erase(it);
167                 flag = true;
168                 break;
169             }
170         }
171         if (flag == true) {
172             std::u16string value = BuildString(imeList, DELIM_IME);
173             SetValue(ENABLED_INPUT_METHODS_TAG, value);
174         }
175         imeList.clear();
176         return flag;
177     }
178 
179     /*! Get the keyboard type list of the given input method engine
180     \param imeId the ime id of the given input method engine
181     \return a vector of hashCodes
182     */
GetEnabledKeyboardTypes(const std::u16string & imeId)183     std::vector<int32_t> InputMethodSetting::GetEnabledKeyboardTypes(const std::u16string& imeId)
184     {
185         std::vector<int32_t> retValue;
186         std::u16string value = GetValue(ENABLED_INPUT_METHODS_TAG);
187         std::vector<std::u16string> tmpVector = Split(value, DELIM_IME);
188         bool flag = false;
189         std::u16string imeStr;
190         for (int i = 0; i < (int)tmpVector.size(); i++) {
191             if (tmpVector[i].find_first_of(imeId) != std::u16string::npos) {
192                 flag = true;
193                 imeStr = tmpVector[i];
194                 break;
195             }
196         }
197         tmpVector.clear();
198         if (flag == false) {
199             return retValue;
200         }
201 
202         std::vector<std::u16string> tmp2 = Split(imeStr, DELIM_KBD_TYPE);
203         for (int i = 1; i < (int)tmp2.size(); i++) {
204             std::u16string str = tmp2[i];
205             retValue.push_back(std::atoi(Utils::to_utf8(str).c_str()));
206         }
207         tmp2.clear();
208 
209         return retValue;
210     }
211 
212     /*! Get the default keyboard type
213     \return return the hashCode value of the default keyboard type.
214     */
GetCurrentKeyboardType()215     int32_t InputMethodSetting::GetCurrentKeyboardType()
216     {
217         std::u16string value = GetValue(CURRENT_KEYBOARD_TYPE_TAG);
218         return std::atoi(Utils::to_utf8(value).c_str());
219     }
220 
221     /*! Set the default keyboard type
222     \param type hashCode of the given keyboard type
223     */
SetCurrentKeyboardType(int32_t type)224     void InputMethodSetting::SetCurrentKeyboardType(int32_t type)
225     {
226         std::u16string str = Utils::to_utf16(std::to_string(type));
227         SetValue(CURRENT_KEYBOARD_TYPE_TAG, str);
228     }
229 
230     /*! Get the default keyboard type for security input method engine
231     \return return hashCode of the default keyboard type of security IME
232     */
GetCurrentSysKeyboardType()233     int32_t InputMethodSetting::GetCurrentSysKeyboardType()
234     {
235         std::u16string value = GetValue(CURRENT_SYS_KEYBOARD_TYPE_TAG);
236         return std::atoi(Utils::to_utf8(value).c_str());
237     }
238 
239     /*! Set the default keyboard type for security IME
240     \param type the hashCode of the given keyboard type of security IME
241     */
SetCurrentSysKeyboardType(int32_t type)242     void InputMethodSetting::SetCurrentSysKeyboardType(int32_t type)
243     {
244         std::u16string str = Utils::to_utf16(std::to_string(type));
245         SetValue(CURRENT_SYS_KEYBOARD_TYPE_TAG, std::u16string(str));
246     }
247 
248     /*! Clear setting data
249     */
ClearData()250     void InputMethodSetting::ClearData()
251     {
252         setting.clear();
253     }
254 
255     /*! Find if the key is in the setting
256     \param key the name of setting item
257     \return true - key is found
258     \return false - key is not found
259     */
FindKey(const std::u16string & key) const260     bool InputMethodSetting::FindKey(const std::u16string& key) const
261     {
262         std::map<std::u16string, std::u16string>::const_iterator it = setting.find(key);
263         if (it == setting.end()) {
264             return false;
265         }
266         return true;
267     }
268 
269     /*! Split a string into a vector
270     \param str the string to be split
271     \param delim the alphabet to split the string.
272     \return a vector of string
273     */
Split(const std::u16string & str,char16_t delim)274     std::vector<std::u16string> InputMethodSetting::Split(const std::u16string& str, char16_t delim)
275     {
276         std::vector<std::u16string> retValue;
277         std::u16string::size_type left, right;
278         left = 0;
279         right = str.find(delim, 0);
280         while (right != std::u16string::npos) {
281             if (right - left) {
282                 retValue.emplace_back(str.substr(left, right - left));
283             }
284             left = right + 1;
285             right = str.find(delim, left);
286         }
287 
288         if (left != str.size()) {
289             retValue.emplace_back(str.substr(left));
290         }
291         return retValue;
292     }
293 
294     /*! Build a string from a vector
295     \param vector a vector of string
296     \delim a separator
297     \return return a string
298     */
BuildString(const std::vector<std::u16string> & vector,char16_t delim)299     std::u16string InputMethodSetting::BuildString(const std::vector<std::u16string>& vector, char16_t delim)
300     {
301         std::u16string retValue = u"";
302         char16_t delimStr[] = {delim, 0};
303         for (int i = 0; i < (int)vector.size(); i++) {
304             retValue += vector[i];
305             if (i < (int)vector.size()-1) {
306                 retValue += std::u16string(delimStr);
307             }
308         }
309         return retValue;
310     }
311 }
312 }
313