1 /*
2  * Copyright (c) 2023 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 #ifndef OHOS_THEME_PACK_RESOURCE_H
16 #define OHOS_THEME_PACK_RESOURCE_H
17 
18 #include <mutex>
19 #include <tuple>
20 #include <unordered_map>
21 
22 #include "cJSON.h"
23 #include "hap_resource.h"
24 #include "res_common.h"
25 #include "res_config_impl.h"
26 #include "res_desc.h"
27 #include "resource_manager.h"
28 #include "theme_pack_config.h"
29 
30 namespace OHOS {
31 namespace Global {
32 namespace Resource {
33 struct ThemeKey {
34     std::string bundleName;
35     std::string moduleName;
36     ResType resType;
37     std::string resName;
38     std::string abilityName;
39 
ThemeKeyOHOS::Global::Resource::ThemeKey40     ThemeKey(std::string bundle, std::string module, ResType type, std::string name, std::string ability = "")
41         : bundleName(bundle), moduleName(module), resType(type), resName(name), abilityName(ability) {};
operator ==OHOS::Global::Resource::ThemeKey42     bool operator == (const ThemeKey& theme) const
43     {
44         return bundleName == theme.bundleName && moduleName == theme.moduleName
45             && resType == theme.resType && resName == theme.resName && abilityName == theme.abilityName;
46     }
47 };
48 
49 struct HashTk {
operator ()OHOS::Global::Resource::HashTk50     std::size_t operator() (const ThemeKey &theme) const
51     {
52         std::size_t h1 = std::hash<std::string>()(theme.bundleName);
53         std::size_t h2 = std::hash<std::string>()(theme.moduleName);
54         std::size_t h3 = std::hash<int32_t>()(theme.resType);
55         std::size_t h4 = std::hash<std::string>()(theme.resName);
56         std::size_t h5 = std::hash<std::string>()(theme.abilityName);
57         return h1 ^ h2 ^ h3 ^ h4 ^ h5;
58     }
59 };
60 
61 class ThemeResource {
62 public:
63     ThemeResource(std::string path);
64     ~ThemeResource();
65     static const std::shared_ptr<ThemeResource> LoadThemeResource(const std::string& rootDir);
66     static const std::shared_ptr<ThemeResource> LoadThemeIconResource(const std::string& rootDir);
67     class ThemeQualifierValue {
68     public:
GetResValue() const69         inline const std::string GetResValue() const
70         {
71             return resValue_;
72         }
73 
GetThemeConfig() const74         inline const std::shared_ptr<ThemeConfig> GetThemeConfig() const
75         {
76             return themeConfig_;
77         }
78 
GetThemeKey() const79         inline const ThemeKey GetThemeKey() const
80         {
81             return themeKey_;
82         }
83         ThemeQualifierValue(const ThemeKey themeKey, std::shared_ptr<ThemeConfig> themeConfig,
84             const std::string &value);
85         ~ThemeQualifierValue();
86     private:
87         ThemeKey themeKey_;
88         std::shared_ptr<ThemeConfig> themeConfig_;
89         std::string resValue_;
90     };
91 
92     class ThemeValue {
93     public:
AddThemeLimitPath(std::shared_ptr<ThemeQualifierValue> themeQualifer)94         inline void AddThemeLimitPath(std::shared_ptr<ThemeQualifierValue> themeQualifer)
95         {
96             limitPaths_.push_back(themeQualifer);
97         }
98 
GetThemeLimitPathsConst() const99         inline const std::vector<std::shared_ptr<ThemeQualifierValue> > &GetThemeLimitPathsConst() const
100         {
101             return limitPaths_;
102         }
103         ~ThemeValue();
104     private:
105         std::vector<std::shared_ptr<ThemeQualifierValue> > limitPaths_;
106     };
107     /**
108      * Get the theme value related bundlename, modulename, resType and resource name.
109      *
110      * @param bundleInfo which contains bundlename, modulename
111      * @param resType the resource type
112      * @param name the resource name
113      * @return the theme value vector
114      */
115     std::vector<std::shared_ptr<ThemeResource::ThemeValue> >  GetThemeValues(
116         const std::pair<std::string, std::string> &bundInfo,
117         const ResType &resType, const std::string &name);
118 
119     /**
120      * Get the theme icon related bundlename, modulename and resource name.
121      *
122      * @param bundleInfo which contains bundlename, modulename
123      * @param name the icon name
124      * @param abilityName the hap abilityName
125      * @return the icon path
126      */
127     const std::string GetThemeAppIcon(const std::pair<std::string, std::string> &bundleInfo, const std::string &name,
128         const std::string &abilityName = "");
129 
130     /**
131      * Get the theme value related bundlename, modulename, resType and resource name.
132      *
133      * @param bundleInfo which contains bundlename, modulename
134      * @param resType the resource type
135      * @param name the resource name
136      * @return the theme value vector
137      */
138     std::string GetThemeResBundleName(const std::string &themePath);
139 
140     /**
141      * Whether an icon exists in the theme
142      *
143      * @param bundleName the hap bundleName
144      * @return true if icon exists, else no exists
145      */
146     bool HasIconInTheme(const std::string &bundleName);
147 
GetThemePath()148     inline std::string GetThemePath()
149     {
150         return themePath_;
151     }
152 
IsNewResource()153     inline bool IsNewResource()
154     {
155         return isNewResource_;
156     }
157 
SetNewResource(bool isNew)158     inline void SetNewResource(bool isNew)
159     {
160         isNewResource_ = isNew;
161     }
162 
163     std::string themePath_;
164     bool isNewResource_ = true;
165 private:
166     std::vector<std::tuple<ResType, std::string, std::shared_ptr<ThemeValue>> > themeValueVec_;
167     std::vector<std::pair<ThemeKey, std::string> > iconValues_;
168     void ParseJson(const std::string &bundleName, const std::string &moduleName, const std::string &jsonPath);
169     void ParseIcon(const std::string &bundleName, const std::string &moduleName, const std::string &iconPath);
170     void InitThemeRes(std::pair<std::string, std::string> bundleInfo, cJSON *root,
171         std::shared_ptr<ThemeConfig> themeConfig, const std::string &resTypeStr);
172     const std::string GetThemeAppIconByAbilityName(const std::pair<std::string, std::string> &bundleInfo,
173         const std::string &name, const std::string &abilityName = "");
174     static ThemeResource *themeRes;
175 };
176 } // namespace Resource
177 } // namespace Global
178 } // namespace OHOS
179 #endif