1 /* 2 * Copyright (c) 2022 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_IME_CFG_MANAGER_H 17 #define SERVICES_INCLUDE_IME_CFG_MANAGER_H 18 19 #include <sys/types.h> 20 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 26 #include "nlohmann/json.hpp" 27 namespace OHOS { 28 namespace MiscServices { 29 struct ImePersistCfg { 30 static constexpr int32_t INVALID_USERID = -1; 31 int32_t userId{ INVALID_USERID }; 32 std::string currentIme; 33 std::string currentSubName; 34 }; 35 36 struct ImeNativeCfg { 37 std::string imeId; 38 std::string bundleName; 39 std::string subName; 40 std::string extName; 41 }; 42 43 class ImeCfgManager { 44 public: 45 static ImeCfgManager &GetInstance(); 46 void Init(); 47 void AddImeCfg(const ImePersistCfg &cfg); 48 void ModifyImeCfg(const ImePersistCfg &cfg); 49 void DeleteImeCfg(int32_t userId); 50 std::shared_ptr<ImeNativeCfg> GetCurrentImeCfg(int32_t userId); 51 52 private: 53 ImeCfgManager() = default; 54 ~ImeCfgManager() = default; 55 void ReadImeCfg(); 56 void WriteImeCfg(); 57 ImePersistCfg GetImeCfg(int32_t userId); 58 static int32_t Create(std::string &path, mode_t pathMode); 59 static bool IsExist(std::string &path); 60 static bool Read(const std::string &path, nlohmann::json &jsonCfg); 61 static bool Write(const std::string &path, const nlohmann::json &jsonCfg); FromJson(const nlohmann::json & jsonCfg,ImePersistCfg & cfg)62 inline static void FromJson(const nlohmann::json &jsonCfg, ImePersistCfg &cfg) 63 { 64 if (jsonCfg.find("userId") != jsonCfg.end() && jsonCfg["userId"].is_number()) { 65 jsonCfg.at("userId").get_to(cfg.userId); 66 } 67 if (jsonCfg.find("currentIme") != jsonCfg.end() && jsonCfg["currentIme"].is_string()) { 68 jsonCfg.at("currentIme").get_to(cfg.currentIme); 69 } 70 if (jsonCfg.find("currentSubName") != jsonCfg.end() && jsonCfg["currentSubName"].is_string()) { 71 jsonCfg.at("currentSubName").get_to(cfg.currentSubName); 72 } 73 } ToJson(nlohmann::json & jsonCfg,const ImePersistCfg & cfg)74 inline static void ToJson(nlohmann::json &jsonCfg, const ImePersistCfg &cfg) 75 { 76 jsonCfg = nlohmann::json{ { "userId", cfg.userId }, { "currentIme", cfg.currentIme }, 77 { "currentSubName", cfg.currentSubName } }; 78 } 79 static void FromJson(const nlohmann::json &jsonConfigs, std::vector<ImePersistCfg> &configs); 80 static void ToJson(nlohmann::json &jsonConfigs, const std::vector<ImePersistCfg> &configs); 81 std::recursive_mutex imeCfgLock_; 82 std::vector<ImePersistCfg> imeConfigs_; 83 }; 84 } // namespace MiscServices 85 } // namespace OHOS 86 #endif // SERVICES_INCLUDE_IME_CFG_MANAGER_H 87