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_DateTimeSymbolsImpl.cpp
19 * @brief This is the implementation file for _DateTimeSymbolsImpl class.
22 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FLclDateTimeSymbols.h>
26 #include "FLcl_DateTimeSymbolsImpl.h"
27 #include "FLcl_LocaleData.h"
28 #include "FLcl_LocaleImpl.h"
29 #include "FLcl_LocaleManagerImpl.h"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
35 namespace Tizen { namespace Locales
38 static const int ERAS_COUNT = 2;
39 static const int MONTH_COUNT = 12;
40 static const int WEEKDAYS_COUNT = 7;
41 static const int AM_PM_COUNT =2;
43 static const int MAX_TIMEZONE_NAME = 4;
45 _DateTimeSymbolsImpl::_DateTimeSymbolsImpl(void)
48 , __pShortMonthsList(null)
49 , __pWeekdaysList(null)
50 , __pShortWeekdaysList(null)
52 , __pTimeZonesMap(null)
53 , __pIcuDateFormatSymbols(null)
54 , __CalendarType(CALENDAR_GREGORIAN)
58 _DateTimeSymbolsImpl::~_DateTimeSymbolsImpl(void)
61 if (__pIcuDateFormatSymbols)
63 delete __pIcuDateFormatSymbols;
68 _DateTimeSymbolsImpl::Initialize(CalendarType calendarType)
70 return Initialize(_LocaleManagerImpl::GetSystemLocale(), calendarType);
74 _DateTimeSymbolsImpl::Initialize(const Locale& locale, CalendarType calendarType)
76 SysTryReturnResult(NID_LCL, _LocaleImpl::IsSupported(locale), E_INVALID_ARG, "Given locale is not supported");
78 UErrorCode ec = U_ZERO_ERROR;
79 __pIcuDateFormatSymbols = new U_ICU_NAMESPACE::DateFormatSymbols(_LocaleData::GetIcuLocale(locale), ec);
80 SysTryReturnResult(NID_LCL, __pIcuDateFormatSymbols, E_OUT_OF_MEMORY, "It is not enough memory.");
82 __pErasList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_ERA_LIST);
83 __pMonthsList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_MONTH_LIST);
84 __pShortMonthsList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_SHORT_MONTH_LIST);
85 __pWeekdaysList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_WEEKDAY_LIST);
86 __pShortWeekdaysList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST);
87 __pAmPmList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_AM_PM_LIST);
88 __CalendarType = calendarType;
94 _DateTimeSymbolsImpl::Set(const _DateTimeSymbolsImpl* pOther)
96 SysTryReturnVoidResult(NID_LCL, pOther != null, E_INVALID_ARG,
97 "[%s] Invalid argument is used. pOther instance is invalid", GetErrorMessage(E_INVALID_ARG));
100 __pErasList = CloneArrayListN(pOther->__pErasList);
101 __pMonthsList = CloneArrayListN(pOther->__pMonthsList);
102 __pShortMonthsList = CloneArrayListN(pOther->__pShortMonthsList);
103 __pWeekdaysList = CloneArrayListN(pOther->__pWeekdaysList);
104 __pShortWeekdaysList = CloneArrayListN(pOther->__pShortWeekdaysList);
105 __pAmPmList = CloneArrayListN(pOther->__pAmPmList);
106 if(pOther->__pTimeZonesMap)
108 __pTimeZonesMap = CloneMultiHashMapN(pOther->__pTimeZonesMap);
110 __CalendarType = pOther->__CalendarType;
112 if(__pIcuDateFormatSymbols)
114 delete __pIcuDateFormatSymbols;
115 __pIcuDateFormatSymbols = null;
117 _DateTimeSymbolsImpl* pDTS = const_cast<_DateTimeSymbolsImpl*>(pOther);
118 __pIcuDateFormatSymbols = new U_ICU_NAMESPACE::DateFormatSymbols(*(pDTS->GetIcuSymbols()));
119 SysTryReturnVoidResult(NID_LCL, __pIcuDateFormatSymbols, E_OUT_OF_MEMORY, "It is not enough memory.");
124 _DateTimeSymbolsImpl::AddTimeZoneName(const String& timeZoneId, const String& concatenatedTimeZoneName, bool isOverwrite)
126 StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
127 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
128 E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
133 String tzName(timeZoneId);
134 GetTimeZoneName(tzName, 0);
137 SysTryReturnResult(NID_LCL, r != E_SUCCESS, E_OBJ_ALREADY_EXIST, "The timeZoneId already exists.");
140 if (!__pTimeZonesMap)
142 MultiHashMap* pMultiHashMap = new (std::nothrow) MultiHashMap(SingleObjectDeleter);
143 SysTryReturnResult(NID_LCL, pMultiHashMap, E_OUT_OF_MEMORY, "Memory allocation failed.");
144 pMultiHashMap->Construct();
146 __pTimeZonesMap = (IMultiMap*) pMultiHashMap;
150 std::unique_ptr< String >pKey(new (std::nothrow) String(timeZoneId));
151 SysTryReturnResult(NID_LCL, pKey, E_OUT_OF_MEMORY, "Memory allocation failed");
154 while (tokenizer.HasMoreTokens())
156 tokenizer.GetNextToken(token);
157 std::unique_ptr< String >pValue(new (std::nothrow)String(token));
160 r = __pTimeZonesMap->Add(*pKey, *pValue);
163 __pTimeZonesMap->Remove(*pKey,true);
164 return E_OUT_OF_MEMORY;
176 _DateTimeSymbolsImpl::GetAmPm(void) const
178 SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
186 _DateTimeSymbolsImpl::SetAmPm(const String& amPm)
188 SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
190 std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
191 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
193 result r = SetList(pTempArrayList.get(), AM_PM_COUNT, amPm);
197 __pAmPmList = pTempArrayList.release();
199 U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pAmPmList, count);
200 __pIcuDateFormatSymbols->setAmPmStrings(pIcuArray, count);
207 _DateTimeSymbolsImpl::GetEras(void) const
209 SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
214 _DateTimeSymbolsImpl::SetEras(const String& eras)
216 SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
218 std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
219 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
221 result r = SetList(pTempArrayList.get(), ERAS_COUNT, eras);
225 __pErasList = pTempArrayList.release();
227 U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pErasList, count);
228 __pIcuDateFormatSymbols->setEras(pIcuArray, count);
236 _DateTimeSymbolsImpl::GetMonths(void) const
238 SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
241 return __pMonthsList;
245 _DateTimeSymbolsImpl::SetMonths(const String& months)
247 SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
249 std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
250 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
252 result r = SetList(pTempArrayList.get(), MONTH_COUNT, months);
255 delete __pMonthsList;
256 __pMonthsList = pTempArrayList.release();
258 U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pMonthsList, count);
259 __pIcuDateFormatSymbols->setMonths(pIcuArray, count);
266 _DateTimeSymbolsImpl::GetShortMonths(void) const
268 SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
271 return __pShortMonthsList;
275 _DateTimeSymbolsImpl::SetShortMonths(const String& shortMonths)
277 SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
279 std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
280 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
282 result r = SetList(pTempArrayList.get(), MONTH_COUNT, shortMonths);
285 delete __pShortMonthsList;
286 __pShortMonthsList = pTempArrayList.release();
288 U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pShortMonthsList, count);
289 __pIcuDateFormatSymbols->setShortMonths(pIcuArray, count);
297 _DateTimeSymbolsImpl::GetWeekdays(void) const
299 SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
302 return __pWeekdaysList;
306 _DateTimeSymbolsImpl::SetWeekdays(const String& weekdays)
308 SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
310 std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
311 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
313 result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, weekdays);
316 delete __pWeekdaysList;
317 __pWeekdaysList = pTempArrayList.release();
319 U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pWeekdaysList, count);
320 __pIcuDateFormatSymbols->setWeekdays(pIcuArray, count);
329 _DateTimeSymbolsImpl::GetShortWeekdays(void) const
331 SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
333 return __pShortWeekdaysList;
337 _DateTimeSymbolsImpl::SetShortWeekdays(const String& shortWeekdays)
339 SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
341 std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
342 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
344 result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, shortWeekdays);
347 delete __pShortWeekdaysList;
348 __pShortWeekdaysList = pTempArrayList.release();
350 U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pShortWeekdaysList, count);
351 __pIcuDateFormatSymbols->setShortWeekdays(pIcuArray, count);
360 _DateTimeSymbolsImpl::GetTimeZoneName(Tizen::Base::String& timeZoneId, int timeZoneStyle)
362 SysTryReturn(NID_LCL, timeZoneStyle < MAX_TIMEZONE_NAME, String(), E_INVALID_ARG,
363 "[%s] Invalid argument is used. timeZoneStyle(%d) is grater than MAX_TIMEZONE_NAME", GetErrorMessage(E_INVALID_ARG), timeZoneStyle);
369 std::unique_ptr< IEnumerator >pValueEnum(__pTimeZonesMap->GetValuesN(timeZoneId));
371 if (pValueEnum != null)
375 while(pValueEnum->MoveNext() == E_SUCCESS)
377 if (i++ == timeZoneStyle)
379 timeZoneName = *(static_cast<String*> (pValueEnum->GetCurrent()));
382 SetLastResult(E_SUCCESS);
387 const IcuUnicodeString** pIcuMap = null;
392 pIcuMap = __pIcuDateFormatSymbols->getZoneStrings(rowCount, columnCount);
393 if (pIcuMap && rowCount && columnCount)
395 for (int rc = 0; rc < rowCount; rc++)
397 if (timeZoneId == _LocaleData::GetOspString(pIcuMap[rc][0]))
399 SetLastResult(E_SUCCESS);
400 return _LocaleData::GetOspString(pIcuMap[rc][timeZoneStyle+1]);
404 SetLastResult(E_OBJ_NOT_FOUND);
409 _DateTimeSymbolsImpl::SetTimeZoneName(const Tizen::Base::String& timeZoneId, const Tizen::Base::String& concatenatedTimeZoneName)
411 StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
412 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
413 E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
418 __pTimeZonesMap->ContainsKey(timeZoneId, check); // checking if Time zone is already exist
422 __pTimeZonesMap->Remove(timeZoneId, true);
423 return AddTimeZoneName(timeZoneId, concatenatedTimeZoneName, true);
427 String tzName(timeZoneId);
428 GetTimeZoneName(tzName, 0);
430 result r = GetLastResult();
434 return AddTimeZoneName(timeZoneId, concatenatedTimeZoneName, true);
436 return E_OBJ_NOT_FOUND;
441 _DateTimeSymbolsImpl::CloneArrayListN(const IList* pList) const
443 SysTryReturn(NID_LCL, pList, null, E_INVALID_ARG, "[%s] Invalid argument is used. pList is Null", GetErrorMessage(E_INVALID_ARG));
446 std::unique_ptr< ArrayList > pClonedArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
447 SysTryReturn(NID_LCL, pClonedArrayList, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
449 result r = pClonedArrayList->Construct(pList->GetCount());
450 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s] Unable to construct ArrayList.", GetErrorMessage(r));
452 for (int i = 0; i < pList->GetCount(); i++)
454 std::unique_ptr< String > pClonedString(new (std::nothrow) String(*(static_cast<const String*>(pList->GetAt(i)))));
455 SysTryReturn(NID_LCL, pClonedString, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
457 r = pClonedArrayList->Add(pClonedString.get());
458 SysTryReturn(NID_LCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
460 pClonedString.release();
463 return pClonedArrayList.release();
469 _DateTimeSymbolsImpl::CloneMultiHashMapN(const Tizen::Base::Collection::IMultiMap* pMultiMap) const
471 SysTryReturn(NID_LCL, pMultiMap, null, E_INVALID_ARG, "[%s] Invalid argument is used. pMultiMap is null.", GetErrorMessage(E_INVALID_ARG));
475 std::unique_ptr< MultiHashMap > pClonedMultiHashMap(new (std::nothrow) MultiHashMap(SingleObjectDeleter));
476 SysTryReturn(NID_LCL, pClonedMultiHashMap, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
478 result r = pClonedMultiHashMap->Construct();
479 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s] Unable to construct hash map.", GetErrorMessage(r));
481 std::unique_ptr< IList > pKeys(pMultiMap->GetKeysN());
483 SysTryReturn(NID_LCL, pKeys != null, null, r, "[%s] Unable to get key list.", GetErrorMessage(r));
485 for (int i = 0; i < pKeys->GetCount(); i++)
487 String* pKey = static_cast< String* >(pKeys->GetAt(i));
488 SysTryReturn(NID_LCL, pKey != null, null, r, "[%s] Propagated.", GetLastResult());
490 std::unique_ptr< IEnumerator > pValueEnum(pMultiMap->GetValuesN(*pKey));
492 SysTryReturn(NID_LCL, pValueEnum != null, null, r, "[%s] Unable to get values for key.", GetErrorMessage(r));
494 std::unique_ptr< String > pMapKey(new (std::nothrow)String(*pKey));
495 SysTryReturn(NID_LCL, pMapKey != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
497 while (pValueEnum->MoveNext() == E_SUCCESS)
499 std::unique_ptr< String > pTimeZoneName(new (std::nothrow) String(*(static_cast<String*> (pValueEnum->GetCurrent()))));
502 r = pClonedMultiHashMap->Add(pMapKey.get(), pTimeZoneName.get());
505 pClonedMultiHashMap->Remove(*pMapKey,true);
509 pTimeZoneName.release();
513 pClonedMultiHashMap->Remove(*pMapKey,true);
521 return pClonedMultiHashMap.release();
525 _DateTimeSymbolsImpl::SetList(Tizen::Base::Collection::ArrayList* pArrayList, int tokenCount, const Tizen::Base::String& stringWillBeTokenized)
527 StringTokenizer tokenizer(stringWillBeTokenized, L"|");
529 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == tokenCount), E_INVALID_ARG,
530 "Invalid argument is used. stringWillBeTokenized=%ls", stringWillBeTokenized.GetPointer());
531 SysTryReturnResult(NID_LCL, pArrayList, E_INVALID_ARG, "Invalid argument is used. pArrayList should not be null");
533 result r = pArrayList->Construct(tokenCount);
537 std::unique_ptr< String > pListStr(null);
538 while (tokenizer.GetNextToken(token) == E_SUCCESS)
540 pListStr.reset(new (std::nothrow) String(token));
541 SysTryReturnResult(NID_LCL, pListStr, E_OUT_OF_MEMORY, "Memory allocation failed.");
543 r = pArrayList->Add(pListStr.get());
544 SysTryReturnResult(NID_LCL, r == E_SUCCESS, r, "Failed to add value to array list.");
553 _DateTimeSymbolsImpl::ReleaseList(IList* pList)
563 _DateTimeSymbolsImpl::ReleaseAll(void)
565 ReleaseList(__pErasList);
566 ReleaseList(__pMonthsList);
567 ReleaseList(__pShortMonthsList);
568 ReleaseList(__pWeekdaysList);
569 ReleaseList(__pShortWeekdaysList);
570 ReleaseList(__pAmPmList);
574 delete __pTimeZonesMap;
575 __pTimeZonesMap = null;
579 _DateTimeSymbolsImpl*
580 _DateTimeSymbolsImpl::GetDateTimeSymbolsImpl(DateTimeSymbols* pDateTimeSymbols)
582 return pDateTimeSymbols->__pDateTimeSymbolsImpl;
585 U_ICU_NAMESPACE::DateFormatSymbols*
586 _DateTimeSymbolsImpl::GetIcuSymbols(void)
588 return __pIcuDateFormatSymbols;
591 Tizen::Base::Collection::IList*
592 _DateTimeSymbolsImpl::GetSymbolAttributeArrayN(DateFormatSymbolsAttributes attributeName)
594 SysTryReturn(NID_LCL, __pIcuDateFormatSymbols, null, E_SYSTEM, "It is not initialized.");
597 const IcuUnicodeString* pIcuList = null;
599 switch (attributeName)
601 case DATE_FORMAT_SYM_ERA_LIST:
603 pIcuList = __pIcuDateFormatSymbols->getEras(count);
606 case DATE_FORMAT_SYM_MONTH_LIST:
608 pIcuList = __pIcuDateFormatSymbols->getMonths(count);
611 case DATE_FORMAT_SYM_SHORT_MONTH_LIST:
613 pIcuList = __pIcuDateFormatSymbols->getShortMonths(count);
616 case DATE_FORMAT_SYM_WEEKDAY_LIST:
618 pIcuList = __pIcuDateFormatSymbols->getWeekdays(count);
621 case DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST:
623 pIcuList = __pIcuDateFormatSymbols->getShortWeekdays(count);
626 case DATE_FORMAT_SYM_AM_PM_LIST:
628 pIcuList = __pIcuDateFormatSymbols->getAmPmStrings(count);
633 SetLastResult(E_UNSUPPORTED_OPERATION);
638 SysTryReturn(NID_LCL, pIcuList, null, E_SYSTEM, "It is faile to get symbols.");
639 return _LocaleData::ConvertIcuStringArrayToOspArrayN(pIcuList, count);