122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#ifndef SERVICES_INCLUDE_IME_CFG_MANAGER_H
1722736c2fSopenharmony_ci#define SERVICES_INCLUDE_IME_CFG_MANAGER_H
1822736c2fSopenharmony_ci
1922736c2fSopenharmony_ci#include <memory>
2022736c2fSopenharmony_ci#include <mutex>
2122736c2fSopenharmony_ci#include <string>
2222736c2fSopenharmony_ci#include <vector>
2322736c2fSopenharmony_ci
2422736c2fSopenharmony_ci#include "serializable.h"
2522736c2fSopenharmony_cinamespace OHOS {
2622736c2fSopenharmony_cinamespace MiscServices {
2722736c2fSopenharmony_cistruct ImePersistInfo : public Serializable {
2822736c2fSopenharmony_ci    ImePersistInfo() = default;
2922736c2fSopenharmony_ci    ImePersistInfo(int32_t userId, std::string currentIme, std::string currentSubName, bool isDefaultImeSet)
3022736c2fSopenharmony_ci        : userId(userId), currentIme(std::move(currentIme)), currentSubName(std::move(currentSubName)),
3122736c2fSopenharmony_ci        isDefaultImeSet(isDefaultImeSet){};
3222736c2fSopenharmony_ci    static constexpr int32_t INVALID_USERID = -1;
3322736c2fSopenharmony_ci    int32_t userId{ INVALID_USERID };
3422736c2fSopenharmony_ci    std::string currentIme;
3522736c2fSopenharmony_ci    std::string currentSubName;
3622736c2fSopenharmony_ci    bool isDefaultImeSet{ false };
3722736c2fSopenharmony_ci
3822736c2fSopenharmony_ci    bool Marshal(cJSON *node) const override
3922736c2fSopenharmony_ci    {
4022736c2fSopenharmony_ci        auto ret = SetValue(node, GET_NAME(userId), userId);
4122736c2fSopenharmony_ci        ret = SetValue(node, GET_NAME(currentIme), currentIme) && ret;
4222736c2fSopenharmony_ci        ret = SetValue(node, GET_NAME(currentSubName), currentSubName) && ret;
4322736c2fSopenharmony_ci        ret = SetValue(node, GET_NAME(isDefaultImeSet), isDefaultImeSet) && ret;
4422736c2fSopenharmony_ci        return ret;
4522736c2fSopenharmony_ci    }
4622736c2fSopenharmony_ci    bool Unmarshal(cJSON *node) override
4722736c2fSopenharmony_ci    {
4822736c2fSopenharmony_ci        auto ret = GetValue(node, GET_NAME(userId), userId);
4922736c2fSopenharmony_ci        ret = GetValue(node, GET_NAME(currentIme), currentIme) && ret;
5022736c2fSopenharmony_ci        ret = GetValue(node, GET_NAME(currentSubName), currentSubName) && ret;
5122736c2fSopenharmony_ci        ret = GetValue(node, GET_NAME(isDefaultImeSet), isDefaultImeSet) && ret;
5222736c2fSopenharmony_ci        return ret;
5322736c2fSopenharmony_ci    }
5422736c2fSopenharmony_ci};
5522736c2fSopenharmony_ci
5622736c2fSopenharmony_cistruct ImePersistCfg : public Serializable {
5722736c2fSopenharmony_ci    std::vector<ImePersistInfo> imePersistInfo;
5822736c2fSopenharmony_ci    bool Marshal(cJSON *node) const override
5922736c2fSopenharmony_ci    {
6022736c2fSopenharmony_ci        return SetValue(node, GET_NAME(imeCfgList), imePersistInfo);
6122736c2fSopenharmony_ci    }
6222736c2fSopenharmony_ci    bool Unmarshal(cJSON *node) override
6322736c2fSopenharmony_ci    {
6422736c2fSopenharmony_ci        return GetValue(node, GET_NAME(imeCfgList), imePersistInfo);
6522736c2fSopenharmony_ci    }
6622736c2fSopenharmony_ci};
6722736c2fSopenharmony_ci
6822736c2fSopenharmony_cistruct ImeNativeCfg {
6922736c2fSopenharmony_ci    std::string imeId;
7022736c2fSopenharmony_ci    std::string bundleName;
7122736c2fSopenharmony_ci    std::string subName;
7222736c2fSopenharmony_ci    std::string extName;
7322736c2fSopenharmony_ci};
7422736c2fSopenharmony_ci
7522736c2fSopenharmony_ciclass ImeCfgManager {
7622736c2fSopenharmony_cipublic:
7722736c2fSopenharmony_ci    static ImeCfgManager &GetInstance();
7822736c2fSopenharmony_ci    void Init();
7922736c2fSopenharmony_ci    void AddImeCfg(const ImePersistInfo &cfg);
8022736c2fSopenharmony_ci    void ModifyImeCfg(const ImePersistInfo &cfg);
8122736c2fSopenharmony_ci    void DeleteImeCfg(int32_t userId);
8222736c2fSopenharmony_ci    std::shared_ptr<ImeNativeCfg> GetCurrentImeCfg(int32_t userId);
8322736c2fSopenharmony_ci    bool IsDefaultImeSet(int32_t userId);
8422736c2fSopenharmony_ci
8522736c2fSopenharmony_ciprivate:
8622736c2fSopenharmony_ci    ImeCfgManager() = default;
8722736c2fSopenharmony_ci    ~ImeCfgManager() = default;
8822736c2fSopenharmony_ci    void ReadImeCfg();
8922736c2fSopenharmony_ci    void WriteImeCfg();
9022736c2fSopenharmony_ci    ImePersistInfo GetImeCfg(int32_t userId);
9122736c2fSopenharmony_ci    bool ParseImeCfg(const std::string &content);
9222736c2fSopenharmony_ci    std::string PackageImeCfg();
9322736c2fSopenharmony_ci    std::recursive_mutex imeCfgLock_;
9422736c2fSopenharmony_ci    std::vector<ImePersistInfo> imeConfigs_;
9522736c2fSopenharmony_ci};
9622736c2fSopenharmony_ci} // namespace MiscServices
9722736c2fSopenharmony_ci} // namespace OHOS
9822736c2fSopenharmony_ci#endif // SERVICES_INCLUDE_IME_CFG_MANAGER_H
99