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 <unicode/locid.h>
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
33 #include <FBaseSysLog.h>
34 #include <FBase_StringConverter.h>
36 #include "FLcl_LocaleImpl.h"
37 #include "FLcl_TimeZoneImpl.h"
38 #include "FLcl_LocaleManagerImpl.h"
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Utility;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Io;
48 void operator ()(xmlDoc* p)
59 void operator ()(char* p)
68 namespace Tizen { namespace Locales
70 static const char* LANGUAGE_LIST_FILE_PATH ="/opt/usr/data/setting/langlist.xml";
71 static const char* TIMEZONE_LIST_FILE_PATH = "/opt/usr/data/clock/tzlist.ini";
72 static const char* CLOCALE_LIST_FILE_PATH = "/opt/usr/etc/clocale.list";
76 _LocaleManagerImpl::GetSystemLocale(void)
79 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
81 SetLastResult(E_SUCCESS);
83 Locale ospLoc = _LocaleImpl(pRegionPtr).GetOspLocale();
88 SetLastResult(E_SYSTEM);
89 return Locale(LANGUAGE_INVALID, COUNTRY_INVALID, null);
92 U_ICU_NAMESPACE::Locale
93 _LocaleManagerImpl::GetSystemIcuLocale(void)
96 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
98 SetLastResult(E_SUCCESS);
99 U_ICU_NAMESPACE::Locale locale(pRegionPtr);
104 return U_ICU_NAMESPACE::Locale();
109 _LocaleManagerImpl::GetAvailableEglibcLocaesN(void)
112 result r = E_SUCCESS;
113 String clocaleFilePath(CLOCALE_LIST_FILE_PATH);
115 std::unique_ptr<HashMap> pClocaleMap(new (std::nothrow) HashMap(SingleObjectDeleter));
116 SysTryReturn(NID_LCL, pClocaleMap, null, E_OUT_OF_MEMORY,
117 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
118 r = file.Construct(clocaleFilePath, "r");
119 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.");
121 pClocaleMap->Construct();
126 r = file.Read(strBuf);
127 if ( r == E_END_OF_FILE)
131 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r, "[%s] It is failed to read the clocale list.", GetErrorMessage(r));
132 std::unique_ptr< String > pClocaleId(new (std::nothrow) String());
133 SysTryReturn(NID_LCL, pClocaleId, null, E_OUT_OF_MEMORY,
134 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
135 r = strBuf.SubString(0, 5, *pClocaleId);
141 if (!pClocaleMap->ContainsKey(*(pClocaleId.get())))
143 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
144 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
145 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
147 r = pClocaleMap->Add(pClocaleId.get(), pDummyValue.get());
148 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r,
149 "[%s] It is failed to add the clocale id into the clocale map.", GetErrorMessage(r));
150 pClocaleId.release();
151 pDummyValue.release();
154 return pClocaleMap.release();
158 _LocaleManagerImpl::GetAvailableLocalesN(void)
160 result r = E_SUCCESS;
162 const U_ICU_NAMESPACE::Locale* pIcuLocaleList = U_ICU_NAMESPACE::Locale::getAvailableLocales(count);
163 SysTryReturn(NID_LCL, count > 0, null, E_SYSTEM,
164 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
166 std::unique_ptr<LinkedList> pAvailableLocaleList(new (std::nothrow) LinkedList(SingleObjectDeleter));
167 SysTryReturn(NID_LCL, pAvailableLocaleList, null, E_OUT_OF_MEMORY,
168 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
170 std::unique_ptr< IMap > pClocaleMap(GetAvailableEglibcLocaesN());
172 for (int i = 0; i < count; i++)
174 const U_ICU_NAMESPACE::Locale* pIcuLocale = (pIcuLocaleList + i);
175 SysTryReturn(NID_LCL, pIcuLocale, null, E_SYSTEM,
176 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
177 Locale ospLocale = _LocaleImpl(*pIcuLocale).GetOspLocale();
178 if (_LocaleImpl::IsSupported(ospLocale))
180 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(ospLocale));
181 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
182 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
184 String localeId(pIcuLocale->getLanguage());
185 localeId = localeId + L"_" + String(pIcuLocale->getCountry());
187 if (!pAvailableLocaleList->Contains(*(pLocale.get())) && pClocaleMap->ContainsKey(localeId))
189 r = pAvailableLocaleList->Add(pLocale.get());
190 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make the locale list");
196 SetLastResult(E_SUCCESS);
197 return pAvailableLocaleList.release();
201 _LocaleManagerImpl::GetSelectedLanguage(void)
205 int ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &pLanguagePtr);
206 SysTryReturn(NID_LCL, ret == RUNTIME_INFO_ERROR_NONE, String(), E_SYSTEM,
207 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
208 String language(_LocaleImpl(pLanguagePtr).GetLanguageCodeString(false));
215 _LocaleManagerImpl::GetAvailableLanguagesN(void)
217 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
218 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
220 xmlNodePtr cur = null;
221 std::unique_ptr< xmlDoc, FreeXmlDoc > pDoc(xmlParseFile(LANGUAGE_LIST_FILE_PATH));
223 SysTryReturn(NID_LCL, pDoc, null, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
225 cur = xmlDocGetRootElement(pDoc.get());
226 SysTryReturn(NID_LCL, cur != null, null, E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
227 SysTryReturn(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, null, E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
229 cur = cur->xmlChildrenNode;
231 pAvailableLanguageList->Construct();
233 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
235 if (cur_node->type == XML_ELEMENT_NODE)
237 std::unique_ptr < char, FreeCharPtr > pLocId((char*)xmlGetProp(cur_node, (const xmlChar *)"id"));
238 Locale loc = _LocaleImpl(pLocId.get()).GetOspLocale();
239 std::unique_ptr<String> pLanguageLocaleID(new (std::nothrow) String(loc.GetLanguageCodeString()));
240 SysTryReturn(NID_LCL, pLanguageLocaleID, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
242 if (!pAvailableLanguageList->Contains(*(pLanguageLocaleID.get())))
244 result r = pAvailableLanguageList->Add(pLanguageLocaleID.get());
245 SysTryReturn(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
246 "[%s] It is failed to add a locale string [%ls].", GetErrorMessage(E_SYSTEM), pLanguageLocaleID->GetPointer());
247 pLanguageLocaleID.release();
252 SetLastResult(E_SUCCESS);
253 return pAvailableLanguageList.release();
257 _LocaleManagerImpl::GetAvailableLanguageLocalesN(void)
259 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
260 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
262 xmlNodePtr cur = null;
263 std::unique_ptr< xmlDoc, FreeXmlDoc > pDoc(xmlParseFile(LANGUAGE_LIST_FILE_PATH));
264 SysTryReturn(NID_LCL, pDoc != null, null, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
266 cur = xmlDocGetRootElement(pDoc.get());
267 SysTryReturn(NID_LCL, cur != null, null, E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
268 SysTryReturn(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, null, E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
270 cur = cur->xmlChildrenNode;
272 pAvailableLanguageList->Construct();
274 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
276 if (cur_node->type == XML_ELEMENT_NODE)
278 std::unique_ptr< char, FreeCharPtr> pLocId((char*)xmlGetProp(cur_node, (const xmlChar *)"id"));
279 Locale loc = _LocaleImpl(pLocId.get()).GetOspLocale();
280 if (_LocaleImpl::IsSupported(loc))
282 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(loc));
283 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
284 "[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
286 if (!pAvailableLanguageList->Contains(*(pLocale.get())))
288 result r = pAvailableLanguageList->Add(pLocale.get());
289 SysTryReturn(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
290 "[%s] It is failed to add a locale string [%s].", GetErrorMessage(E_SYSTEM), pLocId.get());
297 SetLastResult(E_SUCCESS);
298 return pAvailableLanguageList.release();
303 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
305 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
306 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
308 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
309 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
310 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
311 pTimeZoneMap->Construct();
313 result r = E_SUCCESS;
314 int resultLength = -1;
315 UErrorCode ec = U_ZERO_ERROR;
316 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
317 std::unique_ptr<IMap> pTZMap(GetAvailableTimeZonesN());
319 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
321 while (pIcuTZStr != null)
323 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(pIcuTZStr));
324 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
325 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
326 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())) && pTZMap->ContainsKey(*(pTimeZone.get())))
328 std::unique_ptr< String > pDummyValue(new (std::nothrow) String());
329 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
330 r = pTimeZoneMap->Add(pTimeZone.get(), pDummyValue.get());
331 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] It is failed to add a TZ into Map.");
333 pDummyValue.release();
336 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
338 SetLastResult(E_SUCCESS);
339 return pTimeZoneMap.release();
344 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
347 String tzFilePath(TIMEZONE_LIST_FILE_PATH);
348 result r = E_SUCCESS;
350 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
351 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
352 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
353 r = file.Construct(tzFilePath, "r");
354 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.");
356 pTimeZoneMap->Construct();
360 std::unique_ptr<String> pTimeZone(new (std::nothrow) String());
361 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
362 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
363 r = file.Read(*(pTimeZone.get()));
364 if ( r == E_END_OF_FILE)
368 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r, "[%s] It is failed to read the tzlist.", GetErrorMessage(r));
369 pTimeZone->Replace(L"\n", L"\0");
371 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
373 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
374 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
375 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
377 r = pTimeZoneMap->Add(pTimeZone.get(), pDummyValue.get());
378 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r,"[%s] It is failed to make the tz list.", GetErrorMessage(r));
380 pDummyValue.release();
384 SetLastResult(E_SUCCESS);
385 return pTimeZoneMap.release();
390 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
392 std::unique_ptr<U_ICU_NAMESPACE::StringEnumeration> pIcuTzList(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
393 SysTryReturn(NID_LCL, pIcuTzList, null, E_SYSTEM, "[E_SYSTEM] It is failed to get Icu TZ list.");
394 IMap* pTzList = GetAvailableTimeZonesN(pIcuTzList.get());
400 _LocaleManagerImpl::GetSystemTimeZone(void)
402 char* pTZPath = null;
407 struct tm* pGmTime = null;
408 time_t currTime = time(null);
409 result r = E_SUCCESS;
412 pTZPath = (char*)malloc(sizeof(char) * 256);
413 SysTryCatch(NID_LCL, pTZPath, , E_OUT_OF_MEMORY, "It is not enough memory.");
415 nLen = readlink("/opt/etc/localtime", pTZPath, 255);
419 pTZPath[nLen] = '\0';
420 pTZID = pTZPath + 20;
424 pTZID = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
425 SysTryCatch(NID_LCL, pTZID, , E_SYSTEM, "It is failed to get System Time Zone.");
430 pGmTime = gmtime_r(&currTime, &gmTime);
431 SysTryCatch(NID_LCL, pGmTime, , E_SYSTEM, "It is failed to convert the time value to UTC time.");
433 utcTime.SetValue(gmTime.tm_year + 1900, gmTime.tm_mon + 1, gmTime.tm_mday, gmTime.tm_hour, gmTime.tm_min, gmTime.tm_sec);
435 r = Tizen::Locales::TimeZone::GetTimeZone(String(pTZID), utcTime, timeZone);
436 SysTryCatch(NID_LCL, r == E_SUCCESS, , r, "[%s] error occurs.", GetErrorMessage(r));
445 return TimeZone(-1, "");
449 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
451 isSupportedLocale = _LocaleImpl::IsSupported(locale);