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 _LocaleData localeData;
79 result r = localeData.SetDateFormatSymbols(locale); // this will set ICU DateTimeSymbol in _LocaleData object for future use
83 __pErasList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_ERA_LIST);
84 __pMonthsList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_MONTH_LIST);
85 __pShortMonthsList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_SHORT_MONTH_LIST);
86 __pWeekdaysList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_WEEKDAY_LIST);
87 __pShortWeekdaysList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST);
88 __pAmPmList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_AM_PM_LIST);
89 __CalendarType = calendarType;
91 UErrorCode ec = U_ZERO_ERROR;
92 __pIcuDateFormatSymbols = new IcuDateFormatSymbols(ec);
99 _DateTimeSymbolsImpl::Set(const _DateTimeSymbolsImpl* pOther)
101 SysTryReturnVoidResult(NID_LCL, pOther != null, E_INVALID_ARG,
102 "[%s] Invalid argument is used. pOther instance is invalid", GetErrorMessage(E_INVALID_ARG));
104 __pErasList = CloneArrayListN(pOther->__pErasList); // Copying EraList
105 __pMonthsList = CloneArrayListN(pOther->__pMonthsList); // Copying Month List
106 __pShortMonthsList = CloneArrayListN(pOther->__pShortMonthsList); // Copying Short Month List
107 __pWeekdaysList = CloneArrayListN(pOther->__pWeekdaysList); // Copying Weekdays List
108 __pShortWeekdaysList = CloneArrayListN(pOther->__pShortWeekdaysList); // Copying Short Weekdays List
109 __pAmPmList = CloneArrayListN(pOther->__pAmPmList); // Copying AM/ PM String
110 if(pOther->__pTimeZonesMap)
112 __pTimeZonesMap = CloneMultiHashMapN(pOther->__pTimeZonesMap); // Copying available TimeZone List
114 __CalendarType = pOther->__CalendarType; // Copying Calendar Type
119 _DateTimeSymbolsImpl::AddTimeZoneName(const String& timeZoneId, const String& concatenatedTimeZoneName, bool isOverwrite)
121 StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
122 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
123 E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
128 String tzName(timeZoneId);
129 GetTimeZoneName(tzName, 0);
132 SysTryReturnResult(NID_LCL, r != E_SUCCESS, E_OBJ_ALREADY_EXIST, "The timeZoneId already exists.");
135 if (!__pTimeZonesMap)
137 MultiHashMap* pMultiHashMap = new (std::nothrow) MultiHashMap();
138 SysTryReturnResult(NID_LCL, pMultiHashMap, E_OUT_OF_MEMORY, "Memory allocation failed.");
139 pMultiHashMap->Construct();
141 __pTimeZonesMap = (IMultiMap*) pMultiHashMap;
145 std::unique_ptr< String >pKey(new (std::nothrow) String(timeZoneId));
146 SysTryReturnResult(NID_LCL, pKey, E_OUT_OF_MEMORY, "Memory allocation failed");
149 while (tokenizer.HasMoreTokens())
151 tokenizer.GetNextToken(token);
152 std::unique_ptr< String >pValue(new (std::nothrow)String(token));
155 r = __pTimeZonesMap->Add(*pKey, *pValue);
158 __pTimeZonesMap->Remove(*pKey,true);
159 return E_OUT_OF_MEMORY;
171 _DateTimeSymbolsImpl::GetAmPm(void) const
173 SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
175 ClearLastResult(); // Setting Last result to E_SUCCESS
181 _DateTimeSymbolsImpl::SetAmPm(const String& amPm)
183 SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
185 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
186 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
188 result r = SetList(pTempArrayList.get(), AM_PM_COUNT, amPm);
191 __pAmPmList->RemoveAll(true);
193 __pAmPmList = pTempArrayList.release();
200 _DateTimeSymbolsImpl::GetEras(void) const
202 SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
207 _DateTimeSymbolsImpl::SetEras(const String& eras)
209 SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
211 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
212 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
214 result r = SetList(pTempArrayList.get(), ERAS_COUNT, eras);
217 __pErasList->RemoveAll(true);
220 __pErasList = pTempArrayList.release();
227 _DateTimeSymbolsImpl::GetMonths(void) const
229 SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
231 ClearLastResult(); // Setting Last result to E_SUCCESS
232 return __pMonthsList;
236 _DateTimeSymbolsImpl::SetMonths(const String& months)
238 SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
240 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
241 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
243 result r = SetList(pTempArrayList.get(), MONTH_COUNT, months);
246 __pMonthsList->RemoveAll(true);
247 delete __pMonthsList;
249 __pMonthsList = pTempArrayList.release();
255 _DateTimeSymbolsImpl::GetShortMonths(void) const
257 SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
259 ClearLastResult(); // Setting Last result to E_SUCCESS
260 return __pShortMonthsList;
264 _DateTimeSymbolsImpl::SetShortMonths(const String& shortMonths)
266 SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
268 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
269 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
271 result r = SetList(pTempArrayList.get(), MONTH_COUNT, shortMonths);
274 __pShortMonthsList->RemoveAll(true);
275 delete __pShortMonthsList;
277 __pShortMonthsList = pTempArrayList.release();
284 _DateTimeSymbolsImpl::GetWeekdays(void) const
286 SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
288 ClearLastResult(); // Setting Last result to E_SUCCESS
289 return __pWeekdaysList;
293 _DateTimeSymbolsImpl::SetWeekdays(const String& weekdays)
295 SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
297 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
298 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
300 result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, weekdays);
303 __pWeekdaysList->RemoveAll(true);
304 delete __pWeekdaysList;
306 __pWeekdaysList = pTempArrayList.release();
314 _DateTimeSymbolsImpl::GetShortWeekdays(void) const
316 SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
318 ClearLastResult(); // Setting Last result to E_SUCCESS
319 return __pShortWeekdaysList;
323 _DateTimeSymbolsImpl::SetShortWeekdays(const String& shortWeekdays)
325 SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
327 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
328 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
330 result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, shortWeekdays);
333 __pShortWeekdaysList->RemoveAll(true);
334 delete __pShortWeekdaysList;
336 __pShortWeekdaysList = pTempArrayList.release();
344 _DateTimeSymbolsImpl::GetTimeZoneName(Tizen::Base::String& timeZoneId, int timeZoneStyle)
346 SysTryReturn(NID_LCL, timeZoneStyle < MAX_TIMEZONE_NAME, String(), E_INVALID_ARG,
347 "[%s] Invalid argument is used. timeZoneStyle(%d) is grater than MAX_TIMEZONE_NAME", GetErrorMessage(E_INVALID_ARG), timeZoneStyle);
353 std::unique_ptr< IEnumerator >pValueEnum(__pTimeZonesMap->GetValuesN(timeZoneId));
355 if (pValueEnum != null)
359 while(pValueEnum->MoveNext() == E_SUCCESS)
361 if (i++ == timeZoneStyle)
363 timeZoneName = *(static_cast<String*> (pValueEnum->GetCurrent()));
366 SetLastResult(E_SUCCESS);
371 const IcuUnicodeString** pIcuMap = null;
376 pIcuMap = __pIcuDateFormatSymbols->getZoneStrings(rowCount, columnCount);
377 if (pIcuMap && rowCount && columnCount)
379 for (int rc = 0; rc < rowCount; rc++)
381 if (timeZoneId == _LocaleData::GetOspString(pIcuMap[rc][0]))
383 SetLastResult(E_SUCCESS);
384 return _LocaleData::GetOspString(pIcuMap[rc][timeZoneStyle+1]);
388 SetLastResult(E_OBJ_NOT_FOUND);
393 _DateTimeSymbolsImpl::SetTimeZoneName(const Tizen::Base::String& timeZoneId, const Tizen::Base::String& concatenatedTimeZoneName)
395 // checking if concatenatedTimeZoneName has 4 names
396 StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
397 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
398 E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
403 __pTimeZonesMap->ContainsKey(timeZoneId, check); // checking if Time zone is already exist
407 __pTimeZonesMap->Remove(timeZoneId, true);
408 return AddTimeZoneName(timeZoneId, concatenatedTimeZoneName, true);
412 String tzName(timeZoneId);
413 GetTimeZoneName(tzName, 0);
415 result r = GetLastResult();
419 return AddTimeZoneName(timeZoneId, concatenatedTimeZoneName, true);
421 return E_OBJ_NOT_FOUND;
426 // This function makes a copy of input array list of strings
428 _DateTimeSymbolsImpl::CloneArrayListN(const IList* pList) const
430 SysTryReturn(NID_LCL, pList, null, E_INVALID_ARG, "[%s] Invalid argument is used. pList is Null", GetErrorMessage(E_INVALID_ARG));
432 ClearLastResult(); // Setting last result to E_SUCCESS
433 std::unique_ptr< ArrayList, AllElementsDeleter > pClonedArrayList(new (std::nothrow) ArrayList());
434 SysTryReturn(NID_LCL, pClonedArrayList, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
436 result r = pClonedArrayList->Construct(pList->GetCount());
437 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s] Unable to construct ArrayList.", GetErrorMessage(r));
439 for (int i = 0; i < pList->GetCount(); i++)
441 std::unique_ptr< String > pClonedString(new (std::nothrow) String(*(static_cast<const String*>(pList->GetAt(i)))));
442 SysTryReturn(NID_LCL, pClonedString, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
444 r = pClonedArrayList->Add(pClonedString.get());
445 SysTryReturn(NID_LCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
447 pClonedString.release();
450 return pClonedArrayList.release();
456 _DateTimeSymbolsImpl::CloneMultiHashMapN(const Tizen::Base::Collection::IMultiMap* pMultiMap) const
458 SysTryReturn(NID_LCL, pMultiMap, null, E_INVALID_ARG, "[%s] Invalid argument is used. pMultiMap is null.", GetErrorMessage(E_INVALID_ARG));
462 std::unique_ptr< MultiHashMap, AllElementsDeleter > pClonedMultiHashMap(new (std::nothrow) MultiHashMap());
463 SysTryReturn(NID_LCL, pClonedMultiHashMap, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
465 result r = pClonedMultiHashMap->Construct();
466 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s] Unable to construct hash map.", GetErrorMessage(r));
468 std::unique_ptr< IList > pKeys(pMultiMap->GetKeysN());
470 SysTryReturn(NID_LCL, pKeys != null, null, r, "[%s] Unable to get key list.", GetErrorMessage(r));
472 for (int i = 0; i < pKeys->GetCount(); i++)
474 String* pKey = static_cast< String* >(pKeys->GetAt(i));
475 SysTryReturn(NID_LCL, pKey != null, null, r, "[%s] Propagated.", GetLastResult());
477 std::unique_ptr< IEnumerator > pValueEnum(pMultiMap->GetValuesN(*pKey));
479 SysTryReturn(NID_LCL, pValueEnum != null, null, r, "[%s] Unable to get values for key.", GetErrorMessage(r));
481 std::unique_ptr< String > pMapKey(new (std::nothrow)String(*pKey));
482 SysTryReturn(NID_LCL, pMapKey != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
484 while (pValueEnum->MoveNext() == E_SUCCESS)
486 std::unique_ptr< String > pTimeZoneName(new (std::nothrow) String(*(static_cast<String*> (pValueEnum->GetCurrent()))));
489 r = pClonedMultiHashMap->Add(pMapKey.get(), pTimeZoneName.get());
492 pClonedMultiHashMap->Remove(*pMapKey,true);
496 pTimeZoneName.release();
500 pClonedMultiHashMap->Remove(*pMapKey,true);
508 return pClonedMultiHashMap.release();
512 _DateTimeSymbolsImpl::SetList(Tizen::Base::Collection::ArrayList* pArrayList, int tokenCount, const Tizen::Base::String& stringWillBeTokenized)
514 StringTokenizer tokenizer(stringWillBeTokenized, L"|");
516 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == tokenCount), E_INVALID_ARG,
517 "Invalid argument is used. stringWillBeTokenized=%ls", stringWillBeTokenized.GetPointer());
519 std::unique_ptr< ArrayList, AllElementsDeleter > pTmpArrayList(pArrayList);
520 SysTryReturnResult(NID_LCL, pTmpArrayList, E_INVALID_ARG, "Invalid argument is used. pArrayList should not be null");
522 result r = pTmpArrayList->Construct(tokenCount); // Constructing list for tokenCount elements
526 std::unique_ptr< String > pListStr(null);
527 while (tokenizer.GetNextToken(token) == E_SUCCESS)
529 pListStr.reset(new (std::nothrow) String(token));
530 SysTryReturnResult(NID_LCL, pListStr, E_OUT_OF_MEMORY, "Memory allocation failed.");
532 r = pTmpArrayList->Add(pListStr.get());
533 SysTryReturnResult(NID_LCL, r == E_SUCCESS, r, "Failed to add value to array list.");
539 pTmpArrayList.release();
544 _DateTimeSymbolsImpl::ReleaseList(IList* pList)
548 pList->RemoveAll(true); // Remove elements and de-allocate memory used by them
553 // This function release all the list after deleting their contents
555 _DateTimeSymbolsImpl::ReleaseAll(void)
557 ReleaseList(__pErasList); // Deleting EraList after deleting its contents
558 ReleaseList(__pMonthsList); // Deleting MonthsList after deleting its contents
559 ReleaseList(__pShortMonthsList); // Deleting ShortMonthsList after deleting its contents
560 ReleaseList(__pWeekdaysList); // Deleting WeekdaysList after deleting its contents
561 ReleaseList(__pShortWeekdaysList); // Deleting ShortWeekdaysList after deleting its contents
562 ReleaseList(__pAmPmList); // Deleting AmPmList after deleting its contents
566 __pTimeZonesMap->RemoveAll(true); // Removing and deleting contents of TimeZonesMap
567 delete __pTimeZonesMap; // Deleting TimeZonesMap
571 _DateTimeSymbolsImpl*
572 _DateTimeSymbolsImpl::GetDateTimeSymbolsImpl(DateTimeSymbols* pDateTimeSymbols)
574 return pDateTimeSymbols->__pDateTimeSymbolsImpl;