Merge "Export internal APIs needed by samsung-socil" into tizen_2.1
[framework/osp/social.git] / src / FScl_CalEventImpl.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_CalEventImpl.cpp
18  * @brief               This is the implementation for _CalEventImpl class.
19  *
20  * This file contains definitions of @e _CalEventImpl class.
21  */
22
23 #include <new>
24 #include <string.h>
25 #include <FBaseColArrayList.h>
26 #include <FSysSystemTime.h>
27 #include <FLclTimeZone.h>
28 #include <FLclGregorianCalendar.h>
29 #include <FLclCalendar.h>
30 #include <FSclAttendee.h>
31 #include <FSclRecord.h>
32 #include <FSclCalEvent.h>
33 #include <FBaseSysLog.h>
34 #include <FApp_AppInfo.h>
35 #include <FBase_StringConverter.h>
36 #include "FScl_RecordImpl.h"
37 #include "FScl_CalEventImpl.h"
38 #include "FScl_RecurrenceImpl.h"
39 #include "FScl_CalendarbookImpl.h"
40 #include "FScl_CalendarbookDbConnector.h"
41
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Base::Utility;
45 using namespace Tizen::App;
46 using namespace Tizen::System;
47 using namespace Tizen::Locales;
48
49 namespace Tizen { namespace Social
50 {
51
52 static const int _DEFAULT_ADDED_HOUR = 1;
53 static const int _INVALID_ATTENDEE_INDEX = -1;
54
55 static const int _TERM_LIMIT_DAILY = 1;
56 static const int _TERM_LIMIT_WEEKLY = 7;
57 static const int _TERM_LIMIT_MONTHLY = 31;
58 static const int _TERM_LIMIT_YEARLY = 366;
59 static const int _NUMBER_OF_DAYS_OF_WEEK = 7;
60
61 static const wchar_t* _EVENT_CATEGORY_APPOINTMENT_STRING = L"Appointment";
62 static const wchar_t* _EVENT_CATEGORY_ANNIVERSARY_STRING = L"Anniversary";
63
64 static const wchar_t* _RECURRENCE_KEYWORD_SUNDAY = L"SU";
65 static const wchar_t* _RECURRENCE_KEYWORD_MONDAY = L"MO";
66 static const wchar_t* _RECURRENCE_KEYWORD_TUESDAY = L"TU";
67 static const wchar_t* _RECURRENCE_KEYWORD_WEDNESDAY = L"WE";
68 static const wchar_t* _RECURRENCE_KEYWORD_THURSDAY = L"TH";
69 static const wchar_t* _RECURRENCE_KEYWORD_FRIDAY = L"FR";
70 static const wchar_t* _RECURRENCE_KEYWORD_SATURDAY = L"SA";
71
72 static const wchar_t* _RECURRENCE_DELIMITER = L",";
73 static const int _RECURRENCE_DELIMITER_LENGTH = 1;
74 static const wchar_t _RECURRENCE_BY_DAY_CHAR_PLUS = L'+';
75 static const wchar_t _RECURRENCE_BY_DAY_CHAR_MINUS = L'-';
76 static const wchar_t _RECURRENCE_BY_DAY_CHAR_ONE = L'1';
77 static const int _RECURRENCE_BY_DAY_FIRST_INDEX = 0;
78 static const int _RECURRENCE_BY_DAY_SECOND_INDEX = 1;
79 static const int _MAX_WEEK_OF_MONTH = 5;
80
81 static const double _MIN_LATITUDE = -90.0;
82 static const double _MAX_LATITUDE = 90.0;
83 static const double _MIN_LONGITUDE = -180.0;
84 static const double _MAX_LONGITUDE = 180.0;
85
86 _CalEventImpl::_CalEventImpl(void)
87         : __originalEventId(INVALID_RECORD_ID)
88         , __isInstance(false)
89         , __reminderListUpdated(false)
90 {
91         // Set default start and end time
92         result r = E_SUCCESS;
93         DateTime startTime;
94         SystemTime::GetCurrentTime(startTime);
95         if (IsFailed(GetLastResult()))
96         {
97                 startTime = _CalendarbookImpl::GetMinDateTime();
98                 ClearLastResult();
99         }
100
101         DateTime endTime(startTime);
102         r = endTime.AddHours(_DEFAULT_ADDED_HOUR);
103         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
104
105         calendar_time_s startCalendarTime;
106         calendar_time_s endCalendarTime;
107         startCalendarTime.type = CALENDAR_TIME_UTIME;
108         endCalendarTime.type = CALENDAR_TIME_UTIME;
109         startCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(startTime);
110         endCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(endTime);
111
112         int errorCode = CALENDAR_ERROR_NONE;
113         calendar_record_h eventHandle = null;
114
115         r = _CalendarbookDbConnector::Connect();
116         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
117
118         errorCode = calendar_record_create(_calendar_event._uri, &eventHandle);
119         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
120
121         __eventRecord.ResetHandle(eventHandle);
122
123         errorCode = calendar_record_set_caltime(eventHandle, _calendar_event.start_time, startCalendarTime);
124         errorCode = calendar_record_set_caltime(eventHandle, _calendar_event.end_time, endCalendarTime);
125
126         // default value
127         errorCode = calendar_record_set_int(eventHandle, _calendar_event.event_status, CALENDAR_EVENT_STATUS_NONE);
128         errorCode = calendar_record_set_int(eventHandle, _calendar_event.busy_status, CALENDAR_EVENT_BUSY_STATUS_FREE);
129         errorCode = calendar_record_set_int(eventHandle, _calendar_event.priority, CALENDAR_EVENT_PRIORITY_NORMAL);
130         errorCode = calendar_record_set_int(eventHandle, _calendar_event.sensitivity, CALENDAR_SENSITIVITY_PUBLIC);
131
132         r = __reminderList.Construct();
133         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
134 }
135
136 _CalEventImpl::_CalEventImpl(const _CalEventImpl& rhs)
137         : __originalEventId(rhs.__originalEventId)
138         , __isInstance(rhs.__isInstance)
139         , __reminderListUpdated(false)
140 {
141         int errorCode = CALENDAR_ERROR_NONE;
142         calendar_record_h eventHandle = null;
143
144         result r = _CalendarbookDbConnector::Connect();
145         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
146
147         errorCode = calendar_record_clone(rhs.__eventRecord.GetHandle(), &eventHandle);
148         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
149
150         __eventRecord.ResetHandle(eventHandle);
151
152         r = __reminderList.Construct();
153         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
154 }
155
156 _CalEventImpl::~_CalEventImpl(void)
157 {
158         int errorCode = CALENDAR_ERROR_NONE;
159         errorCode = calendar_record_destroy(__eventRecord.ReleaseHandle(), true);
160
161         __reminderList.RemoveAll(true);
162
163         result r = _CalendarbookDbConnector::Disconnect();
164         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
165 }
166
167 _CalEventImpl&
168 _CalEventImpl::operator =(const _CalEventImpl& rhs)
169 {
170         if (this == &rhs)
171         {
172                 return *this;
173         }
174
175         __originalEventId = rhs.__originalEventId;
176         __isInstance = rhs.__isInstance;
177         __reminderList.RemoveAll(true);
178         __reminderListUpdated = false;
179
180         int errorCode = CALENDAR_ERROR_NONE;
181         calendar_record_h eventHandle = null;
182
183         errorCode = calendar_record_clone(rhs.__eventRecord.GetHandle(), &eventHandle);
184         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
185
186         __eventRecord.ResetHandle(eventHandle);
187
188         return *this;
189 }
190
191 bool
192 _CalEventImpl::IsInstance(void) const
193 {
194         return __isInstance;
195 }
196
197 bool
198 _CalEventImpl::IsRecurring(void) const
199 {
200         int errorCode = CALENDAR_ERROR_NONE;
201         int recurrenceFreq = CALENDAR_RECURRENCE_NONE;
202
203         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.freq, &recurrenceFreq);
204
205         if (recurrenceFreq == CALENDAR_RECURRENCE_NONE)
206         {
207                 return false;
208         }
209
210         return true;
211 }
212
213 RecordId
214 _CalEventImpl::GetOriginalCalEventId(void)  const
215 {
216         return __originalEventId;
217 }
218
219 bool
220 _CalEventImpl::IsAllDayEvent(void)  const
221 {
222         int errorCode = CALENDAR_ERROR_NONE;
223         calendar_time_s startCalendarTime;
224
225         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, &startCalendarTime);
226         if (startCalendarTime.type == CALENDAR_TIME_UTIME)
227         {
228                 return false;
229         }
230
231         return true;
232 }
233
234 void
235 _CalEventImpl::SetAllDayEvent(bool isAllDayEvent)
236 {
237         if (GetCategory() == EVENT_CATEGORY_ANNIVERSARY)
238         {
239                 return;
240         }
241
242         int errorCode = CALENDAR_ERROR_NONE;
243         calendar_time_s startCalendarTime;
244         calendar_time_s tmpStartCalendarTime;
245         calendar_time_s endCalendarTime;
246         calendar_time_s tmpEndCalendarTime;
247
248         bool isChanged = false;
249
250         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, &startCalendarTime);
251         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.end_time, &endCalendarTime);
252
253         if (isAllDayEvent && startCalendarTime.type == CALENDAR_TIME_UTIME)
254         {
255                 tmpStartCalendarTime.type = CALENDAR_TIME_LOCALTIME;
256                 DateTime tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
257                 tmpStartCalendarTime.time.date.year = tmpStartTime.GetYear();
258                 tmpStartCalendarTime.time.date.month = tmpStartTime.GetMonth();
259                 tmpStartCalendarTime.time.date.mday = tmpStartTime.GetDay();
260
261                 tmpEndCalendarTime.type = CALENDAR_TIME_LOCALTIME;
262                 DateTime tmpEndTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(endCalendarTime.time.utime);
263                 tmpEndCalendarTime.time.date.year = tmpEndTime.GetYear();
264                 tmpEndCalendarTime.time.date.month = tmpEndTime.GetMonth();
265                 tmpEndCalendarTime.time.date.mday = tmpEndTime.GetDay();
266
267                 errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, tmpStartCalendarTime);
268                 errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.end_time, tmpEndCalendarTime);
269
270                 isChanged = true;
271         }
272         else if (!isAllDayEvent && startCalendarTime.type == CALENDAR_TIME_LOCALTIME)
273         {
274                 tmpStartCalendarTime.type = CALENDAR_TIME_UTIME;
275                 DateTime tmpStartTime;
276                 tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
277                 tmpStartCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(tmpStartTime);
278
279                 tmpEndCalendarTime.type = CALENDAR_TIME_UTIME;
280                 DateTime tmpEndTime;
281                 tmpEndTime.SetValue(endCalendarTime.time.date.year, endCalendarTime.time.date.month, endCalendarTime.time.date.mday);
282                 tmpEndCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(tmpEndTime);
283
284                 errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, tmpStartCalendarTime);
285                 errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.end_time, tmpEndCalendarTime);
286
287                 isChanged = true;
288         }
289
290         if (IsRecurring())
291         {
292                 int rangeType = CALENDAR_RANGE_NONE;
293                 errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.range_type, &rangeType);
294
295                 if (rangeType == CALENDAR_RANGE_UNTIL)
296                 {
297                         calendar_time_s untilCalendarTime;
298                         calendar_time_s tmpUntilCalendarTime;
299
300                         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.until_time, &untilCalendarTime);
301                         if (isAllDayEvent && untilCalendarTime.type == CALENDAR_TIME_UTIME)
302                         {
303                                 tmpUntilCalendarTime.type = CALENDAR_TIME_LOCALTIME;
304                                 DateTime tmpUntilTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(untilCalendarTime.time.utime);
305                                 tmpUntilCalendarTime.time.date.year = tmpUntilTime.GetYear();
306                                 tmpUntilCalendarTime.time.date.month = tmpUntilTime.GetMonth();
307                                 tmpUntilCalendarTime.time.date.mday = tmpUntilTime.GetDay();
308
309                                 errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.until_time, tmpUntilCalendarTime);
310                         }
311                         else if (!isAllDayEvent && untilCalendarTime.type == CALENDAR_TIME_LOCALTIME)
312                         {
313                                 tmpUntilCalendarTime.type = CALENDAR_TIME_UTIME;
314                                 DateTime tmpUntilTime;
315                                 tmpUntilTime.SetValue(untilCalendarTime.time.date.year, untilCalendarTime.time.date.month, untilCalendarTime.time.date.mday);
316                                 tmpUntilCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(tmpUntilTime);
317
318                                 errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.until_time, tmpUntilCalendarTime);
319                         }
320                 }
321
322                 if (isChanged)
323                 {
324                         if (__pRecurrence == null)
325                         {
326                                 __pRecurrence.reset(ConvertEventHandleToRecurrenceN(__eventRecord.GetHandle()));
327                         }
328
329                         std::unique_ptr<char[]> pExDates(_StringConverter::CopyToCharArrayN(ConvertRecurrenceToRRuleExDateString(*__pRecurrence.get(), isAllDayEvent)));
330                         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.exdate, pExDates.get());
331                 }
332         }
333 }
334
335 ByteBuffer*
336 _CalEventImpl::GetUIDN(void) const
337 {
338         ClearLastResult();
339
340         int errorCode = CALENDAR_ERROR_NONE;
341         char* pStrUid = null;
342         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.uid, &pStrUid);
343
344         if (pStrUid == null || strlen(pStrUid) == 0)
345         {
346                 return null;
347         }
348
349         ByteBuffer* pConvertedUidBuffer = _CalendarbookUtil::ConvertCharArrayToByteBufferN(pStrUid);
350         SysTryReturn(NID_SCL, pConvertedUidBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
351
352         return pConvertedUidBuffer;
353 }
354
355 result
356 _CalEventImpl::SetUID(const ByteBuffer* pUid)
357 {
358         int errorCode = CALENDAR_ERROR_NONE;
359
360         if (pUid != null)
361         {
362                 std::unique_ptr<char[]> pConvertedUidArray(_CalendarbookUtil::ConvertByteBufferToCharArrayN(*pUid));
363                 SysTryReturn(NID_SCL, pConvertedUidArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
364
365                 errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.uid, pConvertedUidArray.get());
366         }
367         else
368         {
369                 errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.uid, null);
370         }
371
372         return E_SUCCESS;
373 }
374
375 Tizen::Base::String
376 _CalEventImpl::GetUid(void) const
377 {
378         int errorCode = CALENDAR_ERROR_NONE;
379         char* pStrUid = null;
380         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.uid, &pStrUid);
381
382         return String(pStrUid);
383 }
384
385 void
386 _CalEventImpl::SetUid(const Tizen::Base::String& uid)
387 {
388         int errorCode = CALENDAR_ERROR_NONE;
389
390         std::unique_ptr<char[]> pStrUid(_StringConverter::CopyToCharArrayN(uid));
391         SysTryReturnVoidResult(NID_SCL, pStrUid != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
392
393         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.uid, pStrUid.get());
394 }
395
396 EventStatus
397 _CalEventImpl::GetStatus(void) const
398 {
399         int errorCode = CALENDAR_ERROR_NONE;
400         int srcEventStatus = CALENDAR_EVENT_STATUS_NONE;
401
402         EventStatus eventStatus = EVENT_STATUS_NONE;
403
404         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.event_status, &srcEventStatus);
405
406         switch (srcEventStatus)
407         {
408         case CALENDAR_EVENT_STATUS_NONE:
409                 eventStatus = EVENT_STATUS_NONE;
410                 break;
411         case CALENDAR_EVENT_STATUS_TENTATIVE:
412                 eventStatus = EVENT_STATUS_TENTATIVE;
413                 break;
414         case CALENDAR_EVENT_STATUS_CONFIRMED:
415                 eventStatus = EVENT_STATUS_CONFIRMED;
416                 break;
417         case CALENDAR_EVENT_STATUS_CANCELLED:
418                 eventStatus = EVENT_STATUS_CANCELLED;
419                 break;
420         default :
421                 SysLog(NID_SCL, "The status value is invalid.");
422                 return EVENT_STATUS_NONE;
423         }
424
425         return eventStatus;
426 }
427
428 void
429 _CalEventImpl::SetStatus(EventStatus status)
430 {
431         int errorCode = CALENDAR_ERROR_NONE;
432         errorCode = calendar_record_set_int(__eventRecord.GetHandle(), _calendar_event.event_status, _CalendarbookUtil::ConvertEventStatusToCSEventStatus(status));
433 }
434
435 BusyStatus
436 _CalEventImpl::GetBusyStatus(void) const
437 {
438         int errorCode = CALENDAR_ERROR_NONE;
439         int srcEventBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
440
441         BusyStatus busyStatus = BUSY_STATUS_FREE;
442
443         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.busy_status, &srcEventBusyStatus);
444
445         switch (srcEventBusyStatus)
446         {
447         case CALENDAR_EVENT_BUSY_STATUS_FREE:
448                 busyStatus = BUSY_STATUS_FREE;
449                 break;
450         case CALENDAR_EVENT_BUSY_STATUS_BUSY:
451                 busyStatus = BUSY_STATUS_BUSY;
452                 break;
453         case CALENDAR_EVENT_BUSY_STATUS_UNAVAILABLE:
454                 busyStatus = BUSY_STATUS_UNAVAILABLE;
455                 break;
456         case CALENDAR_EVENT_BUSY_STATUS_TENTATIVE:
457                 busyStatus = BUSY_STATUS_TENTATIVE;
458                 break;
459         default :
460                 SysLog(NID_SCL, "The busy status value is invalid.");
461                 busyStatus = BUSY_STATUS_FREE;
462                 break;
463         }
464
465         return busyStatus;
466 }
467
468 void
469 _CalEventImpl::SetBusyStatus(BusyStatus busyStatus)
470 {
471         int errorCode = CALENDAR_ERROR_NONE;
472         errorCode = calendar_record_set_int(__eventRecord.GetHandle(), _calendar_event.busy_status, _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(busyStatus));
473 }
474
475 EventPriority
476 _CalEventImpl::GetPriority(void) const
477 {
478         int errorCode = CALENDAR_ERROR_NONE;
479         int srcPriority = CALENDAR_EVENT_PRIORITY_LOW;
480
481         EventPriority priority = EVENT_PRIORITY_NORMAL;
482
483         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.priority, &srcPriority);
484
485         switch (srcPriority)
486         {
487         case CALENDAR_EVENT_PRIORITY_LOW:
488                 priority = EVENT_PRIORITY_LOW;
489                 break;
490         case CALENDAR_EVENT_PRIORITY_NONE:
491                 // fall through
492         case CALENDAR_EVENT_PRIORITY_NORMAL:
493                 priority = EVENT_PRIORITY_NORMAL;
494                 break;
495         case CALENDAR_EVENT_PRIORITY_HIGH:
496                 priority = EVENT_PRIORITY_HIGH;
497                 break;
498         default :
499                 SysLog(NID_SCL, "The priority value is invalid.");
500                 priority = EVENT_PRIORITY_NORMAL;
501         }
502
503         return priority;
504 }
505
506 void
507 _CalEventImpl::SetPriority(EventPriority priority)
508 {
509         int errorCode = CALENDAR_ERROR_NONE;
510         errorCode = calendar_record_set_int(__eventRecord.GetHandle(), _calendar_event.priority, _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(priority));
511 }
512
513 result
514 _CalEventImpl::AddAttendee(const Attendee& attendee)
515 {
516         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0)
517         {
518                 SysTryReturnResult(NID_SCL, !attendee.GetEmail().IsEmpty(), E_OBJ_ALREADY_EXIST
519                                         , "The attendee's email string is empty.");
520         }
521         else
522         {
523                 SysTryReturnResult(NID_SCL, !attendee.GetEmail().IsEmpty(), E_INVALID_ARG
524                                         , "The attendee's email string is empty.");
525         }
526
527         std::unique_ptr<char[]> pConvertedEmail(_StringConverter::CopyToCharArrayN(attendee.GetEmail()));
528         SysTryReturnResult(NID_SCL, pConvertedEmail != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
529
530         SysTryReturnResult(NID_SCL, GetAttendeeIndex(pConvertedEmail.get()) == _INVALID_ATTENDEE_INDEX, E_OBJ_ALREADY_EXIST
531                                 , "The specified attendee already exists as this email is already registered.");
532
533         int errorCode = CALENDAR_ERROR_NONE;
534
535         std::unique_ptr<char[]> pConvertedName(_StringConverter::CopyToCharArrayN(attendee.GetName()));
536         SysTryReturnResult(NID_SCL, pConvertedName != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
537
538         std::unique_ptr<char[]> pConvertedPhoneNumber(_StringConverter::CopyToCharArrayN(attendee.GetPhoneNumber()));
539         SysTryReturnResult(NID_SCL, pConvertedPhoneNumber != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
540
541         calendar_attendee_status_e convertedStatus = CALENDAR_ATTENDEE_STATUS_TENTATIVE;
542         switch (attendee.GetStatus())
543         {
544         case ATTENDEE_STATUS_NONE:
545                 convertedStatus = CALENDAR_ATTENDEE_STATUS_COMPLETED;
546                 break;
547         case ATTENDEE_STATUS_NOT_RESPONDED:
548                 convertedStatus = CALENDAR_ATTENDEE_STATUS_PENDING;
549                 break;
550         case ATTENDEE_STATUS_ACCEPTED:
551                 convertedStatus = CALENDAR_ATTENDEE_STATUS_ACCEPTED;
552                 break;
553         case ATTENDEE_STATUS_DECLINED:
554                 convertedStatus = CALENDAR_ATTENDEE_STATUS_DECLINED;
555                 break;
556         case ATTENDEE_STATUS_TENTATIVE:
557                 convertedStatus = CALENDAR_ATTENDEE_STATUS_TENTATIVE;
558                 break;
559         default:
560                 SysLog(NID_SCL, "The attendee status value is invalid. attendee status = %d", attendee.GetStatus());
561                 convertedStatus = CALENDAR_ATTENDEE_STATUS_TENTATIVE;
562                 break;
563         }
564
565         calendar_attendee_role_e convertedRole = CALENDAR_ATTENDEE_ROLE_NON_PARTICIPANT;
566         switch (attendee.GetRole())
567         {
568         case ATTENDEE_ROLE_ATTENDEE:
569                 convertedRole = CALENDAR_ATTENDEE_ROLE_OPT_PARTICIPANT;
570                 break;
571         case ATTENDEE_ROLE_REQUIRED_ATTENDEE:
572                 convertedRole = CALENDAR_ATTENDEE_ROLE_REQ_PARTICIPANT;
573                 break;
574         case ATTENDEE_ROLE_ORGANIZER:
575                 convertedRole = CALENDAR_ATTENDEE_ROLE_CHAIR;
576                 break;
577         default:
578                 SysLog(NID_SCL, "The attendee role value is invalid. attendee role = %d", attendee.GetRole());
579                 convertedRole = CALENDAR_ATTENDEE_ROLE_OPT_PARTICIPANT;
580                 break;
581         }
582
583         calendar_record_h attendeeHandle = null;
584         errorCode = calendar_record_create(_calendar_attendee._uri, &attendeeHandle);
585         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
586
587         errorCode = calendar_record_set_str(attendeeHandle, _calendar_attendee.name, pConvertedName.get());
588         errorCode = calendar_record_set_str(attendeeHandle, _calendar_attendee.email, pConvertedEmail.get());
589         errorCode = calendar_record_set_str(attendeeHandle, _calendar_attendee.number, pConvertedPhoneNumber.get());
590         errorCode = calendar_record_set_int(attendeeHandle, _calendar_attendee.status, convertedStatus);
591         errorCode = calendar_record_set_int(attendeeHandle, _calendar_attendee.role, convertedRole);
592         errorCode = calendar_record_set_int(attendeeHandle, _calendar_attendee.person_id, attendee.GetPersonId());
593
594         errorCode = calendar_record_add_child_record(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, attendeeHandle);
595
596         return E_SUCCESS;
597 }
598
599 result
600 _CalEventImpl::RemoveAttendee(const Attendee& attendee)
601 {
602         int errorCode = CALENDAR_ERROR_NONE;
603         int index = _INVALID_ATTENDEE_INDEX;
604
605         std::unique_ptr<char[]> pConvertedEmail(_StringConverter::CopyToCharArrayN(attendee.GetEmail()));
606         SysTryReturnResult(NID_SCL, pConvertedEmail != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
607
608         index = GetAttendeeIndex(pConvertedEmail.get());
609         SysTryReturnResult(NID_SCL, index != _INVALID_ATTENDEE_INDEX, E_OBJ_NOT_FOUND, "The specified attendee does not exist.")
610
611         calendar_record_h tmpAttendeeHandle = null;
612         errorCode = calendar_record_get_child_record_at_p(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, index, &tmpAttendeeHandle);
613         errorCode = calendar_record_remove_child_record(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, tmpAttendeeHandle);
614
615         return E_SUCCESS;
616 }
617
618 IList*
619 _CalEventImpl::GetAllAttendeesN(void) const
620 {
621         ClearLastResult();
622
623         int errorCode = CALENDAR_ERROR_NONE;
624
625         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
626         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
627         result r = pList->Construct();
628         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
629
630         unsigned int attendeeCount = 0;
631         errorCode = calendar_record_get_child_record_count(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, &attendeeCount);
632
633         for (int i = 0; i < attendeeCount; i++)
634         {
635                 calendar_record_h tmpAttendeeHandle = null;
636                 char* pTmpEmail = null;
637                 char* pTmpName = null;
638                 char* pTmpPhoneNumber = null;
639                 int tmpStatus = 0;
640                 int tmpRole = 0;
641                 int tmpPersonId = -1;
642                 AttendeeStatus attendeeStatus = ATTENDEE_STATUS_NONE;
643                 AttendeeRole attendeeRole = ATTENDEE_ROLE_ATTENDEE;
644
645                 errorCode = calendar_record_get_child_record_at_p(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, i, &tmpAttendeeHandle);
646                 errorCode = calendar_record_get_str_p(tmpAttendeeHandle, _calendar_attendee.email, &pTmpEmail);
647                 errorCode = calendar_record_get_str_p(tmpAttendeeHandle, _calendar_attendee.name, &pTmpName);
648                 errorCode = calendar_record_get_str_p(tmpAttendeeHandle, _calendar_attendee.number, &pTmpPhoneNumber);
649                 errorCode = calendar_record_get_int(tmpAttendeeHandle, _calendar_attendee.status, &tmpStatus);
650                 errorCode = calendar_record_get_int(tmpAttendeeHandle, _calendar_attendee.role, &tmpRole);
651                 errorCode = calendar_record_get_int(tmpAttendeeHandle, _calendar_attendee.person_id, &tmpPersonId);
652
653                 switch (tmpStatus)
654                 {
655                 case CALENDAR_ATTENDEE_STATUS_PENDING:
656                         attendeeStatus = ATTENDEE_STATUS_NOT_RESPONDED;
657                         break;
658                 case CALENDAR_ATTENDEE_STATUS_ACCEPTED:
659                         attendeeStatus = ATTENDEE_STATUS_ACCEPTED;
660                         break;
661                 case CALENDAR_ATTENDEE_STATUS_DECLINED:
662                         attendeeStatus = ATTENDEE_STATUS_DECLINED;
663                         break;
664                 case CALENDAR_ATTENDEE_STATUS_TENTATIVE:
665                         attendeeStatus = ATTENDEE_STATUS_TENTATIVE;
666                         break;
667                 case CALENDAR_ATTENDEE_STATUS_DELEGATED:
668                         attendeeStatus = ATTENDEE_STATUS_NONE;
669                         break;
670                 case CALENDAR_ATTENDEE_STATUS_COMPLETED:
671                         attendeeStatus = ATTENDEE_STATUS_NONE;
672                         break;
673                 case CALENDAR_ATTENDEE_STATUS_IN_PROCESS:
674                         attendeeStatus = ATTENDEE_STATUS_NOT_RESPONDED;
675                         break;
676                 default:
677                         SysLog(NID_SCL, "The attendee status value is invalid. attendee status = %d", tmpStatus);
678                         attendeeStatus = ATTENDEE_STATUS_NONE;
679                         break;
680                 }
681
682                 switch (tmpRole)
683                 {
684                 case CALENDAR_ATTENDEE_ROLE_REQ_PARTICIPANT:
685                         attendeeRole = ATTENDEE_ROLE_REQUIRED_ATTENDEE;
686                         break;
687                 case CALENDAR_ATTENDEE_ROLE_OPT_PARTICIPANT:
688                         attendeeRole = ATTENDEE_ROLE_ATTENDEE;
689                         break;
690                 case CALENDAR_ATTENDEE_ROLE_NON_PARTICIPANT:
691                         attendeeRole = ATTENDEE_ROLE_ATTENDEE;
692                         break;
693                 case CALENDAR_ATTENDEE_ROLE_CHAIR:
694                         attendeeRole = ATTENDEE_ROLE_ORGANIZER;
695                         break;
696                 default:
697                         SysLog(NID_SCL, "The attendee role value is invalid. attendee role = %d", tmpRole);
698                         attendeeRole = ATTENDEE_ROLE_ATTENDEE;
699                         break;
700                 }
701
702                 Attendee* pTmpAttendee = new (std::nothrow) Attendee(pTmpEmail);
703                 SysTryReturn(NID_SCL, pTmpAttendee != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
704
705                 if (pTmpName != null && strlen(pTmpName) > 0)
706                 {
707                         pTmpAttendee->SetName(pTmpName);
708                 }
709
710                 if (pTmpPhoneNumber != null && strlen(pTmpPhoneNumber) > 0)
711                 {
712                         pTmpAttendee->SetPhoneNumber(pTmpPhoneNumber);
713                 }
714
715                 pTmpAttendee->SetStatus(attendeeStatus);
716                 pTmpAttendee->SetRole(attendeeRole);
717                 pTmpAttendee->SetPersonId(tmpPersonId);
718
719                 pList->Add(*pTmpAttendee);
720         }
721
722         return pList.release();
723 }
724
725 Tizen::Locales::TimeZone
726 _CalEventImpl::GetTimeZone(void) const
727 {
728         int errorCode = CALENDAR_ERROR_NONE;
729         char* pTimeZoneId = null;
730         TimeZone timeZone;
731
732         result r = E_SUCCESS;
733         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.start_tzid, &pTimeZoneId);
734         if (pTimeZoneId != null && strlen(pTimeZoneId) > 0)
735         {
736                 r = TimeZone::GetTimeZone(pTimeZoneId, timeZone);
737         }
738
739         return timeZone;
740 }
741
742 result
743 _CalEventImpl::SetTimeZone(const Tizen::Locales::TimeZone& timeZone)
744 {
745         int errorCode = CALENDAR_ERROR_NONE;
746
747         std::unique_ptr<char[]> pConvertedTimeZoneId(_StringConverter::CopyToCharArrayN(timeZone.GetId()));
748         SysTryReturnResult(NID_SCL, pConvertedTimeZoneId != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
749
750         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.start_tzid, pConvertedTimeZoneId.get());
751         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.end_tzid, pConvertedTimeZoneId.get());
752
753         return E_SUCCESS;
754 }
755
756 RecurrenceId
757 _CalEventImpl::GetRecurrenceId(void) const
758 {
759         ClearLastResult();
760         SysTryReturn(NID_SCL, __isInstance, DateTime(), E_INVALID_STATE,
761                                 "[E_INVALID_STATE] The instance does not have a recurrence or is not a CalEvent instance which doesn't have a RecurrenceId.");
762
763         return GetStartTime();
764 }
765
766 String
767 _CalEventImpl::GetSubject(void) const
768 {
769         int errorCode = CALENDAR_ERROR_NONE;
770         char* pSubject = null;
771
772         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.summary, &pSubject);
773
774         return String(pSubject);
775 }
776
777 String
778 _CalEventImpl::GetDescription(void) const
779 {
780         int errorCode = CALENDAR_ERROR_NONE;
781         char* pDescription = null;
782
783         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.description, &pDescription);
784
785         return String(pDescription);
786 }
787 DateTime
788 _CalEventImpl::GetStartTime(void) const
789 {
790         int errorCode = CALENDAR_ERROR_NONE;
791         calendar_time_s startCalendarTime;
792         DateTime tmpStartTime;
793
794         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, &startCalendarTime);
795
796         if (startCalendarTime.type == CALENDAR_TIME_UTIME)
797         {
798                 tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
799         }
800         else
801         {
802                 tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
803         }
804
805         return tmpStartTime;
806 }
807
808 DateTime
809 _CalEventImpl::GetEndTime(void) const
810 {
811         int errorCode = CALENDAR_ERROR_NONE;
812         calendar_time_s endCalendarTime;
813         DateTime tmpEndTime;
814
815         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.end_time, &endCalendarTime);
816
817         if (endCalendarTime.type == CALENDAR_TIME_UTIME)
818         {
819                 tmpEndTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(endCalendarTime.time.utime);
820         }
821         else if (endCalendarTime.type == CALENDAR_TIME_LOCALTIME)
822         {
823                 tmpEndTime.SetValue(endCalendarTime.time.date.year, endCalendarTime.time.date.month, endCalendarTime.time.date.mday);
824         }
825
826         return tmpEndTime;
827 }
828
829 String
830 _CalEventImpl::GetLocation(void) const
831 {
832         int errorCode = CALENDAR_ERROR_NONE;
833         char* pLocation = null;
834
835         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.location, &pLocation);
836
837         return String(pLocation);
838 }
839
840 EventCategory
841 _CalEventImpl::GetCategory(void) const
842 {
843
844         int errorCode = CALENDAR_ERROR_NONE;
845         char* pCategories = null;
846
847         errorCode = calendar_record_get_str_p(__eventRecord.GetHandle(), _calendar_event.categories, &pCategories);
848
849         return _CalendarbookUtil::ConvertCSCategoriesToEventCategory(pCategories);
850 }
851
852 RecordSensitivity
853 _CalEventImpl::GetSensitivity(void) const
854 {
855         int errorCode = CALENDAR_ERROR_NONE;
856         int srcSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
857         RecordSensitivity sensitivity = SENSITIVITY_PUBLIC;
858
859         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.sensitivity, &srcSensitivity);
860
861         switch (srcSensitivity)
862         {
863         case CALENDAR_SENSITIVITY_PUBLIC:
864                 sensitivity = SENSITIVITY_PUBLIC;
865                 break;
866         case CALENDAR_SENSITIVITY_PRIVATE:
867                 sensitivity = SENSITIVITY_PRIVATE;
868                 break;
869         case CALENDAR_SENSITIVITY_CONFIDENTIAL:
870                 sensitivity = SENSITIVITY_CONFIDENTIAL;
871                 break;
872         default :
873                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. sensitivity = %d", srcSensitivity);
874                 sensitivity = SENSITIVITY_PUBLIC;
875         }
876
877         return sensitivity;
878 }
879
880 const Reminder*
881 _CalEventImpl::GetReminder(void) const
882 {
883         if (__reminderListUpdated == false)
884         {
885                 result r = _CalendarbookUtil::ConvertEventAlarmsToReminderList(__eventRecord.GetHandle(), __reminderList);
886                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
887
888                 __reminderListUpdated = true;
889         }
890
891         if (__reminderList.GetCount() > 0)
892         {
893                 return static_cast<const Reminder*>(__reminderList.GetAt(0));
894         }
895
896         return null;
897 }
898
899 const Recurrence*
900 _CalEventImpl::GetRecurrence(void) const
901 {
902         if (__pRecurrence == null)
903         {
904                 __pRecurrence.reset(ConvertEventHandleToRecurrenceN(__eventRecord.GetHandle()));
905         }
906
907         return __pRecurrence.get();
908 }
909
910 Recurrence*
911 _CalEventImpl::ConvertEventHandleToRecurrenceN(calendar_record_h eventHandle) const
912 {
913         int errorCode = CALENDAR_ERROR_NONE;
914         int srcFrequency = CALENDAR_RECURRENCE_NONE;
915         calendar_time_s untilCalendarTime;
916         int rangeType = CALENDAR_RANGE_NONE;
917         int count = 0;
918         int interval = 0;
919         char* pByDay = null;
920         char* pByMonthDay = null;
921         char* pByMonth = null;
922         int weekStart = 0;
923         char* pExDates = null;
924
925         result r = E_SUCCESS;
926         int dayOfWeek = 0;
927         int monthOfYear = 0;
928         int dayOfMonth = 0;
929         int weekOfMonth = 0;
930         CalDayOfWeek convertedWeekStart = CAL_SUNDAY;
931
932         std::unique_ptr<Recurrence> pRecurrence(new (std::nothrow) Recurrence());
933
934         errorCode = calendar_record_get_int(eventHandle, _calendar_event.freq, &srcFrequency);
935
936         if (srcFrequency == CALENDAR_RECURRENCE_NONE)
937         {
938                 return null;
939         }
940
941         errorCode = calendar_record_get_int(eventHandle, _calendar_event.range_type, &rangeType);
942         errorCode = calendar_record_get_int(eventHandle, _calendar_event.count, &count);
943         errorCode = calendar_record_get_int(eventHandle, _calendar_event.interval, &interval);
944         errorCode = calendar_record_get_caltime(eventHandle, _calendar_event.until_time, &untilCalendarTime);
945         errorCode = calendar_record_get_int(eventHandle, _calendar_event.wkst, &weekStart);
946
947         errorCode = calendar_record_get_str_p(eventHandle, _calendar_event.byday, &pByDay);
948         errorCode = calendar_record_get_str_p(eventHandle, _calendar_event.bymonthday, &pByMonthDay);
949         if (pByMonthDay != null)
950         {
951                 r = Integer::Parse(pByMonthDay, dayOfMonth);
952                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
953         }
954         errorCode = calendar_record_get_str_p(eventHandle, _calendar_event.bymonth, &pByMonth);
955         if (pByMonth != null)
956         {
957                 r = Integer::Parse(pByMonth, monthOfYear);
958                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
959         }
960
961         switch (srcFrequency)
962         {
963         case CALENDAR_RECURRENCE_DAILY:
964                 pRecurrence->SetFrequency(FREQ_DAILY);
965                 break;
966         case CALENDAR_RECURRENCE_WEEKLY:
967                 pRecurrence->SetFrequency(FREQ_WEEKLY);
968
969                 dayOfWeek = ConvertRRuleByDayStringToDayOfWeek(pByDay);
970                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. byday = %s", pByDay);
971
972                 r = pRecurrence->SetDayOfWeek(dayOfWeek);
973
974                 break;
975         case CALENDAR_RECURRENCE_MONTHLY:
976                 pRecurrence->SetFrequency(FREQ_MONTHLY);
977
978                 if (dayOfMonth != 0)
979                 {
980                         if (dayOfMonth < 0)
981                         {
982                                 int maxDaysOfMonth = _CalendarbookUtil::GetMaxDaysOfMonth(untilCalendarTime.time.date.year, monthOfYear);
983                                 r = pRecurrence->SetDayOfMonth(maxDaysOfMonth + dayOfMonth);
984                         }
985                         else
986                         {
987                                 r = pRecurrence->SetDayOfMonth(dayOfMonth);
988                         }
989                 }
990                 else
991                 {
992                         r = ConvertRRuleByDayStringToDayOfWeekAndWeekOfMonth(pByDay, weekOfMonth, dayOfWeek);
993                         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. byday = %s", pByDay);
994
995                         r = pRecurrence->SetWeekOfMonth(weekOfMonth);
996                         r = pRecurrence->SetDayOfWeek(dayOfWeek);
997                 }
998
999                 break;
1000         case CALENDAR_RECURRENCE_YEARLY:
1001                 pRecurrence->SetFrequency(FREQ_YEARLY);
1002
1003                 if (dayOfMonth != 0)
1004                 {
1005                         if (dayOfMonth < 0)
1006                         {
1007                                 int maxDaysOfMonth = _CalendarbookUtil::GetMaxDaysOfMonth(untilCalendarTime.time.date.year, monthOfYear);
1008                                 r = pRecurrence->SetDayOfMonth(maxDaysOfMonth + dayOfMonth);
1009                         }
1010                         else
1011                         {
1012                                 r = pRecurrence->SetDayOfMonth(dayOfMonth);
1013                         }
1014                 }
1015                 else
1016                 {
1017                         r = ConvertRRuleByDayStringToDayOfWeekAndWeekOfMonth(pByDay, weekOfMonth, dayOfWeek);
1018                         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. byday = %s", pByDay);
1019
1020                         r = pRecurrence->SetWeekOfMonth(weekOfMonth);
1021                         r = pRecurrence->SetDayOfWeek(dayOfWeek);
1022                 }
1023                 r = pRecurrence->SetMonthOfYear(monthOfYear);
1024
1025                 break;
1026         default :
1027                 SysLogException(NID_SCL, E_INVALID_ARG, "Invalid argument is passed. frequency = %d", srcFrequency);
1028                 return null;
1029         }
1030
1031         if (rangeType == CALENDAR_RANGE_NONE)
1032         {
1033                 r = pRecurrence->SetUntil(&DateTime::GetMaxValue());
1034         }
1035         else if (rangeType == CALENDAR_RANGE_UNTIL)
1036         {
1037                 DateTime until;
1038
1039                 if (untilCalendarTime.type == CALENDAR_TIME_LOCALTIME)
1040                 {
1041                         until.SetValue(untilCalendarTime.time.date.year, untilCalendarTime.time.date.month, untilCalendarTime.time.date.mday);
1042                 }
1043                 else
1044                 {
1045                         until = _CalendarbookUtil::ConvertEpochTimeToDateTime(untilCalendarTime.time.utime);
1046                 }
1047
1048                 r = pRecurrence->SetUntil(&until);
1049         }
1050         else
1051         {
1052                 r = pRecurrence->SetCounts(count);
1053         }
1054         r = pRecurrence->SetInterval(interval);
1055
1056         switch (weekStart)
1057         {
1058         case CALENDAR_SUNDAY:
1059                 convertedWeekStart = CAL_SUNDAY;
1060                 break;
1061         case CALENDAR_MONDAY:
1062                 convertedWeekStart = CAL_MONDAY;
1063                 break;
1064         default:
1065                 SysLogException(NID_SCL, E_INVALID_ARG, "Invalid argument is passed. week start = %d", weekStart);
1066                 return null;
1067         }
1068         r = pRecurrence->SetWeekStart(convertedWeekStart);
1069
1070         errorCode = calendar_record_get_str_p(eventHandle, _calendar_event.exdate, &pExDates);
1071         if (pExDates != null && strlen(pExDates) != 0)
1072         {
1073                 r = ConvertRRuleExDateStringToRecurrence(pExDates, *pRecurrence.get());
1074                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. exdate = %s", pExDates);
1075         }
1076
1077         return pRecurrence.release();
1078 }
1079
1080 DateTime
1081 _CalEventImpl::GetLastRevisedTime(void) const
1082 {
1083         int errorCode = CALENDAR_ERROR_NONE;
1084         long long lastModifiedTime = 0;
1085         DateTime tmpLastRevisedTime;
1086
1087         errorCode = calendar_record_get_lli(__eventRecord.GetHandle(), _calendar_event.last_modified_time, &lastModifiedTime);
1088
1089         tmpLastRevisedTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(lastModifiedTime);
1090
1091         return tmpLastRevisedTime;
1092 }
1093
1094 result
1095 _CalEventImpl::SetSubject(const String& subject)
1096 {
1097         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1098         {
1099                 SysTryReturnResult(NID_SCL, subject.GetLength() <= MAX_EVENT_SUBJECT_LENGTH, E_INVALID_ARG
1100                                 , "The length of the value exceeds MAX_EVENT_SUBJECT_LENGTH.");
1101         }
1102
1103         std::unique_ptr<char[]> pConvertedSubject(_StringConverter::CopyToCharArrayN(subject));
1104         SysTryReturnResult(NID_SCL, pConvertedSubject != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1105
1106         int errorCode = CALENDAR_ERROR_NONE;
1107         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.summary, pConvertedSubject.get());
1108
1109         return E_SUCCESS;
1110 }
1111
1112 result
1113 _CalEventImpl::SetDescription(const String& description)
1114 {
1115         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1116         {
1117                 SysTryReturnResult(NID_SCL, description.GetLength() <= MAX_EVENT_DESCRIPTION_LENGTH, E_INVALID_ARG
1118                                 , "The length of the value exceeds MAX_EVENT_DESCRIPTION_LENGTH.");
1119         }
1120
1121         std::unique_ptr<char[]> pConvertedDescription(_StringConverter::CopyToCharArrayN(description));
1122         SysTryReturnResult(NID_SCL, pConvertedDescription != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1123
1124         int errorCode = CALENDAR_ERROR_NONE;
1125         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.description, pConvertedDescription.get());
1126
1127         return E_SUCCESS;
1128 }
1129
1130 result
1131 _CalEventImpl::SetLocation(const String& location)
1132 {
1133         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1134         {
1135                 SysTryReturnResult(NID_SCL, location.GetLength() <= MAX_EVENT_LOCATION_LENGTH, E_INVALID_ARG
1136                                 , "The length of the value exceeds MAX_EVENT_LOCATION_LENGTH.");
1137         }
1138
1139         std::unique_ptr<char[]> pConvertedLocation(_StringConverter::CopyToCharArrayN(location));
1140         SysTryReturnResult(NID_SCL, pConvertedLocation != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1141
1142         int errorCode = CALENDAR_ERROR_NONE;
1143         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.location, pConvertedLocation.get());
1144
1145         return E_SUCCESS;
1146 }
1147
1148 result
1149 _CalEventImpl::SetStartAndEndTime(const DateTime& start, const DateTime& end)
1150 {
1151         int errorCode = CALENDAR_ERROR_NONE;
1152         int recurrenceFrequency = 0;
1153
1154         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.freq, &recurrenceFrequency);
1155
1156         SysTryReturnResult(NID_SCL, recurrenceFrequency == CALENDAR_RECURRENCE_NONE, E_INVALID_CONDITION
1157                         , "The recurrence date is already set. Cannot modify the start and end date/time.");
1158         SysTryReturnResult(NID_SCL, _CalendarbookUtil::CheckValidDateTime(start), E_INVALID_ARG, "Invalid argument is used. start = %S", start.ToString().GetPointer());
1159         SysTryReturnResult(NID_SCL, _CalendarbookUtil::CheckValidDateTime(end), E_INVALID_ARG, "Invalid argument is used. end = %S", end.ToString().GetPointer());
1160         SysTryReturnResult(NID_SCL, start <= end, E_INVALID_ARG, "Invalid argument is used. The end date is earlier than the start date.");
1161
1162         return SetStartAndEndTimeCommon(start, end);
1163 }
1164
1165 result
1166 _CalEventImpl::SetStartAndEndTimeCommon(const DateTime& start, const DateTime& end)
1167 {
1168         int errorCode = CALENDAR_ERROR_NONE;
1169         calendar_time_s startCalendarTime;
1170         calendar_time_s endCalendarTime;
1171
1172         errorCode = calendar_record_get_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, &startCalendarTime);
1173
1174         if (startCalendarTime.type == CALENDAR_TIME_LOCALTIME)
1175         {
1176                 startCalendarTime.time.date.year = start.GetYear();
1177                 startCalendarTime.time.date.month = start.GetMonth();
1178                 startCalendarTime.time.date.mday = start.GetDay();
1179
1180                 endCalendarTime.type = CALENDAR_TIME_LOCALTIME;
1181                 endCalendarTime.time.date.year = end.GetYear();
1182                 endCalendarTime.time.date.month = end.GetMonth();
1183                 endCalendarTime.time.date.mday = end.GetDay();
1184         }
1185         else
1186         {
1187                 startCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(start);
1188
1189                 endCalendarTime.type = CALENDAR_TIME_UTIME;
1190                 endCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(end);
1191         }
1192
1193         errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.start_time, startCalendarTime);
1194         errorCode = calendar_record_set_caltime(__eventRecord.GetHandle(), _calendar_event.end_time, endCalendarTime);
1195
1196         return E_SUCCESS;
1197 }
1198
1199 void
1200 _CalEventImpl::SetCategory(EventCategory category)
1201 {
1202         int errorCode = CALENDAR_ERROR_NONE;
1203         std::unique_ptr<char[]> pConvertedCategory;
1204
1205         if (category == EVENT_CATEGORY_ANNIVERSARY)
1206         {
1207                 SetAllDayEvent(true);
1208
1209                 pConvertedCategory.reset(_StringConverter::CopyToCharArrayN(_EVENT_CATEGORY_ANNIVERSARY_STRING));
1210                 SysTryReturnVoidResult(NID_SCL, pConvertedCategory != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1211         }
1212         else
1213         {
1214                 pConvertedCategory.reset(_StringConverter::CopyToCharArrayN(_EVENT_CATEGORY_APPOINTMENT_STRING));
1215                 SysTryReturnVoidResult(NID_SCL, pConvertedCategory != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1216         }
1217
1218         errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.categories, pConvertedCategory.get());
1219 }
1220
1221 void
1222 _CalEventImpl::SetSensitivity(RecordSensitivity sensitivity)
1223 {
1224         int errorCode = CALENDAR_ERROR_NONE;
1225         errorCode = calendar_record_set_int(__eventRecord.GetHandle(), _calendar_event.sensitivity, _CalendarbookUtil::ConvertSensitivityToCSSensitivity(sensitivity));
1226 }
1227
1228 result
1229 _CalEventImpl::SetCoordinates(double latitude, double longitude)
1230 {
1231         SysTryReturnResult(NID_SCL, latitude >= _MIN_LATITUDE && latitude <= _MAX_LATITUDE, E_INVALID_ARG, "Invalid argument is used. The latitude is out of range.");
1232         SysTryReturnResult(NID_SCL, longitude >= _MIN_LONGITUDE && longitude <= _MAX_LONGITUDE, E_INVALID_ARG, "Invalid argument is used. The longitude is out of range.");
1233
1234         int errorCode = CALENDAR_ERROR_NONE;
1235
1236         errorCode = calendar_record_set_double(__eventRecord.GetHandle(), _calendar_event.latitude, latitude);
1237         errorCode = calendar_record_set_double(__eventRecord.GetHandle(), _calendar_event.longitude, longitude);
1238
1239         return E_SUCCESS;
1240 }
1241
1242 void
1243 _CalEventImpl::GetCoordinates(double& latitude, double& longitude) const
1244 {
1245         int errorCode = CALENDAR_ERROR_NONE;
1246
1247         errorCode = calendar_record_get_double(__eventRecord.GetHandle(), _calendar_event.latitude, &latitude);
1248         errorCode = calendar_record_get_double(__eventRecord.GetHandle(), _calendar_event.longitude, &longitude);
1249 }
1250
1251 result
1252 _CalEventImpl::SetReminder(const Reminder* pReminder)
1253 {
1254         int errorCode = CALENDAR_ERROR_NONE;
1255
1256         unsigned int reminderCount = 0;
1257         calendar_record_h tmpAlarmHandle = null;
1258
1259         errorCode = calendar_record_get_child_record_count(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, &reminderCount);
1260
1261         if (pReminder != null)
1262         {
1263                 int convertedTimeUnit = 0;
1264                 switch (pReminder->GetTimeUnit())
1265                 {
1266                 case REMINDER_TIME_UNIT_MINUTE:
1267                         convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
1268                         break;
1269                 case REMINDER_TIME_UNIT_HOUR:
1270                         convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_HOUR;
1271                         break;
1272                 case REMINDER_TIME_UNIT_DAY:
1273                         convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_DAY;
1274                         break;
1275                 case REMINDER_TIME_UNIT_WEEK:
1276                         convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_WEEK;
1277                         break;
1278                 default:
1279                         break;
1280                 }
1281
1282                 calendar_record_h tmpAlarmHandle = null;
1283
1284                 if (reminderCount > 0)
1285                 {
1286                         errorCode = calendar_record_get_child_record_at_p(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, 0, &tmpAlarmHandle);
1287                         errorCode = calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick_unit, convertedTimeUnit);
1288                         errorCode = calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick, pReminder->GetTimeOffset());
1289
1290                         std::unique_ptr<char[]> pTmpAlarmTone(_StringConverter::CopyToCharArrayN(pReminder->GetSoundFile()));
1291                         SysTryReturnResult(NID_SCL, pTmpAlarmTone != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1292
1293                         errorCode = calendar_record_set_str(tmpAlarmHandle, _calendar_alarm.tone, pTmpAlarmTone.get());
1294                 }
1295                 else
1296                 {
1297                         std::unique_ptr<char[]> pTmpAlarmTone(_StringConverter::CopyToCharArrayN(pReminder->GetSoundFile()));
1298                         SysTryReturnResult(NID_SCL, pTmpAlarmTone != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1299
1300                         errorCode = calendar_record_create(_calendar_alarm._uri, &tmpAlarmHandle);
1301                         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1302
1303                         errorCode = calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick_unit, convertedTimeUnit);
1304                         errorCode = calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick, pReminder->GetTimeOffset());
1305                         errorCode = calendar_record_set_str(tmpAlarmHandle, _calendar_alarm.tone, pTmpAlarmTone.get());
1306
1307                         errorCode = calendar_record_add_child_record(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, tmpAlarmHandle);
1308                 }
1309         }
1310         else
1311         {
1312                 if (reminderCount > 0)
1313                 {
1314                         errorCode = calendar_record_get_child_record_at_p(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, 0, &tmpAlarmHandle);
1315                         errorCode = calendar_record_remove_child_record(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, tmpAlarmHandle);
1316                 }
1317         }
1318
1319         __reminderList.RemoveAll(true);
1320         __reminderListUpdated = false;
1321
1322         return E_SUCCESS;
1323 }
1324
1325 result
1326 _CalEventImpl::SetRecurrence(const Recurrence* pRecurrence)
1327 {
1328         result r = E_SUCCESS;
1329         int errorCode = CALENDAR_ERROR_NONE;
1330
1331         if (pRecurrence != null)
1332         {
1333                 r = VerifyRecurrence(*pRecurrence);
1334                 SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1335
1336                 r = ResetStartAndEndTimeByRecurrence(*pRecurrence);
1337                 SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1338
1339                 r = ConvertRecurrenceToEventHandle(*pRecurrence, __eventRecord.GetHandle());
1340                 SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1341
1342                 std::unique_ptr<char[]> pExDates(_StringConverter::CopyToCharArrayN(ConvertRecurrenceToRRuleExDateString(*pRecurrence, IsAllDayEvent())));
1343                 errorCode = calendar_record_set_str(__eventRecord.GetHandle(), _calendar_event.exdate, pExDates.get());
1344
1345                 __pRecurrence.reset(new (std::nothrow) Recurrence(*pRecurrence));
1346                 SysTryReturnResult(NID_SCL, __pRecurrence != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1347         }
1348         else
1349         {
1350                 int errorCode = CALENDAR_ERROR_NONE;
1351                 errorCode = calendar_record_set_int(__eventRecord.GetHandle(), _calendar_event.freq, CALENDAR_RECURRENCE_NONE);
1352
1353                 __pRecurrence.reset(null);
1354         }
1355
1356         return E_SUCCESS;
1357 }
1358
1359 result
1360 _CalEventImpl::AddReminder(const Reminder& reminder)
1361 {
1362         int errorCode = CALENDAR_ERROR_NONE;
1363         calendar_record_h tmpAlarmHandle = null;
1364
1365         std::unique_ptr<char[]> pTmpAlarmTone(_StringConverter::CopyToCharArrayN(reminder.GetSoundFile()));
1366         SysTryReturnResult(NID_SCL, pTmpAlarmTone != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1367
1368         errorCode = calendar_record_create(_calendar_alarm._uri, &tmpAlarmHandle);
1369         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1370
1371         int convertedTimeUnit = 0;
1372
1373         ReminderTimeUnit timeUnit = reminder.GetTimeUnit();
1374         switch (timeUnit)
1375         {
1376         case REMINDER_TIME_UNIT_MINUTE:
1377                 convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
1378                 break;
1379         case REMINDER_TIME_UNIT_HOUR:
1380                 convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_HOUR;
1381                 break;
1382         case REMINDER_TIME_UNIT_DAY:
1383                 convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_DAY;
1384                 break;
1385         case REMINDER_TIME_UNIT_WEEK:
1386                 convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_WEEK;
1387                 break;
1388         case REMINDER_TIME_UNIT_NONE:
1389                 convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_SPECIFIC;
1390                 break;
1391         default:
1392                 break;
1393         }
1394
1395         errorCode = calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick_unit, convertedTimeUnit);
1396         errorCode = calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick, reminder.GetTimeOffset());
1397         errorCode = calendar_record_set_str(tmpAlarmHandle, _calendar_alarm.tone, pTmpAlarmTone.get());
1398
1399         if (convertedTimeUnit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC)
1400         {
1401                 errorCode = calendar_record_set_lli(tmpAlarmHandle, _calendar_alarm.time, _CalendarbookUtil::ConvertDateTimeToEpochTime(reminder.GetAbsoluteTime()));
1402         }
1403
1404         errorCode = calendar_record_add_child_record(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, tmpAlarmHandle);
1405
1406         __reminderList.RemoveAll(true);
1407         __reminderListUpdated = false;
1408
1409         return E_SUCCESS;
1410 }
1411
1412 result
1413 _CalEventImpl::RemoveReminderAt(int index)
1414 {
1415         int errorCode = CALENDAR_ERROR_NONE;
1416         calendar_record_h tmpAlarmHandle = null;
1417
1418         errorCode = calendar_record_get_child_record_at_p(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, index, &tmpAlarmHandle);
1419         SysTryReturnResult(NID_SCL, tmpAlarmHandle != null, E_OUT_OF_RANGE, "The index is out of range. index = %d", index);
1420
1421         errorCode = calendar_record_remove_child_record(__eventRecord.GetHandle(), _calendar_event.calendar_alarm, tmpAlarmHandle);
1422
1423         __reminderList.RemoveAll(true);
1424         __reminderListUpdated = false;
1425
1426         return E_SUCCESS;
1427 }
1428
1429 const IList&
1430 _CalEventImpl::GetAllReminders(void) const
1431 {
1432         ClearLastResult();
1433
1434         if (__reminderListUpdated == false)
1435         {
1436                 result r = _CalendarbookUtil::ConvertEventAlarmsToReminderList(__eventRecord.GetHandle(), __reminderList);
1437                 SysTryReturn(NID_SCL, r == E_SUCCESS, __reminderList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1438
1439                 __reminderListUpdated = true;
1440         }
1441
1442         return __reminderList;
1443 }
1444
1445 result
1446 _CalEventImpl::ConvertRecurrenceToEventHandle(const Recurrence& recurrence, calendar_record_h eventHandle) const
1447 {
1448         result r = E_SUCCESS;
1449
1450         int errorCode = CALENDAR_ERROR_NONE;
1451         int weekStart = CALENDAR_SUNDAY;
1452         std::unique_ptr<char[]> pByDay;
1453         std::unique_ptr<char[]> pByMonth;
1454         std::unique_ptr<char[]> pByMonthDay;
1455
1456         int dayOfWeek = 0;
1457         String dayOfWeekString;
1458         String exDateString;
1459
1460         switch (recurrence.GetWeekStart())
1461         {
1462         case CAL_SUNDAY:
1463                 weekStart = CALENDAR_SUNDAY;
1464                 break;
1465         case CAL_MONDAY:
1466                 weekStart = CALENDAR_MONDAY;
1467                 break;
1468         default:
1469                 SysLogException(NID_SCL, E_INVALID_ARG, "Invalid argument is passed. week start = %d", recurrence.GetWeekStart());
1470                 return E_INVALID_ARG;
1471         }
1472         errorCode = calendar_record_set_int(eventHandle, _calendar_event.wkst, weekStart);
1473         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_INVALID_ARG, "Invalid argument is passed. week start = %d", weekStart);
1474
1475         if (recurrence.GetUntil() != null)
1476         {
1477                 if (*recurrence.GetUntil() == DateTime::GetMaxValue())
1478                 {
1479                         errorCode = calendar_record_set_int(eventHandle, _calendar_event.range_type, CALENDAR_RANGE_NONE);
1480                 }
1481                 else
1482                 {
1483                         calendar_time_s startCalendarTime;
1484                         calendar_time_s untilCalendarTime;
1485
1486                         errorCode = calendar_record_get_caltime(eventHandle, _calendar_event.start_time, &startCalendarTime);
1487                         if (startCalendarTime.type == CALENDAR_TIME_LOCALTIME)
1488                         {
1489                                 untilCalendarTime.type = CALENDAR_TIME_LOCALTIME;
1490                                 untilCalendarTime.time.date.year = recurrence.GetUntil()->GetYear();
1491                                 untilCalendarTime.time.date.month = recurrence.GetUntil()->GetMonth();
1492                                 untilCalendarTime.time.date.mday = recurrence.GetUntil()->GetDay();
1493                         }
1494                         else
1495                         {
1496                                 untilCalendarTime.type = CALENDAR_TIME_UTIME;
1497                                 untilCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(*recurrence.GetUntil());
1498                         }
1499
1500                         errorCode = calendar_record_set_int(eventHandle, _calendar_event.range_type, CALENDAR_RANGE_UNTIL);
1501                         errorCode = calendar_record_set_caltime(eventHandle, _calendar_event.until_time, untilCalendarTime);
1502                 }
1503         }
1504         else
1505         {
1506                 errorCode = calendar_record_set_int(eventHandle, _calendar_event.range_type, CALENDAR_RANGE_COUNT);
1507                 errorCode = calendar_record_set_int(eventHandle, _calendar_event.count, recurrence.GetCounts());
1508         }
1509
1510         errorCode = calendar_record_set_int(eventHandle, _calendar_event.interval, recurrence.GetInterval());
1511
1512         switch (recurrence.GetFrequency())
1513         {
1514         case FREQ_DAILY:
1515                 errorCode = calendar_record_set_int(eventHandle, _calendar_event.freq, CALENDAR_RECURRENCE_DAILY);
1516                 break;
1517         case FREQ_WEEKLY:
1518                 errorCode = calendar_record_set_int(eventHandle, _calendar_event.freq, CALENDAR_RECURRENCE_WEEKLY);
1519
1520                 dayOfWeek = recurrence.GetDayOfWeek();
1521                 r = ConvertDayOfWeekToRRuleByDayString(dayOfWeek, 0, dayOfWeekString);
1522                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. day of week = %d", dayOfWeek);
1523
1524                 pByDay.reset(_StringConverter::CopyToCharArrayN(dayOfWeekString));
1525
1526                 break;
1527         case FREQ_MONTHLY:
1528                 errorCode = calendar_record_set_int(eventHandle, _calendar_event.freq, CALENDAR_RECURRENCE_MONTHLY);
1529
1530                 if (recurrence.GetDayOfMonth() != 0)
1531                 {
1532                         pByMonthDay.reset(_StringConverter::CopyToCharArrayN(Integer::ToString(recurrence.GetDayOfMonth())));
1533                 }
1534                 else
1535                 {
1536                         dayOfWeek = recurrence.GetDayOfWeek();
1537                         r = ConvertDayOfWeekToRRuleByDayString(dayOfWeek, recurrence.GetWeekOfMonth(), dayOfWeekString);
1538                         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. day of week = %d", dayOfWeek);
1539
1540                         pByDay.reset(_StringConverter::CopyToCharArrayN(dayOfWeekString));
1541                 }
1542
1543                 break;
1544         case FREQ_YEARLY:
1545                 errorCode = calendar_record_set_int(eventHandle, _calendar_event.freq, CALENDAR_RECURRENCE_YEARLY);
1546
1547                 pByMonth.reset(_StringConverter::CopyToCharArrayN(Integer::ToString(recurrence.GetMonthOfYear())));
1548
1549                 if (recurrence.GetDayOfMonth() != 0)
1550                 {
1551                         pByMonthDay.reset(_StringConverter::CopyToCharArrayN(Integer::ToString(recurrence.GetDayOfMonth())));
1552                 }
1553                 else
1554                 {
1555                         dayOfWeek = recurrence.GetDayOfWeek();
1556                         r = ConvertDayOfWeekToRRuleByDayString(dayOfWeek, recurrence.GetWeekOfMonth(), dayOfWeekString);
1557                         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. day of week = %d", dayOfWeek);
1558
1559                         pByDay.reset(_StringConverter::CopyToCharArrayN(dayOfWeekString));
1560                 }
1561
1562                 break;
1563         default:
1564                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. frequency = %d", recurrence.GetFrequency());
1565                 return E_INVALID_ARG;
1566         }
1567
1568         errorCode = calendar_record_set_str(eventHandle, _calendar_event.byday, pByDay.get());
1569         errorCode = calendar_record_set_str(eventHandle, _calendar_event.bymonth, pByMonth.get());
1570         errorCode = calendar_record_set_str(eventHandle, _calendar_event.bymonthday, pByMonthDay.get());
1571
1572         return E_SUCCESS;
1573 }
1574
1575 result
1576 _CalEventImpl::ConvertDayOfWeekToRRuleByDayString(int dayOfWeek, int weekOfMonth, String& byDayString) const
1577 {
1578         int tmpDayOfWeek = CAL_SUNDAY;
1579
1580         byDayString.Clear();
1581         for (int i = 0; i < _NUMBER_OF_DAYS_OF_WEEK; i++)
1582         {
1583                 if (dayOfWeek & tmpDayOfWeek)
1584                 {
1585                         if (weekOfMonth != 0)
1586                         {
1587                                 byDayString.Append(weekOfMonth);
1588                         }
1589
1590                         switch (tmpDayOfWeek)
1591                         {
1592                         case CAL_SUNDAY:
1593                                 byDayString.Append(_RECURRENCE_KEYWORD_SUNDAY);
1594                                 break;
1595                         case CAL_MONDAY:
1596                                 byDayString.Append(_RECURRENCE_KEYWORD_MONDAY);
1597                                 break;
1598                         case CAL_TUESDAY:
1599                                 byDayString.Append(_RECURRENCE_KEYWORD_TUESDAY);
1600                                 break;
1601                         case CAL_WEDNESDAY:
1602                                 byDayString.Append(_RECURRENCE_KEYWORD_WEDNESDAY);
1603                                 break;
1604                         case CAL_THURSDAY:
1605                                 byDayString.Append(_RECURRENCE_KEYWORD_THURSDAY);
1606                                 break;
1607                         case CAL_FRIDAY:
1608                                 byDayString.Append(_RECURRENCE_KEYWORD_FRIDAY);
1609                                 break;
1610                         case CAL_SATURDAY:
1611                                 byDayString.Append(_RECURRENCE_KEYWORD_SATURDAY);
1612                                 break;
1613                         }
1614                         byDayString.Append(_RECURRENCE_DELIMITER);
1615                 }
1616
1617                 tmpDayOfWeek <<= 1;
1618         }
1619
1620         byDayString.Remove(byDayString.GetLength() - 1, _RECURRENCE_DELIMITER_LENGTH);
1621
1622         return E_SUCCESS;
1623 }
1624
1625 RecordId
1626 _CalEventImpl::GetCalendarId(void) const
1627 {
1628         int errorCode = CALENDAR_ERROR_NONE;
1629         int srcCalendarbookId = 0;
1630
1631         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.calendar_book_id, &srcCalendarbookId);
1632
1633         return srcCalendarbookId;
1634 }
1635
1636 RecordId
1637 _CalEventImpl::GetBaseEventId(void) const
1638 {
1639         int errorCode = CALENDAR_ERROR_NONE;
1640         int srcBaseEventId = INVALID_RECORD_ID;
1641
1642         errorCode = calendar_record_get_int(__eventRecord.GetHandle(), _calendar_event.original_event_id, &srcBaseEventId);
1643
1644         return srcBaseEventId;
1645 }
1646
1647 void
1648 _CalEventImpl::SetOriginalCalEventId(RecordId originalEventId)
1649 {
1650         __originalEventId = originalEventId;
1651         __isInstance = true;
1652 }
1653
1654 int
1655 _CalEventImpl::GetAttendeeIndex(const char* pAttendeeEmail)
1656 {
1657         int index = _INVALID_ATTENDEE_INDEX;
1658         unsigned int attendeeCount = 0;
1659
1660         int errorCode = CALENDAR_ERROR_NONE;
1661         errorCode = calendar_record_get_child_record_count(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, &attendeeCount);
1662
1663         calendar_record_h tmpAttendeeHandle = null;
1664         char* pTmpAttendeeEmail = null;
1665         for (int i = 0; i < attendeeCount; i++)
1666         {
1667                 errorCode = calendar_record_get_child_record_at_p(__eventRecord.GetHandle(), _calendar_event.calendar_attendee, i, &tmpAttendeeHandle);
1668                 errorCode = calendar_record_get_str_p(tmpAttendeeHandle, _calendar_attendee.email, &pTmpAttendeeEmail);
1669
1670                 if (strcmp(pAttendeeEmail, pTmpAttendeeEmail) == 0)
1671                 {
1672                         index = i;
1673                         break;
1674                 }
1675         }
1676
1677         return index;
1678 }
1679
1680 result
1681 _CalEventImpl::VerifyRecurrence(const Recurrence& recurrence)
1682 {
1683         const DateTime* pUntil = recurrence.GetUntil();
1684         RecurFrequency frequency = recurrence.GetFrequency();
1685         int durationLimit = 0;
1686         int interval = recurrence.GetInterval();
1687
1688         DateTime startTime = GetStartTime();
1689         DateTime endTime = GetEndTime();
1690         EventCategory category = GetCategory();
1691
1692         if (pUntil != null)
1693         {
1694                 SysTryReturnResult(NID_SCL, startTime <= *pUntil, E_INVALID_CONDITION, "The until date of recurrence is earlier than start date.");
1695         }
1696
1697         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1698         {
1699                 if (category == EVENT_CATEGORY_ANNIVERSARY)
1700                 {
1701                         SysTryReturnResult(NID_SCL, frequency == FREQ_YEARLY, E_TYPE_MISMATCH
1702                                         , "The recurrence pattern is not a yearly pattern in case of the event being an Anniversary.");
1703                 }
1704         }
1705
1706         if (frequency == FREQ_DAILY)
1707         {
1708                 durationLimit = _TERM_LIMIT_DAILY * interval;
1709         }
1710         else if (frequency == FREQ_WEEKLY)
1711         {
1712                 SysTryReturnResult(NID_SCL, recurrence.GetDayOfWeek() != 0, E_INVALID_ARG, "Invalid argument is used. The day of week is not set.");
1713                 durationLimit = _TERM_LIMIT_WEEKLY * interval;
1714         }
1715         else if (frequency == FREQ_MONTHLY)
1716         {
1717                 SysTryReturnResult(NID_SCL, recurrence.GetDayOfMonth() != 0 || (recurrence.GetDayOfWeek() != 0 && recurrence.GetWeekOfMonth() != 0)
1718                                 , E_INVALID_ARG, "Invalid argument is used. The day of month or day of week/week of month is not set.");
1719                 durationLimit = _TERM_LIMIT_MONTHLY * interval;
1720         }
1721         else if (frequency == FREQ_YEARLY)
1722         {
1723                 int dayOfMonth = recurrence.GetDayOfMonth();
1724                 int monthOfYear = recurrence.GetMonthOfYear();
1725                 SysTryReturnResult(NID_SCL, monthOfYear != 0, E_INVALID_ARG, "Invalid argument is used. The month of year is not set.");
1726                 SysTryReturnResult(NID_SCL, dayOfMonth != 0 || (recurrence.GetDayOfWeek() != 0 && recurrence.GetWeekOfMonth() != 0)
1727                                 , E_INVALID_ARG, "Invalid argument is used. The day of month or day of week/week of month is not set.");
1728
1729                 SysTryReturnResult(NID_SCL, monthOfYear != 2 || dayOfMonth < 30
1730                                 , E_INVALID_ARG, "Invalid argument is used. If the frequency is yearly, the max days of the February is less than 30.");
1731                 if (dayOfMonth > 30)
1732                 {
1733                         SysTryReturnResult(NID_SCL, monthOfYear != 4 && monthOfYear != 6 && monthOfYear != 9 && monthOfYear != 11
1734                                         , E_INVALID_ARG, "Invalid argument is used. If the frequency is yearly, the max days of the April, June, September and November is less than 31.");
1735                 }
1736
1737                 durationLimit = _TERM_LIMIT_YEARLY * interval;
1738         }
1739
1740         TimeSpan duration = endTime.GetTime() - startTime.GetTime();
1741         SysTryReturnResult(NID_SCL, duration.GetDays() <= durationLimit, E_INVALID_CONDITION
1742                         , "The duration of the event is greater than (interval x frequency) days. duration days = %d", duration.GetDays());
1743
1744         return E_SUCCESS;
1745 }
1746
1747 result
1748 _CalEventImpl::ResetStartAndEndTimeByRecurrence(const Recurrence& recurrence)
1749 {
1750         result r = E_SUCCESS;
1751         DateTime startTime;
1752         DateTime endTime;
1753
1754         bool isAllDay = IsAllDayEvent();
1755         Locales::TimeZone timeZone;
1756
1757         if (!isAllDay)
1758         {
1759                 timeZone = GetTimeZone();
1760                 startTime = timeZone.UtcTimeToWallTime(GetStartTime());
1761                 endTime = timeZone.UtcTimeToWallTime(GetEndTime());
1762         }
1763         else
1764         {
1765                 startTime = GetStartTime();
1766                 endTime = GetEndTime();
1767         }
1768
1769         DateTime calculatedStartTime(startTime);
1770         DateTime calculatedEndTime(endTime);
1771         RecurFrequency frequency = recurrence.GetFrequency();
1772         int interval = recurrence.GetInterval();
1773         int dayOfMonth = recurrence.GetDayOfMonth();
1774         int dayOfWeek = recurrence.GetDayOfWeek();
1775         int weekOfMonth = recurrence.GetWeekOfMonth();
1776         int monthOfYear = recurrence.GetMonthOfYear();
1777         CalDayOfWeek weekStart = recurrence.GetWeekStart();
1778         CalDayOfWeek weekEnd = CAL_SATURDAY;
1779         DateTime until;
1780         TimeSpan duration = endTime.GetTime() - startTime.GetTime();
1781
1782         if (weekStart == CAL_MONDAY)
1783         {
1784                 weekEnd = CAL_SUNDAY;
1785         }
1786
1787         if (recurrence.GetUntil() != null)
1788         {
1789                 until = *(recurrence.GetUntil());
1790                 if (!isAllDay)
1791                 {
1792                         until = timeZone.UtcTimeToWallTime(until);
1793                 }
1794         }
1795         else
1796         {
1797                 until = _CalendarbookImpl::GetMaxDateTime();
1798         }
1799
1800         CalDayOfWeek dayOfStartTime = GetDayOfWeek(startTime, timeZone);
1801
1802         if (frequency == FREQ_WEEKLY)
1803         {
1804                 int count = 0;
1805
1806                 for (int i = 0; i < _NUMBER_OF_DAYS_OF_WEEK; i++)
1807                 {
1808                         if ((dayOfStartTime & dayOfWeek) != 0)
1809                         {
1810                                 break;
1811                         }
1812
1813                         if ((dayOfStartTime & weekEnd) != 0)
1814                         {
1815                                 count += (interval - 1) * _NUMBER_OF_DAYS_OF_WEEK;
1816                         }
1817
1818                         dayOfStartTime = GetNextDayOfWeek(dayOfStartTime);
1819                         count++;
1820                 }
1821
1822                 r = calculatedStartTime.AddDays(count);
1823                 SysTryReturnResult(NID_SCL, !IsFailed(r), r, "[%s][Propagating]", GetErrorMessage(r));
1824                 r = calculatedEndTime.AddDays(count);
1825                 SysTryReturnResult(NID_SCL, !IsFailed(r), r, "[%s][Propagating]", GetErrorMessage(r));
1826
1827         }
1828         else if (frequency == FREQ_MONTHLY)
1829         {
1830                 if (dayOfMonth != 0)
1831                 {
1832                         bool isDone = false;
1833                         while (calculatedStartTime <= until)
1834                         {
1835                                 if (calculatedStartTime.SetValue(calculatedStartTime.GetYear(), calculatedStartTime.GetMonth(), dayOfMonth, calculatedStartTime.GetHour(), calculatedStartTime.GetMinute(), calculatedStartTime.GetSecond()) == E_SUCCESS)
1836                                 {
1837                                         if (calculatedStartTime >= startTime)
1838                                         {
1839                                                 isDone = true;
1840                                                 break;
1841                                         }
1842                                 }
1843                                 calculatedStartTime.AddMonths(interval);
1844                         }
1845                         SysTryReturnResult(NID_SCL, isDone, E_INVALID_ARG, "Invalid argument is used. There is no instance of the recurrence.");
1846                         calculatedEndTime = calculatedStartTime;
1847                         calculatedEndTime.Add(duration);
1848                 }
1849                 else
1850                 {
1851                         bool isDone = false;
1852                         int dayCount = 0;
1853                         CalDayOfWeek tmpDayOfWeek = GetFirstDay(weekStart, dayOfWeek, dayCount);
1854                         while (calculatedStartTime <= until)
1855                         {
1856                                 for (int i = 0; i < dayCount; i++)
1857                                 {
1858                                         calculatedStartTime = GetDate(calculatedStartTime.GetYear(), calculatedStartTime.GetMonth(), weekOfMonth, tmpDayOfWeek, calculatedStartTime, timeZone);
1859                                         if (calculatedStartTime >= startTime)
1860                                         {
1861                                                 isDone = true;
1862                                                 break;
1863                                         }
1864                                         tmpDayOfWeek = GetNextDay(dayOfWeek, tmpDayOfWeek);
1865                                 }
1866                                 if (isDone)
1867                                 {
1868                                         break;
1869                                 }
1870                                 calculatedStartTime.AddMonths(interval);
1871                         }
1872
1873                         SysTryReturnResult(NID_SCL, isDone, E_INVALID_ARG, "Invalid argument is used. There is no instance of the recurrence.");
1874                         calculatedEndTime = calculatedStartTime;
1875                         calculatedEndTime.Add(duration);
1876                 }
1877         }
1878         else if (frequency == FREQ_YEARLY)
1879         {
1880                 if (dayOfMonth != 0)
1881                 {
1882                         bool isDone = false;
1883                         while (calculatedStartTime <= until)
1884                         {
1885                                 if (calculatedStartTime.SetValue(calculatedStartTime.GetYear(), monthOfYear, dayOfMonth, calculatedStartTime.GetHour(), calculatedStartTime.GetMinute(), calculatedStartTime.GetSecond()) == E_SUCCESS)
1886                                 {
1887                                         if (calculatedStartTime >= startTime)
1888                                         {
1889                                                 isDone = true;
1890                                                 break;
1891                                         }
1892                                 }
1893                                 calculatedStartTime.AddYears(interval);
1894                         }
1895                         SysTryReturnResult(NID_SCL, isDone, E_INVALID_ARG, "Invalid argument is used. There is no instance of the recurrence.");
1896                         calculatedEndTime = calculatedStartTime;
1897                         calculatedEndTime.Add(duration);
1898                 }
1899                 else
1900                 {
1901                         bool isDone = false;
1902                         int dayCount = 0;
1903                         CalDayOfWeek tmpDayOfWeek = GetFirstDay(weekStart, dayOfWeek, dayCount);
1904                         while (calculatedStartTime <= until)
1905                         {
1906                                 for (int i = 0; i < dayCount; i++)
1907                                 {
1908                                         calculatedStartTime = GetDate(calculatedStartTime.GetYear(), monthOfYear, weekOfMonth, tmpDayOfWeek, calculatedStartTime, timeZone);
1909                                         if (calculatedStartTime >= startTime)
1910                                         {
1911                                                 isDone = true;
1912                                                 break;
1913                                         }
1914                                         tmpDayOfWeek = GetNextDay(dayOfWeek, tmpDayOfWeek);
1915                                 }
1916                                 if (isDone)
1917                                 {
1918                                         break;
1919                                 }
1920                                 calculatedStartTime.AddYears(interval);
1921                         }
1922
1923                         SysTryReturnResult(NID_SCL, isDone, E_INVALID_ARG, "Invalid argument is used. There is no instance of the recurrence.");
1924                         calculatedEndTime = calculatedStartTime;
1925                         calculatedEndTime.Add(duration);
1926                 }
1927         }
1928
1929         if (!isAllDay)
1930         {
1931                 calculatedStartTime = timeZone.WallTimeToUtcTime(calculatedStartTime);
1932                 calculatedEndTime = timeZone.WallTimeToUtcTime(calculatedEndTime);
1933         }
1934
1935         SysTryReturnResult(NID_SCL, calculatedStartTime <= until, E_INVALID_ARG, "Invalid argument is used. There is no instance of the recurrence.");
1936         SysTryReturnResult(NID_SCL, calculatedEndTime <= _CalendarbookImpl::GetMaxDateTime(), E_INVALID_ARG, "Invalid argument is used. There is no instance of the recurrence.");
1937
1938         SysLog(NID_SCL, "Reset start time : %S", calculatedStartTime.ToString().GetPointer());
1939         SetStartAndEndTimeCommon(calculatedStartTime, calculatedEndTime);
1940
1941         return E_SUCCESS;
1942 }
1943
1944 int
1945 _CalEventImpl::ConvertRRuleByDayStringToDayOfWeek(const String& byDay) const
1946 {
1947         result r = E_SUCCESS;
1948         int dayOfWeek = 0;
1949         ClearLastResult();
1950
1951         SysTryReturn(NID_SCL, !byDay.IsEmpty(), 0, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. The byDay is empty.");
1952
1953         String delim(_RECURRENCE_DELIMITER);
1954         String token;
1955
1956         StringTokenizer strTok(byDay, delim);
1957         while (strTok.HasMoreTokens())
1958         {
1959                 r = strTok.GetNextToken(token);
1960                 SysTryReturn(NID_SCL, r == E_SUCCESS, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1961
1962                 if (token == String(_RECURRENCE_KEYWORD_SUNDAY))
1963                 {
1964                         dayOfWeek |= CAL_SUNDAY;
1965                 }
1966                 else    if (token == String(_RECURRENCE_KEYWORD_MONDAY))
1967                 {
1968                         dayOfWeek |= CAL_MONDAY;
1969                 }
1970                 else    if (token == String(_RECURRENCE_KEYWORD_TUESDAY))
1971                 {
1972                         dayOfWeek |= CAL_TUESDAY;
1973                 }
1974                 else    if (token == String(_RECURRENCE_KEYWORD_WEDNESDAY))
1975                 {
1976                         dayOfWeek |= CAL_WEDNESDAY;
1977                 }
1978                 else    if (token == String(_RECURRENCE_KEYWORD_THURSDAY))
1979                 {
1980                         dayOfWeek |= CAL_THURSDAY;
1981                 }
1982                 else    if (token == String(_RECURRENCE_KEYWORD_FRIDAY))
1983                 {
1984                         dayOfWeek |= CAL_FRIDAY;
1985                 }
1986                 else    if (token == String(_RECURRENCE_KEYWORD_SATURDAY))
1987                 {
1988                         dayOfWeek |= CAL_SATURDAY;
1989                 }
1990                 else
1991                 {
1992                         SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. byday = %S", byDay.GetPointer());
1993                         return 0;
1994                 }
1995         }
1996
1997         return dayOfWeek;
1998 }
1999
2000 result
2001 _CalEventImpl::ConvertRRuleByDayStringToDayOfWeekAndWeekOfMonth(const String& byDay, int& weekOfMonth, int& dayOfWeek) const
2002 {
2003         result r = E_SUCCESS;
2004         String delim(_RECURRENCE_DELIMITER);
2005         String token;
2006         wchar_t tmpChar = 0;
2007         int tmpWeekOfMonth = 0;
2008         int weekStringStartIndex = 0;
2009         String tmpString;
2010
2011         weekOfMonth = 0;
2012         dayOfWeek = 0;
2013
2014         SysTryReturnResult(NID_SCL, !byDay.IsEmpty(), E_INVALID_ARG, "Invalid argument is passed. The byDay is empty.");
2015
2016         StringTokenizer strTok(byDay, delim);
2017
2018         while (strTok.HasMoreTokens())
2019         {
2020                 r = strTok.GetNextToken(token);
2021                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
2022
2023                 r = token.GetCharAt(_RECURRENCE_BY_DAY_FIRST_INDEX, tmpChar);
2024                 if (tmpChar == _RECURRENCE_BY_DAY_CHAR_MINUS)
2025                 {
2026                         SysTryReturnResult(NID_SCL, weekOfMonth == 0 || weekOfMonth == _MAX_WEEK_OF_MONTH
2027                                         , E_INVALID_ARG, "Invalid argument is passed. byday = %S", byDay.GetPointer());
2028
2029                         if (weekOfMonth == 0)
2030                         {
2031                                 tmpChar = 0;
2032                                 r = token.GetCharAt(_RECURRENCE_BY_DAY_SECOND_INDEX, tmpChar);
2033                                 SysTryReturnResult(NID_SCL, tmpChar == _RECURRENCE_BY_DAY_CHAR_ONE
2034                                                 , E_INVALID_ARG, "Invalid argument is passed. byday = %S", byDay.GetPointer());
2035
2036                                 weekOfMonth = _MAX_WEEK_OF_MONTH;
2037                         }
2038                         weekStringStartIndex = _RECURRENCE_BY_DAY_SECOND_INDEX + 1;
2039                 }
2040                 else if (tmpChar == _RECURRENCE_BY_DAY_CHAR_PLUS)
2041                 {
2042                         if (weekOfMonth == 0)
2043                         {
2044                                 tmpChar = 0;
2045                                 r = token.GetCharAt(_RECURRENCE_BY_DAY_SECOND_INDEX, tmpChar);
2046                                 tmpWeekOfMonth = Character::ToDigit(tmpChar, Character::RADIX_DECIMAL);
2047                                 SysTryReturnResult(NID_SCL, tmpWeekOfMonth > 0 && tmpWeekOfMonth <= _MAX_WEEK_OF_MONTH
2048                                                 , E_INVALID_ARG, "Invalid argument is passed. byday = %S", byDay.GetPointer());
2049
2050                                 weekOfMonth = tmpWeekOfMonth;
2051                         }
2052                         else
2053                         {
2054                                 SysTryReturnResult(NID_SCL, weekOfMonth == tmpWeekOfMonth
2055                                                 , E_INVALID_ARG, "Invalid argument is passed. byday = %S", byDay.GetPointer());
2056                         }
2057
2058                         weekStringStartIndex = _RECURRENCE_BY_DAY_SECOND_INDEX + 1;
2059                 }
2060                 else
2061                 {
2062                         if (weekOfMonth == 0)
2063                         {
2064                                 tmpWeekOfMonth = Character::ToDigit(tmpChar, Character::RADIX_DECIMAL);
2065                                 SysTryReturnResult(NID_SCL, tmpWeekOfMonth > 0 && tmpWeekOfMonth <= _MAX_WEEK_OF_MONTH
2066                                                 , E_INVALID_ARG, "Invalid argument is passed. byday = %S", byDay.GetPointer());
2067
2068                                 weekOfMonth = tmpWeekOfMonth;
2069                         }
2070                         else
2071                         {
2072                                 SysTryReturnResult(NID_SCL, weekOfMonth == tmpWeekOfMonth
2073                                                 , E_INVALID_ARG, "Invalid argument is passed. byday = %S", byDay.GetPointer());
2074                         }
2075
2076                         weekStringStartIndex = _RECURRENCE_BY_DAY_FIRST_INDEX + 1;
2077                 }
2078
2079                 token.SubString(weekStringStartIndex, tmpString);
2080
2081                 if (tmpString == String(_RECURRENCE_KEYWORD_SUNDAY))
2082                 {
2083                         dayOfWeek |= CAL_SUNDAY;
2084                 }
2085                 else    if (tmpString == String(_RECURRENCE_KEYWORD_MONDAY))
2086                 {
2087                         dayOfWeek |= CAL_MONDAY;
2088                 }
2089                 else    if (tmpString == String(_RECURRENCE_KEYWORD_TUESDAY))
2090                 {
2091                         dayOfWeek |= CAL_TUESDAY;
2092                 }
2093                 else    if (tmpString == String(_RECURRENCE_KEYWORD_WEDNESDAY))
2094                 {
2095                         dayOfWeek |= CAL_WEDNESDAY;
2096                 }
2097                 else    if (tmpString == String(_RECURRENCE_KEYWORD_THURSDAY))
2098                 {
2099                         dayOfWeek |= CAL_THURSDAY;
2100                 }
2101                 else    if (tmpString == String(_RECURRENCE_KEYWORD_FRIDAY))
2102                 {
2103                         dayOfWeek |= CAL_FRIDAY;
2104                 }
2105                 else    if (tmpString == String(_RECURRENCE_KEYWORD_SATURDAY))
2106                 {
2107                         dayOfWeek |= CAL_SATURDAY;
2108                 }
2109                 else
2110                 {
2111                         SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. byday = %S", byDay.GetPointer());
2112                 }
2113         }
2114
2115         return E_SUCCESS;
2116 }
2117
2118 result
2119 _CalEventImpl::ConvertRRuleExDateStringToRecurrence(const String& exdate, Recurrence& recurrence) const
2120 {
2121         result r = E_SUCCESS;
2122
2123         String delim(_RECURRENCE_DELIMITER);
2124         String token;
2125         DateTime tmpDateTime;
2126         TimeZone tmpTimeZone;
2127         TimeZone utcTimeZone = TimeZone::GetGmtTimeZone();
2128         bool isDate = false;
2129
2130         SysTryReturnResult(NID_SCL, !exdate.IsEmpty(), E_INVALID_ARG, "Invalid argument is passed. The exdate is empty.");
2131
2132         StringTokenizer strTok(exdate, delim);
2133         while (strTok.HasMoreTokens())
2134         {
2135                 r = strTok.GetNextToken(token);
2136                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
2137
2138                 r = _CalendarbookUtil::ConvertRRuleDateTimeStringToDateTime(token, tmpDateTime, tmpTimeZone, isDate);
2139                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. exdate = %S", exdate.GetPointer());
2140
2141                 if (!isDate && tmpTimeZone != utcTimeZone)
2142                 {
2143                         tmpDateTime = tmpTimeZone.WallTimeToUtcTime(tmpDateTime);
2144                 }
2145
2146                 r = recurrence.AddExceptionDate(tmpDateTime);
2147                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. exdate = %S", exdate.GetPointer());
2148         }
2149
2150         return E_SUCCESS;
2151 }
2152
2153 String
2154 _CalEventImpl::ConvertRecurrenceToRRuleExDateString(const Recurrence& recurrence, bool isDate) const
2155 {
2156         result r = E_SUCCESS;
2157
2158         bool isNotFirst = false;
2159         String exdateString;
2160
2161         std::unique_ptr<IList, AllElementsDeleter> pExDateList(recurrence.GetExceptionDatesN());
2162
2163         IEnumerator* pEnum = pExDateList->GetEnumeratorN();
2164         while (pEnum->MoveNext() == E_SUCCESS)
2165         {
2166                 DateTime* pDateTime = static_cast<DateTime*>(pEnum->GetCurrent());
2167
2168                 if (isNotFirst)
2169                 {
2170                         r = exdateString.Append(_RECURRENCE_DELIMITER);
2171                 }
2172
2173                 r = exdateString.Append(_CalendarbookUtil::ConvertDateTimeToRRuleDateTimeString(*pDateTime, isDate));
2174
2175                 isNotFirst = true;
2176         }
2177         delete pEnum;
2178
2179         return exdateString;
2180 }
2181
2182 CalDayOfWeek
2183 _CalEventImpl::GetNextDayOfWeek(CalDayOfWeek currentDay)
2184 {
2185         int tmpDay = currentDay;
2186
2187         tmpDay <<= 1;
2188         if (tmpDay > CAL_SATURDAY)
2189         {
2190                 tmpDay = CAL_SUNDAY;
2191         }
2192
2193         return static_cast<CalDayOfWeek>(tmpDay);
2194 }
2195
2196 DateTime
2197 _CalEventImpl::GetDate(int year, int month, int weekOfMonth, CalDayOfWeek dayOfWeek, const DateTime& time, const Locales::TimeZone& timeZone)
2198 {
2199         Tizen::Locales::DayOfWeek dayOfWeekByGregorianCalendar = Tizen::Locales::DAY_OF_WEEK_UNDEFINED;
2200
2201         switch (dayOfWeek)
2202         {
2203         case CAL_SUNDAY:
2204                 dayOfWeekByGregorianCalendar = Tizen::Locales::SUNDAY;
2205                 break;
2206         case CAL_MONDAY:
2207                 dayOfWeekByGregorianCalendar = Tizen::Locales::MONDAY;
2208                 break;
2209         case CAL_TUESDAY:
2210                 dayOfWeekByGregorianCalendar = Tizen::Locales::TUESDAY;
2211                 break;
2212         case CAL_WEDNESDAY:
2213                 dayOfWeekByGregorianCalendar = Tizen::Locales::WEDNESDAY;
2214                 break;
2215         case CAL_THURSDAY:
2216                 dayOfWeekByGregorianCalendar = Tizen::Locales::THURSDAY;
2217                 break;
2218         case CAL_FRIDAY:
2219                 dayOfWeekByGregorianCalendar = Tizen::Locales::FRIDAY;
2220                 break;
2221         case CAL_SATURDAY:
2222                 dayOfWeekByGregorianCalendar = Tizen::Locales::SATURDAY;
2223                 break;
2224         default:
2225                 break;
2226         }
2227
2228         Tizen::Locales::Calendar* pGregorianCalendar = Tizen::Locales::Calendar::CreateInstanceN(timeZone, Tizen::Locales::CALENDAR_GREGORIAN);
2229
2230         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_YEAR, year);
2231         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_MONTH, month);
2232         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_DAY_OF_MONTH, 1);
2233
2234         Tizen::Locales::DayOfWeek tmpDayOfWeek = static_cast<Tizen::Locales::DayOfWeek>(pGregorianCalendar->GetTimeField(TIME_FIELD_DAY_OF_WEEK));
2235         int maxDaysOfMonth = pGregorianCalendar->GetActualMaxTimeField(TIME_FIELD_DAY_OF_MONTH);
2236         int tmpDayOfMonth = ((_NUMBER_OF_DAYS_OF_WEEK + dayOfWeekByGregorianCalendar) - tmpDayOfWeek) % _NUMBER_OF_DAYS_OF_WEEK + 1;
2237
2238         tmpDayOfMonth += ((weekOfMonth - 1) * _NUMBER_OF_DAYS_OF_WEEK);
2239         if (tmpDayOfMonth > maxDaysOfMonth)
2240         {
2241                 tmpDayOfMonth -= _NUMBER_OF_DAYS_OF_WEEK;
2242         }
2243
2244         DateTime resultTime;
2245         resultTime.SetValue(year, month, tmpDayOfMonth, time.GetHour(), time.GetMinute(), time.GetSecond());
2246
2247         delete pGregorianCalendar;
2248
2249         return resultTime;
2250 }
2251
2252 CalDayOfWeek
2253 _CalEventImpl::GetDayOfWeek(const DateTime& date, const Locales::TimeZone& timeZone)
2254 {
2255         CalDayOfWeek dayOfWeek = CAL_SUNDAY;
2256
2257         std::unique_ptr<Tizen::Locales::Calendar> pGregorianCalendar(Tizen::Locales::Calendar::CreateInstanceN(timeZone, Tizen::Locales::CALENDAR_GREGORIAN));
2258
2259         pGregorianCalendar->SetTime(date);
2260         int dayOfWeekByGregorianCalendar = pGregorianCalendar->GetTimeField(Tizen::Locales::TIME_FIELD_DAY_OF_WEEK);
2261
2262         switch (dayOfWeekByGregorianCalendar)
2263         {
2264         case Tizen::Locales::SUNDAY:
2265                 dayOfWeek = CAL_SUNDAY;
2266                 break;
2267         case Tizen::Locales::MONDAY:
2268                 dayOfWeek = CAL_MONDAY;
2269                 break;
2270         case Tizen::Locales::TUESDAY:
2271                 dayOfWeek = CAL_TUESDAY;
2272                 break;
2273         case Tizen::Locales::WEDNESDAY:
2274                 dayOfWeek = CAL_WEDNESDAY;
2275                 break;
2276         case Tizen::Locales::THURSDAY:
2277                 dayOfWeek = CAL_THURSDAY;
2278                 break;
2279         case Tizen::Locales::FRIDAY:
2280                 dayOfWeek = CAL_FRIDAY;
2281                 break;
2282         case Tizen::Locales::SATURDAY:
2283                 dayOfWeek = CAL_SATURDAY;
2284                 break;
2285         default:
2286                 break;
2287         }
2288
2289         return dayOfWeek;
2290 }
2291
2292 CalDayOfWeek
2293 _CalEventImpl::GetFirstDay(CalDayOfWeek weekStart, int dayOfWeek, int& count)
2294 {
2295         int firstDay = 0;
2296         int tmpDay = weekStart;
2297
2298         count = 0;
2299         for (int i = 0; i < _NUMBER_OF_DAYS_OF_WEEK; i++)
2300         {
2301                 if ((tmpDay & dayOfWeek) != 0)
2302                 {
2303                         if (firstDay == 0)
2304                         {
2305                                 firstDay = tmpDay;
2306                         }
2307
2308                         count++;
2309                 }
2310
2311                 tmpDay = GetNextDayOfWeek(static_cast<CalDayOfWeek>(tmpDay));
2312         }
2313
2314         return static_cast<CalDayOfWeek>(firstDay);
2315 }
2316
2317 CalDayOfWeek
2318 _CalEventImpl::GetNextDay(int dayOfWeek, CalDayOfWeek currentDay)
2319 {
2320         int tmpDay = currentDay;
2321
2322         for (int i = 0; i < _NUMBER_OF_DAYS_OF_WEEK; i++)
2323         {
2324                 tmpDay = GetNextDayOfWeek(static_cast<CalDayOfWeek>(tmpDay));
2325
2326                 if ((tmpDay & dayOfWeek) != 0)
2327                 {
2328                         break;
2329                 }
2330         }
2331
2332         return static_cast<CalDayOfWeek>(tmpDay);
2333 }
2334
2335 void
2336 _CalEventImpl::SetRecordHandle(calendar_record_h eventHandle)
2337 {
2338         __eventRecord.ResetHandle(eventHandle);
2339 }
2340
2341 calendar_record_h
2342 _CalEventImpl::GetRecordHandle(void) const
2343 {
2344         return __eventRecord.GetHandle();
2345 }
2346
2347 CalEvent*
2348 _CalEventImpl::CreateDefaultInstanceN(void)
2349 {
2350         return new (std::nothrow) CalEvent();
2351 }
2352
2353 _CalEventImpl*
2354 _CalEventImpl::GetInstance(CalEvent& event)
2355 {
2356         return event.__pCalEventImpl;
2357 }
2358
2359 const _CalEventImpl*
2360 _CalEventImpl::GetInstance(const CalEvent& event)
2361 {
2362         return event.__pCalEventImpl;
2363 }
2364
2365 }} //Tizen::Social