Fix for CompatTC Fail
[framework/osp/social.git] / src / FScl_RecurrenceImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 /**
17  * @file                FScl_RecurrenceImpl.cpp
18  * @brief               This is the implementation for _RecurrenceImpl class.
19  *
20  * This file contains definitions of @e _RecurrenceImpl class.
21  */
22
23 #include <new>
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"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::App;
37
38 namespace Tizen { namespace Social
39 {
40
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;
53
54 _RecurrenceImpl::_RecurrenceImpl(void)
55         : __type(_DEFAULT_RECURRENCE_TYPE)
56         , __interval(_DEFAULT_INTERVAL)
57         , __pUntil(null)
58         , __count(_DEFAULT_COUNT)
59         , __weekStart(_DEFAULT_WEEK_START)
60         , __dayOfWeek(0)
61         , __dayOfMonth(0)
62         , __weekOfMonth(0)
63         , __monthOfYear(0)
64 {
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.");
67
68         result r = pExceptionDates->Construct();
69         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
70
71         __pExceptionDates = std::move(pExceptionDates);
72 }
73
74 _RecurrenceImpl::_RecurrenceImpl(const _RecurrenceImpl& rhs)
75         : __type(rhs.__type)
76         , __interval(rhs.__interval)
77         , __pUntil(null)
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)
85 {
86         std::unique_ptr<Tizen::Base::DateTime> pUntil;
87
88         if (rhs.__pUntil != null)
89         {
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.");
92         }
93
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.");
96
97         result r = pExceptionDates->Construct();
98         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
99
100         if (rhs.__pExceptionDates != null)
101         {
102                 DateTime* pTmpExDate = null;
103                 std::unique_ptr<IEnumerator> pEnum(rhs.__pExceptionDates->GetEnumeratorN());
104                 while (pEnum->MoveNext() == E_SUCCESS)
105                 {
106                         pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
107                         pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate)));
108                 }
109         }
110
111         __pExceptionDates = std::move(pExceptionDates);
112         __pUntil = std::move(pUntil);
113 }
114
115 _RecurrenceImpl::~_RecurrenceImpl(void)
116 {
117 }
118
119 _RecurrenceImpl&
120 _RecurrenceImpl::operator =(const _RecurrenceImpl& rhs)
121 {
122         if (this == &rhs)
123         {
124                 return *this;
125         }
126
127         std::unique_ptr<Tizen::Base::DateTime> pUntil;
128
129         __type = rhs.__type;
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;
137
138         if (rhs.__pUntil != null)
139         {
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.");
142         }
143
144         if (__pExceptionDates != null && rhs.__pExceptionDates != null)
145         {
146                 __pExceptionDates->RemoveAll(true);
147
148                 DateTime* pTmpExDate = null;
149                 std::unique_ptr<IEnumerator> pEnum(rhs.__pExceptionDates->GetEnumeratorN());
150                 while (pEnum->MoveNext() == E_SUCCESS)
151                 {
152                         pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
153                         __pExceptionDates->Add(*(new (std::nothrow) DateTime(*pTmpExDate)));
154                 }
155         }
156
157         __pUntil = std::move(pUntil);
158
159         return *this;
160 }
161
162 bool
163 _RecurrenceImpl::Equals(const Object& rhs) const
164 {
165         const _RecurrenceImpl* pRecurrenceImpl = dynamic_cast<const _RecurrenceImpl*>(&rhs);
166
167         if (pRecurrenceImpl == null)
168         {
169                 return false;
170         }
171
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);
176 }
177
178 int
179 _RecurrenceImpl::GetHashCode(void) const
180 {
181         int hashCode = 17;
182
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;
191
192         DateTime* pTmpExDate = null;
193         std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
194         while (pEnum->MoveNext() == E_SUCCESS)
195         {
196                 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
197                 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ pTmpExDate->GetHashCode();
198         }
199
200         return hashCode;
201 }
202
203 RecurFrequency
204 _RecurrenceImpl::GetFrequency(void) const
205 {
206         return __type;
207 }
208
209 int
210 _RecurrenceImpl::GetInterval(void) const
211 {
212         return __interval;
213 }
214
215 const DateTime*
216 _RecurrenceImpl::GetUntil(void) const
217 {
218         return __pUntil.get();
219 }
220
221 int
222 _RecurrenceImpl::GetCounts(void) const
223 {
224         return __count;
225 }
226
227 CalDayOfWeek
228 _RecurrenceImpl::GetWeekStart(void) const
229 {
230         return __weekStart;
231 }
232
233 int
234 _RecurrenceImpl::GetDayOfWeek(void) const
235 {
236         return __dayOfWeek;
237 }
238
239 int
240 _RecurrenceImpl::GetDayOfMonth(void) const
241 {
242         return __dayOfMonth;
243 }
244
245 int
246 _RecurrenceImpl::GetWeekOfMonth(void) const
247 {
248         return __weekOfMonth;
249 }
250
251 int
252 _RecurrenceImpl::GetMonthOfYear(void)const
253 {
254         return __monthOfYear;
255 }
256
257 void
258 _RecurrenceImpl::SetFrequency(RecurFrequency type)
259 {
260         __type = type;
261
262         // reset all properties
263         if (__pUntil != null)
264         {
265                 __pUntil.reset();
266         }
267
268         __dayOfWeek = 0;
269         __dayOfMonth = 0;
270         __weekOfMonth = 0;
271         __monthOfYear = 0;
272         __interval = _DEFAULT_INTERVAL;
273         __count = _DEFAULT_COUNT;
274         __weekStart = _DEFAULT_WEEK_START;
275
276         __pExceptionDates->RemoveAll(true);
277 }
278
279 result
280 _RecurrenceImpl::SetInterval(int interval)
281 {
282         SysTryReturnResult(NID_SCL, interval >= _MIN_RECURRENCE_INTERVAL, E_INVALID_ARG, "Invalid argument is used. The interval is less than 1");
283
284         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
285         {
286                 SysTryReturnResult(NID_SCL, interval <= MAX_RECURRENCE_INTERVAL_VALUE, E_INVALID_ARG
287                                 , "The inverval exceeds MAX_RECURRENCE_INTERVAL_VALUE");
288         }
289
290         __interval = interval;
291
292         // reset exception dates
293         __pExceptionDates->RemoveAll(true);
294
295         return E_SUCCESS;
296 }
297
298 result
299 _RecurrenceImpl::SetUntil(const DateTime* pUntil)
300 {
301         if (pUntil != null)
302         {
303                 SysTryReturnResult(NID_SCL, (_CalendarbookUtil::CheckValidDateTime(*pUntil) == true) || (*pUntil == DateTime::GetMaxValue()), E_INVALID_ARG, "Invalid argument is used. The until date is invalid.");
304
305                 __pUntil.reset(new (std::nothrow) DateTime(*pUntil));
306                 if (__pUntil == null)
307                 {
308                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "Memory allocation failed.");
309                         return E_OUT_OF_MEMORY;
310                 }
311
312                 // reset count
313                 __count = 0;
314         }
315         else
316         {
317                 __pUntil.reset();
318                 __count = _DEFAULT_COUNT;
319         }
320
321         // reset exception dates
322         __pExceptionDates->RemoveAll(true);
323
324         return E_SUCCESS;
325 }
326
327 result
328 _RecurrenceImpl::SetCounts(int count)
329 {
330         SysTryReturnResult(NID_SCL, count >= 0, E_INVALID_ARG, "Invalid argument is used. The count is less than 0");
331
332         __count = count;
333
334         // reset until
335         if (__pUntil != null)
336         {
337                 __pUntil.release();
338         }
339
340         // reset exception dates
341         __pExceptionDates->RemoveAll(true);
342
343         return E_SUCCESS;
344 }
345
346 result
347 _RecurrenceImpl::SetWeekStart(CalDayOfWeek weekStart)
348 {
349         SysTryReturnResult(NID_SCL, weekStart == CAL_SUNDAY || weekStart == CAL_MONDAY, E_INVALID_ARG,
350                         "Invalid argument is used. weekStart = %d", weekStart);
351
352         __weekStart = weekStart;
353
354         // reset exception dates
355         __pExceptionDates->RemoveAll(true);
356
357         return E_SUCCESS;
358 }
359
360 result
361 _RecurrenceImpl::SetDayOfWeek(int day)
362 {
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);
367
368         __dayOfWeek = day;
369
370         // reset day of month
371         __dayOfMonth = 0;
372
373         // reset exception dates
374         __pExceptionDates->RemoveAll(true);
375
376         return E_SUCCESS;
377 }
378
379 result
380 _RecurrenceImpl::SetDayOfMonth(int day)
381 {
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");
386
387         __dayOfMonth = day;
388
389         // reset day of week and week of month
390         __dayOfWeek = 0;
391         __weekOfMonth = 0;
392
393         // reset exception dates
394         __pExceptionDates->RemoveAll(true);
395
396         return E_SUCCESS;
397 }
398
399 result
400 _RecurrenceImpl::SetWeekOfMonth(int week)
401 {
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);
406
407         __weekOfMonth = week;
408
409         // reset day of month
410         __dayOfMonth = 0;
411
412         // reset exception dates
413         __pExceptionDates->RemoveAll(true);
414
415         return E_SUCCESS;
416 }
417
418
419 result
420 _RecurrenceImpl::SetMonthOfYear(int month)
421 {
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);
427
428         __monthOfYear = month;
429
430         // reset exception dates
431         __pExceptionDates->RemoveAll(true);
432
433         return E_SUCCESS;
434 }
435
436 result
437 _RecurrenceImpl::AddExceptionDate(const DateTime& exceptionDate)
438 {
439         SysTryReturnResult(NID_SCL, exceptionDate >= _CalendarbookImpl::GetMinDateTime() &&
440                         exceptionDate <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. exceptionDate = %S", exceptionDate.ToString().GetPointer());
441
442         bool alreadyExist = false;
443
444         DateTime* pTmpExDate = null;
445         std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
446         while (pEnum->MoveNext() == E_SUCCESS)
447         {
448                 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
449                 if (*pTmpExDate == exceptionDate)
450                 {
451                         alreadyExist = true;
452                         break;
453                 }
454         }
455
456         SysTryReturnResult(NID_SCL, !alreadyExist, E_OBJ_ALREADY_EXIST, "The exceptionDate already exists");
457
458         DateTime* pExDate = new (std::nothrow) DateTime(exceptionDate);
459         SysTryReturnResult(NID_SCL, pExDate != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
460
461         __pExceptionDates->Add(*pExDate);
462
463         return E_SUCCESS;
464 }
465
466 IList*
467 _RecurrenceImpl::GetExceptionDatesN(void) const
468 {
469         ClearLastResult();
470         result r = E_SUCCESS;
471
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.");
476
477         DateTime* pExDate = null;
478         DateTime* pTmpExDate = null;
479         std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
480         while (pEnum->MoveNext() == E_SUCCESS)
481         {
482                 pExDate = static_cast<DateTime*>(pEnum->GetCurrent());
483                 if (pExDate)
484                 {
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);
488                 }
489         }
490
491         return pList.release();
492 }
493
494 _RecurrenceImpl*
495 _RecurrenceImpl::GetInstance(Recurrence& recurrence)
496 {
497         return recurrence.__pRecurrenceImpl;
498 }
499
500 const _RecurrenceImpl*
501 _RecurrenceImpl::GetInstance(const Recurrence& recurrence)
502 {
503         return recurrence.__pRecurrenceImpl;
504 }
505
506 }}      // Tizen::Social