Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / locales / FLcl_DateTimeFormatterImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FLclDateTimeFormatter.cpp
19  * @brief       This is the implementation file for DateTimeFormatter class.
20  */
21
22 // Includes
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FApp_AppInfo.h>
26 #include <FLclDateTimeFormatter.h>
27 #include "FLcl_DateTimeFormatterImpl.h"
28 #include "FLcl_LocaleData.h"
29 #include "FLcl_LocaleManagerImpl.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
34
35 namespace Tizen { namespace Locales
36 {
37
38 _DateTimeFormatterImpl::_DateTimeFormatterImpl(void)
39         : __pLocaleData(null)
40         , __pSymbols(null)
41 {
42 }
43
44 _DateTimeFormatterImpl::~_DateTimeFormatterImpl(void)
45 {
46         delete __pLocaleData;
47         delete __pSymbols;
48 }
49
50 // The DateTimeFormatter always use System Locale if not provided by the user.
51 // System locale may be different for default ICU locale.
52 DateTimeFormatter*
53 _DateTimeFormatterImpl::CreateInstanceN(DateTimeStyle dateStyle, DateTimeStyle timeStyle)
54 {
55         result r = E_INVALID_ARG;
56
57         if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
58         {
59                 r = E_UNSUPPORTED_OPERATION;
60         }
61
62         SysTryReturn(NID_LCL, ValidateDateTimeStyle(dateStyle), null, r, "[%s] Invalid argument is used. dateStyle is invalid", GetErrorMessage(r));
63         SysTryReturn(NID_LCL, ValidateDateTimeStyle(timeStyle), null, r, "[%s] Invalid argument is used. timeStyle is invalid", GetErrorMessage(r));
64
65         // using system locale, may be different from ICU default locale
66         DateTimeFormatter* pDateTimeFormatter = CreateInstanceN(_LocaleManagerImpl::GetSystemLocale(), dateStyle, timeStyle);
67
68         if (GetLastResult() == E_INVALID_ARG)
69         {
70                 SetLastResult(E_UNSUPPORTED_OPERATION);
71         }
72         return pDateTimeFormatter;
73 }
74
75 DateTimeFormatter*
76 _DateTimeFormatterImpl::CreateInstanceN(const Locale& locale, DateTimeStyle dateStyle, DateTimeStyle timeStyle)
77 {
78         result r = (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat()) ? E_UNSUPPORTED_OPERATION : E_INVALID_ARG;
79
80         SysTryReturn(NID_LCL, ValidateDateTimeStyle(dateStyle), null, r, "[%s] Invalid argument is used. dateStyle is invalid", GetErrorMessage(r));
81         SysTryReturn(NID_LCL, ValidateDateTimeStyle(timeStyle), null, r, "[%s] Invalid argument is used. timeStyle is invalid", GetErrorMessage(r));
82
83         ClearLastResult();
84
85         // Create a DateTimeFormatter of the desired style.
86         std::unique_ptr<DateTimeFormatter> pDateTimeFormatter(new (std::nothrow) DateTimeFormatter());
87         SysTryReturn(NID_LCL, pDateTimeFormatter, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
88
89         std::unique_ptr<_DateTimeFormatterImpl> pDateTimeFormatterImpl(new (std::nothrow) _DateTimeFormatterImpl);
90         SysTryReturn(NID_LCL,pDateTimeFormatterImpl, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
91
92         std::unique_ptr<_LocaleData> pLocaleData(new (std::nothrow) _LocaleData);
93         SysTryReturn(NID_LCL, pLocaleData, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
94
95         // This create and set ICU DateFormatter in _Locale data object for future use
96         r = pLocaleData->SetDateTimeFormatter(dateStyle, timeStyle, locale);
97         SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s]",GetErrorMessage(r));
98
99         // Create DateTimeSymbols and construct using locale
100         pDateTimeFormatterImpl->__pSymbols = new (std::nothrow) DateTimeSymbols;
101         if (pDateTimeFormatterImpl->__pSymbols)
102         {
103                 pDateTimeFormatterImpl->__pSymbols->Construct(locale);
104         }
105
106         pDateTimeFormatter->__pDateTimeFormatterImpl = pDateTimeFormatterImpl.release();
107         pDateTimeFormatter->__pDateTimeFormatterImpl->__pLocaleData = pLocaleData.release();
108         return pDateTimeFormatter.release();      // Here as LastResult is already cleared, no need to set it to E_SUCCESS
109 }
110
111 // This function will not have TimeZone informations and always print time zone as GMT+00:00
112 result
113 _DateTimeFormatterImpl::Format(const DateTime& date, String& strBuf) const
114 {
115         SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
116         return __pLocaleData->FormatDateTime(date, strBuf);   // Format date using ICU date formatter
117 }
118
119 result
120 _DateTimeFormatterImpl::Format(const Calendar& calendar, String& strBuf) const
121 {
122         SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
123         return __pLocaleData->FormatDateTime(calendar, strBuf);   // Format date using ICU date formatter
124 }
125
126 // This function sets pattern used by DateTimeFormatter for formatting date.
127 // Please check http://icu-project.org/apiref/icu4c/classSimpleDateFormat.html for possible letter and
128 // their meaning in patter string.
129 result
130 _DateTimeFormatterImpl::ApplyPattern(const String& pattern)
131 {
132         SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
133         return __pLocaleData->SetDateTimePattern(pattern);      // Set pattern to ICU date formatter
134 }
135
136 String
137 _DateTimeFormatterImpl::GetPattern(void) const
138 {
139         return __pLocaleData->GetDateTimePattern();
140 }
141
142 const DateTimeSymbols*
143 _DateTimeFormatterImpl::GetDateTimeSymbols(void) const
144 {
145         return __pSymbols;
146 }
147
148 void
149 _DateTimeFormatterImpl::SetDateTimeSymbols(const DateTimeSymbols& newSymbols)
150 {
151         SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
152
153         result r = __pLocaleData->SetDateTimeSymbols(newSymbols);                   // Set symbols to ICU date formatter
154         if (!IsFailed(r))
155         {
156                 if (__pSymbols)
157                 {
158                         __pSymbols = __pLocaleData->GetDateTimeSymbolsN(__pSymbols);        // Set __pSymbols obtained from ICU date formatter
159                 }
160         }
161 }
162
163 bool
164 _DateTimeFormatterImpl::ValidateDateTimeStyle(DateTimeStyle dateTimeStyle)
165 {
166         return (dateTimeStyle < DATE_TIME_STYLE_NONE || dateTimeStyle > DATE_TIME_STYLE_SHORT)? false : true;
167 }
168
169 };
170 };      // Tizen::Locales