// // Copyright (c) 2012 Samsung Electronics Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the License); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /** * @file FScl_RecurrenceImpl.cpp * @brief This is the implementation for _RecurrenceImpl class. * * This file contains definitions of @e _RecurrenceImpl class. */ #include #include #include #include #include #include #include #include "FScl_CalendarbookImpl.h" #include "FScl_CalendarbookUtil.h" #include "FScl_RecurrenceImpl.h" using namespace Tizen::App; using namespace Tizen::Base; using namespace Tizen::Base::Collection; namespace Tizen { namespace Social { static const RecurFrequency _DEFAULT_RECURRENCE_TYPE = FREQ_DAILY; static const int _DEFAULT_INTERVAL = 1; static const int _DEFAULT_COUNT = 1; static const CalDayOfWeek _DEFAULT_WEEK_START = CAL_MONDAY; static const int _WEEK_ONE = 1; static const int _WEEK_FIVE = 5; static const int _MONTH_ONE = 1; static const int _MONTH_TWELVE = 12; static const int _DAY_ONE = 1; static const int _DAY_THIRTY_ONE = 31; static const int _MIN_RECURRENCE_INTERVAL = 1; static const int _MAX_DAY_OF_WEEK = 0x7F; _RecurrenceImpl::_RecurrenceImpl(void) : __type(_DEFAULT_RECURRENCE_TYPE) , __interval(_DEFAULT_INTERVAL) , __pUntil(null) , __count(_DEFAULT_COUNT) , __weekStart(_DEFAULT_WEEK_START) , __dayOfWeek(0) , __dayOfMonth(0) , __weekOfMonth(0) , __monthOfYear(0) { std::unique_ptr pExceptionDates(new (std::nothrow) ArrayList()); SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); result r = pExceptionDates->Construct(); SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r)); __pExceptionDates = std::move(pExceptionDates); } _RecurrenceImpl::_RecurrenceImpl(const _RecurrenceImpl& rhs) : __type(rhs.__type) , __interval(rhs.__interval) , __pUntil(null) , __count(rhs.__count) , __weekStart(rhs.__weekStart) , __dayOfWeek(rhs.__dayOfWeek) , __dayOfMonth(rhs.__dayOfMonth) , __weekOfMonth(rhs.__weekOfMonth) , __monthOfYear(rhs.__monthOfYear) , __pExceptionDates(null) { std::unique_ptr pUntil; if (rhs.__pUntil != null) { pUntil = std::unique_ptr(new (std::nothrow) DateTime(*rhs.__pUntil)); SysTryReturnVoidResult(NID_SCL, pUntil != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); } std::unique_ptr pExceptionDates(new (std::nothrow) ArrayList()); SysTryReturnVoidResult(NID_SCL, pExceptionDates, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); result r = pExceptionDates->Construct(); SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r)); if (rhs.__pExceptionDates != null) { DateTime* pTmpExDate = null; std::unique_ptr pEnum(rhs.__pExceptionDates->GetEnumeratorN()); while (pEnum->MoveNext() == E_SUCCESS) { pTmpExDate = static_cast(pEnum->GetCurrent()); pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate))); } } __pExceptionDates = std::move(pExceptionDates); __pUntil = std::move(pUntil); } _RecurrenceImpl::~_RecurrenceImpl(void) { } _RecurrenceImpl& _RecurrenceImpl::operator =(const _RecurrenceImpl& rhs) { if (this == &rhs) { return *this; } std::unique_ptr pUntil; __type = rhs.__type; __interval = rhs.__interval; __count = rhs.__count; __weekStart = rhs.__weekStart; __dayOfWeek = rhs.__dayOfWeek; __dayOfMonth = rhs.__dayOfMonth; __weekOfMonth = rhs.__weekOfMonth; __monthOfYear = rhs.__monthOfYear; if (rhs.__pUntil != null) { pUntil.reset(new (std::nothrow) DateTime(*(rhs.__pUntil))); SysTryReturn(NID_SCL, pUntil != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); } if (__pExceptionDates != null && rhs.__pExceptionDates != null) { __pExceptionDates->RemoveAll(true); DateTime* pTmpExDate = null; std::unique_ptr pEnum(rhs.__pExceptionDates->GetEnumeratorN()); while (pEnum->MoveNext() == E_SUCCESS) { pTmpExDate = static_cast(pEnum->GetCurrent()); if (pTmpExDate != null) { __pExceptionDates->Add(new (std::nothrow) DateTime(*pTmpExDate)); } } } __pUntil = std::move(pUntil); return *this; } bool _RecurrenceImpl::Equals(const Object& rhs) const { const _RecurrenceImpl* pRecurrenceImpl = dynamic_cast(&rhs); if (pRecurrenceImpl == null) { return false; } return (__type == pRecurrenceImpl->__type && __interval == pRecurrenceImpl->__interval && __count == pRecurrenceImpl->__count && __weekStart == pRecurrenceImpl->__weekStart && __dayOfWeek == pRecurrenceImpl->__dayOfWeek && __dayOfMonth == pRecurrenceImpl->__dayOfMonth && __weekOfMonth == pRecurrenceImpl->__weekOfMonth && __monthOfYear == pRecurrenceImpl->__monthOfYear); } int _RecurrenceImpl::GetHashCode(void) const { int hashCode = 17; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __type; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __interval; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __count; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekStart; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfWeek; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __dayOfMonth; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __weekOfMonth; hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __monthOfYear; DateTime* pTmpExDate = null; std::unique_ptr pEnum(__pExceptionDates->GetEnumeratorN()); while (pEnum->MoveNext() == E_SUCCESS) { pTmpExDate = static_cast(pEnum->GetCurrent()); hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ pTmpExDate->GetHashCode(); } return hashCode; } RecurFrequency _RecurrenceImpl::GetFrequency(void) const { return __type; } int _RecurrenceImpl::GetInterval(void) const { return __interval; } const DateTime* _RecurrenceImpl::GetUntil(void) const { return __pUntil.get(); } int _RecurrenceImpl::GetCounts(void) const { return __count; } CalDayOfWeek _RecurrenceImpl::GetWeekStart(void) const { return __weekStart; } int _RecurrenceImpl::GetDayOfWeek(void) const { return __dayOfWeek; } int _RecurrenceImpl::GetDayOfMonth(void) const { return __dayOfMonth; } int _RecurrenceImpl::GetWeekOfMonth(void) const { return __weekOfMonth; } int _RecurrenceImpl::GetMonthOfYear(void)const { return __monthOfYear; } void _RecurrenceImpl::SetFrequency(RecurFrequency type) { __type = type; // reset all properties if (__pUntil != null) { __pUntil.reset(); } __dayOfWeek = 0; __dayOfMonth = 0; __weekOfMonth = 0; __monthOfYear = 0; __interval = _DEFAULT_INTERVAL; __count = _DEFAULT_COUNT; __weekStart = _DEFAULT_WEEK_START; __pExceptionDates->RemoveAll(true); } result _RecurrenceImpl::SetInterval(int interval) { SysTryReturnResult(NID_SCL, interval >= _MIN_RECURRENCE_INTERVAL, E_INVALID_ARG, "Invalid argument is used. The interval is less than 1"); if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) { SysTryReturnResult(NID_SCL, interval <= MAX_RECURRENCE_INTERVAL_VALUE, E_INVALID_ARG , "The inverval exceeds MAX_RECURRENCE_INTERVAL_VALUE"); } __interval = interval; // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetUntil(const DateTime* pUntil) { if (pUntil != null) { SysTryReturnResult(NID_SCL, (_CalendarbookUtil::CheckValidDateTime(*pUntil) == true) || (*pUntil == DateTime::GetMaxValue()), E_INVALID_ARG, "Invalid argument is used. The until date is invalid."); __pUntil.reset(new (std::nothrow) DateTime(*pUntil)); if (__pUntil == null) { SysLogException(NID_SCL, E_OUT_OF_MEMORY, "Memory allocation failed."); return E_OUT_OF_MEMORY; } // reset count __count = 0; } else { __pUntil.reset(); __count = _DEFAULT_COUNT; } // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetCounts(int count) { SysTryReturnResult(NID_SCL, count >= 0, E_INVALID_ARG, "Invalid argument is used. The count is less than 0"); __count = count; // reset until if (__pUntil != null) { __pUntil.release(); } // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetWeekStart(CalDayOfWeek weekStart) { SysTryReturnResult(NID_SCL, weekStart == CAL_SUNDAY || weekStart == CAL_MONDAY, E_INVALID_ARG, "Invalid argument is used. weekStart = %d", weekStart); __weekStart = weekStart; // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetDayOfWeek(int day) { SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency type is daily. It should be WEEKLY ,MOTHLY or YEARLY"); SysTryReturnResult(NID_SCL, day >= CAL_SUNDAY, E_INVALID_ARG, "Invalid argument is used. The day(%d) is less than min value.", day); SysTryReturnResult(NID_SCL, day <= _MAX_DAY_OF_WEEK, E_INVALID_ARG, "Invalid argument is used. The day(%d) exceeds max value.", day); __dayOfWeek = day; // reset day of month __dayOfMonth = 0; // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetDayOfMonth(int day) { SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily."); SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly."); SysTryReturnResult(NID_SCL, day >= _DAY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is less than 1"); SysTryReturnResult(NID_SCL, day <= _DAY_THIRTY_ONE, E_INVALID_ARG, "Invalid argument is used. The day is greater than 31"); __dayOfMonth = day; // reset day of week and week of month __dayOfWeek = 0; __weekOfMonth = 0; // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetWeekOfMonth(int week) { SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily."); SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly."); SysTryReturnResult(NID_SCL, week >= _WEEK_ONE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is less than 1.", week); SysTryReturnResult(NID_SCL, week <= _WEEK_FIVE, E_INVALID_ARG, "Invalid argument is used. The week (%d) is greater than 5.", week); __weekOfMonth = week; // reset day of month __dayOfMonth = 0; // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::SetMonthOfYear(int month) { SysTryReturnResult(NID_SCL, __type != FREQ_DAILY, E_TYPE_MISMATCH, "The frequency is daily."); SysTryReturnResult(NID_SCL, __type != FREQ_WEEKLY, E_TYPE_MISMATCH, "The frequency is weekly."); SysTryReturnResult(NID_SCL, __type != FREQ_MONTHLY, E_TYPE_MISMATCH, "The frequency is monthly."); SysTryReturnResult(NID_SCL, month >= _MONTH_ONE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is less than 1 ", month); SysTryReturnResult(NID_SCL, month <= _MONTH_TWELVE, E_INVALID_ARG, "Invalid argument is used. The month (%d) is greater than 12", month); __monthOfYear = month; // reset exception dates __pExceptionDates->RemoveAll(true); return E_SUCCESS; } result _RecurrenceImpl::AddExceptionDate(const DateTime& exceptionDate) { SysTryReturnResult(NID_SCL, exceptionDate >= _CalendarbookImpl::GetMinDateTime() && exceptionDate <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. exceptionDate = %S", exceptionDate.ToString().GetPointer()); bool alreadyExist = false; DateTime* pTmpExDate = null; std::unique_ptr pEnum(__pExceptionDates->GetEnumeratorN()); while (pEnum->MoveNext() == E_SUCCESS) { pTmpExDate = static_cast(pEnum->GetCurrent()); if (*pTmpExDate == exceptionDate) { alreadyExist = true; break; } } SysTryReturnResult(NID_SCL, !alreadyExist, E_OBJ_ALREADY_EXIST, "The exceptionDate already exists"); std::unique_ptr pExDate(new (std::nothrow) DateTime(exceptionDate)); SysTryReturnResult(NID_SCL, pExDate != null, E_OUT_OF_MEMORY, "Memory allocation failed."); result r = __pExceptionDates->Add(pExDate.get()); SysTryReturnResult(NID_SCL, !IsFailed(r), E_INVALID_ARG, "Propagating."); pExDate.release(); return E_SUCCESS; } IList* _RecurrenceImpl::GetExceptionDatesN(void) const { ClearLastResult(); result r = E_SUCCESS; std::unique_ptr pList(new (std::nothrow) ArrayList()); SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); r = pList->Construct(); SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); DateTime* pExDate = null; DateTime* pTmpExDate = null; std::unique_ptr pEnum(__pExceptionDates->GetEnumeratorN()); while (pEnum->MoveNext() == E_SUCCESS) { pExDate = static_cast(pEnum->GetCurrent()); if (pExDate) { pTmpExDate = new (std::nothrow) DateTime(*pExDate); SysTryReturn(NID_SCL, pTmpExDate != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); pList->Add(pTmpExDate); } } return pList.release(); } _RecurrenceImpl* _RecurrenceImpl::GetInstance(Recurrence& recurrence) { return recurrence.__pRecurrenceImpl; } const _RecurrenceImpl* _RecurrenceImpl::GetInstance(const Recurrence& recurrence) { return recurrence.__pRecurrenceImpl; } }} // Tizen::Social