Modify the spec file for secure log
[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 <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"
33
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
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, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
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<DateTime> pUntil;
87
88         if (rhs.__pUntil != null)
89         {
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));
92         }
93
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));
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<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, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
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
154                         if (pTmpExDate != null)
155                         {
156                                 __pExceptionDates->Add(new (std::nothrow) DateTime(*pTmpExDate));
157                         }
158                 }
159         }
160
161         __pUntil = std::move(pUntil);
162
163         return *this;
164 }
165
166 bool
167 _RecurrenceImpl::Equals(const Object& rhs) const
168 {
169         const _RecurrenceImpl* pRecurrenceImpl = dynamic_cast<const _RecurrenceImpl*>(&rhs);
170
171         if (pRecurrenceImpl == null)
172         {
173                 return false;
174         }
175
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);
180 }
181
182 int
183 _RecurrenceImpl::GetHashCode(void) const
184 {
185         int hashCode = 17;
186
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;
195
196         DateTime* pTmpExDate = null;
197         std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
198         while (pEnum->MoveNext() == E_SUCCESS)
199         {
200                 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
201                 hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ pTmpExDate->GetHashCode();
202         }
203
204         return hashCode;
205 }
206
207 RecurFrequency
208 _RecurrenceImpl::GetFrequency(void) const
209 {
210         return __type;
211 }
212
213 int
214 _RecurrenceImpl::GetInterval(void) const
215 {
216         return __interval;
217 }
218
219 const DateTime*
220 _RecurrenceImpl::GetUntil(void) const
221 {
222         return __pUntil.get();
223 }
224
225 int
226 _RecurrenceImpl::GetCounts(void) const
227 {
228         return __count;
229 }
230
231 CalDayOfWeek
232 _RecurrenceImpl::GetWeekStart(void) const
233 {
234         return __weekStart;
235 }
236
237 int
238 _RecurrenceImpl::GetDayOfWeek(void) const
239 {
240         return __dayOfWeek;
241 }
242
243 int
244 _RecurrenceImpl::GetDayOfMonth(void) const
245 {
246         return __dayOfMonth;
247 }
248
249 int
250 _RecurrenceImpl::GetWeekOfMonth(void) const
251 {
252         return __weekOfMonth;
253 }
254
255 int
256 _RecurrenceImpl::GetMonthOfYear(void)const
257 {
258         return __monthOfYear;
259 }
260
261 void
262 _RecurrenceImpl::SetFrequency(RecurFrequency type)
263 {
264         __type = type;
265
266         // reset all properties
267         if (__pUntil != null)
268         {
269                 __pUntil.reset();
270         }
271
272         __dayOfWeek = 0;
273         __dayOfMonth = 0;
274         __weekOfMonth = 0;
275         __monthOfYear = 0;
276         __interval = _DEFAULT_INTERVAL;
277         __count = _DEFAULT_COUNT;
278         __weekStart = _DEFAULT_WEEK_START;
279
280         __pExceptionDates->RemoveAll(true);
281 }
282
283 result
284 _RecurrenceImpl::SetInterval(int interval)
285 {
286         SysTryReturnResult(NID_SCL, interval >= _MIN_RECURRENCE_INTERVAL, E_INVALID_ARG, "Invalid argument is used. The interval is less than 1");
287
288         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
289         {
290                 SysTryReturnResult(NID_SCL, interval <= MAX_RECURRENCE_INTERVAL_VALUE, E_INVALID_ARG
291                                 , "The inverval exceeds MAX_RECURRENCE_INTERVAL_VALUE");
292         }
293
294         __interval = interval;
295
296         // reset exception dates
297         __pExceptionDates->RemoveAll(true);
298
299         return E_SUCCESS;
300 }
301
302 result
303 _RecurrenceImpl::SetUntil(const DateTime* pUntil)
304 {
305         if (pUntil != null)
306         {
307                 SysTryReturnResult(NID_SCL, (_CalendarbookUtil::CheckValidDateTime(*pUntil) == true) || (*pUntil == DateTime::GetMaxValue()), E_INVALID_ARG, "Invalid argument is used. The until date is invalid.");
308
309                 __pUntil.reset(new (std::nothrow) DateTime(*pUntil));
310                 if (__pUntil == null)
311                 {
312                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "Memory allocation failed.");
313                         return E_OUT_OF_MEMORY;
314                 }
315
316                 // reset count
317                 __count = 0;
318         }
319         else
320         {
321                 __pUntil.reset();
322                 __count = _DEFAULT_COUNT;
323         }
324
325         // reset exception dates
326         __pExceptionDates->RemoveAll(true);
327
328         return E_SUCCESS;
329 }
330
331 result
332 _RecurrenceImpl::SetCounts(int count)
333 {
334         SysTryReturnResult(NID_SCL, count >= 0, E_INVALID_ARG, "Invalid argument is used. The count is less than 0");
335
336         __count = count;
337
338         // reset until
339         if (__pUntil != null)
340         {
341                 __pUntil.release();
342         }
343
344         // reset exception dates
345         __pExceptionDates->RemoveAll(true);
346
347         return E_SUCCESS;
348 }
349
350 result
351 _RecurrenceImpl::SetWeekStart(CalDayOfWeek weekStart)
352 {
353         SysTryReturnResult(NID_SCL, weekStart == CAL_SUNDAY || weekStart == CAL_MONDAY, E_INVALID_ARG,
354                         "Invalid argument is used. weekStart = %d", weekStart);
355
356         __weekStart = weekStart;
357
358         // reset exception dates
359         __pExceptionDates->RemoveAll(true);
360
361         return E_SUCCESS;
362 }
363
364 result
365 _RecurrenceImpl::SetDayOfWeek(int day)
366 {
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);
371
372         __dayOfWeek = day;
373
374         // reset day of month
375         __dayOfMonth = 0;
376
377         // reset exception dates
378         __pExceptionDates->RemoveAll(true);
379
380         return E_SUCCESS;
381 }
382
383 result
384 _RecurrenceImpl::SetDayOfMonth(int day)
385 {
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");
390
391         __dayOfMonth = day;
392
393         // reset day of week and week of month
394         __dayOfWeek = 0;
395         __weekOfMonth = 0;
396
397         // reset exception dates
398         __pExceptionDates->RemoveAll(true);
399
400         return E_SUCCESS;
401 }
402
403 result
404 _RecurrenceImpl::SetWeekOfMonth(int week)
405 {
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);
410
411         __weekOfMonth = week;
412
413         // reset day of month
414         __dayOfMonth = 0;
415
416         // reset exception dates
417         __pExceptionDates->RemoveAll(true);
418
419         return E_SUCCESS;
420 }
421
422
423 result
424 _RecurrenceImpl::SetMonthOfYear(int month)
425 {
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);
431
432         __monthOfYear = month;
433
434         // reset exception dates
435         __pExceptionDates->RemoveAll(true);
436
437         return E_SUCCESS;
438 }
439
440 result
441 _RecurrenceImpl::AddExceptionDate(const DateTime& exceptionDate)
442 {
443         SysTryReturnResult(NID_SCL, exceptionDate >= _CalendarbookImpl::GetMinDateTime() &&
444                         exceptionDate <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. exceptionDate = %S", exceptionDate.ToString().GetPointer());
445
446         bool alreadyExist = false;
447
448         DateTime* pTmpExDate = null;
449         std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
450         while (pEnum->MoveNext() == E_SUCCESS)
451         {
452                 pTmpExDate = static_cast<DateTime*>(pEnum->GetCurrent());
453                 if (*pTmpExDate == exceptionDate)
454                 {
455                         alreadyExist = true;
456                         break;
457                 }
458         }
459
460         SysTryReturnResult(NID_SCL, !alreadyExist, E_OBJ_ALREADY_EXIST, "The exceptionDate already exists");
461
462         std::unique_ptr<DateTime> pExDate(new (std::nothrow) DateTime(exceptionDate));
463         SysTryReturnResult(NID_SCL, pExDate != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
464
465         result r = __pExceptionDates->Add(pExDate.get());
466         SysTryReturnResult(NID_SCL, !IsFailed(r), E_INVALID_ARG, "Propagating.");
467
468         pExDate.release();
469
470         return E_SUCCESS;
471 }
472
473 IList*
474 _RecurrenceImpl::GetExceptionDatesN(void) const
475 {
476         ClearLastResult();
477         result r = E_SUCCESS;
478
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));
483
484         DateTime* pExDate = null;
485         DateTime* pTmpExDate = null;
486         std::unique_ptr<IEnumerator> pEnum(__pExceptionDates->GetEnumeratorN());
487         while (pEnum->MoveNext() == E_SUCCESS)
488         {
489                 pExDate = static_cast<DateTime*>(pEnum->GetCurrent());
490                 if (pExDate)
491                 {
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);
495                 }
496         }
497
498         return pList.release();
499 }
500
501 _RecurrenceImpl*
502 _RecurrenceImpl::GetInstance(Recurrence& recurrence)
503 {
504         return recurrence.__pRecurrenceImpl;
505 }
506
507 const _RecurrenceImpl*
508 _RecurrenceImpl::GetInstance(const Recurrence& recurrence)
509 {
510         return recurrence.__pRecurrenceImpl;
511 }
512
513 }}      // Tizen::Social