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