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