1 /*
2 * Copyright (c) 2021-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 #include "global.h"
17
18 #include <securec.h>
19
20 #include "auto_mutex.h"
21 #include "hap_manager.h"
22 #include "hilog_wrapper.h"
23 #include "utils/errors.h"
24 #include "utils/string_utils.h"
25
26 using namespace OHOS::Global::Resource;
27
28 static ResConfigImpl *g_resConfig = nullptr;
29
30 static Lock g_lock;
31
FreeValue(char ** value)32 static void FreeValue(char **value)
33 {
34 if (*value != nullptr) {
35 free(*value);
36 *value = nullptr;
37 }
38 }
39
InitResConfig()40 static void InitResConfig()
41 {
42 AutoMutex mutex(g_lock);
43 if (g_resConfig == nullptr) {
44 g_resConfig = new (std::nothrow) ResConfigImpl;
45 if (g_resConfig == nullptr) {
46 HILOG_ERROR("new ResConfigImpl failed when GLOBAL_ConfigLanguage");
47 }
48 }
49 }
50
GLOBAL_ConfigLanguage(const char * appLanguage)51 void GLOBAL_ConfigLanguage(const char *appLanguage)
52 {
53 if (appLanguage == nullptr) {
54 return;
55 }
56 InitResConfig();
57
58 std::string lan(appLanguage);
59 auto index1 = lan.find("-");
60 auto index2 = lan.find("_");
61 auto indexStart = std::string::npos;
62 auto indexEnd = std::string::npos;
63 if (index1 != std::string::npos) {
64 indexStart = index1;
65 indexEnd = lan.find("-", index1 + 1);
66 } else if (index2 != std::string::npos) {
67 indexStart = index2;
68 indexEnd = lan.find("_", index2 + 1);
69 }
70 std::string language;
71 std::string script;
72 std::string region;
73 if (indexStart != std::string::npos) {
74 if (indexEnd != std::string::npos) {
75 language.assign(appLanguage, indexStart);
76 script.assign(appLanguage + indexStart + 1, indexEnd - indexStart - 1);
77 region.assign(appLanguage + indexEnd + 1);
78 g_resConfig->SetLocaleInfo(language.c_str(), script.c_str(), region.c_str());
79 } else {
80 language.assign(appLanguage, indexStart);
81 region.assign(appLanguage + indexStart + 1);
82 g_resConfig->SetLocaleInfo(language.c_str(), nullptr, region.c_str());
83 }
84 } else {
85 g_resConfig->SetLocaleInfo(appLanguage, nullptr, nullptr);
86 }
87 }
88
GLOBAL_GetLanguage(char * language,uint8_t len)89 int32_t GLOBAL_GetLanguage(char *language, uint8_t len)
90 {
91 if (g_resConfig == nullptr || g_resConfig->GetLocaleInfo() == nullptr
92 || g_resConfig->GetLocaleInfo()->GetLanguage() == nullptr) {
93 return SYS_ERROR;
94 }
95 if (language == nullptr || len == 0) {
96 return SYS_ERROR;
97 }
98 if (strncpy_s(language, len, g_resConfig->GetLocaleInfo()->GetLanguage(),
99 strlen(g_resConfig->GetLocaleInfo()->GetLanguage())) != EOK) {
100 return SYS_ERROR;
101 }
102 return OK;
103 }
104
GLOBAL_GetRegion(char * region,uint8_t len)105 int32_t GLOBAL_GetRegion(char *region, uint8_t len)
106 {
107 if (g_resConfig == nullptr || g_resConfig->GetLocaleInfo() == nullptr
108 || g_resConfig->GetLocaleInfo()->GetRegion() == nullptr) {
109 return SYS_ERROR;
110 }
111 if (region == nullptr || len == 0) {
112 return SYS_ERROR;
113 }
114 if (strncpy_s(region, len, g_resConfig->GetLocaleInfo()->GetRegion(),
115 strlen(g_resConfig->GetLocaleInfo()->GetRegion())) != EOK) {
116 return SYS_ERROR;
117 }
118 return OK;
119 }
120
GetValue(const IdItem * idItem,char ** value)121 int32_t GetValue(const IdItem *idItem, char **value)
122 {
123 if (idItem == nullptr) {
124 return SYS_ERROR;
125 }
126 if (idItem->isArray_) {
127 std::string ret("[");
128 for (size_t i = 0; i < idItem->values_.size(); ++i) {
129 ret.append(FormatString("'%s',", idItem->values_[i].c_str()));
130 }
131 ret.append("]");
132 *value = static_cast<char *>(malloc(ret.size() + 1));
133 if (*value == nullptr || strcpy_s(*value, ret.size() + 1, ret.c_str()) != EOK) {
134 FreeValue(value);
135 return SYS_ERROR;
136 }
137 (*value)[ret.size()] = '\0';
138 } else {
139 *value = static_cast<char *>(malloc(idItem->valueLen_ + 1));
140 if (*value == nullptr || strcpy_s(*value, idItem->valueLen_ + 1, idItem->value_.c_str()) != EOK) {
141 FreeValue(value);
142 return SYS_ERROR;
143 }
144 (*value)[idItem->valueLen_] = '\0';
145 }
146 return OK;
147 }
148
GLOBAL_GetValueById(uint32_t id,const char * path,char ** value)149 int32_t GLOBAL_GetValueById(uint32_t id, const char *path, char **value)
150 {
151 bool resConfigSet = true;
152 if (g_resConfig == nullptr || g_resConfig->GetLocaleInfo() == nullptr
153 || g_resConfig->GetLocaleInfo()->GetLanguage() == nullptr) {
154 HILOG_WARN("GLOBAL_GetValueById language is null, use default");
155 resConfigSet = false;
156 }
157 ResConfigImpl *resConfig = new(std::nothrow) ResConfigImpl;
158 if (resConfig == nullptr) {
159 HILOG_ERROR("new ResConfigImpl failed when GLOBAL_GetValueByName");
160 return SYS_ERROR;
161 }
162 HapManager hapManager(resConfig);
163 if (resConfigSet) {
164 hapManager.UpdateResConfig(*g_resConfig);
165 }
166
167 bool ret = hapManager.AddResource(path);
168 if (!ret) {
169 HILOG_ERROR("GLOBAL_GetValueById AddResource error %s", path);
170 return SYS_ERROR;
171 }
172
173 auto idItem = hapManager.FindResourceById(id);
174 if (idItem == nullptr) {
175 return OBJ_NOT_FOUND;
176 }
177
178 return GetValue(idItem, value);
179 }
180
GLOBAL_GetValueByName(const char * name,const char * path,char ** value)181 int32_t GLOBAL_GetValueByName(const char *name, const char *path, char **value)
182 {
183 bool resConfigSet = true;
184 if (g_resConfig == nullptr || g_resConfig->GetLocaleInfo() == nullptr
185 || g_resConfig->GetLocaleInfo()->GetLanguage() == nullptr) {
186 HILOG_WARN("GLOBAL_GetValueByName language is null, use default");
187 resConfigSet = false;
188 }
189
190 ResConfigImpl *resConfig = new(std::nothrow) ResConfigImpl;
191 if (resConfig == nullptr) {
192 HILOG_ERROR("new ResConfigImpl failed when GLOBAL_GetValueByName");
193 return SYS_ERROR;
194 }
195 HapManager hapManager(resConfig);
196 if (resConfigSet) {
197 hapManager.UpdateResConfig(*g_resConfig);
198 }
199
200 bool ret = hapManager.AddResource(path);
201 if (!ret) {
202 HILOG_ERROR("GLOBAL_GetValueByName AddResource error %s", path);
203 return SYS_ERROR;
204 }
205
206 const IdItem *idItem = nullptr;
207 for (int i = 0; i < ResType::MAX_RES_TYPE; ++i) {
208 idItem = hapManager.FindResourceByName(name, (ResType)i);
209 if (idItem != nullptr) {
210 break;
211 }
212 }
213 if (idItem == nullptr) {
214 return OBJ_NOT_FOUND;
215 }
216
217 return GetValue(idItem, value);
218 }
219