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 
16 #ifndef OHOS_GLOBAL_SYSTEM_LOCALE_MANAGER_H
17 #define OHOS_GLOBAL_SYSTEM_LOCALE_MANAGER_H
18 
19 #include <string>
20 #include <vector>
21 #include "collator.h"
22 #include "i18n_types.h"
23 #include "taboo_utils.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 enum SuggestionType {
29     // language or region is not suggested with system region or system language.
30     SUGGESTION_TYPE_NONE = 0,
31 
32     // language is suggested with system region or region is suggested with system language.
33     SUGGESTION_TYPE_RELATED = 1,
34 
35     // language is suggested with sim card region.
36     SUGGESTION_TYPE_SIM = 2,
37 };
38 
39 struct LocaleItem {
40     // language or region code.
41     std::string id;
42 
43     // the suggestion type of language or region.
44     SuggestionType suggestionType;
45 
46     // the name of language or region in specified language.
47     std::string displayName;
48 
49     // the name of language or region in local language.
50     std::string localName;
51 };
52 
53 struct SortOptions {
54     // locale use to sort language or region.
55     std::string localeTag;
56 
57     // whether to use local name to sort language or region.
58     bool isUseLocalName;
59 
60     // whether to put the suggested item at the top
61     bool isSuggestedFirst;
62 };
63 
64 struct TimeZoneCityItem {
65     // time zone code
66     std::string zoneId;
67 
68     //city code
69     std::string cityId;
70 
71     //the display name of city
72     std::string cityDisplayName;
73 
74     //the timezone offset for the city
75     int32_t offset;
76 
77     //the display name of the timezone of the city
78     std::string zoneDisplayName;
79 
80     //the timezone raw offset for the city
81     int32_t rawOffset;
82 };
83 
84 class SystemLocaleManager {
85 public:
86     SystemLocaleManager();
87     ~SystemLocaleManager();
88 
89     /**
90      * @brief sort input languages according to SortOptions and obtain sorted result.
91      *
92      * @param languages input language array to be sorted.
93      * @param options sort options.
94      * @return std::vector<LocaleItem> sort result.
95      */
96     std::vector<LocaleItem> GetLanguageInfoArray(const std::vector<std::string> &languages,
97         const SortOptions &options, I18nErrorCode &status);
98 
99     /**
100      * @brief sort input countries according to SortOptions and obtain sorted result.
101      *
102      * @param regions input country array to be sorted.
103      * @param options sort options.
104      * @return std::vector<LocaleItem> sort result.
105      */
106     std::vector<LocaleItem> GetCountryInfoArray(const std::vector<std::string> &countries, const SortOptions &options,
107         I18nErrorCode &status);
108 
109     /**
110      * @brief obtains sorted time zone city array.
111      *
112      * @return std::vector<TimeZoneCityItem> sort result.
113      */
114     static std::vector<TimeZoneCityItem> GetTimezoneCityInfoArray(I18nErrorCode &status);
115 
116 private:
117     void SortLocaleItemList(std::vector<LocaleItem> &localeItemList, const SortOptions &options);
118     static void SortTimezoneCityItemList(const std::string &locale,
119                                          std::vector<TimeZoneCityItem> &timezoneCityItemList);
120     static std::vector<TimeZoneCityItem> GetTimezoneCityInfoArray();
121     std::unique_ptr<TabooUtils> tabooUtils;
122     static const char* SIM_COUNTRY_CODE_KEY;
123     static constexpr int CONFIG_LEN = 128;
124 };
125 } // namespace I18n
126 } // namespace Global
127 } // namespace OHOS
128 #endif