Modified an api call flow
[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 #include "FLcl_LocaleManagerImpl.h"
32
33 using namespace Tizen::Base;
34
35 namespace Tizen { namespace Locales
36 {
37
38 _LocaleImpl::_LocaleImpl(void)
39         : __icuLocale()
40 {
41 }
42
43 _LocaleImpl::_LocaleImpl(const char* pLocaleCode)
44         : __icuLocale(pLocaleCode)
45 {
46         if (pLocaleCode == null || *pLocaleCode == '\0')
47         {
48                 __icuLocale.setToBogus();
49         }
50 }
51
52 _LocaleImpl::_LocaleImpl(const char* pLanguage, const char* pCountry, const char* pVariant)
53         : __icuLocale(pLanguage, pCountry, pVariant)
54 {
55         if (pLanguage == null || *pLanguage == '\0' || pCountry == null || *pCountry == '\0')
56         {
57                 __icuLocale.setToBogus();
58         }
59 }
60
61 _LocaleImpl::_LocaleImpl(const U_ICU_NAMESPACE::Locale& icuLocale)
62         : __icuLocale(icuLocale)
63 {
64 }
65
66 _LocaleImpl::~_LocaleImpl(void)
67 {
68 }
69
70 _LocaleImpl*
71 _LocaleImpl::CloneN(void)
72 {
73         return new (std::nothrow) _LocaleImpl(__icuLocale);
74 }
75
76 U_ICU_NAMESPACE::Locale
77 _LocaleImpl::GetIcuLocale (void) const
78 {
79         return __icuLocale;
80 }
81
82 Locale
83 _LocaleImpl::GetOspLocale(void)
84 {
85         LanguageCode languageCode = Locale::StringToLanguageCode(GetLanguageCodeString(false));
86         CountryCode countryCode = Locale::StringToCountryCode(Get2LetterCountryCodeString());
87         String variant(GetVariantCodeString());
88         return Locale(languageCode, countryCode, &variant);
89 }
90
91 bool
92 _LocaleImpl::operator ==(const _LocaleImpl& otherLocale) const
93 {
94         return __icuLocale == otherLocale.__icuLocale;
95 }
96
97 bool
98 _LocaleImpl::operator !=(const _LocaleImpl& otherLocale) const
99 {
100         return !(*this == otherLocale);
101 }
102
103 bool
104 _LocaleImpl::Equals(const Object& obj) const
105 {
106         const _LocaleImpl* pOtherLocale = dynamic_cast< const _LocaleImpl* >(&obj);
107         if (pOtherLocale)
108         {
109                 return *this == *pOtherLocale;
110         }
111         return false;
112 }
113
114 int
115 _LocaleImpl::GetHashCode(void) const
116 {
117         return __icuLocale.hashCode();
118 }
119
120 String
121 _LocaleImpl::GetLanguageCodeString(bool isTwoLetter)
122 {
123         String languageCode((isTwoLetter ? __icuLocale.getLanguage() : __icuLocale.getISO3Language()));
124         String languageScriptTmp(__icuLocale.getScript());
125
126         if (!languageScriptTmp.IsEmpty())
127         {
128                 String languageScript;
129                 languageScriptTmp.ToLowerCase(languageScript);
130                 languageCode.Append(L"-");
131                 languageCode.Append(languageScript);
132         }
133         return languageCode;
134 }
135
136 result
137 _LocaleImpl::GetLanguageName(String& languageName) const
138 {
139         Locale loc = _LocaleManagerImpl::GetSystemLocale();
140         return GetLanguageName(*loc.__pLocaleImpl, languageName);
141 }
142
143 result
144 _LocaleImpl::GetLanguageName(const _LocaleImpl& otherLocale, String& languageName) const
145 {
146         U_ICU_NAMESPACE::UnicodeString icuStr;
147         languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(otherLocale.__icuLocale, icuStr));
148         if (!languageName.IsEmpty())
149         {
150                 String languageScriptTmp(__icuLocale.getScript());
151
152                 if (!languageScriptTmp.IsEmpty())
153                 {
154                         U_ICU_NAMESPACE::UnicodeString icuScriptStr;
155                         String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(otherLocale.__icuLocale, icuScriptStr));
156                         languageName.Insert(L" (", languageName.GetLength());
157                         languageName.Insert(languageScriptName, languageName.GetLength());
158                         languageName.Insert(L")", languageName.GetLength());
159                 }
160                 return E_SUCCESS;
161         }
162         return E_UNSUPPORTED_OPERATION;
163 }
164
165 const char*
166 _LocaleImpl::Get2LetterCountryCodeString(void) const
167 {
168         return __icuLocale.getCountry();
169 }
170
171 const char*
172 _LocaleImpl::Get3LetterCountryCodeString(void) const
173 {
174         return __icuLocale.getISO3Country();
175 }
176
177 result
178 _LocaleImpl::GetCountryName(String& countryName) const
179 {
180         Locale loc = _LocaleManagerImpl::GetSystemLocale();
181         return GetCountryName(*loc.__pLocaleImpl, countryName);
182 }
183
184 result
185 _LocaleImpl::GetCountryName(const _LocaleImpl& otherLocale, String& countryName) const
186 {
187         U_ICU_NAMESPACE::UnicodeString icuStr;
188         countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(otherLocale.__icuLocale, icuStr));
189         if (!countryName.IsEmpty())
190         {
191                 return E_SUCCESS;
192         }
193         return E_UNSUPPORTED_OPERATION;
194 }
195
196
197 const char*
198 _LocaleImpl::GetVariantCodeString(void) const
199 {
200         return __icuLocale.getVariant();
201 }
202
203 void
204 _LocaleImpl::SetVariantCodeString(const char* pVariantCode)
205 {
206         __icuLocale = U_ICU_NAMESPACE::Locale(__icuLocale.getLanguage(), __icuLocale.getCountry(), pVariantCode);
207 }
208
209 const char*
210 _LocaleImpl::GetLocaleCodeString(void) const
211 {
212         return __icuLocale.getName();
213 }
214
215 bool
216 _LocaleImpl::IsSupported(void) const
217 {
218         return !__icuLocale.isBogus();
219 }
220
221 bool
222 _LocaleImpl::IsSupported(const Locale& ospLocale)
223 {
224     return !ospLocale.__pLocaleImpl->__icuLocale.isBogus();
225 }
226
227 const _LocaleImpl*
228 _LocaleImpl::GetLocaleImpl(const Locale& ospLocale)
229 {
230         return ospLocale.__pLocaleImpl;
231 }
232
233 String
234 _LocaleImpl::Get2LetterLanguageCodeString(const char* pLanguageCodeString)
235 {
236         if (pLanguageCodeString)
237         {
238                 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
239                 return localeImpl.GetLanguageCodeString(true);
240         }
241         return null;
242 }
243
244 String
245 _LocaleImpl::Get3LetterLanguageCodeString(const char* pLanguageCodeString)
246 {
247         if (pLanguageCodeString)
248         {
249                 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
250                 return localeImpl.GetLanguageCodeString(false);
251         }
252         return null;
253 }
254
255 const char*
256 _LocaleImpl::Get2LetterCountryCodeString(const char* pCountryCodeString)
257 {
258         if (pCountryCodeString && strlen(pCountryCodeString) == 3)
259         {
260                 _LocaleImpl localeImpl("en", pCountryCodeString, null);
261                 return localeImpl.Get2LetterCountryCodeString();
262         }
263         return null;
264 }
265
266 const char*
267 _LocaleImpl::Get3LetterCountryCodeString(const char* pCountryCodeString)
268 {
269         if (pCountryCodeString)
270         {
271                 _LocaleImpl localeImpl("en", pCountryCodeString, null);
272                 return localeImpl.Get3LetterCountryCodeString();
273         }
274         return null;
275 }
276
277 };
278 };      // Tizen::Locales
279