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>
28 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
31 #include "FLcl_LocaleImpl.h"
32 #include "FLcl_TimeZoneImpl.h"
33 #include "FLcl_LocaleManagerImpl.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Utility;
38 using namespace Tizen::Base::Collection;
40 namespace Tizen { namespace Locales
43 static const int TIMEZONE_MAX = 224;
44 static const char* TimeZoneList[TIMEZONE_MAX] =
58 "Africa/Dar_es_Salaam",
64 "Africa/Johannesburg",
85 "America/Argentina/Buenos_Aires",
96 "America/El_Salvador",
104 "America/Indiana/Indianapolis",
106 "America/Kentucky/Louisville",
109 "America/Los_Angeles",
112 "America/Martinique",
114 "America/Mexico_City",
115 "America/Montevideo",
120 "America/Paramaribo",
122 "America/Port-au-Prince",
123 "America/Puerto_Rico",
127 "America/Santo_Domingo",
131 "America/Tegucigalpa",
194 "Asia/Yekaterinburg",
198 "Atlantic/Reykjavik",
199 "Atlantic/South_Georgia",
200 "Australia/Adelaide",
201 "Australia/Brisbane",
202 "Australia/Canberra",
205 "Australia/Melbourne",
223 "Europe/Kaliningrad",
251 "Indian/Antananarivo",
273 _LocaleManagerImpl::GetSystemLocale(void)
276 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
278 SetLastResult(E_SUCCESS);
280 Locale ospLoc = _LocaleImpl(pRegionPtr).GetOspLocale();
285 SetLastResult(E_SYSTEM);
286 return Locale(LANGUAGE_INVALID, COUNTRY_INVALID, null);
290 _LocaleManagerImpl::GetAvailableLocalesN(void)
292 result r = E_SUCCESS;
294 const U_ICU_NAMESPACE::Locale* pIcuLocaleList = U_ICU_NAMESPACE::Locale::getAvailableLocales(count);
295 SysTryReturn(NID_LCL, count > 0, null, E_SYSTEM,
296 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
298 std::unique_ptr<LinkedList, AllElementsDeleter> pAvailableLocaleList(new (std::nothrow) LinkedList());
299 SysTryReturn(NID_LCL, pAvailableLocaleList, null, E_OUT_OF_MEMORY,
300 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
302 for (int i = 0; i < count; i++)
304 SysTryReturn(NID_LCL, (pIcuLocaleList + i) != null, null, E_SYSTEM,
305 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
307 Locale ospLocale = _LocaleImpl(*(pIcuLocaleList + i)).GetOspLocale();
308 if (ospLocale.GetLanguageCode() != LANGUAGE_INVALID && ospLocale.GetCountryCode() != COUNTRY_INVALID)
310 Locale* pLocale = new (std::nothrow) Locale(ospLocale);
311 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
312 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
313 if (!pAvailableLocaleList->Contains(*pLocale))
315 r = pAvailableLocaleList->Add(*pLocale);
319 SetLastResult(E_SYSTEM);
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).Get3LetterCountryCodeString());
345 _LocaleManagerImpl::GetAvailableLanguagesN(void)
347 result r = E_SUCCESS;
348 std::unique_ptr<IList, AllElementsDeleter> pLocaleList (GetAvailableLocalesN());
349 std::unique_ptr<HashMap> pLanguageMap(new (std::nothrow) HashMap());
351 SysTryReturn(NID_LCL, pLanguageMap, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
353 pLanguageMap->Construct();
355 for (int i = 0; i < pLocaleList->GetCount() ; i++)
357 Locale* pLocale = (Locale*)pLocaleList->GetAt(i);
358 std::unique_ptr<String> pLanguageCode(new (std::nothrow) String(pLocale->GetLanguageCodeString()));
359 SysTryReturn(NID_LCL, pLanguageCode, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
361 if (!pLanguageMap->ContainsKey(*pLanguageCode))
363 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
364 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
366 r = pLanguageMap->Add(*(pLanguageCode.get()), *(pDummyValue.get()));
370 SetLastResult(E_UNSUPPORTED_OPERATION);
373 pLanguageCode.release();
374 pDummyValue.release();
378 IList* pAvailableLanguageList;
379 pAvailableLanguageList = pLanguageMap->GetKeysN();
380 SetLastResult(E_SUCCESS);
381 pLanguageMap->RemoveAll();
382 return pAvailableLanguageList;
387 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
389 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
390 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
392 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
394 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
395 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
396 pTimeZoneMap->Construct();
397 result r = E_SUCCESS;
398 int resultLength = -1;
399 UErrorCode ec = U_ZERO_ERROR;
400 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
401 IMap* pTZMap = GetAvailableTimeZonesN();
404 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
406 while (pIcuTZStr != null)
408 String* pTimeZone = new (std::nothrow) String(pIcuTZStr);
409 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
410 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
411 if (!pTimeZoneMap->ContainsKey(*pTimeZone) && pTZMap->ContainsKey(*pTimeZone))
413 String* pDummyValue = new (std::nothrow) String();
414 r = pTimeZoneMap->Add(*pTimeZone, *pDummyValue);
420 SetLastResult(E_SYSTEM);
425 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
427 SetLastResult(E_SUCCESS);
429 return pTimeZoneMap.release();
433 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
435 std::unique_ptr<HashMap, AllElementsDeleter> pTimeZoneMap(new (std::nothrow) HashMap());
437 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
438 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
440 result r = E_SUCCESS;
443 pTimeZoneMap->Construct();
447 String* pTimeZone = new (std::nothrow) String(TimeZoneList[index++]);
448 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
449 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
451 if (!pTimeZoneMap->ContainsKey(*pTimeZone))
453 String* pDummyValue = new (std::nothrow) String();
455 r = pTimeZoneMap->Add(*pTimeZone, *pDummyValue);
460 SetLastResult(E_SYSTEM);
464 }while (index < TIMEZONE_MAX);
466 SetLastResult(E_SUCCESS);
467 return pTimeZoneMap.release();
471 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
473 return GetAvailableTimeZonesN(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
478 _LocaleManagerImpl::GetSystemTimeZone(void)
480 char tz[PATH_MAX + 1];
481 int len = readlink("/opt/etc/localtime", tz, PATH_MAX);
482 std::unique_ptr<U_ICU_NAMESPACE::TimeZone> pTzICU(null);
486 pTzICU.reset(U_ICU_NAMESPACE::TimeZone::createTimeZone(tz + 20));
490 pTzICU.reset(U_ICU_NAMESPACE::TimeZone::createDefault());
495 _TimeZoneImpl tzImpl(*pTzICU.get(), U_ICU_NAMESPACE::Calendar::getNow());
496 return tzImpl.GetOspTimeZone();
499 return TimeZone(-1, "");
503 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
505 isSupportedLocale = _LocaleImpl::GetLocaleImpl(locale)->IsLocaleSupported();