Merge "Update PackageAppFilter API" into tizen_2.1
[platform/framework/native/appfw.git] / src / locales / FLcl_LocaleImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file            FLcl_LocaleImpl.cpp
20  * @brief           This is the implementation file for _LocaleImpl class.
21  */
22
23 #include <unicode/errorcode.h>
24 #include <unicode/ustring.h>
25
26 #include <FBaseSysLog.h>
27 #include <FLclLocale.h>
28
29 #include "FLcl_LocaleImpl.h"
30 #include "FLcl_LocaleData.h"
31
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Locales
35 {
36
37 _LocaleImpl::_LocaleImpl(void)
38         : __icuLocale()
39 {
40 }
41
42 _LocaleImpl::_LocaleImpl(const char* pLocaleCode)
43         : __icuLocale(pLocaleCode)
44 {
45         if (pLocaleCode == null || *pLocaleCode == '\0')
46         {
47                 __icuLocale.setToBogus();
48         }
49 }
50
51 _LocaleImpl::_LocaleImpl(const char* pLanguage, const char* pCountry, const char* pVariant)
52         : __icuLocale(pLanguage, pCountry, pVariant)
53 {
54         if (pLanguage == null || *pLanguage == '\0' || pCountry == null || *pCountry == '\0')
55         {
56                 __icuLocale.setToBogus();
57         }
58 }
59
60 _LocaleImpl::_LocaleImpl(const U_ICU_NAMESPACE::Locale& icuLocale)
61         : __icuLocale(icuLocale)
62 {
63 }
64
65 _LocaleImpl::~_LocaleImpl(void)
66 {
67 }
68
69 _LocaleImpl*
70 _LocaleImpl::CloneN(void)
71 {
72         return new (std::nothrow) _LocaleImpl(__icuLocale);
73 }
74
75 U_ICU_NAMESPACE::Locale
76 _LocaleImpl::GetIcuLocale (void) const
77 {
78         return __icuLocale;
79 }
80
81 Locale
82 _LocaleImpl::GetOspLocale(void)
83 {
84         LanguageCode languageCode = Locale::StringToLanguageCode(GetLanguageCodeString(false));
85         CountryCode countryCode = Locale::StringToCountryCode(Get2LetterCountryCodeString());
86         String variant(GetVariantCodeString());
87         return Locale(languageCode, countryCode, &variant);
88 }
89
90 bool
91 _LocaleImpl::operator ==(const _LocaleImpl& otherLocale) const
92 {
93         return __icuLocale == otherLocale.__icuLocale;
94 }
95
96 bool
97 _LocaleImpl::operator !=(const _LocaleImpl& otherLocale) const
98 {
99         return !(*this == otherLocale);
100 }
101
102 bool
103 _LocaleImpl::Equals(const Object& obj) const
104 {
105         const _LocaleImpl* pOtherLocale = dynamic_cast< const _LocaleImpl* >(&obj);
106         if (pOtherLocale)
107         {
108                 return *this == *pOtherLocale;
109         }
110         return false;
111 }
112
113 int
114 _LocaleImpl::GetHashCode(void) const
115 {
116         return __icuLocale.hashCode();
117 }
118
119 String
120 _LocaleImpl::GetLanguageCodeString(bool isTwoLetter)
121 {
122         String languageCode((isTwoLetter ? __icuLocale.getLanguage() : __icuLocale.getISO3Language()));
123         String languageScriptTmp(__icuLocale.getScript());
124
125         if (!languageScriptTmp.IsEmpty())
126         {
127                 String languageScript;
128                 languageScriptTmp.ToLowerCase(languageScript);
129                 languageCode.Append(L"-");
130                 languageCode.Append(languageScript);
131         }
132         return languageCode;
133 }
134
135 result
136 _LocaleImpl::GetLanguageName(String& languageName) const
137 {
138         U_ICU_NAMESPACE::UnicodeString icuStr;
139         languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(icuStr));
140         if (!languageName.IsEmpty())
141         {
142                 String languageScriptTmp(__icuLocale.getScript());
143
144                 if (!languageScriptTmp.IsEmpty())
145                 {
146                         U_ICU_NAMESPACE::UnicodeString icuScriptStr;
147                         String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(icuScriptStr));
148                         languageName.Insert(L" (", languageName.GetLength());
149                         languageName.Insert(languageScriptName, languageName.GetLength());
150                         languageName.Insert(L")", languageName.GetLength());
151                 }
152                 return E_SUCCESS;
153         }
154         return E_UNSUPPORTED_OPERATION;
155 }
156
157 result
158 _LocaleImpl::GetLanguageName(const _LocaleImpl& otherLocale, String& languageName) const
159 {
160         U_ICU_NAMESPACE::UnicodeString icuStr;
161         languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(otherLocale.__icuLocale, icuStr));
162         if (!languageName.IsEmpty())
163         {
164                 String languageScriptTmp(__icuLocale.getScript());
165
166                 if (!languageScriptTmp.IsEmpty())
167                 {
168                         U_ICU_NAMESPACE::UnicodeString icuScriptStr;
169                         String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(otherLocale.__icuLocale, icuScriptStr));
170                         languageName.Insert(L" (", languageName.GetLength());
171                         languageName.Insert(languageScriptName, languageName.GetLength());
172                         languageName.Insert(L")", languageName.GetLength());
173                 }
174                 return E_SUCCESS;
175         }
176         return E_UNSUPPORTED_OPERATION;
177 }
178
179 const char*
180 _LocaleImpl::Get2LetterCountryCodeString(void) const
181 {
182         return __icuLocale.getCountry();
183 }
184
185 const char*
186 _LocaleImpl::Get3LetterCountryCodeString(void) const
187 {
188         return __icuLocale.getISO3Country();
189 }
190
191 result
192 _LocaleImpl::GetCountryName(String& countryName) const
193 {
194         U_ICU_NAMESPACE::UnicodeString icuStr;
195         countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(icuStr));
196         if (!countryName.IsEmpty())
197         {
198                 return E_SUCCESS;
199         }
200         return E_UNSUPPORTED_OPERATION;
201 }
202
203 result
204 _LocaleImpl::GetCountryName(const _LocaleImpl& otherLocale, String& countryName) const
205 {
206         U_ICU_NAMESPACE::UnicodeString icuStr;
207         countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(otherLocale.__icuLocale, icuStr));
208         if (!countryName.IsEmpty())
209         {
210                 return E_SUCCESS;
211         }
212         return E_UNSUPPORTED_OPERATION;
213 }
214
215
216 const char*
217 _LocaleImpl::GetVariantCodeString(void) const
218 {
219         return __icuLocale.getVariant();
220 }
221
222 void
223 _LocaleImpl::SetVariantCodeString(const char* pVariantCode)
224 {
225         __icuLocale = U_ICU_NAMESPACE::Locale(__icuLocale.getLanguage(), __icuLocale.getCountry(), pVariantCode);
226 }
227
228 const char*
229 _LocaleImpl::GetLocaleCodeString(void) const
230 {
231         return __icuLocale.getName();
232 }
233
234 bool
235 _LocaleImpl::IsSupported(void) const
236 {
237         return !__icuLocale.isBogus();
238 }
239
240 bool
241 _LocaleImpl::IsSupported(const Locale& ospLocale)
242 {
243     return !ospLocale.__pLocaleImpl->__icuLocale.isBogus();
244 }
245
246 const _LocaleImpl*
247 _LocaleImpl::GetLocaleImpl(const Locale& ospLocale)
248 {
249         return ospLocale.__pLocaleImpl;
250 }
251
252 String
253 _LocaleImpl::Get2LetterLanguageCodeString(const char* pLanguageCodeString)
254 {
255         if (pLanguageCodeString)
256         {
257                 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
258                 return localeImpl.GetLanguageCodeString(true);
259         }
260         return null;
261 }
262
263 String
264 _LocaleImpl::Get3LetterLanguageCodeString(const char* pLanguageCodeString)
265 {
266         if (pLanguageCodeString)
267         {
268                 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
269                 return localeImpl.GetLanguageCodeString(false);
270         }
271         return null;
272 }
273
274 const char*
275 _LocaleImpl::Get2LetterCountryCodeString(const char* pCountryCodeString)
276 {
277         if (pCountryCodeString && strlen(pCountryCodeString) == 3)
278         {
279                 _LocaleImpl localeImpl("en", pCountryCodeString, null);
280                 return localeImpl.Get2LetterCountryCodeString();
281         }
282         return null;
283 }
284
285 const char*
286 _LocaleImpl::Get3LetterCountryCodeString(const char* pCountryCodeString)
287 {
288         if (pCountryCodeString)
289         {
290                 _LocaleImpl localeImpl("en", pCountryCodeString, null);
291                 return localeImpl.Get3LetterCountryCodeString();
292         }
293         return null;
294 }
295
296 };
297 };      // Tizen::Locales
298