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_DateTimeSymbolsImpl.cpp
20 * @brief This is the implementation file for _DateTimeSymbolsImpl class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FLclDateTimeSymbols.h>
27 #include "FLcl_DateTimeSymbolsImpl.h"
28 #include "FLcl_LocaleData.h"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
35 namespace Tizen { namespace Locales
38 // This will be used to define non localized pattern string.
39 static const wchar_t PATTERN_CHARS[] =
41 // GyMdkHmsSEDFwWahKzYeugAZ
42 0x47, 0x79, 0x4D, 0x64, 0x6B, 0x48, 0x6D, 0x73, 0x53, 0x45, 0x44,
43 0x46, 0x77, 0x57, 0x61, 0x68, 0x4B, 0x7A, 0x59, 0x65, 0x75, 0x67,
44 0x41, 0x5A, 0 // Should be ended with 0 as we are not setting null at the end of the pattern
47 static const int PATTERN_LENGTH = 24 + 1; // 24 Char and one null (0)
49 static const int ERAS_COUNT = 2;
50 static const int MONTH_COUNT = 12;
51 static const int WEEKDAYS_COUNT = 7;
52 static const int AM_PM_COUNT =2;
54 static const int MAX_TIMEZONE_NAME = 4;
57 _DateTimeSymbolsImpl::GetNonLocalizedDateTimePatternN(void)
60 std::unique_ptr< WcharBuffer > pChars(new (std::nothrow) WcharBuffer());
61 SysTryReturn(NID_TEXT, pChars, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
63 result r = pChars->Construct(PATTERN_LENGTH);
64 SysTryCatch(NID_LCL, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
66 r = pChars->SetArray(PATTERN_CHARS, 0, PATTERN_LENGTH); // Setting value from PATTERN_CHARS defined above
67 SysTryCatch(NID_LCL, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
70 return pChars.release();
76 _DateTimeSymbolsImpl::_DateTimeSymbolsImpl(void)
79 , __pShortMonthsList(null)
80 , __pWeekdaysList(null)
81 , __pShortWeekdaysList(null)
83 , __pTimeZonesMap(null)
84 , __CalendarType(CALENDAR_GREGORIAN)
85 , __pIcuDateFormatSymbols(null)
89 _DateTimeSymbolsImpl::~_DateTimeSymbolsImpl(void)
92 if (__pIcuDateFormatSymbols)
94 delete __pIcuDateFormatSymbols;
99 _DateTimeSymbolsImpl::Initialize(CalendarType calendarType)
101 return Initialize(_LocaleData::GetOspSystemLocale(), calendarType);
105 _DateTimeSymbolsImpl::Initialize(const Locale& locale, CalendarType calendarType)
107 SysTryReturnResult(NID_LCL, locale.GetCountryCode() != COUNTRY_INVALID, E_INVALID_ARG,
108 "Invalid argument is used. Country code is invalid. Given locale is not supported");
109 SysTryReturnResult(NID_LCL, locale.GetLanguageCode() != LANGUAGE_INVALID, E_INVALID_ARG,
110 "Invalid argument is used. Language code is invalid. Given locale is not supported");
112 _LocaleData localeData;
113 result r = localeData.SetDateFormatSymbols(locale); // this will set ICU DateTimeSymbol in _LocaleData object for future use
117 __pErasList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_ERA_LIST);
118 __pMonthsList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_MONTH_LIST);
119 __pShortMonthsList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_SHORT_MONTH_LIST);
120 __pWeekdaysList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_WEEKDAY_LIST);
121 __pShortWeekdaysList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST);
122 __pAmPmList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_AM_PM_LIST);
123 __CalendarType = calendarType;
125 UErrorCode ec = U_ZERO_ERROR;
126 __pIcuDateFormatSymbols = new IcuDateFormatSymbols(ec);
133 _DateTimeSymbolsImpl::Set(const _DateTimeSymbolsImpl* pOther)
135 SysTryReturnVoidResult(NID_LCL, pOther != null, E_INVALID_ARG,
136 "[%s] Invalid argument is used. pOther instance is invalid", GetErrorMessage(E_INVALID_ARG));
138 __pErasList = CloneArrayListN(pOther->__pErasList); // Copying EraList
139 __pMonthsList = CloneArrayListN(pOther->__pMonthsList); // Copying Month List
140 __pShortMonthsList = CloneArrayListN(pOther->__pShortMonthsList); // Copying Short Month List
141 __pWeekdaysList = CloneArrayListN(pOther->__pWeekdaysList); // Copying Weekdays List
142 __pShortWeekdaysList = CloneArrayListN(pOther->__pShortWeekdaysList); // Copying Short Weekdays List
143 __pAmPmList = CloneArrayListN(pOther->__pAmPmList); // Copying AM/ PM String
144 if(pOther->__pTimeZonesMap)
146 __pTimeZonesMap = CloneMultiHashMapN(pOther->__pTimeZonesMap); // Copying available TimeZone List
148 __CalendarType = pOther->__CalendarType; // Copying Calendar Type
153 _DateTimeSymbolsImpl::AddTimeZoneName(const String& timeZoneId, const String& concatenatedTimeZoneName, bool isOverwrite)
155 StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
156 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
157 E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
162 String tzName(timeZoneId);
163 GetTimeZoneName(tzName, 0);
166 SysTryReturnResult(NID_LCL, r != E_SUCCESS, E_OBJ_ALREADY_EXIST, "The timeZoneId already exists.");
169 if (!__pTimeZonesMap)
171 MultiHashMap* pMultiHashMap = new (std::nothrow) MultiHashMap();
172 SysTryReturnResult(NID_LCL, pMultiHashMap, E_OUT_OF_MEMORY, "Memory allocation failed.");
173 pMultiHashMap->Construct();
175 __pTimeZonesMap = (IMultiMap*) pMultiHashMap;
179 std::unique_ptr< String >pKey(new (std::nothrow) String(timeZoneId));
180 SysTryReturnResult(NID_LCL, pKey, E_OUT_OF_MEMORY, "Memory allocation failed");
183 while (tokenizer.HasMoreTokens())
185 tokenizer.GetNextToken(token);
186 std::unique_ptr< String >pValue(new (std::nothrow)String(token));
189 r = __pTimeZonesMap->Add(*pKey, *pValue);
192 __pTimeZonesMap->Remove(*pKey,true);
193 return E_OUT_OF_MEMORY;
205 _DateTimeSymbolsImpl::GetAmPm(void) const
207 SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
209 ClearLastResult(); // Setting Last result to E_SUCCESS
215 _DateTimeSymbolsImpl::SetAmPm(const String& amPm)
217 SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
219 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
220 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
222 result r = SetList(pTempArrayList.get(), AM_PM_COUNT, amPm);
225 __pAmPmList->RemoveAll(true);
227 __pAmPmList = pTempArrayList.release();
234 _DateTimeSymbolsImpl::GetEras(void) const
236 SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
241 _DateTimeSymbolsImpl::SetEras(const String& eras)
243 SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
245 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
246 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
248 result r = SetList(pTempArrayList.get(), ERAS_COUNT, eras);
251 __pErasList->RemoveAll(true);
254 __pErasList = pTempArrayList.release();
261 _DateTimeSymbolsImpl::GetMonths(void) const
263 SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
265 ClearLastResult(); // Setting Last result to E_SUCCESS
266 return __pMonthsList;
270 _DateTimeSymbolsImpl::SetMonths(const String& months)
272 SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
274 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
275 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
277 result r = SetList(pTempArrayList.get(), MONTH_COUNT, months);
280 __pMonthsList->RemoveAll(true);
281 delete __pMonthsList;
283 __pMonthsList = pTempArrayList.release();
289 _DateTimeSymbolsImpl::GetShortMonths(void) const
291 SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
293 ClearLastResult(); // Setting Last result to E_SUCCESS
294 return __pShortMonthsList;
298 _DateTimeSymbolsImpl::SetShortMonths(const String& shortMonths)
300 SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
302 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
303 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
305 result r = SetList(pTempArrayList.get(), MONTH_COUNT, shortMonths);
308 __pShortMonthsList->RemoveAll(true);
309 delete __pShortMonthsList;
311 __pShortMonthsList = pTempArrayList.release();
318 _DateTimeSymbolsImpl::GetWeekdays(void) const
320 SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
322 ClearLastResult(); // Setting Last result to E_SUCCESS
323 return __pWeekdaysList;
327 _DateTimeSymbolsImpl::SetWeekdays(const String& weekdays)
329 SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
331 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
332 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
334 result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, weekdays);
337 __pWeekdaysList->RemoveAll(true);
338 delete __pWeekdaysList;
340 __pWeekdaysList = pTempArrayList.release();
348 _DateTimeSymbolsImpl::GetShortWeekdays(void) const
350 SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
352 ClearLastResult(); // Setting Last result to E_SUCCESS
353 return __pShortWeekdaysList;
357 _DateTimeSymbolsImpl::SetShortWeekdays(const String& shortWeekdays)
359 SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
361 std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
362 SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
364 result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, shortWeekdays);
367 __pShortWeekdaysList->RemoveAll(true);
368 delete __pShortWeekdaysList;
370 __pShortWeekdaysList = pTempArrayList.release();
378 _DateTimeSymbolsImpl::GetTimeZoneName(Tizen::Base::String& timeZoneId, int timeZoneStyle)
380 SysTryReturn(NID_LCL, timeZoneStyle < MAX_TIMEZONE_NAME, String(), E_INVALID_ARG,
381 "[%s] Invalid argument is used. timeZoneStyle(%d) is grater than MAX_TIMEZONE_NAME", GetErrorMessage(E_INVALID_ARG), timeZoneStyle);
385 if (__pTimeZonesMap && __pTimeZonesMap->GetValuesN(timeZoneId))
387 std::unique_ptr< IEnumerator >pValueEnum(__pTimeZonesMap->GetValuesN(timeZoneId));
391 while(pValueEnum->MoveNext() == E_SUCCESS)
393 if (i++ == timeZoneStyle)
395 timeZoneName = *(static_cast<String*> (pValueEnum->GetCurrent()));
398 SetLastResult(E_SUCCESS);
402 const IcuUnicodeString** pIcuMap = null;
407 pIcuMap = __pIcuDateFormatSymbols->getZoneStrings(rowCount, columnCount);
408 if (pIcuMap && rowCount && columnCount)
410 for (int rc = 0; rc < rowCount; rc++)
412 if (timeZoneId == _LocaleData::GetOspString(pIcuMap[rc][0]))
414 SetLastResult(E_SUCCESS);
415 return _LocaleData::GetOspString(pIcuMap[rc][timeZoneStyle+1]);
419 SetLastResult(E_OBJ_NOT_FOUND);
424 _DateTimeSymbolsImpl::SetTimeZoneName(const Tizen::Base::String& timeZoneId, const Tizen::Base::String& concatenatedTimeZoneName)
426 // checking if concatenatedTimeZoneName has 4 names
427 StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
428 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
429 E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
434 __pTimeZonesMap->ContainsKey(timeZoneId, check); // checking if Time zone is already exist
438 __pTimeZonesMap->Remove(timeZoneId, true);
439 return AddTimeZoneName(timeZoneId, concatenatedTimeZoneName, true);
443 String tzName(timeZoneId);
444 GetTimeZoneName(tzName, 0);
446 result r = GetLastResult();
450 return AddTimeZoneName(timeZoneId, concatenatedTimeZoneName, true);
452 return E_OBJ_NOT_FOUND;
457 // This function makes a copy of input array list of strings
459 _DateTimeSymbolsImpl::CloneArrayListN(const IList* pList) const
461 SysTryReturn(NID_LCL, pList, null, E_INVALID_ARG, "[%s] Invalid argument is used. pList is Null", GetErrorMessage(E_INVALID_ARG));
463 ClearLastResult(); // Setting last result to E_SUCCESS
464 std::unique_ptr< ArrayList, AllElementsDeleter > pClonedArrayList(new (std::nothrow) ArrayList());
465 SysTryReturn(NID_LCL, pClonedArrayList, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
467 result r = pClonedArrayList->Construct(pList->GetCount());
468 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s] Unable to construct ArrayList.", GetErrorMessage(r));
470 for (int i = 0; i < pList->GetCount(); i++)
472 std::unique_ptr< String > pClonedString(new (std::nothrow) String(*(static_cast<const String*>(pList->GetAt(i)))));
473 SysTryReturn(NID_LCL, pClonedString, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
475 r = pClonedArrayList->Add(*pClonedString);
476 SysTryReturn(NID_LCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
478 pClonedString.release();
481 return pClonedArrayList.release();
487 _DateTimeSymbolsImpl::CloneMultiHashMapN(const Tizen::Base::Collection::IMultiMap* pMultiMap) const
489 SysTryReturn(NID_LCL, pMultiMap, null, E_INVALID_ARG, "[%s] Invalid argument is used. pMultiMap is null.", GetErrorMessage(E_INVALID_ARG));
493 std::unique_ptr< MultiHashMap, AllElementsDeleter > pClonedMultiHashMap(new (std::nothrow) MultiHashMap());
494 SysTryReturn(NID_LCL, pClonedMultiHashMap, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
496 result r = pClonedMultiHashMap->Construct();
497 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s] Unable to construct hash map.", GetErrorMessage(r));
499 std::unique_ptr< IList > pKeys(pMultiMap->GetKeysN());
501 SysTryReturn(NID_LCL, pKeys != null, null, r, "[%s] Unable to get key list.", GetErrorMessage(r));
503 for (int i = 0; i < pKeys->GetCount(); i++)
505 String* pKey = static_cast< String* >(pKeys->GetAt(i));
506 SysTryReturn(NID_LCL, pKey != null, null, r, "[%s] Propagated.", GetLastResult());
508 std::unique_ptr< IEnumerator > pValueEnum(pMultiMap->GetValuesN(*pKey));
510 SysTryReturn(NID_LCL, pValueEnum != null, null, r, "[%s] Unable to get values for key.", GetErrorMessage(r));
512 std::unique_ptr< String > pMapKey(new (std::nothrow)String(*pKey));
513 SysTryReturn(NID_LCL, pMapKey != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
515 while (pValueEnum->MoveNext() == E_SUCCESS)
517 std::unique_ptr< String > pTimeZoneName(new (std::nothrow) String(*(static_cast<String*> (pValueEnum->GetCurrent()))));
520 r = pClonedMultiHashMap->Add(*pMapKey, *pTimeZoneName);
523 pClonedMultiHashMap->Remove(*pMapKey,true);
527 pTimeZoneName.release();
531 pClonedMultiHashMap->Remove(*pMapKey,true);
539 return pClonedMultiHashMap.release();
543 _DateTimeSymbolsImpl::SetList(Tizen::Base::Collection::ArrayList* pArrayList, int tokenCount, const Tizen::Base::String& stringWillBeTokenized)
545 StringTokenizer tokenizer(stringWillBeTokenized, L"|");
547 SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == tokenCount), E_INVALID_ARG,
548 "Invalid argument is used. stringWillBeTokenized=%ls", stringWillBeTokenized.GetPointer());
550 std::unique_ptr< ArrayList, AllElementsDeleter > pTmpArrayList(pArrayList);
551 SysTryReturnResult(NID_LCL, pTmpArrayList, E_INVALID_ARG, "Invalid argument is used. pArrayList should not be null");
553 result r = pTmpArrayList->Construct(tokenCount); // Constructing list for tokenCount elements
557 std::unique_ptr< String > pListStr(null);
558 while (tokenizer.GetNextToken(token) == E_SUCCESS)
560 pListStr.reset(new (std::nothrow) String(token));
561 SysTryReturnResult(NID_LCL, pListStr, E_OUT_OF_MEMORY, "Memory allocation failed.");
563 r = pTmpArrayList->Add(*pListStr);
564 SysTryReturnResult(NID_LCL, r == E_SUCCESS, r, "Failed to add value to array list.");
570 pTmpArrayList.release();
575 _DateTimeSymbolsImpl::ReleaseList(IList* pList)
579 pList->RemoveAll(true); // Remove elements and de-allocate memory used by them
584 // This function release all the list after deleting their contents
586 _DateTimeSymbolsImpl::ReleaseAll(void)
588 ReleaseList(__pErasList); // Deleting EraList after deleting its contents
589 ReleaseList(__pMonthsList); // Deleting MonthsList after deleting its contents
590 ReleaseList(__pShortMonthsList); // Deleting ShortMonthsList after deleting its contents
591 ReleaseList(__pWeekdaysList); // Deleting WeekdaysList after deleting its contents
592 ReleaseList(__pShortWeekdaysList); // Deleting ShortWeekdaysList after deleting its contents
593 ReleaseList(__pAmPmList); // Deleting AmPmList after deleting its contents
597 __pTimeZonesMap->RemoveAll(true); // Removing and deleting contents of TimeZonesMap
598 delete __pTimeZonesMap; // Deleting TimeZonesMap
602 _DateTimeSymbolsImpl*
603 _DateTimeSymbolsImpl::GetDateTimeSymbolsImpl(DateTimeSymbols* pDateTimeSymbols)
605 return pDateTimeSymbols->__pDateTimeSymbolsImpl;