2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FLcl_LocaleManagerImpl.cpp
20 * @brief This is the implementation file for _LocaleManagerImpl class.
22 #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>
31 #include <FBaseSysLog.h>
32 #include <FBase_StringConverter.h>
34 #include "FLcl_LocaleImpl.h"
35 #include "FLcl_TimeZoneImpl.h"
36 #include "FLcl_LocaleManagerImpl.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
44 namespace Tizen { namespace Locales
46 static const char* LANGUAGE_LIST_FILE_PATH ="/opt/data/setting/langlist.xml";
47 static const char* TIMEZONE_LIST_FILE_PATH = "/opt/data/setting/tzlist.ini";
48 static const int TIMEZONE_MAX = 224;
49 static const char* TimeZoneList[TIMEZONE_MAX] =
63 "Africa/Dar_es_Salaam",
69 "Africa/Johannesburg",
90 "America/Argentina/Buenos_Aires",
101 "America/El_Salvador",
103 "America/Guadeloupe",
109 "America/Indiana/Indianapolis",
111 "America/Kentucky/Louisville",
114 "America/Los_Angeles",
117 "America/Martinique",
119 "America/Mexico_City",
120 "America/Montevideo",
125 "America/Paramaribo",
127 "America/Port-au-Prince",
128 "America/Puerto_Rico",
132 "America/Santo_Domingo",
136 "America/Tegucigalpa",
199 "Asia/Yekaterinburg",
203 "Atlantic/Reykjavik",
204 "Atlantic/South_Georgia",
205 "Australia/Adelaide",
206 "Australia/Brisbane",
207 "Australia/Canberra",
210 "Australia/Melbourne",
228 "Europe/Kaliningrad",
256 "Indian/Antananarivo",
278 _LocaleManagerImpl::GetSystemLocale(void)
281 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
283 SetLastResult(E_SUCCESS);
285 Locale ospLoc = _LocaleImpl(pRegionPtr).GetOspLocale();
290 SetLastResult(E_SYSTEM);
291 return Locale(LANGUAGE_INVALID, COUNTRY_INVALID, null);
295 _LocaleManagerImpl::GetAvailableLocalesN(void)
297 result r = E_SUCCESS;
299 const U_ICU_NAMESPACE::Locale* pIcuLocaleList = U_ICU_NAMESPACE::Locale::getAvailableLocales(count);
300 SysTryReturn(NID_LCL, count > 0, null, E_SYSTEM,
301 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
303 std::unique_ptr<LinkedList, AllElementsDeleter> pAvailableLocaleList(new (std::nothrow) LinkedList());
304 SysTryReturn(NID_LCL, pAvailableLocaleList, null, E_OUT_OF_MEMORY,
305 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
307 for (int i = 0; i < count; i++)
309 SysTryReturn(NID_LCL, (pIcuLocaleList + i) != null, null, E_SYSTEM,
310 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
312 Locale ospLocale = _LocaleImpl(*(pIcuLocaleList + i)).GetOspLocale();
313 if (_LocaleImpl::IsSupported(ospLocale))
315 Locale* pLocale = new (std::nothrow) Locale(ospLocale);
316 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
317 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
318 if (!pAvailableLocaleList->Contains(*pLocale))
320 r = pAvailableLocaleList->Add(*pLocale);
324 SetLastResult(E_SYSTEM);
331 SetLastResult(E_SUCCESS);
332 return pAvailableLocaleList.release();
336 _LocaleManagerImpl::GetSelectedLanguage(void)
340 int ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &pLanguagePtr);
341 SysTryReturn(NID_LCL, ret == RUNTIME_INFO_ERROR_NONE, String(), E_SYSTEM,
342 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
343 String language(_LocaleImpl(pLanguagePtr).GetLanguageCodeString(false));
350 _LocaleManagerImpl::GetAvailableLanguagesN(void)
352 xmlDocPtr doc = null;
353 xmlNodePtr cur = null;
355 std::unique_ptr<ArrayList, AllElementsDeleter> pAvailableLanguageList(new (std::nothrow) ArrayList());
356 SysTryCatch(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
358 doc = xmlParseFile(LANGUAGE_LIST_FILE_PATH);
359 SysTryCatch(NID_LCL, doc != null, , E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
361 cur = xmlDocGetRootElement(doc);
362 SysTryCatch(NID_LCL, cur != null, , E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
363 SysTryCatch(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, , E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
365 cur = cur->xmlChildrenNode;
367 pAvailableLanguageList->Construct();
369 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
371 if (cur_node->type == XML_ELEMENT_NODE)
373 char* pLocId = (char*)xmlGetProp(cur_node, (const xmlChar *)"id");
374 Locale loc = _LocaleImpl(pLocId).GetOspLocale();
375 std::unique_ptr<String> pLanguageLocaleID(new (std::nothrow) String(loc.GetLanguageCodeString()));
376 SysTryCatch(NID_LCL, pLanguageLocaleID, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
378 result r = pAvailableLanguageList->Add(pLanguageLocaleID.get());
379 SysTryCatch(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
380 "[%s] It is failed to add a locale string [%ls].", GetErrorMessage(E_SYSTEM), pLanguageLocaleID->GetPointer());
381 pLanguageLocaleID.release();
385 SetLastResult(E_SUCCESS);
392 return pAvailableLanguageList.release();
399 SysLog(NID_LCL, "It is calling fallback api.");
400 return GetAvailableLanguagesFallbackN();
405 _LocaleManagerImpl::GetAvailableLanguagesFallbackN(void)
407 std::unique_ptr<IList, AllElementsDeleter> pLocaleList (GetAvailableLocalesN());
408 std::unique_ptr<HashMap> pLanguageMap(new (std::nothrow) HashMap());
410 SysTryReturn(NID_LCL, pLanguageMap, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
412 pLanguageMap->Construct();
414 for (int i = 0; i < pLocaleList->GetCount() ; i++)
416 Locale* pLocale = (Locale*)pLocaleList->GetAt(i);
417 std::unique_ptr<String> pLanguageCode(new (std::nothrow) String(pLocale->GetLanguageCodeString()));
418 SysTryReturn(NID_LCL, pLanguageCode, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
420 if (!pLanguageMap->ContainsKey(*pLanguageCode))
422 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
423 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
425 result r = pLanguageMap->Add(*(pLanguageCode.get()), *(pDummyValue.get()));
429 SetLastResult(E_SYSTEM);
432 pLanguageCode.release();
433 pDummyValue.release();
437 IList* pAvailableLanguageList;
438 pAvailableLanguageList = pLanguageMap->GetKeysN();
439 SetLastResult(E_SUCCESS);
440 pLanguageMap->RemoveAll();
441 return pAvailableLanguageList;
446 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
448 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
449 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
451 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
453 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
454 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
455 pTimeZoneMap->Construct();
456 result r = E_SUCCESS;
457 int resultLength = -1;
458 UErrorCode ec = U_ZERO_ERROR;
459 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
460 IMap* pTZMap = GetAvailableTimeZonesN();
463 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
465 while (pIcuTZStr != null)
467 String* pTimeZone = new (std::nothrow) String(pIcuTZStr);
468 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
469 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
470 if (!pTimeZoneMap->ContainsKey(*pTimeZone) && pTZMap->ContainsKey(*pTimeZone))
472 String* pDummyValue = new (std::nothrow) String();
473 r = pTimeZoneMap->Add(*pTimeZone, *pDummyValue);
479 SetLastResult(E_SYSTEM);
484 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
486 SetLastResult(E_SUCCESS);
488 return pTimeZoneMap.release();
493 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
496 String tzFilePath(TIMEZONE_LIST_FILE_PATH);
497 result r = E_SUCCESS;
499 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
500 SysTryCatch(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
501 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
502 r = file.Construct(tzFilePath, "r");
503 SysTryCatch(NID_LCL, r == E_SUCCESS, ,E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the tzlist from the ini file.");
505 pTimeZoneMap->Construct();
509 std::unique_ptr<String> pTimeZone(new (std::nothrow) String());
510 SysTryCatch(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
511 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
512 r = file.Read(*(pTimeZone.get()));
513 if ( r == E_END_OF_FILE)
517 SysTryCatch(NID_LCL, r == E_SUCCESS, , r, "[%s] It is failed to read the tzlist.", GetErrorMessage(r));
518 pTimeZone->Replace(L"\n", L"\0");
520 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
522 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
523 SysTryCatch(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
524 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
526 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
527 SysTryCatch(NID_LCL, r == E_SUCCESS, , r,"[%s] It is failed to add the tz into the tz map.", GetErrorMessage(r));
529 pDummyValue.release();
533 SetLastResult(E_SUCCESS);
534 return pTimeZoneMap.release();
537 return GetAvailableTimeZonesFallbackN();
542 _LocaleManagerImpl::GetAvailableTimeZonesFallbackN(void)
544 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
545 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
546 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
548 result r = E_SUCCESS;
551 pTimeZoneMap->Construct();
555 String* pTimeZone = new (std::nothrow) String(TimeZoneList[index++]);
556 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
557 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
559 if (!pTimeZoneMap->ContainsKey(*pTimeZone))
561 String* pDummyValue = new (std::nothrow) String();
563 r = pTimeZoneMap->Add(*pTimeZone, *pDummyValue);
568 SetLastResult(E_SYSTEM);
572 }while (index < TIMEZONE_MAX);
574 SetLastResult(E_SUCCESS);
575 return pTimeZoneMap.release();
580 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
582 return GetAvailableTimeZonesN(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
587 _LocaleManagerImpl::GetSystemTimeZone(void)
589 char tz[PATH_MAX + 1];
590 int len = readlink("/opt/etc/localtime", tz, PATH_MAX);
592 SysTryReturn(NID_LCL, len > 0, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone");
594 SysTryReturn(NID_LCL, len > 20, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone");
596 String tzId(tz + 20);
597 SysLog(NID_LCL, "System TimeZone id [%ls]", tzId.GetPointer());
599 result r = Tizen::Locales::TimeZone::GetTimeZone(tzId, timeZone);
600 SysTryReturn(NID_LCL, r == E_SUCCESS, TimeZone(-1, ""), r, "[%s] error occurs.", GetErrorMessage(r));
605 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
607 isSupportedLocale = _LocaleImpl::IsSupported(locale);