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>
23 #include <runtime_info.h>
24 #include <unicode/calendar.h>
25 #include <unicode/timezone.h>
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
30 #include <FBaseSysLog.h>
31 #include <FBase_StringConverter.h>
33 #include "FLcl_LocaleImpl.h"
34 #include "FLcl_TimeZoneImpl.h"
35 #include "FLcl_LocaleManagerImpl.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Io;
43 namespace Tizen { namespace Locales
45 static const char* LANGUAGE_LIST_FILE_PATH ="/opt/data/setting/langlist.xml";
46 static const char* TIMEZONE_LIST_FILE_PATH = "/opt/data/setting/tzlist.ini";
47 static const int TIMEZONE_MAX = 224;
48 static const char* TimeZoneList[TIMEZONE_MAX] =
62 "Africa/Dar_es_Salaam",
68 "Africa/Johannesburg",
89 "America/Argentina/Buenos_Aires",
100 "America/El_Salvador",
102 "America/Guadeloupe",
108 "America/Indiana/Indianapolis",
110 "America/Kentucky/Louisville",
113 "America/Los_Angeles",
116 "America/Martinique",
118 "America/Mexico_City",
119 "America/Montevideo",
124 "America/Paramaribo",
126 "America/Port-au-Prince",
127 "America/Puerto_Rico",
131 "America/Santo_Domingo",
135 "America/Tegucigalpa",
198 "Asia/Yekaterinburg",
202 "Atlantic/Reykjavik",
203 "Atlantic/South_Georgia",
204 "Australia/Adelaide",
205 "Australia/Brisbane",
206 "Australia/Canberra",
209 "Australia/Melbourne",
227 "Europe/Kaliningrad",
255 "Indian/Antananarivo",
277 _LocaleManagerImpl::GetSystemLocale(void)
280 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
282 SetLastResult(E_SUCCESS);
284 Locale ospLoc = _LocaleImpl(pRegionPtr).GetOspLocale();
289 SetLastResult(E_SYSTEM);
290 return Locale(LANGUAGE_INVALID, COUNTRY_INVALID, null);
294 _LocaleManagerImpl::GetAvailableLocalesN(void)
296 result r = E_SUCCESS;
298 const U_ICU_NAMESPACE::Locale* pIcuLocaleList = U_ICU_NAMESPACE::Locale::getAvailableLocales(count);
299 SysTryReturn(NID_LCL, count > 0, null, E_SYSTEM,
300 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
302 std::unique_ptr<LinkedList, AllElementsDeleter> pAvailableLocaleList(new (std::nothrow) LinkedList());
303 SysTryReturn(NID_LCL, pAvailableLocaleList, null, E_OUT_OF_MEMORY,
304 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
306 for (int i = 0; i < count; i++)
308 SysTryReturn(NID_LCL, (pIcuLocaleList + i) != null, null, E_SYSTEM,
309 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
311 Locale ospLocale = _LocaleImpl(*(pIcuLocaleList + i)).GetOspLocale();
312 if (_LocaleImpl::IsSupported(ospLocale))
314 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(ospLocale));
315 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
316 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
317 if (!pAvailableLocaleList->Contains(*(pLocale.get())))
319 r = pAvailableLocaleList->Add(*(pLocale.get()));
320 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make the locale list");
326 SetLastResult(E_SUCCESS);
327 return pAvailableLocaleList.release();
331 _LocaleManagerImpl::GetSelectedLanguage(void)
335 int ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &pLanguagePtr);
336 SysTryReturn(NID_LCL, ret == RUNTIME_INFO_ERROR_NONE, String(), E_SYSTEM,
337 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
338 String language(_LocaleImpl(pLanguagePtr).GetLanguageCodeString(false));
345 _LocaleManagerImpl::GetAvailableLanguagesN(void)
347 xmlDocPtr doc = null;
348 xmlNodePtr cur = null;
350 std::unique_ptr<ArrayList, AllElementsDeleter> pAvailableLanguageList(new (std::nothrow) ArrayList());
351 SysTryCatch(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
353 doc = xmlParseFile(LANGUAGE_LIST_FILE_PATH);
354 SysTryCatch(NID_LCL, doc != null, , E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
356 cur = xmlDocGetRootElement(doc);
357 SysTryCatch(NID_LCL, cur != null, , E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
358 SysTryCatch(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, , E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
360 cur = cur->xmlChildrenNode;
362 pAvailableLanguageList->Construct();
364 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
366 if (cur_node->type == XML_ELEMENT_NODE)
368 char* pLocId = (char*)xmlGetProp(cur_node, (const xmlChar *)"id");
369 Locale loc = _LocaleImpl(pLocId).GetOspLocale();
370 std::unique_ptr<String> pLanguageLocaleID(new (std::nothrow) String(loc.GetLanguageCodeString()));
371 SysTryCatch(NID_LCL, pLanguageLocaleID, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
373 result r = pAvailableLanguageList->Add(pLanguageLocaleID.get());
374 SysTryCatch(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
375 "[%s] It is failed to add a locale string [%ls].", GetErrorMessage(E_SYSTEM), pLanguageLocaleID->GetPointer());
376 pLanguageLocaleID.release();
380 SetLastResult(E_SUCCESS);
387 return pAvailableLanguageList.release();
394 SysLog(NID_LCL, "It is calling fallback api.");
395 return GetAvailableLanguagesFallbackN();
400 _LocaleManagerImpl::GetAvailableLanguagesFallbackN(void)
402 std::unique_ptr<IList, AllElementsDeleter> pLocaleList (GetAvailableLocalesN());
403 std::unique_ptr<HashMap> pLanguageMap(new (std::nothrow) HashMap());
405 SysTryReturn(NID_LCL, pLanguageMap, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
407 pLanguageMap->Construct();
409 for (int i = 0; i < pLocaleList->GetCount() ; i++)
411 Locale* pLocale = (Locale*)pLocaleList->GetAt(i);
412 std::unique_ptr<String> pLanguageCode(new (std::nothrow) String(pLocale->GetLanguageCodeString()));
413 SysTryReturn(NID_LCL, pLanguageCode, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
415 if (!pLanguageMap->ContainsKey(*pLanguageCode))
417 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
418 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
420 result r = pLanguageMap->Add(*(pLanguageCode.get()), *(pDummyValue.get()));
421 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make a language list.");
422 pLanguageCode.release();
423 pDummyValue.release();
427 IList* pAvailableLanguageList;
428 pAvailableLanguageList = pLanguageMap->GetKeysN();
429 SetLastResult(E_SUCCESS);
430 pLanguageMap->RemoveAll();
431 return pAvailableLanguageList;
436 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
438 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
439 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
441 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
443 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
444 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
445 pTimeZoneMap->Construct();
446 result r = E_SUCCESS;
447 int resultLength = -1;
448 UErrorCode ec = U_ZERO_ERROR;
449 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
450 IMap* pTZMap = GetAvailableTimeZonesN();
453 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
455 while (pIcuTZStr != null)
457 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(pIcuTZStr));
458 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
459 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
460 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())) && pTZMap->ContainsKey(*(pTimeZone.get())))
462 std::unique_ptr< String > pDummyValue(new (std::nothrow) String());
463 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
464 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
469 SetLastResult(E_SYSTEM);
473 pDummyValue.release();
476 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
478 SetLastResult(E_SUCCESS);
481 return pTimeZoneMap.release();
486 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
489 String tzFilePath(TIMEZONE_LIST_FILE_PATH);
490 result r = E_SUCCESS;
492 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
493 SysTryCatch(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
494 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
495 r = file.Construct(tzFilePath, "r");
496 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.");
498 pTimeZoneMap->Construct();
502 std::unique_ptr<String> pTimeZone(new (std::nothrow) String());
503 SysTryCatch(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
504 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
505 r = file.Read(*(pTimeZone.get()));
506 if ( r == E_END_OF_FILE)
510 SysTryCatch(NID_LCL, r == E_SUCCESS, , r, "[%s] It is failed to read the tzlist.", GetErrorMessage(r));
511 pTimeZone->Replace(L"\n", L"\0");
513 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
515 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
516 SysTryCatch(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
517 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
519 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
520 SysTryCatch(NID_LCL, r == E_SUCCESS, , r,"[%s] It is failed to add the tz into the tz map.", GetErrorMessage(r));
522 pDummyValue.release();
526 SetLastResult(E_SUCCESS);
527 return pTimeZoneMap.release();
530 return GetAvailableTimeZonesFallbackN();
535 _LocaleManagerImpl::GetAvailableTimeZonesFallbackN(void)
537 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
538 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
539 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
541 result r = E_SUCCESS;
544 pTimeZoneMap->Construct();
548 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(TimeZoneList[index++]));
549 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
550 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
552 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
554 std::unique_ptr< String > pDummyValue (new (std::nothrow) String());
555 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
557 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
558 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make Timezone list.");
561 pDummyValue.release();
563 }while (index < TIMEZONE_MAX);
565 SetLastResult(E_SUCCESS);
566 return pTimeZoneMap.release();
571 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
573 return GetAvailableTimeZonesN(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
578 _LocaleManagerImpl::GetSystemTimeZone(void)
580 char tz[PATH_MAX + 1];
581 int len = readlink("/opt/etc/localtime", tz, PATH_MAX);
583 SysTryReturn(NID_LCL, len > 0, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone");
585 SysTryReturn(NID_LCL, len > 20, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone");
587 String tzId(tz + 20);
588 SysLog(NID_LCL, "System TimeZone id [%ls]", tzId.GetPointer());
590 result r = Tizen::Locales::TimeZone::GetTimeZone(tzId, timeZone);
591 SysTryReturn(NID_LCL, r == E_SUCCESS, TimeZone(-1, ""), r, "[%s] error occurs.", GetErrorMessage(r));
596 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
598 isSupportedLocale = _LocaleImpl::IsSupported(locale);