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 std::unique_ptr< 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.get())))
320 r = pAvailableLocaleList->Add(*(pLocale.get()));
321 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make the locale list");
327 SetLastResult(E_SUCCESS);
328 return pAvailableLocaleList.release();
332 _LocaleManagerImpl::GetSelectedLanguage(void)
336 int ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &pLanguagePtr);
337 SysTryReturn(NID_LCL, ret == RUNTIME_INFO_ERROR_NONE, String(), E_SYSTEM,
338 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
339 String language(_LocaleImpl(pLanguagePtr).GetLanguageCodeString(false));
346 _LocaleManagerImpl::GetAvailableLanguagesN(void)
348 xmlDocPtr doc = null;
349 xmlNodePtr cur = null;
351 std::unique_ptr<ArrayList, AllElementsDeleter> pAvailableLanguageList(new (std::nothrow) ArrayList());
352 SysTryCatch(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
354 doc = xmlParseFile(LANGUAGE_LIST_FILE_PATH);
355 SysTryCatch(NID_LCL, doc != null, , E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
357 cur = xmlDocGetRootElement(doc);
358 SysTryCatch(NID_LCL, cur != null, , E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
359 SysTryCatch(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, , E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
361 cur = cur->xmlChildrenNode;
363 pAvailableLanguageList->Construct();
365 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
367 if (cur_node->type == XML_ELEMENT_NODE)
369 char* pLocId = (char*)xmlGetProp(cur_node, (const xmlChar *)"id");
370 Locale loc = _LocaleImpl(pLocId).GetOspLocale();
371 std::unique_ptr<String> pLanguageLocaleID(new (std::nothrow) String(loc.GetLanguageCodeString()));
372 SysTryCatch(NID_LCL, pLanguageLocaleID, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
374 result r = pAvailableLanguageList->Add(pLanguageLocaleID.get());
375 SysTryCatch(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
376 "[%s] It is failed to add a locale string [%ls].", GetErrorMessage(E_SYSTEM), pLanguageLocaleID->GetPointer());
377 pLanguageLocaleID.release();
381 SetLastResult(E_SUCCESS);
388 return pAvailableLanguageList.release();
395 SysLog(NID_LCL, "It is calling fallback api.");
396 return GetAvailableLanguagesFallbackN();
401 _LocaleManagerImpl::GetAvailableLanguagesFallbackN(void)
403 std::unique_ptr<IList, AllElementsDeleter> pLocaleList (GetAvailableLocalesN());
404 std::unique_ptr<HashMap> pLanguageMap(new (std::nothrow) HashMap());
406 SysTryReturn(NID_LCL, pLanguageMap, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
408 pLanguageMap->Construct();
410 for (int i = 0; i < pLocaleList->GetCount() ; i++)
412 Locale* pLocale = (Locale*)pLocaleList->GetAt(i);
413 std::unique_ptr<String> pLanguageCode(new (std::nothrow) String(pLocale->GetLanguageCodeString()));
414 SysTryReturn(NID_LCL, pLanguageCode, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
416 if (!pLanguageMap->ContainsKey(*pLanguageCode))
418 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
419 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
421 result r = pLanguageMap->Add(*(pLanguageCode.get()), *(pDummyValue.get()));
422 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make a language list.");
423 pLanguageCode.release();
424 pDummyValue.release();
428 IList* pAvailableLanguageList;
429 pAvailableLanguageList = pLanguageMap->GetKeysN();
430 SetLastResult(E_SUCCESS);
431 pLanguageMap->RemoveAll();
432 return pAvailableLanguageList;
437 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
439 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
440 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
442 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
444 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
445 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
446 pTimeZoneMap->Construct();
447 result r = E_SUCCESS;
448 int resultLength = -1;
449 UErrorCode ec = U_ZERO_ERROR;
450 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
451 IMap* pTZMap = GetAvailableTimeZonesN();
454 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
456 while (pIcuTZStr != null)
458 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(pIcuTZStr));
459 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
460 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
461 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())) && pTZMap->ContainsKey(*(pTimeZone.get())))
463 std::unique_ptr< String > pDummyValue(new (std::nothrow) String());
464 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
465 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
470 SetLastResult(E_SYSTEM);
474 pDummyValue.release();
477 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
479 SetLastResult(E_SUCCESS);
482 return pTimeZoneMap.release();
487 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
490 String tzFilePath(TIMEZONE_LIST_FILE_PATH);
491 result r = E_SUCCESS;
493 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
494 SysTryCatch(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
495 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
496 r = file.Construct(tzFilePath, "r");
497 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.");
499 pTimeZoneMap->Construct();
503 std::unique_ptr<String> pTimeZone(new (std::nothrow) String());
504 SysTryCatch(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
505 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
506 r = file.Read(*(pTimeZone.get()));
507 if ( r == E_END_OF_FILE)
511 SysTryCatch(NID_LCL, r == E_SUCCESS, , r, "[%s] It is failed to read the tzlist.", GetErrorMessage(r));
512 pTimeZone->Replace(L"\n", L"\0");
514 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
516 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
517 SysTryCatch(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
518 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
520 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
521 SysTryCatch(NID_LCL, r == E_SUCCESS, , r,"[%s] It is failed to add the tz into the tz map.", GetErrorMessage(r));
523 pDummyValue.release();
527 SetLastResult(E_SUCCESS);
528 return pTimeZoneMap.release();
531 return GetAvailableTimeZonesFallbackN();
536 _LocaleManagerImpl::GetAvailableTimeZonesFallbackN(void)
538 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
539 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
540 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
542 result r = E_SUCCESS;
545 pTimeZoneMap->Construct();
549 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(TimeZoneList[index++]));
550 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
551 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
553 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
555 std::unique_ptr< String > pDummyValue (new (std::nothrow) String());
556 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
558 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
559 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make Timezone list.");
562 pDummyValue.release();
564 }while (index < TIMEZONE_MAX);
566 SetLastResult(E_SUCCESS);
567 return pTimeZoneMap.release();
572 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
574 return GetAvailableTimeZonesN(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
579 _LocaleManagerImpl::GetSystemTimeZone(void)
581 char tz[PATH_MAX + 1];
582 int len = readlink("/opt/etc/localtime", tz, PATH_MAX);
584 SysTryReturn(NID_LCL, len > 0, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone");
586 SysTryReturn(NID_LCL, len > 20, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone");
588 String tzId(tz + 20);
589 SysLog(NID_LCL, "System TimeZone id [%ls]", tzId.GetPointer());
591 result r = Tizen::Locales::TimeZone::GetTimeZone(tzId, timeZone);
592 SysTryReturn(NID_LCL, r == E_SUCCESS, TimeZone(-1, ""), r, "[%s] error occurs.", GetErrorMessage(r));
597 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
599 isSupportedLocale = _LocaleImpl::IsSupported(locale);