sync with tizen_2.0
[platform/framework/native/appfw.git] / src / locales / FLclCalendar.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         FLclCalendar.cpp
20 * @brief        This is the implementation file for Calendar class.
21 */
22 #include <FBaseSysLog.h>
23 #include <FApp_AppInfo.h>
24 #include <FLclCalendar.h>
25 #include <FLclGregorianCalendar.h>
26 #include <FLclLocale.h>
27 #include <FLclTimeZone.h>
28
29 #include "FLcl_CalendarImpl.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
96         if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
97         {
98                 r = E_UNSUPPORTED_OPERATION;
99         }
100
101         SysTryReturnResult(NID_LCL, locale.GetLanguageCode() != LANGUAGE_INVALID,
102                                 r, "Invalid argument is used. Language code is invalid. Given locale is not supported");
103
104         SysTryReturnResult(NID_LCL, locale.GetCountryCode() != COUNTRY_INVALID,
105                                 r, "Invalid argument is used. Country code is invalid. Given locale is not supported");
106
107         _timeZone = timezone;
108         _nextStamp = MINIMUM_USER_STAMP;
109         _isLenient = true;
110         _isConstructed = true;
111         return E_SUCCESS;
112 }
113
114 Calendar::~Calendar(void)
115 {
116 }
117
118 Calendar*
119 Calendar::CreateInstanceN(CalendarType calendarType)
120 {
121         return _CalendarImpl::CreateCalendarInstanceN(calendarType);
122 }
123
124 Calendar*
125 Calendar::CreateInstanceN(const TimeZone& timeZone, CalendarType calendarType)
126 {
127         return _CalendarImpl::CreateCalendarInstanceN(timeZone, calendarType);
128 }
129
130 Calendar*
131 Calendar::CreateInstanceN(const Locale& locale, CalendarType calendarType)
132 {
133         return _CalendarImpl::CreateCalendarInstanceN(locale, calendarType);
134 }
135
136 Calendar*
137 Calendar::CreateInstanceN(const TimeZone& timeZone, const Locale& locale, CalendarType calendarType)
138 {
139         return _CalendarImpl::CreateCalendarInstanceN(timeZone, locale, calendarType);
140 }
141
142 result
143 Calendar::After(const Calendar& otherCalendar, bool& after)
144 {
145         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
146
147         result r = E_INVALID_ARG;
148
149         if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
150         {
151                 r = E_INVALID_STATE;
152         }
153         SysTryReturnResult(NID_LCL, otherCalendar._pCalendarImpl, r, "Invalid argument is used. otherCalendar instance is invalid");
154         return _pCalendarImpl->After(*otherCalendar._pCalendarImpl, after);
155 }
156
157 result
158 Calendar::Before(const Calendar& otherCalendar, bool& before)
159 {
160         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
161
162         result r = E_INVALID_ARG;
163
164         if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
165         {
166                 r = E_INVALID_STATE;
167         }
168         SysTryReturnResult(NID_LCL, otherCalendar._pCalendarImpl, r, "Invalid argument is used. otherCalendar instance is invalid");
169         return _pCalendarImpl->Before(*otherCalendar._pCalendarImpl, before);
170 }
171
172 result
173 Calendar::Clear(void)
174 {
175         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
176         return _pCalendarImpl->ClearImpl();
177 }
178
179 result
180 Calendar::Clear(TimeField field)
181 {
182         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
183         return _pCalendarImpl->ClearImpl(field);
184 }
185
186 bool
187 Calendar::Equals(const Object& obj) const
188 {
189         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
190
191         const Calendar* pOtherInstance = dynamic_cast< const Calendar* >(&obj);
192         if ((pOtherInstance == null) || (pOtherInstance->_pCalendarImpl == null))
193         {
194                 return false;
195         }
196
197         return _pCalendarImpl->Equals(*pOtherInstance->_pCalendarImpl);
198 }
199
200 int
201 Calendar::GetHashCode(void) const
202 {
203         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
204         return _pCalendarImpl->GetHashCode();
205 }
206
207 int
208 Calendar::GetActualMaxTimeField(TimeField field) const
209 {
210         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
211         return _pCalendarImpl->GetActualMaxTimeField(field);
212 }
213
214 int
215 Calendar::GetActualMinTimeField(TimeField field) const
216 {
217         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
218         return _pCalendarImpl->GetActualMinTimeField(field);
219 }
220
221 int
222 Calendar::GetFirstDayOfWeek(void) const
223 {
224         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
225         return _pCalendarImpl->GetFirstDayOfWeekImpl();
226 }
227
228 int
229 Calendar::GetGreatestMinTimeField(TimeField field) const
230 {
231         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
232         return _pCalendarImpl->GetGreatestMinTimeField(field);
233 }
234
235 int
236 Calendar::GetLeastMaxTimeField(TimeField field) const
237 {
238         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
239         return _pCalendarImpl->GetLeastMaxTimeField(field);
240 }
241
242 int
243 Calendar::GetMaxTimeField(TimeField field) const
244 {
245         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
246         return _pCalendarImpl->GetMaxTimeField(field);
247 }
248
249 int
250 Calendar::GetMinTimeField(TimeField field) const
251 {
252         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
253         return _pCalendarImpl->GetMinTimeField(field);
254 }
255
256 int
257 Calendar::GetMinDaysInFirstWeek(void) const
258 {
259         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
260         return _pCalendarImpl->GetMinDaysInFirstWeekImpl();
261 }
262
263 DateTime
264 Calendar::GetTime(void) const
265 {
266         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
267         return _pCalendarImpl->GetTimeImpl();
268 }
269
270 int
271 Calendar::GetTimeField(TimeField field) const
272 {
273         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
274         return _pCalendarImpl->GetTimeFieldImpl(field);
275 }
276
277 TimeZone
278 Calendar::GetTimeZone(void) const
279 {
280         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
281         return _pCalendarImpl->GetTimeZoneImpl();
282 }
283
284 bool
285 Calendar::IsLenient(void) const
286 {
287         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
288         return _pCalendarImpl->IsLenientImpl();
289 }
290
291 bool
292 Calendar::IsSet(TimeField field) const
293 {
294         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
295         return _pCalendarImpl->IsSetImpl(field);
296 }
297
298 result
299 Calendar::Roll(TimeField field, int amount)
300 {
301         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
302         return _pCalendarImpl->Roll(field, amount);
303 }
304
305 result
306 Calendar::SetTime(const DateTime& dateTime)
307 {
308         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
309         return _pCalendarImpl->SetTimeImpl(dateTime);
310 }
311
312 result
313 Calendar::SetTime(int year, int month, int day, int hour, int minute, int second)
314 {
315         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
316         return _pCalendarImpl->SetTimeImpl(year, month, day, hour, minute, second);
317 }
318
319 result
320 Calendar::SetTimeField(TimeField field, int value)
321 {
322         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
323         return _pCalendarImpl->SetTimeFieldImpl(field, value);
324 }
325
326 result
327 Calendar::SetFirstDayOfWeek(DayOfWeek dayOfWeek)
328 {
329         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
330         return _pCalendarImpl->SetFirstDayOfWeekImpl(dayOfWeek);
331 }
332
333 result
334 Calendar::SetLenient(bool lenient)
335 {
336         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
337         return _pCalendarImpl->SetLenientImpl(lenient);
338 }
339
340 result
341 Calendar::SetMinDaysInFirstWeek(short value)
342 {
343         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
344         return _pCalendarImpl->SetMinDaysInFirstWeekImpl(value);
345 }
346
347 result
348 Calendar::SetTimeZone(const TimeZone& timeZone)
349 {
350         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
351         return _pCalendarImpl->SetTimeZoneImpl(timeZone);
352 }
353
354 result
355 Calendar::GetTimeInMillisec(long long& millisec) const
356 {
357         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
358         return _pCalendarImpl->GetTimeInMillisecImpl(millisec);
359 }
360
361 result
362 Calendar::SetTimeInMillisec(long long millisec)
363 {
364         SysAssertf(_pCalendarImpl != null, "Not yet constructed! Construct() should be called before use.");
365         return _pCalendarImpl->SetTimeInMillisecImpl(millisec);
366 }
367
368 bool
369 Calendar::IsEquivalentTo(const Calendar& calendar) const
370 {
371         bool isEquivalent = ((_isLenient == calendar.IsLenient()) &&
372                                                  (GetFirstDayOfWeek() == calendar.GetFirstDayOfWeek()) &&
373                                                  (GetMinDaysInFirstWeek() == calendar.GetMinDaysInFirstWeek()) &&
374                                                  (_timeZone == calendar.GetTimeZone()));
375         return isEquivalent;
376 }
377
378 };
379 };      // Tizen::Locales