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