• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "nlohmann/json.hpp"
25 namespace OHOS {
26 namespace MiscServices {
27 struct ImeCfg {
28     int32_t userId;
29     std::string currentIme;
30 };
31 class ImeCfgManager {
32 public:
33     static ImeCfgManager &GetInstance();
34     void Init();
35     void AddImeCfg(const ImeCfg &cfg);
36     void ModifyImeCfg(const ImeCfg &cfg);
37     void DeleteImeCfg(int32_t userId);
38     ImeCfg GetImeCfg(int32_t userId);
39     static std::string GetDefaultIme();
40 
41 private:
42     void ReadImeCfgFile();
43     void WriteImeCfgFile();
44     static int32_t CreateCachePath(std::string &path, mode_t pathMode);
45     static bool IsCachePathExit(std::string &path);
46     static bool ReadCacheFile(const std::string &path, nlohmann::json &jsonCfg);
47     static bool WriteCacheFile(const std::string &path, const nlohmann::json &jsonCfg);
FromJson(const nlohmann::json & jsonCfg,ImeCfg & cfg)48     inline static void FromJson(const nlohmann::json &jsonCfg, ImeCfg &cfg)
49     {
50         jsonCfg.at("userId").get_to(cfg.userId);
51         jsonCfg.at("currentIme").get_to(cfg.currentIme);
52     }
ToJson(nlohmann::json & jsonCfg,const ImeCfg & cfg)53     inline static void ToJson(nlohmann::json &jsonCfg, const ImeCfg &cfg)
54     {
55         jsonCfg = nlohmann::json{ { "userId", cfg.userId }, { "currentIme", cfg.currentIme } };
56     }
57     static void FromJson(const nlohmann::json &jsonConfigs, std::vector<ImeCfg> &configs);
58     static void ToJson(nlohmann::json &jsonConfigs, const std::vector<ImeCfg> &configs);
59     std::recursive_mutex imeCfgLock_;
60     std::vector<ImeCfg> imeConfigs_;
61 };
62 } // namespace MiscServices
63 } // namespace OHOS
64 #endif // SERVICES_INCLUDE_IME_CFG_MANAGER_H