2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FScl_RecurrenceImpl.cpp
18 * @brief This is the implementation for _RecurrenceImpl class.
20 * This file contains definitions of @e _RecurrenceImpl class.
24 #include <FApp_AppInfo.h>
25 #include <FBaseColArrayList.h>
26 #include <FBaseColIList.h>
27 #include <FBaseDateTime.h>
28 #include <FBaseSysLog.h>
29 #include <FSclRecurrence.h>
30 #include "FScl_CalendarbookImpl.h"
31 #include "FScl_CalendarbookUtil.h"
32 #include "FScl_RecurrenceImpl.h"
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
38 namespace Tizen { namespace Social
41 static const RecurFrequency _DEFAULT_RECURRENCE_TYPE = FREQ_DAILY;
42 static const int _DEFAULT_INTERVAL = 1;
43 static const int _DEFAULT_COUNT = 1;
44 static const CalDayOfWeek _DEFAULT_WEEK_START = CAL_MONDAY;
45 static const int _WEEK_ONE = 1;
46 static const int _WEEK_FIVE = 5;
47 static const int _MONTH_ONE = 1;
48 static const int _MONTH_TWELVE = 12;
49 static const int _DAY_ONE = 1;
50 static const int _DAY_THIRTY_ONE = 31;
51 static const int _MIN_RECURRENCE_INTERVAL = 1;
52 static const int _MAX_DAY_OF_WEEK = 0x7F;
54 _RecurrenceImpl::_RecurrenceImpl(void)
55 : __type(_DEFAULT_RECURRENCE_TYPE)
56 , __interval(_DEFAULT_INTERVAL)
58 , __count(_DEFAULT_COUNT)
59 , __weekStart(_DEFAULT_WEEK_START)
65 std::unique_ptr<ArrayList, AllElementsDeleter> pExceptionDates(new (std::nothrow) ArrayList());
66 SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
68 result r = pExceptionDates->Construct();
69 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
71 __pExceptionDates = std::move(pExceptionDates);
74 _RecurrenceImpl::_RecurrenceImpl(const _RecurrenceImpl& rhs)
76 , __interval(rhs.__interval)
78 , __count(rhs.__count)
79 , __weekStart(rhs.__weekStart)
80 , __dayOfWeek(rhs.__dayOfWeek)
81 , __dayOfMonth(rhs.__dayOfMonth)
82 , __weekOfMonth(rhs.__weekOfMonth)
83 , __monthOfYear(rhs.__monthOfYear)
84 , __pExceptionDates(null)
86 std::unique_ptr<DateTime> pUntil;
88 if (rhs.__pUntil != null)
90 pUntil = std::unique_ptr<DateTime>(new (std::nothrow) DateTime(*rhs.__pUntil));
91 SysTryReturnVoidResult(NID_SCL, pUntil != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
94 std::unique_ptr<ArrayList, AllElementsDeleter> pExceptionDates(new (std::nothrow) ArrayList());
95 SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
97 result r = pExceptionDates->Construct();
98 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
100 if (rhs.__pExceptionDates != null)
102 DateTime* pTmpExDate = null;
103 std::unique_ptr<IEnumerator> pEnum(rhs.__pExceptionDates->GetEnumeratorN());
104 while (pEnum->MoveNext() == E_SUCCESS)
106 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
107 pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate)));
111 __pExceptionDates = std::move(pExceptionDates);
112 __pUntil = std::move(pUntil);
115 _RecurrenceImpl::~_RecurrenceImpl(void)
120 _RecurrenceImpl::operator =(const _RecurrenceImpl& rhs)
127 std::unique_ptr<DateTime> pUntil;
130 __interval = rhs.__interval;
131 __count = rhs.__count;
132 __weekStart = rhs.__weekStart;
133 __dayOfWeek = rhs.__dayOfWeek;
134 __dayOfMonth = rhs.__dayOfMonth;
135 __weekOfMonth = rhs.__weekOfMonth;
136 __monthOfYear = rhs.__monthOfYear;
138 if (rhs.__pUntil != null)
140 pUntil.reset(new (std::nothrow) DateTime(*(rhs.__pUntil)));
141 SysTryReturn(NID_SCL, pUntil != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
144 if (__pExceptionDates != null && rhs.__pExceptionDates != null)
146 __pExceptionDates->RemoveAll(true);
148 DateTime* pTmpExDate = null;
149 std::unique_ptr<IEnumerator> pEnum(rhs.__pExceptionDates->GetEnumeratorN());
150 while (pEnum->MoveNext() == E_SUCCESS)
152 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
154 if (pTmpExDate != null)
156 __pExceptionDates->Add(new (std::nothrow) DateTime(*pTmpExDate));
161 __pUntil = std::move(pUntil);
167 _RecurrenceImpl::Equals(const Object& rhs) const
169 const _RecurrenceImpl* pRecurrenceImpl = dynamic_cast<const _RecurrenceImpl*>(&rhs);
171 if (pRecurrenceImpl == null)
176 return (__type == pRecurrenceImpl->__type && __interval == pRecurrenceImpl->__interval
177 && __count == pRecurrenceImpl->__count && __weekStart == pRecurrenceImpl->__weekStart
178 && __dayOfWeek == pRecurrenceImpl->__dayOfWeek && __dayOfMonth == pRecurrenceImpl->__dayOfMonth
179 && __weekOfMonth == pRecurrenceImpl->__weekOfMonth && __monthOfYear == pRecurrenceImpl->__monthOfYear);
183 _RecurrenceImpl::GetHashCode(void) const
187 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __type;
188 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __interval;
189 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __count;
190 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekStart;
191 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfWeek;
192 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfMonth;
193 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekOfMonth;
194 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __monthOfYear;
196 DateTime* pTmpExDate = null;
197 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
198 while (pEnum->MoveNext() == E_SUCCESS)
200 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
201 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ pTmpExDate->GetHashCode();
208 _RecurrenceImpl::GetFrequency(void) const
214 _RecurrenceImpl::GetInterval(void) const
220 _RecurrenceImpl::GetUntil(void) const
222 return __pUntil.get();
226 _RecurrenceImpl::GetCounts(void) const
232 _RecurrenceImpl::GetWeekStart(void) const
238 _RecurrenceImpl::GetDayOfWeek(void) const
244 _RecurrenceImpl::GetDayOfMonth(void) const
250 _RecurrenceImpl::GetWeekOfMonth(void) const
252 return __weekOfMonth;
256 _RecurrenceImpl::GetMonthOfYear(void)const
258 return __monthOfYear;
262 _RecurrenceImpl::SetFrequency(RecurFrequency type)
266 // reset all properties
267 if (__pUntil != null)
276 __interval = _DEFAULT_INTERVAL;
277 __count = _DEFAULT_COUNT;
278 __weekStart = _DEFAULT_WEEK_START;
280 __pExceptionDates->RemoveAll(true);
284 _RecurrenceImpl::SetInterval(int interval)
286 SysTryReturnResult(NID_SCL, interval >= _MIN_RECURRENCE_INTERVAL, E_INVALID_ARG, "Invalid argument is used. The interval is less than 1");
288 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
290 SysTryReturnResult(NID_SCL, interval <= MAX_RECURRENCE_INTERVAL_VALUE, E_INVALID_ARG
291 , "The inverval exceeds MAX_RECURRENCE_INTERVAL_VALUE");
294 __interval = interval;
296 // reset exception dates
297 __pExceptionDates->RemoveAll(true);
303 _RecurrenceImpl::SetUntil(const DateTime* pUntil)
307 SysTryReturnResult(NID_SCL, (_CalendarbookUtil::CheckValidDateTime(*pUntil) == true) || (*pUntil == DateTime::GetMaxValue()), E_INVALID_ARG, "Invalid argument is used. The until date is invalid.");
309 __pUntil.reset(new (std::nothrow) DateTime(*pUntil));
310 if (__pUntil == null)
312 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "Memory allocation failed.");
313 return E_OUT_OF_MEMORY;
322 __count = _DEFAULT_COUNT;
325 // reset exception dates
326 __pExceptionDates->RemoveAll(true);
332 _RecurrenceImpl::SetCounts(int count)
334 SysTryReturnResult(NID_SCL, count >= 0, E_INVALID_ARG, "Invalid argument is used. The count is less than 0");
339 if (__pUntil != null)
344 // reset exception dates
345 __pExceptionDates->RemoveAll(true);
351 _RecurrenceImpl::SetWeekStart(CalDayOfWeek weekStart)
353 SysTryReturnResult(NID_SCL, weekStart == CAL_SUNDAY || weekStart == CAL_MONDAY, E_INVALID_ARG,
354 "Invalid argument is used. weekStart = %d", weekStart);
356 __weekStart = weekStart;
358 // reset exception dates
359 __pExceptionDates->RemoveAll(true);
365 _RecurrenceImpl::SetDayOfWeek(int day)
367 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH,
368 "The frequency type is daily. It should be WEEKLY ,MOTHLY or YEARLY");
369 SysTryReturnResult(NID_SCL, day >= CAL_SUNDAY, E_INVALID_ARG, "Invalid argument is used. The day(%d) is less than min value.", day);
370 SysTryReturnResult(NID_SCL, day <= _MAX_DAY_OF_WEEK, E_INVALID_ARG, "Invalid argument is used. The day(%d) exceeds max value.", day);
374 // reset day of month
377 // reset exception dates
378 __pExceptionDates->RemoveAll(true);
384 _RecurrenceImpl::SetDayOfMonth(int day)
386 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
387 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
388 SysTryReturnResult(NID_SCL, day >= _DAY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is less than 1");
389 SysTryReturnResult(NID_SCL, day <= _DAY_THIRTY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is greater than 31");
393 // reset day of week and week of month
397 // reset exception dates
398 __pExceptionDates->RemoveAll(true);
404 _RecurrenceImpl::SetWeekOfMonth(int week)
406 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
407 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
408 SysTryReturnResult(NID_SCL, week >= _WEEK_ONE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is less than 1.", week);
409 SysTryReturnResult(NID_SCL, week <= _WEEK_FIVE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is greater than 5.", week);
411 __weekOfMonth = week;
413 // reset day of month
416 // reset exception dates
417 __pExceptionDates->RemoveAll(true);
424 _RecurrenceImpl::SetMonthOfYear(int month)
426 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
427 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
428 SysTryReturnResult(NID_SCL, __type != FREQ_MONTHLY, E_TYPE_MISMATCH, "The frequency is monthly.");
429 SysTryReturnResult(NID_SCL, month >= _MONTH_ONE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is less than 1 ", month);
430 SysTryReturnResult(NID_SCL, month <= _MONTH_TWELVE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is greater than 12", month);
432 __monthOfYear = month;
434 // reset exception dates
435 __pExceptionDates->RemoveAll(true);
441 _RecurrenceImpl::AddExceptionDate(const DateTime& exceptionDate)
443 SysTryReturnResult(NID_SCL, exceptionDate >= _CalendarbookImpl::GetMinDateTime() &&
444 exceptionDate <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. exceptionDate = %S", exceptionDate.ToString().GetPointer());
446 bool alreadyExist = false;
448 DateTime* pTmpExDate = null;
449 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
450 while (pEnum->MoveNext() == E_SUCCESS)
452 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
453 if (*pTmpExDate == exceptionDate)
460 SysTryReturnResult(NID_SCL, !alreadyExist, E_OBJ_ALREADY_EXIST, "The exceptionDate already exists");
462 std::unique_ptr<DateTime> pExDate(new (std::nothrow) DateTime(exceptionDate));
463 SysTryReturnResult(NID_SCL, pExDate != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
465 result r = __pExceptionDates->Add(pExDate.get());
466 SysTryReturnResult(NID_SCL, !IsFailed(r), E_INVALID_ARG, "Propagating.");
474 _RecurrenceImpl::GetExceptionDatesN(void) const
477 result r = E_SUCCESS;
479 std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
480 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
481 r = pList->Construct();
482 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
484 DateTime* pExDate = null;
485 DateTime* pTmpExDate = null;
486 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
487 while (pEnum->MoveNext() == E_SUCCESS)
489 pExDate = static_cast<DateTime*>(pEnum->GetCurrent());
492 pTmpExDate = new (std::nothrow) DateTime(*pExDate);
493 SysTryReturn(NID_SCL, pTmpExDate != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
494 pList->Add(pTmpExDate);
498 return pList.release();
502 _RecurrenceImpl::GetInstance(Recurrence& recurrence)
504 return recurrence.__pRecurrenceImpl;
507 const _RecurrenceImpl*
508 _RecurrenceImpl::GetInstance(const Recurrence& recurrence)
510 return recurrence.__pRecurrenceImpl;