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 int32_t userId; 31 std::string currentIme; 32 std::string currentSubName; 33 }; 34 35 struct ImeNativeCfg { 36 std::string imeId; 37 std::string bundleName; 38 std::string subName; 39 std::string extName; 40 }; 41 42 class ImeCfgManager { 43 public: 44 static ImeCfgManager &GetInstance(); 45 void Init(); 46 void AddImeCfg(const ImePersistCfg &cfg); 47 void ModifyImeCfg(const ImePersistCfg &cfg); 48 void DeleteImeCfg(int32_t userId); 49 std::shared_ptr<ImeNativeCfg> GetCurrentImeCfg(int32_t userId); 50 51 private: 52 ImeCfgManager() = default; 53 ~ImeCfgManager() = default; 54 void ReadImeCfgFile(); 55 void WriteImeCfgFile(); 56 ImePersistCfg GetImeCfg(int32_t userId); 57 static int32_t CreateCachePath(std::string &path, mode_t pathMode); 58 static bool IsCachePathExit(std::string &path); 59 static bool ReadCacheFile(const std::string &path, nlohmann::json &jsonCfg); 60 static bool WriteCacheFile(const std::string &path, const nlohmann::json &jsonCfg); FromJson(const nlohmann::json & jsonCfg,ImePersistCfg & cfg)61 inline static void FromJson(const nlohmann::json &jsonCfg, ImePersistCfg &cfg) 62 { 63 if (jsonCfg.find("userId") != jsonCfg.end() && jsonCfg["userId"].is_number()) { 64 jsonCfg.at("userId").get_to(cfg.userId); 65 } 66 if (jsonCfg.find("currentIme") != jsonCfg.end() && jsonCfg["currentIme"].is_string()) { 67 jsonCfg.at("currentIme").get_to(cfg.currentIme); 68 } 69 if (jsonCfg.find("currentSubName") != jsonCfg.end() && jsonCfg["currentSubName"].is_string()) { 70 jsonCfg.at("currentSubName").get_to(cfg.currentSubName); 71 } 72 } ToJson(nlohmann::json & jsonCfg,const ImePersistCfg & cfg)73 inline static void ToJson(nlohmann::json &jsonCfg, const ImePersistCfg &cfg) 74 { 75 jsonCfg = nlohmann::json{ { "userId", cfg.userId }, { "currentIme", cfg.currentIme }, 76 { "currentSubName", cfg.currentSubName } }; 77 } 78 static void FromJson(const nlohmann::json &jsonConfigs, std::vector<ImePersistCfg> &configs); 79 static void ToJson(nlohmann::json &jsonConfigs, const std::vector<ImePersistCfg> &configs); 80 std::recursive_mutex imeCfgLock_; 81 std::vector<ImePersistCfg> imeConfigs_; 82 }; 83 } // namespace MiscServices 84 } // namespace OHOS 85 #endif // SERVICES_INCLUDE_IME_CFG_MANAGER_H 86