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 <FBaseColIList.h>
25 #include <FBaseColArrayList.h>
26 #include <FBaseDateTime.h>
27 #include <FSclRecurrence.h>
28 #include <FBaseSysLog.h>
29 #include <FApp_AppInfo.h>
30 #include "FScl_RecurrenceImpl.h"
31 #include "FScl_CalendarbookImpl.h"
32 #include "FScl_CalendarbookUtil.h"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::App;
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, "[E_OUT_OF_MEMORY] Memory allocation failed.");
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<Tizen::Base::DateTime> pUntil;
88 if (rhs.__pUntil != null)
90 pUntil = std::unique_ptr<Tizen::Base::DateTime>(new (std::nothrow) DateTime(*rhs.__pUntil));
91 SysTryReturnVoidResult(NID_SCL, pUntil != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
94 std::unique_ptr<ArrayList, AllElementsDeleter> pExceptionDates(new (std::nothrow) ArrayList());
95 SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
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<Tizen::Base::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, "[E_OUT_OF_MEMORY] Memory allocation failed.");
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());
153 __pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate)));
157 __pUntil = std::move(pUntil);
163 _RecurrenceImpl::Equals(const Object& rhs) const
165 const _RecurrenceImpl* pRecurrenceImpl = dynamic_cast<const _RecurrenceImpl*>(&rhs);
167 if (pRecurrenceImpl == null)
172 return (__type == pRecurrenceImpl->__type && __interval == pRecurrenceImpl->__interval
173 && __count == pRecurrenceImpl->__count && __weekStart == pRecurrenceImpl->__weekStart
174 && __dayOfWeek == pRecurrenceImpl->__dayOfWeek && __dayOfMonth == pRecurrenceImpl->__dayOfMonth
175 && __weekOfMonth == pRecurrenceImpl->__weekOfMonth && __monthOfYear == pRecurrenceImpl->__monthOfYear);
179 _RecurrenceImpl::GetHashCode(void) const
183 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __type;
184 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __interval;
185 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __count;
186 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekStart;
187 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfWeek;
188 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfMonth;
189 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekOfMonth;
190 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __monthOfYear;
192 DateTime* pTmpExDate = null;
193 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
194 while (pEnum->MoveNext() == E_SUCCESS)
196 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
197 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ pTmpExDate->GetHashCode();
204 _RecurrenceImpl::GetFrequency(void) const
210 _RecurrenceImpl::GetInterval(void) const
216 _RecurrenceImpl::GetUntil(void) const
218 return __pUntil.get();
222 _RecurrenceImpl::GetCounts(void) const
228 _RecurrenceImpl::GetWeekStart(void) const
234 _RecurrenceImpl::GetDayOfWeek(void) const
240 _RecurrenceImpl::GetDayOfMonth(void) const
246 _RecurrenceImpl::GetWeekOfMonth(void) const
248 return __weekOfMonth;
252 _RecurrenceImpl::GetMonthOfYear(void)const
254 return __monthOfYear;
258 _RecurrenceImpl::SetFrequency(RecurFrequency type)
262 // reset all properties
263 if (__pUntil != null)
272 __interval = _DEFAULT_INTERVAL;
273 __count = _DEFAULT_COUNT;
274 __weekStart = _DEFAULT_WEEK_START;
276 __pExceptionDates->RemoveAll(true);
280 _RecurrenceImpl::SetInterval(int interval)
282 SysTryReturnResult(NID_SCL, interval >= _MIN_RECURRENCE_INTERVAL, E_INVALID_ARG, "Invalid argument is used. The interval is less than 1");
284 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
286 SysTryReturnResult(NID_SCL, interval <= MAX_RECURRENCE_INTERVAL_VALUE, E_INVALID_ARG
287 , "The inverval exceeds MAX_RECURRENCE_INTERVAL_VALUE");
290 __interval = interval;
292 // reset exception dates
293 __pExceptionDates->RemoveAll(true);
299 _RecurrenceImpl::SetUntil(const DateTime* pUntil)
303 SysTryReturnResult(NID_SCL, (_CalendarbookUtil::CheckValidDateTime(*pUntil) == true) || (*pUntil == DateTime::GetMaxValue()), E_INVALID_ARG, "Invalid argument is used. The until date is invalid.");
305 __pUntil.reset(new (std::nothrow) DateTime(*pUntil));
306 if (__pUntil == null)
308 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "Memory allocation failed.");
309 return E_OUT_OF_MEMORY;
318 __count = _DEFAULT_COUNT;
321 // reset exception dates
322 __pExceptionDates->RemoveAll(true);
328 _RecurrenceImpl::SetCounts(int count)
330 SysTryReturnResult(NID_SCL, count >= 0, E_INVALID_ARG, "Invalid argument is used. The count is less than 0");
335 if (__pUntil != null)
340 // reset exception dates
341 __pExceptionDates->RemoveAll(true);
347 _RecurrenceImpl::SetWeekStart(CalDayOfWeek weekStart)
349 SysTryReturnResult(NID_SCL, weekStart == CAL_SUNDAY || weekStart == CAL_MONDAY, E_INVALID_ARG,
350 "Invalid argument is used. weekStart = %d", weekStart);
352 __weekStart = weekStart;
354 // reset exception dates
355 __pExceptionDates->RemoveAll(true);
361 _RecurrenceImpl::SetDayOfWeek(int day)
363 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH,
364 "The frequency type is daily. It should be WEEKLY ,MOTHLY or YEARLY");
365 SysTryReturnResult(NID_SCL, day >= CAL_SUNDAY, E_INVALID_ARG, "Invalid argument is used. The day(%d) is less than min value.", day);
366 SysTryReturnResult(NID_SCL, day <= _MAX_DAY_OF_WEEK, E_INVALID_ARG, "Invalid argument is used. The day(%d) exceeds max value.", day);
370 // reset day of month
373 // reset exception dates
374 __pExceptionDates->RemoveAll(true);
380 _RecurrenceImpl::SetDayOfMonth(int day)
382 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
383 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
384 SysTryReturnResult(NID_SCL, day >= _DAY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is less than 1");
385 SysTryReturnResult(NID_SCL, day <= _DAY_THIRTY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is greater than 31");
389 // reset day of week and week of month
393 // reset exception dates
394 __pExceptionDates->RemoveAll(true);
400 _RecurrenceImpl::SetWeekOfMonth(int week)
402 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
403 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
404 SysTryReturnResult(NID_SCL, week >= _WEEK_ONE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is less than 1.", week);
405 SysTryReturnResult(NID_SCL, week <= _WEEK_FIVE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is greater than 5.", week);
407 __weekOfMonth = week;
409 // reset day of month
412 // reset exception dates
413 __pExceptionDates->RemoveAll(true);
420 _RecurrenceImpl::SetMonthOfYear(int month)
422 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
423 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
424 SysTryReturnResult(NID_SCL, __type != FREQ_MONTHLY, E_TYPE_MISMATCH, "The frequency is monthly.");
425 SysTryReturnResult(NID_SCL, month >= _MONTH_ONE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is less than 1 ", month);
426 SysTryReturnResult(NID_SCL, month <= _MONTH_TWELVE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is greater than 12", month);
428 __monthOfYear = month;
430 // reset exception dates
431 __pExceptionDates->RemoveAll(true);
437 _RecurrenceImpl::AddExceptionDate(const DateTime& exceptionDate)
439 SysTryReturnResult(NID_SCL, exceptionDate >= _CalendarbookImpl::GetMinDateTime() &&
440 exceptionDate <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. exceptionDate = %S", exceptionDate.ToString().GetPointer());
442 bool alreadyExist = false;
444 DateTime* pTmpExDate = null;
445 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
446 while (pEnum->MoveNext() == E_SUCCESS)
448 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
449 if (*pTmpExDate == exceptionDate)
456 SysTryReturnResult(NID_SCL, !alreadyExist, E_OBJ_ALREADY_EXIST, "The exceptionDate already exists");
458 DateTime* pExDate = new (std::nothrow) DateTime(exceptionDate);
459 SysTryReturnResult(NID_SCL, pExDate != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
461 __pExceptionDates->Add(*pExDate);
467 _RecurrenceImpl::GetExceptionDatesN(void) const
470 result r = E_SUCCESS;
472 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) ArrayList());
473 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
474 r = pList->Construct();
475 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
477 DateTime* pExDate = null;
478 DateTime* pTmpExDate = null;
479 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
480 while (pEnum->MoveNext() == E_SUCCESS)
482 pExDate = static_cast<DateTime*>(pEnum->GetCurrent());
485 pTmpExDate = new (std::nothrow) DateTime(*pExDate);
486 SysTryReturn(NID_SCL, pTmpExDate != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
487 pList->Add(*pTmpExDate);
491 return pList.release();
495 _RecurrenceImpl::GetInstance(Recurrence& recurrence)
497 return recurrence.__pRecurrenceImpl;
500 const _RecurrenceImpl*
501 _RecurrenceImpl::GetInstance(const Recurrence& recurrence)
503 return recurrence.__pRecurrenceImpl;