2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FLcl_LocaleManagerImpl.cpp
19 * @brief This is the implementation file for _LocaleManagerImpl class.
21 #include <unique_ptr.h>
24 #include <runtime_info.h>
25 #include <unicode/calendar.h>
26 #include <unicode/timezone.h>
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
35 #include "FLcl_LocaleImpl.h"
36 #include "FLcl_TimeZoneImpl.h"
37 #include "FLcl_LocaleManagerImpl.h"
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Utility;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Io;
47 void operator ()(xmlDoc* p)
58 void operator ()(char* p)
67 namespace Tizen { namespace Locales
69 static const char* LANGUAGE_LIST_FILE_PATH ="/opt/usr/data/setting/langlist.xml";
70 static const char* TIMEZONE_LIST_FILE_PATH = "/opt/usr/data/clock/tzlist.ini";
71 static const char* CLOCALE_LIST_FILE_PATH = "/opt/usr/etc/clocale.list";
75 _LocaleManagerImpl::GetSystemLocale(void)
78 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
80 SetLastResult(E_SUCCESS);
82 Locale ospLoc = _LocaleImpl(pRegionPtr).GetOspLocale();
87 SetLastResult(E_SYSTEM);
88 return Locale(LANGUAGE_INVALID, COUNTRY_INVALID, null);
93 _LocaleManagerImpl::GetAvailableEglibcLocaesN(void)
97 String clocaleFilePath(CLOCALE_LIST_FILE_PATH);
99 std::unique_ptr<HashMap> pClocaleMap(new (std::nothrow) HashMap(SingleObjectDeleter));
100 SysTryReturn(NID_LCL, pClocaleMap, null, E_OUT_OF_MEMORY,
101 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
102 r = file.Construct(clocaleFilePath, "r");
103 SysTryReturn(NID_LCL, r == E_SUCCESS, null,E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the clocale list from the list file.");
105 pClocaleMap->Construct();
110 r = file.Read(strBuf);
111 if ( r == E_END_OF_FILE)
115 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r, "[%s] It is failed to read the clocale list.", GetErrorMessage(r));
116 std::unique_ptr< String > pClocaleId(new (std::nothrow) String());
117 SysTryReturn(NID_LCL, pClocaleId, null, E_OUT_OF_MEMORY,
118 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
119 r = strBuf.SubString(0, 5, *pClocaleId);
125 if (!pClocaleMap->ContainsKey(*(pClocaleId.get())))
127 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
128 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
129 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
131 r = pClocaleMap->Add(pClocaleId.get(), pDummyValue.get());
132 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r,
133 "[%s] It is failed to add the clocale id into the clocale map.", GetErrorMessage(r));
134 pClocaleId.release();
135 pDummyValue.release();
138 return pClocaleMap.release();
142 _LocaleManagerImpl::GetAvailableLocalesN(void)
144 result r = E_SUCCESS;
146 const U_ICU_NAMESPACE::Locale* pIcuLocaleList = U_ICU_NAMESPACE::Locale::getAvailableLocales(count);
147 SysTryReturn(NID_LCL, count > 0, null, E_SYSTEM,
148 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
150 std::unique_ptr<LinkedList> pAvailableLocaleList(new (std::nothrow) LinkedList(SingleObjectDeleter));
151 SysTryReturn(NID_LCL, pAvailableLocaleList, null, E_OUT_OF_MEMORY,
152 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
154 std::unique_ptr< IMap > pClocaleMap(GetAvailableEglibcLocaesN());
156 for (int i = 0; i < count; i++)
158 const U_ICU_NAMESPACE::Locale* pIcuLocale = (pIcuLocaleList + i);
159 SysTryReturn(NID_LCL, pIcuLocale, null, E_SYSTEM,
160 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
161 Locale ospLocale = _LocaleImpl(*pIcuLocale).GetOspLocale();
162 if (_LocaleImpl::IsSupported(ospLocale))
164 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(ospLocale));
165 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
166 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
168 String localeId(pIcuLocale->getLanguage());
169 localeId = localeId + L"_" + String(pIcuLocale->getCountry());
171 if (!pAvailableLocaleList->Contains(*(pLocale.get())) && pClocaleMap->ContainsKey(localeId))
173 r = pAvailableLocaleList->Add(pLocale.get());
174 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make the locale list");
180 SetLastResult(E_SUCCESS);
181 return pAvailableLocaleList.release();
185 _LocaleManagerImpl::GetSelectedLanguage(void)
189 int ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &pLanguagePtr);
190 SysTryReturn(NID_LCL, ret == RUNTIME_INFO_ERROR_NONE, String(), E_SYSTEM,
191 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
192 String language(_LocaleImpl(pLanguagePtr).GetLanguageCodeString(false));
198 // FALLBACk should be removed after checking the SLP change.
200 _LocaleManagerImpl::GetAvailableLanguagesFallbackN(void)
202 std::unique_ptr<IList> pLocaleList (GetAvailableLocalesN());
203 std::unique_ptr<HashMap> pLanguageMap(new (std::nothrow) HashMap(SingleObjectDeleter));
204 SysTryReturn(NID_LCL, pLanguageMap, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
206 pLanguageMap->Construct();
208 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
209 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
210 pAvailableLanguageList->Construct();
212 for (int i = 0; i < pLocaleList->GetCount() ; i++)
214 Locale* pLocale = (Locale*)pLocaleList->GetAt(i);
215 std::unique_ptr<String> pLanguageCode(new (std::nothrow) String(pLocale->GetLanguageCodeString()));
216 SysTryReturn(NID_LCL, pLanguageCode, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
218 if (!pLanguageMap->ContainsKey(*pLanguageCode))
220 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
221 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
223 result r = pLanguageMap->Add(pLanguageCode.get(), pDummyValue.get());
224 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make a language map.");
226 std::unique_ptr<String> pLangCode(new (std::nothrow) String(*(pLanguageCode.get())));
227 r = pAvailableLanguageList->Add(pLangCode.get());
228 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make a language list.");
230 pLanguageCode.release();
231 pDummyValue.release();
236 SetLastResult(E_SUCCESS);
237 return pAvailableLanguageList.release();
240 _LocaleManagerImpl::GetAvailableLanguagesN(void)
242 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
243 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
245 xmlNodePtr cur = null;
246 std::unique_ptr< xmlDoc, FreeXmlDoc > pDoc(xmlParseFile(LANGUAGE_LIST_FILE_PATH));
249 SysLog(NID_LCL, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
250 return GetAvailableLanguagesFallbackN();
252 // SysTryReturn(NID_LCL, pDoc != null, null, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
254 cur = xmlDocGetRootElement(pDoc.get());
255 SysTryReturn(NID_LCL, cur != null, null, E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
256 SysTryReturn(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, null, E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
258 cur = cur->xmlChildrenNode;
260 pAvailableLanguageList->Construct();
262 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
264 if (cur_node->type == XML_ELEMENT_NODE)
266 std::unique_ptr < char, FreeCharPtr > pLocId((char*)xmlGetProp(cur_node, (const xmlChar *)"id"));
267 Locale loc = _LocaleImpl(pLocId.get()).GetOspLocale();
268 std::unique_ptr<String> pLanguageLocaleID(new (std::nothrow) String(loc.GetLanguageCodeString()));
269 SysTryReturn(NID_LCL, pLanguageLocaleID, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
271 if (!pAvailableLanguageList->Contains(*(pLanguageLocaleID.get())))
273 result r = pAvailableLanguageList->Add(pLanguageLocaleID.get());
274 SysTryReturn(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
275 "[%s] It is failed to add a locale string [%ls].", GetErrorMessage(E_SYSTEM), pLanguageLocaleID->GetPointer());
276 pLanguageLocaleID.release();
281 SetLastResult(E_SUCCESS);
282 return pAvailableLanguageList.release();
286 _LocaleManagerImpl::GetAvailableLanguageLocalesN(void)
288 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
289 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
291 xmlNodePtr cur = null;
292 std::unique_ptr< xmlDoc, FreeXmlDoc > pDoc(xmlParseFile(LANGUAGE_LIST_FILE_PATH));
293 SysTryReturn(NID_LCL, pDoc != null, null, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
295 cur = xmlDocGetRootElement(pDoc.get());
296 SysTryReturn(NID_LCL, cur != null, null, E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
297 SysTryReturn(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, null, E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
299 cur = cur->xmlChildrenNode;
301 pAvailableLanguageList->Construct();
303 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
305 if (cur_node->type == XML_ELEMENT_NODE)
307 std::unique_ptr< char, FreeCharPtr> pLocId((char*)xmlGetProp(cur_node, (const xmlChar *)"id"));
308 Locale loc = _LocaleImpl(pLocId.get()).GetOspLocale();
309 if (_LocaleImpl::IsSupported(loc))
311 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(loc));
312 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
313 "[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
315 if (!pAvailableLanguageList->Contains(*(pLocale.get())))
317 result r = pAvailableLanguageList->Add(pLocale.get());
318 SysTryReturn(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
319 "[%s] It is failed to add a locale string [%s].", GetErrorMessage(E_SYSTEM), pLocId.get());
326 SetLastResult(E_SUCCESS);
327 return pAvailableLanguageList.release();
332 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
334 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
335 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
337 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
338 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
339 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
340 pTimeZoneMap->Construct();
342 result r = E_SUCCESS;
343 int resultLength = -1;
344 UErrorCode ec = U_ZERO_ERROR;
345 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
346 std::unique_ptr<IMap> pTZMap(GetAvailableTimeZonesN());
348 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
350 while (pIcuTZStr != null)
352 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(pIcuTZStr));
353 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
354 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
355 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())) && pTZMap->ContainsKey(*(pTimeZone.get())))
357 std::unique_ptr< String > pDummyValue(new (std::nothrow) String());
358 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
359 r = pTimeZoneMap->Add(pTimeZone.get(), pDummyValue.get());
360 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] It is failed to add a TZ into Map.");
362 pDummyValue.release();
365 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
367 SetLastResult(E_SUCCESS);
368 return pTimeZoneMap.release();
371 static const int TIMEZONE_MAX = 224;
372 static const char* TimeZoneList[TIMEZONE_MAX] =
376 "Africa/Addis_Ababa",
386 "Africa/Dar_es_Salaam",
392 "Africa/Johannesburg",
408 "Africa/Ouagadougou",
413 "America/Argentina/Buenos_Aires",
421 "America/Costa_Rica",
424 "America/El_Salvador",
426 "America/Guadeloupe",
432 "America/Indiana/Indianapolis",
434 "America/Kentucky/Louisville",
437 "America/Los_Angeles",
440 "America/Martinique",
442 "America/Mexico_City",
443 "America/Montevideo",
448 "America/Paramaribo",
450 "America/Port-au-Prince",
451 "America/Puerto_Rico",
455 "America/Santo_Domingo",
459 "America/Tegucigalpa",
522 "Asia/Yekaterinburg",
526 "Atlantic/Reykjavik",
527 "Atlantic/South_Georgia",
528 "Australia/Adelaide",
529 "Australia/Brisbane",
530 "Australia/Canberra",
533 "Australia/Melbourne",
551 "Europe/Kaliningrad",
579 "Indian/Antananarivo",
602 _LocaleManagerImpl::GetAvailableTimeZonesFallbackN(void)
604 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
605 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
606 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
608 result r = E_SUCCESS;
611 pTimeZoneMap->Construct();
615 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(TimeZoneList[index++]));
616 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
617 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
619 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
621 std::unique_ptr< String > pDummyValue (new (std::nothrow) String());
622 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
624 r = pTimeZoneMap->Add(pTimeZone.get(), pDummyValue.get());
625 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make Timezone list.");
628 pDummyValue.release();
630 }while (index < TIMEZONE_MAX);
632 SetLastResult(E_SUCCESS);
633 return pTimeZoneMap.release();
638 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
641 String tzFilePath(TIMEZONE_LIST_FILE_PATH);
642 result r = E_SUCCESS;
644 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
645 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
646 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
647 r = file.Construct(tzFilePath, "r");
650 SysLog(NID_LCL,"[E_FILE_NOT_FOUND] It is failed to get the tzlist from the ini file.");
651 return GetAvailableTimeZonesFallbackN();
653 // SysTryReturn(NID_LCL, r == E_SUCCESS, null,E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the tzlist from the ini file.");
655 pTimeZoneMap->Construct();
659 std::unique_ptr<String> pTimeZone(new (std::nothrow) String());
660 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
661 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
662 r = file.Read(*(pTimeZone.get()));
663 if ( r == E_END_OF_FILE)
667 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r, "[%s] It is failed to read the tzlist.", GetErrorMessage(r));
668 pTimeZone->Replace(L"\n", L"\0");
670 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
672 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
673 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
674 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
676 r = pTimeZoneMap->Add(pTimeZone.get(), pDummyValue.get());
677 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r,"[%s] It is failed to make the tz list.", GetErrorMessage(r));
679 pDummyValue.release();
683 SetLastResult(E_SUCCESS);
684 return pTimeZoneMap.release();
689 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
691 std::unique_ptr<U_ICU_NAMESPACE::StringEnumeration> pIcuTzList(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
692 SysTryReturn(NID_LCL, pIcuTzList, null, E_SYSTEM, "[E_SYSTEM] It is failed to get Icu TZ list.");
693 IMap* pTzList = GetAvailableTimeZonesN(pIcuTzList.get());
699 _LocaleManagerImpl::GetSystemTimeZone(void)
701 std::unique_ptr< char, FreeCharPtr> tzId(vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID));
702 SysTryReturn(NID_LCL, tzId, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone.");
703 SysLog(NID_LCL, "System TimeZone id [%s]", tzId.get());
707 struct tm* pGmTime = null;
710 SysTryReturn(NID_LCL, currTime, TimeZone(-1, ""), E_SYSTEM, "It is failed to get system time.");
711 pGmTime = gmtime(&currTime);
712 SysTryReturn(NID_LCL, pGmTime, TimeZone(-1, ""), E_SYSTEM, "It is failed to convert the time value to UTC time.");
714 utcTime.SetValue(pGmTime->tm_year + 1900, pGmTime->tm_mon + 1, pGmTime->tm_mday, pGmTime->tm_hour, pGmTime->tm_min, pGmTime->tm_sec);
716 result r = Tizen::Locales::TimeZone::GetTimeZone(String(tzId.get()), utcTime, timeZone);
717 SysTryReturn(NID_LCL, r == E_SUCCESS, TimeZone(-1, ""), r, "[%s] error occurs.", GetErrorMessage(r));
722 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
724 isSupportedLocale = _LocaleImpl::IsSupported(locale);