minor code cleanup
[platform/framework/native/appfw.git] / src / locales / FLclCalendar.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         FLclCalendar.cpp
19 * @brief        This is the implementation file for Calendar class.
20 */
21 #include <FBaseSysLog.h>
22 #include <FApp_AppInfo.h>
23 #include <FLclCalendar.h>
24 #include <FLclGregorianCalendar.h>
25 #include <FLclLocale.h>
26 #include <FLclTimeZone.h>
27
28 #include "FLcl_CalendarImpl.h"
29 #include "FLcl_LocaleImpl.h"
30
31 using namespace Tizen::Base;
32
33 namespace Tizen { namespace Locales
34 {
35
36 Calendar::Calendar(void)
37         : _time(0)
38         , _timeZone()
39         , _isTimeSet(false)
40         , _areFieldsSet(false)
41         , _areAllFieldsSet(false)
42         , _nextStamp(0)
43         , _isLenient(true)
44         , _isConstructed(false)
45         , _pCalendarImpl(null)
46 {
47         for (int i = 0; i < TIME_FIELD_FIELD_COUNT; i++)
48         {
49                 _timeFields[i] = 0;
50                 _stamp[i] = UNSET;
51                 _isSet[i] = false;
52         }
53 }
54
55 result
56 Calendar::Construct(void)
57 {
58         return Construct(TimeZone::GetGmtTimeZone(), Locale(LANGUAGE_ENG, COUNTRY_GB));
59 }
60
61 result
62 Calendar::Construct(const Calendar& other)
63 {
64         // Object is not allowed to construct twice
65         SysAssertf(_isConstructed == false,
66                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
67
68         for (int i = 0; i < TIME_FIELD_FIELD_COUNT; i++)
69         {
70                 _timeFields[i] = other._timeFields[i];
71                 _stamp[i] = other._stamp[i];
72                 _isSet[i] = other._isSet[i];
73         }
74
75         _time = other._time;
76         _timeZone = other._timeZone;
77         _isTimeSet = other._isTimeSet;
78         _areFieldsSet = other._areFieldsSet;
79         _areAllFieldsSet = other._areAllFieldsSet;
80         _nextStamp = other._nextStamp;
81         _isLenient = other._isLenient;
82         _isConstructed = true;
83
84         return E_SUCCESS;
85 }
86
87 result
88 Calendar::Construct(const TimeZone& timezone, const Locale& locale)
89 {
90         // Object is not allowed to construct twice
91         SysAssertf(_isConstructed == false,
92                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
93
94         result r = E_INVALID_ARG;
95     if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
96     {
97             r = E_UNSUPPORTED_OPERATION;
98     }
99
100         SysTryReturnResult(NID_LCL, _LocaleImpl::IsSupported(locale), r, "Given locale is not supported");
101
102         _timeZone = timezone;
103         _nextStamp = MINIMUM_USER_STAMP;
104         _isLenient = true;
105         _isConstructed = true;
106         return E_SUCCESS;
107 }
108
109 Calendar::~Calendar(void)
110 {
111 }
112
113 Calendar*
114 Calendar::CreateInstanceN(CalendarType calendarType)
115 {
116         return _CalendarImpl::CreateCalendarInstanceN(calendarType);
117 }
118
119 Calendar*
120 Calendar::CreateInstanceN(const TimeZone& timeZone, CalendarType calendarType)
121 {
122         return _CalendarImpl::CreateCalendarInstanceN(timeZone, calendarType);
123 }
124
125 Calendar*
126 Calendar::CreateInstanceN(const Locale& locale, CalendarType calendarType)
127 {
128         return _CalendarImpl::CreateCalendarInstanceN(locale, calendarType);
129 }
130
131 Calendar*
132 Calendar::CreateInstanceN(const TimeZone& timeZone, const Locale& locale, CalendarType calendarType)
133 {
134         return _CalendarImpl::CreateCalendarInstanceN(timeZone, locale, calendarType);
135 }
136
137 result
138 Calendar::After(const Calendar& otherCalendar, bool& after)
139 {
140         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
141
142         result r = E_INVALID_ARG;
143
144         if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
145         {
146                 r = E_INVALID_STATE;
147         }
148         SysTryReturnResult(NID_LCL, otherCalendar._pCalendarImpl, r, "Invalid argument is used. otherCalendar instance is invalid");
149         return _pCalendarImpl->AfterImpl(*otherCalendar._pCalendarImpl, after);
150 }
151
152 result
153 Calendar::Before(const Calendar& otherCalendar, bool& before)
154 {
155         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
156
157         result r = E_INVALID_ARG;
158
159         if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
160         {
161                 r = E_INVALID_STATE;
162         }
163         SysTryReturnResult(NID_LCL, otherCalendar._pCalendarImpl, r, "Invalid argument is used. otherCalendar instance is invalid");
164         return _pCalendarImpl->BeforeImpl(*otherCalendar._pCalendarImpl, before);
165 }
166
167 result
168 Calendar::Clear(void)
169 {
170         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
171         return _pCalendarImpl->ClearImpl();
172 }
173
174 result
175 Calendar::Clear(TimeField field)
176 {
177         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
178         return _pCalendarImpl->ClearImpl(field);
179 }
180
181 bool
182 Calendar::Equals(const Object& obj) const
183 {
184         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
185
186         const Calendar* pOtherInstance = dynamic_cast< const Calendar* >(&obj);
187         if ((pOtherInstance == null) || (pOtherInstance->_pCalendarImpl == null) || GetType() != pOtherInstance->GetType())
188         {
189                 return false;
190         }
191
192         return _pCalendarImpl->EqualsImpl(*pOtherInstance->_pCalendarImpl);
193 }
194
195 int
196 Calendar::GetHashCode(void) const
197 {
198         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
199         return _pCalendarImpl->GetHashCodeImpl();
200 }
201
202 int
203 Calendar::GetActualMaxTimeField(TimeField field) const
204 {
205         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
206         return _pCalendarImpl->GetActualMaxTimeFieldImpl(field);
207 }
208
209 int
210 Calendar::GetActualMinTimeField(TimeField field) const
211 {
212         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
213         return _pCalendarImpl->GetActualMinTimeFieldImpl(field);
214 }
215
216 int
217 Calendar::GetFirstDayOfWeek(void) const
218 {
219         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
220         return _pCalendarImpl->GetFirstDayOfWeekImpl();
221 }
222
223 int
224 Calendar::GetGreatestMinTimeField(TimeField field) const
225 {
226         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
227         return _pCalendarImpl->GetGreatestMinTimeFieldImpl(field);
228 }
229
230 int
231 Calendar::GetLeastMaxTimeField(TimeField field) const
232 {
233         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
234         return _pCalendarImpl->GetLeastMaxTimeFieldImpl(field);
235 }
236
237 int
238 Calendar::GetMaxTimeField(TimeField field) const
239 {
240         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
241         return _pCalendarImpl->GetMaxTimeFieldImpl(field);
242 }
243
244 int
245 Calendar::GetMinTimeField(TimeField field) const
246 {
247         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
248         return _pCalendarImpl->GetMinTimeFieldImpl(field);
249 }
250
251 int
252 Calendar::GetMinDaysInFirstWeek(void) const
253 {
254         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
255         return _pCalendarImpl->GetMinDaysInFirstWeekImpl();
256 }
257
258 DateTime
259 Calendar::GetTime(void) const
260 {
261         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
262         return _pCalendarImpl->GetTimeImpl();
263 }
264
265 int
266 Calendar::GetTimeField(TimeField field) const
267 {
268         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
269         return _pCalendarImpl->GetTimeFieldImpl(field);
270 }
271
272 TimeZone
273 Calendar::GetTimeZone(void) const
274 {
275         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
276         return _pCalendarImpl->GetTimeZoneImpl();
277 }
278
279 bool
280 Calendar::IsLenient(void) const
281 {
282         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
283         return _pCalendarImpl->IsLenientImpl();
284 }
285
286 bool
287 Calendar::IsSet(TimeField field) const
288 {
289         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
290         return _pCalendarImpl->IsSetImpl(field);
291 }
292
293 result
294 Calendar::Roll(TimeField field, int amount)
295 {
296         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
297         return _pCalendarImpl->RollImpl(field, amount);
298 }
299
300 result
301 Calendar::SetTime(const DateTime& dateTime)
302 {
303         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
304         return _pCalendarImpl->SetTimeImpl(dateTime);
305 }
306
307 result
308 Calendar::SetTime(int year, int month, int day, int hour, int minute, int second)
309 {
310         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
311         return _pCalendarImpl->SetTimeImpl(year, month, day, hour, minute, second);
312 }
313
314 result
315 Calendar::SetTimeField(TimeField field, int value)
316 {
317         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
318         return _pCalendarImpl->SetTimeFieldImpl(field, value);
319 }
320
321 result
322 Calendar::SetFirstDayOfWeek(DayOfWeek dayOfWeek)
323 {
324         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
325         return _pCalendarImpl->SetFirstDayOfWeekImpl(dayOfWeek);
326 }
327
328 result
329 Calendar::SetLenient(bool lenient)
330 {
331         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
332         return _pCalendarImpl->SetLenientImpl(lenient);
333 }
334
335 result
336 Calendar::SetMinDaysInFirstWeek(short value)
337 {
338         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
339         return _pCalendarImpl->SetMinDaysInFirstWeekImpl(value);
340 }
341
342 result
343 Calendar::SetTimeZone(const TimeZone& timeZone)
344 {
345         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
346         return _pCalendarImpl->SetTimeZoneImpl(timeZone);
347 }
348
349 result
350 Calendar::GetTimeInMillisec(long long& millisec) const
351 {
352         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
353         return _pCalendarImpl->GetTimeInMillisecImpl(millisec);
354 }
355
356 result
357 Calendar::SetTimeInMillisec(long long millisec)
358 {
359         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
360         return _pCalendarImpl->SetTimeInMillisecImpl(millisec);
361 }
362
363 bool
364 Calendar::IsEquivalentTo(const Calendar& calendar) const
365 {
366         bool isEquivalent = ((IsLenient() == calendar.IsLenient()) &&
367                                 (GetFirstDayOfWeek() == calendar.GetFirstDayOfWeek()) &&
368                                 (GetMinDaysInFirstWeek() == calendar.GetMinDaysInFirstWeek()) &&
369                                 (GetTimeZone() == calendar.GetTimeZone()) &&
370                                 (GetType() == calendar.GetType()));
371         return isEquivalent;
372 }
373
374 };
375 };      // Tizen::Locales