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 <libxml/parser.h>
28 #include <libxml/tree.h>
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
35 #include "FLcl_LocaleImpl.h"
36 #include "FLcl_TimeZoneImpl.h"
37 #include "FLcl_LocaleManagerImpl.h"
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Utility;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Io;
47 void operator ()(xmlDoc* p)
58 void operator ()(char* p)
67 namespace Tizen { namespace Locales
69 static const char* LANGUAGE_LIST_FILE_PATH ="/opt/data/setting/langlist.xml";
70 static const char* TIMEZONE_LIST_FILE_PATH = "/opt/data/setting/tzlist.ini";
71 static const char* CLOCALE_LIST_FILE_PATH = "/opt/usr/etc/clocale.list";
75 _LocaleManagerImpl::GetSystemLocale(void)
78 if (runtime_info_get_value_string(RUNTIME_INFO_KEY_REGION, &pRegionPtr) == RUNTIME_INFO_ERROR_NONE)
80 SetLastResult(E_SUCCESS);
82 Locale ospLoc = _LocaleImpl(pRegionPtr).GetOspLocale();
87 SetLastResult(E_SYSTEM);
88 return Locale(LANGUAGE_INVALID, COUNTRY_INVALID, null);
93 _LocaleManagerImpl::GetAvailableEglibcLocaesN(void)
97 String clocaleFilePath(CLOCALE_LIST_FILE_PATH);
99 std::unique_ptr<HashMap> pClocaleMap(new (std::nothrow) HashMap(SingleObjectDeleter));
100 SysTryReturn(NID_LCL, pClocaleMap, null, E_OUT_OF_MEMORY,
101 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
102 r = file.Construct(clocaleFilePath, "r");
103 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.");
105 pClocaleMap->Construct();
110 r = file.Read(strBuf);
111 if ( r == E_END_OF_FILE)
115 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r, "[%s] It is failed to read the clocale list.", GetErrorMessage(r));
116 std::unique_ptr< String > pClocaleId(new (std::nothrow) String());
117 SysTryReturn(NID_LCL, pClocaleId, null, E_OUT_OF_MEMORY,
118 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
119 r = strBuf.SubString(0, 5, *pClocaleId);
125 if (!pClocaleMap->ContainsKey(*(pClocaleId.get())))
127 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
128 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
129 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
131 r = pClocaleMap->Add(*(pClocaleId.get()), *(pDummyValue.get()));
132 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r,
133 "[%s] It is failed to add the clocale id into the clocale map.", GetErrorMessage(r));
134 pClocaleId.release();
135 pDummyValue.release();
138 return pClocaleMap.release();
142 _LocaleManagerImpl::GetAvailableLocalesN(void)
144 result r = E_SUCCESS;
146 const U_ICU_NAMESPACE::Locale* pIcuLocaleList = U_ICU_NAMESPACE::Locale::getAvailableLocales(count);
147 SysTryReturn(NID_LCL, count > 0, null, E_SYSTEM,
148 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
150 std::unique_ptr<LinkedList> pAvailableLocaleList(new (std::nothrow) LinkedList(SingleObjectDeleter));
151 SysTryReturn(NID_LCL, pAvailableLocaleList, null, E_OUT_OF_MEMORY,
152 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
154 std::unique_ptr< IMap > pClocaleMap(GetAvailableEglibcLocaesN());
156 for (int i = 0; i < count; i++)
158 const U_ICU_NAMESPACE::Locale* pIcuLocale = (pIcuLocaleList + i);
159 SysTryReturn(NID_LCL, pIcuLocale, null, E_SYSTEM,
160 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
161 Locale ospLocale = _LocaleImpl(*pIcuLocale).GetOspLocale();
162 if (_LocaleImpl::IsSupported(ospLocale))
164 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(ospLocale));
165 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
166 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
168 String localeId(pIcuLocale->getLanguage());
169 localeId = localeId + L"_" + String(pIcuLocale->getCountry());
171 if (!pAvailableLocaleList->Contains(*(pLocale.get())) && pClocaleMap->ContainsKey(localeId))
173 r = pAvailableLocaleList->Add(*(pLocale.get()));
174 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to make the locale list");
180 SetLastResult(E_SUCCESS);
181 return pAvailableLocaleList.release();
185 _LocaleManagerImpl::GetSelectedLanguage(void)
189 int ret = runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &pLanguagePtr);
190 SysTryReturn(NID_LCL, ret == RUNTIME_INFO_ERROR_NONE, String(), E_SYSTEM,
191 "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
192 String language(_LocaleImpl(pLanguagePtr).GetLanguageCodeString(false));
199 _LocaleManagerImpl::GetAvailableLanguagesN(void)
201 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
202 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
204 xmlNodePtr cur = null;
205 std::unique_ptr< xmlDoc, FreeXmlDoc > pDoc(xmlParseFile(LANGUAGE_LIST_FILE_PATH));
206 SysTryReturn(NID_LCL, pDoc != null, null, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
208 cur = xmlDocGetRootElement(pDoc.get());
209 SysTryReturn(NID_LCL, cur != null, null, E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
210 SysTryReturn(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, null, E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
212 cur = cur->xmlChildrenNode;
214 pAvailableLanguageList->Construct();
216 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
218 if (cur_node->type == XML_ELEMENT_NODE)
220 std::unique_ptr < char, FreeCharPtr > pLocId((char*)xmlGetProp(cur_node, (const xmlChar *)"id"));
221 Locale loc = _LocaleImpl(pLocId.get()).GetOspLocale();
222 std::unique_ptr<String> pLanguageLocaleID(new (std::nothrow) String(loc.GetLanguageCodeString()));
223 SysTryReturn(NID_LCL, pLanguageLocaleID, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
225 if (!pAvailableLanguageList->Contains(*(pLanguageLocaleID.get())))
227 result r = pAvailableLanguageList->Add(pLanguageLocaleID.get());
228 SysTryReturn(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
229 "[%s] It is failed to add a locale string [%ls].", GetErrorMessage(E_SYSTEM), pLanguageLocaleID->GetPointer());
230 pLanguageLocaleID.release();
235 SetLastResult(E_SUCCESS);
236 return pAvailableLanguageList.release();
241 _LocaleManagerImpl::GetAvailableLanguageLocalesN(void)
243 std::unique_ptr<ArrayList> pAvailableLanguageList(new (std::nothrow) ArrayList(SingleObjectDeleter));
244 SysTryReturn(NID_LCL, pAvailableLanguageList, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
246 xmlNodePtr cur = null;
247 std::unique_ptr< xmlDoc, FreeXmlDoc > pDoc(xmlParseFile(LANGUAGE_LIST_FILE_PATH));
248 SysTryReturn(NID_LCL, pDoc != null, null, E_FILE_NOT_FOUND, "[E_FILE_NOT_FOUND] It is failed to get the langlist from the resource.");
250 cur = xmlDocGetRootElement(pDoc.get());
251 SysTryReturn(NID_LCL, cur != null, null, E_EMPTY_BODY, "[E_EMPTY_BODY] It is empty document.");
252 SysTryReturn(NID_LCL, xmlStrcmp(cur->name, (const xmlChar *) "langlist") == 0, null, E_INVALID_CONTENT, "[E_INVALID_CONTENT] The document is wrong type");
254 cur = cur->xmlChildrenNode;
256 pAvailableLanguageList->Construct();
258 for (xmlNodePtr cur_node = cur; cur_node; cur_node = cur_node->next)
260 if (cur_node->type == XML_ELEMENT_NODE)
262 std::unique_ptr< char, FreeCharPtr> pLocId((char*)xmlGetProp(cur_node, (const xmlChar *)"id"));
263 Locale loc = _LocaleImpl(pLocId.get()).GetOspLocale();
264 if (_LocaleImpl::IsSupported(loc))
266 std::unique_ptr< Locale > pLocale(new (std::nothrow) Locale(loc));
267 SysTryReturn(NID_LCL, pLocale, null, E_OUT_OF_MEMORY,
268 "[%s] Memory allocation failed",GetErrorMessage(E_OUT_OF_MEMORY));
270 if (!pAvailableLanguageList->Contains(*(pLocale.get())))
272 result r = pAvailableLanguageList->Add(*(pLocale.get()));
273 SysTryReturn(NID_LCL, r == E_SUCCESS, null, E_SYSTEM,
274 "[%s] It is failed to add a locale string [%s].", GetErrorMessage(E_SYSTEM), pLocId.get());
281 SetLastResult(E_SUCCESS);
282 return pAvailableLanguageList.release();
287 _LocaleManagerImpl::GetAvailableTimeZonesN(U_ICU_NAMESPACE::StringEnumeration* pIcuTZStrList)
289 SysTryReturn(NID_LCL, pIcuTZStrList, null, E_SYSTEM,
290 "[%s] The method cannot proceed due to a severe system error.",GetErrorMessage(E_SYSTEM));
292 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
293 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
294 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
295 pTimeZoneMap->Construct();
297 result r = E_SUCCESS;
298 int resultLength = -1;
299 UErrorCode ec = U_ZERO_ERROR;
300 const char* pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
301 std::unique_ptr<IMap> pTZMap(GetAvailableTimeZonesN());
303 SysTryReturn(NID_LCL, pTZMap, null, r, "[%s] Fail to get available time zone list", GetErrorMessage(r));
305 while (pIcuTZStr != null)
307 std::unique_ptr< String > pTimeZone(new (std::nothrow) String(pIcuTZStr));
308 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
309 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
310 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())) && pTZMap->ContainsKey(*(pTimeZone.get())))
312 std::unique_ptr< String > pDummyValue(new (std::nothrow) String());
313 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,"[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
314 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
315 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] It is failed to add a TZ into Map.");
317 pDummyValue.release();
320 pIcuTZStr = pIcuTZStrList->next(&resultLength, ec);
322 SetLastResult(E_SUCCESS);
323 return pTimeZoneMap.release();
328 _LocaleManagerImpl::GetAvailableTimeZonesN(void)
331 String tzFilePath(TIMEZONE_LIST_FILE_PATH);
332 result r = E_SUCCESS;
334 std::unique_ptr<HashMap> pTimeZoneMap(new (std::nothrow) HashMap(SingleObjectDeleter));
335 SysTryReturn(NID_LCL, pTimeZoneMap, null, E_OUT_OF_MEMORY,
336 "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
337 r = file.Construct(tzFilePath, "r");
338 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.");
340 pTimeZoneMap->Construct();
344 std::unique_ptr<String> pTimeZone(new (std::nothrow) String());
345 SysTryReturn(NID_LCL, pTimeZone, null, E_OUT_OF_MEMORY,
346 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
347 r = file.Read(*(pTimeZone.get()));
348 if ( r == E_END_OF_FILE)
352 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r, "[%s] It is failed to read the tzlist.", GetErrorMessage(r));
353 pTimeZone->Replace(L"\n", L"\0");
355 if (!pTimeZoneMap->ContainsKey(*(pTimeZone.get())))
357 std::unique_ptr<String> pDummyValue(new (std::nothrow) String());
358 SysTryReturn(NID_LCL, pDummyValue, null, E_OUT_OF_MEMORY,
359 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
361 r = pTimeZoneMap->Add(*(pTimeZone.get()), *(pDummyValue.get()));
362 SysTryReturn(NID_LCL, r == E_SUCCESS, null, r,"[%s] It is failed to make the tz list.", GetErrorMessage(r));
364 pDummyValue.release();
368 SetLastResult(E_SUCCESS);
369 return pTimeZoneMap.release();
374 _LocaleManagerImpl::GetAvailableTimeZonesN(int rawOffset)
376 std::unique_ptr<U_ICU_NAMESPACE::StringEnumeration> pIcuTzList(U_ICU_NAMESPACE::TimeZone::createEnumeration(rawOffset * _TimeZoneImpl::ONE_MIN_IN_MILLISEC));
377 SysTryReturn(NID_LCL, pIcuTzList, null, E_SYSTEM, "[E_SYSTEM] It is failed to get Icu TZ list.");
378 IMap* pTzList = GetAvailableTimeZonesN(pIcuTzList.get());
384 _LocaleManagerImpl::GetSystemTimeZone(void)
386 std::unique_ptr< char, FreeCharPtr> tzId(vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID));
387 SysTryReturn(NID_LCL, tzId, TimeZone(-1, ""), E_SYSTEM, "It is failed to get System Time Zone.");
388 SysLog(NID_LCL, "System TimeZone id [%s]", tzId.get());
392 struct tm* pGmTime = null;
395 SysTryReturn(NID_LCL, currTime, TimeZone(-1, ""), E_SYSTEM, "It is failed to get system time.");
396 pGmTime = gmtime(&currTime);
397 SysTryReturn(NID_LCL, pGmTime, TimeZone(-1, ""), E_SYSTEM, "It is failed to convert the time value to UTC time.");
399 utcTime.SetValue(pGmTime->tm_year + 1900, pGmTime->tm_mon + 1, pGmTime->tm_mday, pGmTime->tm_hour, pGmTime->tm_min, pGmTime->tm_sec);
401 result r = Tizen::Locales::TimeZone::GetTimeZone(String(tzId.get()), utcTime, timeZone);
402 SysTryReturn(NID_LCL, r == E_SUCCESS, TimeZone(-1, ""), r, "[%s] error occurs.", GetErrorMessage(r));
407 _LocaleManagerImpl::IsSupportedLocale(const Tizen::Locales::Locale& locale, bool& isSupportedLocale)
409 isSupportedLocale = _LocaleImpl::IsSupported(locale);