2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FScl_RecurrenceImpl.cpp
19 * @brief This is the implementation for _RecurrenceImpl class.
21 * This file contains definitions of @e _RecurrenceImpl class.
25 #include <FBaseColIList.h>
26 #include <FBaseColArrayList.h>
27 #include <FBaseDateTime.h>
28 #include <FSclRecurrence.h>
29 #include <FBaseSysLog.h>
30 #include <FApp_AppInfo.h>
31 #include "FScl_RecurrenceImpl.h"
32 #include "FScl_CalendarbookImpl.h"
33 #include "FScl_CalendarbookUtil.h"
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::App;
39 namespace Tizen { namespace Social
42 static const RecurFrequency _DEFAULT_RECURRENCE_TYPE = FREQ_DAILY;
43 static const int _DEFAULT_INTERVAL = 1;
44 static const int _DEFAULT_COUNT = 1;
45 static const CalDayOfWeek _DEFAULT_WEEK_START = CAL_MONDAY;
46 static const int _WEEK_ONE = 1;
47 static const int _WEEK_FIVE = 5;
48 static const int _MONTH_ONE = 1;
49 static const int _MONTH_TWELVE = 12;
50 static const int _DAY_ONE = 1;
51 static const int _DAY_THIRTY_ONE = 31;
52 static const int _MIN_RECURRENCE_INTERVAL = 1;
53 static const int _MAX_DAY_OF_WEEK = 0x7F;
55 _RecurrenceImpl::_RecurrenceImpl(void)
56 : __type(_DEFAULT_RECURRENCE_TYPE)
57 , __interval(_DEFAULT_INTERVAL)
59 , __count(_DEFAULT_COUNT)
60 , __weekStart(_DEFAULT_WEEK_START)
66 std::unique_ptr<ArrayList, AllElementsDeleter> pExceptionDates(new (std::nothrow) ArrayList());
67 SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
69 result r = pExceptionDates->Construct();
70 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
72 __pExceptionDates = std::move(pExceptionDates);
75 _RecurrenceImpl::_RecurrenceImpl(const _RecurrenceImpl& rhs)
77 , __interval(rhs.__interval)
79 , __count(rhs.__count)
80 , __weekStart(rhs.__weekStart)
81 , __dayOfWeek(rhs.__dayOfWeek)
82 , __dayOfMonth(rhs.__dayOfMonth)
83 , __weekOfMonth(rhs.__weekOfMonth)
84 , __monthOfYear(rhs.__monthOfYear)
85 , __pExceptionDates(null)
87 std::unique_ptr<Tizen::Base::DateTime> pUntil;
89 if (rhs.__pUntil != null)
91 pUntil = std::unique_ptr<Tizen::Base::DateTime>(new (std::nothrow) DateTime(*rhs.__pUntil));
92 SysTryReturnVoidResult(NID_SCL, pUntil != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
95 std::unique_ptr<ArrayList, AllElementsDeleter> pExceptionDates(new (std::nothrow) ArrayList());
96 SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
98 result r = pExceptionDates->Construct();
99 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
101 if (rhs.__pExceptionDates != null)
103 DateTime* pTmpExDate = null;
104 std::unique_ptr<IEnumerator> pEnum(rhs.__pExceptionDates->GetEnumeratorN());
105 while (pEnum->MoveNext() == E_SUCCESS)
107 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
108 pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate)));
112 __pExceptionDates = std::move(pExceptionDates);
113 __pUntil = std::move(pUntil);
116 _RecurrenceImpl::~_RecurrenceImpl(void)
121 _RecurrenceImpl::operator =(const _RecurrenceImpl& rhs)
128 std::unique_ptr<Tizen::Base::DateTime> pUntil;
131 __interval = rhs.__interval;
132 __count = rhs.__count;
133 __weekStart = rhs.__weekStart;
134 __dayOfWeek = rhs.__dayOfWeek;
135 __dayOfMonth = rhs.__dayOfMonth;
136 __weekOfMonth = rhs.__weekOfMonth;
137 __monthOfYear = rhs.__monthOfYear;
139 if (rhs.__pUntil != null)
141 pUntil.reset(new (std::nothrow) DateTime(*(rhs.__pUntil)));
142 SysTryReturn(NID_SCL, pUntil != null, *this, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
145 if (__pExceptionDates != null && rhs.__pExceptionDates != null)
147 __pExceptionDates->RemoveAll(true);
149 DateTime* pTmpExDate = null;
150 std::unique_ptr<IEnumerator> pEnum(rhs.__pExceptionDates->GetEnumeratorN());
151 while (pEnum->MoveNext() == E_SUCCESS)
153 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
154 __pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate)));
158 __pUntil = std::move(pUntil);
164 _RecurrenceImpl::Equals(const Object& rhs) const
166 const _RecurrenceImpl* pRecurrenceImpl = dynamic_cast<const _RecurrenceImpl*>(&rhs);
168 if (pRecurrenceImpl == null)
173 return (__type == pRecurrenceImpl->__type && __interval == pRecurrenceImpl->__interval
174 && __count == pRecurrenceImpl->__count && __weekStart == pRecurrenceImpl->__weekStart
175 && __dayOfWeek == pRecurrenceImpl->__dayOfWeek && __dayOfMonth == pRecurrenceImpl->__dayOfMonth
176 && __weekOfMonth == pRecurrenceImpl->__weekOfMonth && __monthOfYear == pRecurrenceImpl->__monthOfYear);
180 _RecurrenceImpl::GetHashCode(void) const
184 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __type;
185 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __interval;
186 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __count;
187 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekStart;
188 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfWeek;
189 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfMonth;
190 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekOfMonth;
191 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __monthOfYear;
193 DateTime* pTmpExDate = null;
194 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
195 while (pEnum->MoveNext() == E_SUCCESS)
197 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
198 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ pTmpExDate->GetHashCode();
205 _RecurrenceImpl::GetFrequency(void) const
211 _RecurrenceImpl::GetInterval(void) const
217 _RecurrenceImpl::GetUntil(void) const
219 return __pUntil.get();
223 _RecurrenceImpl::GetCounts(void) const
229 _RecurrenceImpl::GetWeekStart(void) const
235 _RecurrenceImpl::GetDayOfWeek(void) const
241 _RecurrenceImpl::GetDayOfMonth(void) const
247 _RecurrenceImpl::GetWeekOfMonth(void) const
249 return __weekOfMonth;
253 _RecurrenceImpl::GetMonthOfYear(void)const
255 return __monthOfYear;
259 _RecurrenceImpl::SetFrequency(RecurFrequency type)
263 // reset all properties
264 if (__pUntil != null)
273 __interval = _DEFAULT_INTERVAL;
274 __count = _DEFAULT_COUNT;
275 __weekStart = _DEFAULT_WEEK_START;
277 __pExceptionDates->RemoveAll(true);
281 _RecurrenceImpl::SetInterval(int interval)
283 SysTryReturnResult(NID_SCL, interval >= _MIN_RECURRENCE_INTERVAL, E_INVALID_ARG, "Invalid argument is used. The interval is less than 1");
285 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
287 SysTryReturnResult(NID_SCL, interval <= MAX_RECURRENCE_INTERVAL_VALUE, E_INVALID_ARG
288 , "The inverval exceeds MAX_RECURRENCE_INTERVAL_VALUE");
291 __interval = interval;
293 // reset exception dates
294 __pExceptionDates->RemoveAll(true);
300 _RecurrenceImpl::SetUntil(const DateTime* pUntil)
304 SysTryReturnResult(NID_SCL, (_CalendarbookUtil::CheckValidDateTime(*pUntil) == true) || (*pUntil == DateTime::GetMaxValue()), E_INVALID_ARG, "Invalid argument is used. The until date is invalid.");
306 __pUntil.reset(new (std::nothrow) DateTime(*pUntil));
307 if (__pUntil == null)
309 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "Memory allocation failed.");
310 return E_OUT_OF_MEMORY;
319 __count = _DEFAULT_COUNT;
322 // reset exception dates
323 __pExceptionDates->RemoveAll(true);
329 _RecurrenceImpl::SetCounts(int count)
331 SysTryReturnResult(NID_SCL, count >= 0, E_INVALID_ARG, "Invalid argument is used. The count is less than 0");
336 if (__pUntil != null)
341 // reset exception dates
342 __pExceptionDates->RemoveAll(true);
348 _RecurrenceImpl::SetWeekStart(CalDayOfWeek weekStart)
350 SysTryReturnResult(NID_SCL, weekStart == CAL_SUNDAY || weekStart == CAL_MONDAY, E_INVALID_ARG,
351 "Invalid argument is used. weekStart = %d", weekStart);
353 __weekStart = weekStart;
355 // reset exception dates
356 __pExceptionDates->RemoveAll(true);
362 _RecurrenceImpl::SetDayOfWeek(int day)
364 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH,
365 "The frequency type is daily. It should be WEEKLY ,MOTHLY or YEARLY");
366 SysTryReturnResult(NID_SCL, day >= CAL_SUNDAY, E_INVALID_ARG, "Invalid argument is used. The day(%d) is less than min value.", day);
367 SysTryReturnResult(NID_SCL, day <= _MAX_DAY_OF_WEEK, E_INVALID_ARG, "Invalid argument is used. The day(%d) exceeds max value.", day);
371 // reset day of month
374 // reset exception dates
375 __pExceptionDates->RemoveAll(true);
381 _RecurrenceImpl::SetDayOfMonth(int day)
383 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
384 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
385 SysTryReturnResult(NID_SCL, day >= _DAY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is less than 1");
386 SysTryReturnResult(NID_SCL, day <= _DAY_THIRTY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is greater than 31");
390 // reset day of week and week of month
394 // reset exception dates
395 __pExceptionDates->RemoveAll(true);
401 _RecurrenceImpl::SetWeekOfMonth(int week)
403 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
404 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
405 SysTryReturnResult(NID_SCL, week >= _WEEK_ONE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is less than 1.", week);
406 SysTryReturnResult(NID_SCL, week <= _WEEK_FIVE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is greater than 5.", week);
408 __weekOfMonth = week;
410 // reset day of month
413 // reset exception dates
414 __pExceptionDates->RemoveAll(true);
421 _RecurrenceImpl::SetMonthOfYear(int month)
423 SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily.");
424 SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly.");
425 SysTryReturnResult(NID_SCL, __type != FREQ_MONTHLY, E_TYPE_MISMATCH, "The frequency is monthly.");
426 SysTryReturnResult(NID_SCL, month >= _MONTH_ONE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is less than 1 ", month);
427 SysTryReturnResult(NID_SCL, month <= _MONTH_TWELVE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is greater than 12", month);
429 __monthOfYear = month;
431 // reset exception dates
432 __pExceptionDates->RemoveAll(true);
438 _RecurrenceImpl::AddExceptionDate(const DateTime& exceptionDate)
440 SysTryReturnResult(NID_SCL, exceptionDate >= _CalendarbookImpl::GetMinDateTime() &&
441 exceptionDate <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. exceptionDate = %S", exceptionDate.ToString().GetPointer());
443 bool alreadyExist = false;
445 DateTime* pTmpExDate = null;
446 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
447 while (pEnum->MoveNext() == E_SUCCESS)
449 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
450 if (*pTmpExDate == exceptionDate)
457 SysTryReturnResult(NID_SCL, !alreadyExist, E_OBJ_ALREADY_EXIST, "The exceptionDate already exists");
459 DateTime* pExDate = new (std::nothrow) DateTime(exceptionDate);
460 SysTryReturnResult(NID_SCL, pExDate != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
462 __pExceptionDates->Add(*pExDate);
468 _RecurrenceImpl::GetExceptionDatesN(void) const
471 result r = E_SUCCESS;
473 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) ArrayList());
474 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
475 r = pList->Construct();
476 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
478 DateTime* pExDate = null;
479 DateTime* pTmpExDate = null;
480 std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
481 while (pEnum->MoveNext() == E_SUCCESS)
483 pExDate = static_cast<DateTime*>(pEnum->GetCurrent());
486 pTmpExDate = new (std::nothrow) DateTime(*pExDate);
487 SysTryReturn(NID_SCL, pTmpExDate != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
488 pList->Add(*pTmpExDate);
492 return pList.release();
496 _RecurrenceImpl::GetInstance(Recurrence& recurrence)
498 return recurrence.__pRecurrenceImpl;
501 const _RecurrenceImpl*
502 _RecurrenceImpl::GetInstance(const Recurrence& recurrence)
504 return recurrence.__pRecurrenceImpl;