Update change log and spec for wrt-plugins-tizen_0.2.84
[framework/web/wrt-plugins-tizen.git] / src / platform / Tizen / Calendar / EventWrapper.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #include <sstream>
19 #include <string.h>
20 #include <algorithm>
21 #include <pcrecpp.h>
22 #include <dpl/log/log.h>
23 #include <Commons/Exception.h>
24 #include "EventWrapper.h"
25 #include "Calendar.h"
26
27 using namespace TizenApis::Api::Calendar;
28 using namespace WrtDeviceApis::Commons;
29
30 namespace {
31 const char WEEKDAYS[] = "0111110";
32 const int NEW_EVENT_ID = -1;
33 }
34
35 namespace TizenApis {
36 namespace Platform {
37 namespace Calendar {
38
39 EventWrapper::EventWrapper(CalendarEvent::CalendarType type) :
40     m_platformEvent(NULL),
41     m_abstractEvent(NULL),
42     m_calendarType(type)
43 {
44     LogDebug("entered");
45     m_abstractEvent = CalendarEventPtr(new CalendarEvent());
46     if (!m_abstractEvent) {
47         ThrowMsg(UnknownException, "abstract object is not created");
48     }
49 }
50
51 EventWrapper::EventWrapper(const CalendarEventPtr &event, CalendarEvent::CalendarType type) :
52     m_platformEvent(NULL),
53     m_abstractEvent(event),
54     m_calendarType(type)
55 {
56     LogDebug("entered");
57 }
58
59 EventWrapper::EventWrapper(cal_struct *event, CalendarEvent::CalendarType type) :
60     m_platformEvent(event),
61     m_abstractEvent(NULL),
62     m_calendarType(type)
63 {
64     LogDebug("entered");
65     m_abstractEvent = CalendarEventPtr(new CalendarEvent());
66     if (!m_abstractEvent) {
67         ThrowMsg(UnknownException, "abstract object is not created");
68     }
69 }
70
71 EventWrapper::~EventWrapper()
72 {
73     LogDebug("entered");
74     freePlatformEvent();
75 }
76
77 int EventWrapper::getIdFromPlatformEvent() const
78 {
79     LogDebug("Entered");
80     if (m_platformEvent == NULL) {
81         ThrowMsg(NullPointerException, "m_platformEvent is not set");
82     }
83
84     return calendar_svc_struct_get_int(m_platformEvent, CAL_VALUE_INT_INDEX);
85 }
86
87 void EventWrapper::setCalendarId(const std::string &value)
88 {
89     LogInfo("calendar id "<<value);
90     m_calendarId = value;
91     std::istringstream stream(m_calendarId);
92     int calendarId = -1;
93     stream>>calendarId;
94     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
95                                                    CAL_VALUE_INT_CALENDAR_ID,
96                                                    calendarId))
97     {
98         ThrowMsg(PlatformException, "Can't set calendar id.");
99     }
100 }
101
102 void EventWrapper::setCalendarAccountId(const int value)
103 {
104     LogInfo("account id "<<value);
105     m_calendarAccountId = value;
106     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
107                                                    CAL_VALUE_INT_ACCOUNT_ID,
108                                                    m_calendarAccountId))
109     {
110         ThrowMsg(PlatformException, "Can't set calendar account id.");
111     }
112 }
113
114 void EventWrapper::saveEvent()
115 {
116     LogDebug("entered");
117     if (m_platformEvent == NULL) {
118         ThrowMsg(NullPointerException, "m_platformEvent is not set");
119     }
120
121     int eventId = getIdFromPlatformEvent();
122     LogDebug("Before saving/update eventId: " << eventId);
123
124     //displayPlatformEvent();
125
126     //insert new record or update existing one
127     if (eventId < 0) {
128         int returnValue = calendar_svc_insert(m_platformEvent);
129         if (CAL_SUCCESS > returnValue) {
130             LogError("Can't insert new event, error code: " << returnValue);
131             ThrowMsg(PlatformException, "Can't insert new event.");
132         }
133         m_abstractEvent->setId(returnValue);
134         LogInfo("New calendar item inserted with id "<<returnValue);
135     } else {
136         int errorCode = calendar_svc_update(m_platformEvent);
137         if (CAL_SUCCESS != errorCode) {
138             ThrowMsg(PlatformException, "Can't update new event with error code: "<<errorCode);
139         }
140         LogDebug("Calendar event updated");
141     }
142 }
143
144 void EventWrapper::loadEvent(int id)
145 {
146     LogDebug("Item id to load: " << id);
147     freePlatformEvent();
148     
149     const char *dataType;
150     if(getType() == CalendarEvent::TASK_TYPE) {
151         dataType = CAL_STRUCT_TODO;
152     } else {
153         dataType = CAL_STRUCT_SCHEDULE;
154     }
155
156     int errorCode = calendar_svc_get(dataType, id, NULL, &m_platformEvent);
157     if (CAL_SUCCESS!=errorCode) {
158         if (m_platformEvent) {
159             calendar_svc_struct_free(&m_platformEvent);
160         }
161         ThrowMsg(NotFoundException, "Can't get item with id = " << id << ", error code: " <<errorCode);
162     }
163
164     int isDeleted = calendar_svc_struct_get_int(m_platformEvent, CAL_VALUE_INT_IS_DELETED);
165     if (isDeleted) {
166         if (m_platformEvent) {
167             calendar_svc_struct_free(&m_platformEvent);
168         }
169         ThrowMsg(NotFoundException, "Item already deleted.");
170     }
171
172     convertPlatformEventToAbstractEvent();
173     m_abstractEvent->setCalendarType(m_calendarType);
174     //displayAbstractEvent();
175 }
176
177 void EventWrapper::deleteEvent()
178 {
179     if (m_platformEvent == NULL) {
180         ThrowMsg(NullPointerException, "Platform event is null.");
181     }
182
183     // If the recurrenceId is set, delete the instance of recurring event only.
184     int itemId = getIdFromPlatformEvent();
185     long long int recurrenceId = m_abstractEvent->getRecurrenceId();
186     LogDebug("itemId to delete: " << itemId << ", recurrenceId: " << recurrenceId << ", type: " << getType());
187
188     cal_struct *item = NULL;
189     const char *dataType;
190     if(getType() == CalendarEvent::TASK_TYPE) {
191         dataType = CAL_STRUCT_TODO;
192     } else {
193         dataType = CAL_STRUCT_SCHEDULE;
194     }
195
196     int error = calendar_svc_get(dataType, itemId, NULL, &item);
197     if (CAL_SUCCESS != error) {
198         if (item) {
199             calendar_svc_struct_free(&item);
200         }
201         ThrowMsg(NotFoundException, "Can't get calendar item. Error code: "<<error);
202     }
203
204     int isDeleted = calendar_svc_struct_get_int(item, CAL_VALUE_INT_IS_DELETED);
205     if (isDeleted) {
206         if (item) {
207             calendar_svc_struct_free(&item);
208         }
209         ThrowMsg(NotFoundException, "Item already deleted.");
210     }
211
212     if (item) {
213         calendar_svc_struct_free(&item);
214     }
215
216     if ( 0>=recurrenceId || 
217         !(EventRecurrenceRule::DAILY_RECURRENCE <= m_abstractEvent->getRecurrenceRule()->getFrequency() 
218         && m_abstractEvent->getRecurrenceRule()->getFrequency() <= EventRecurrenceRule::YEARLY_RECURRENCE)) {
219         // Remove the parent and child instances by the given uid.
220         int parentId = itemId;
221
222         // First, the parent event.
223         error = calendar_svc_delete(dataType, parentId);
224         if (CAL_SUCCESS != error) {
225             if(CAL_ERR_NO_DATA==error) {
226                 ThrowMsg(NotFoundException, "Item not found.");
227             } else {
228                 ThrowMsg(PlatformException, "Can't delete calendar item. Error code " << error);
229             }
230         }
231         m_abstractEvent->resetId();
232         setIdToPlatformEvent();
233
234         if(CalendarEvent::EVENT_TYPE==getType()) {
235             LogDebug("The event parent item is deleted.");
236
237             // Next, all the detached instances.
238             cal_iter *iter = NULL;
239             cal_struct *platformEvent = NULL;
240             if(CAL_SUCCESS != calendar_svc_find_event_list(m_calendarAccountId, CAL_VALUE_INT_ORIGINAL_EVENT_ID, (void*) parentId, &iter)) {
241                 LogInfo("Founding detached instances failed.");
242             }
243
244             while (CAL_SUCCESS == calendar_svc_iter_next(iter)) {
245                 LogInfo("Deleting a detached instance...");
246                 platformEvent = NULL;
247                 if (CAL_SUCCESS != calendar_svc_iter_get_info(iter, &platformEvent)) {
248                     if(iter) {
249                         calendar_svc_iter_remove(&iter);
250                     }
251                     ThrowMsg(PlatformException, "Can't get event info.");
252                 }
253
254                 int detachedEventId = calendar_svc_struct_get_int(platformEvent, CAL_VALUE_INT_INDEX);
255                 LogDebug("detachedEventId "<<detachedEventId);
256                 error = calendar_svc_delete(dataType, detachedEventId);
257                 if (CAL_SUCCESS != error) {
258                     if(iter) {
259                         calendar_svc_iter_remove(&iter);
260                     }
261
262                     if(CAL_ERR_NO_DATA==error) {
263                         ThrowMsg(NotFoundException, "Item not found.");
264                     } else {
265                         ThrowMsg(PlatformException, "Can't delete calendar event. Error code " << error);
266                     }
267                 } else {
268                     LogDebug("Deleted a detached event by id: "<<detachedEventId);
269                 }
270             }
271
272             if(iter) {
273                 calendar_svc_iter_remove(&iter);
274             }
275         } else {
276             LogInfo("Deleting a task finished.");
277         }
278     } else {
279         error = calendar_svc_event_delete_normal_instance(itemId, recurrenceId);
280         if(error!=CAL_SUCCESS) {
281             ThrowMsg(PlatformException, "Can't delete the instance. Error code " << error);
282         }
283
284         (*m_abstractEvent->getRecurrenceRule()->getExceptions()).push_back(recurrenceId);
285         LogDebug("The recurring event is updated.");
286     }
287 }
288
289 void EventWrapper::createEventFromString(std::string value)
290 {
291     if (value.empty()) {
292         ThrowMsg(NullPointerException, "Failed to create an item from string.");
293     } else {
294         LogInfo("string to convert: "<<value);
295     }
296
297     GList *items = NULL;
298     int returnValue;
299
300     returnValue = calendar_svc_read_schedules(value.c_str(), &items);
301     if (CAL_SUCCESS != returnValue) {
302         ThrowMsg(PlatformException, "Can't convert string. Error code: "<<returnValue);
303     }
304     if (NULL==items) {
305         ThrowMsg(PlatformException, "No items converted.");
306     }
307
308     while (items) {
309         m_platformEvent = (cal_struct*)items->data;
310         if (NULL==m_platformEvent) {
311             items = g_list_next(items);
312             continue;
313         } else {
314             // We only cover one event per string.
315             break;
316         }
317
318         items = g_list_next(items);
319     }
320
321     //displayPlatformEvent();
322 }
323
324 std::string EventWrapper::exportEventToString()
325 {
326     char *vStrings = NULL;
327     GList *items = NULL;
328     int returnValue;
329     std::string returnString;
330
331     items = g_list_append(items, m_platformEvent);
332
333     returnValue = calendar_svc_write_schedules(items, &vStrings);
334     if (CAL_SUCCESS != returnValue) {
335         ThrowMsg(PlatformException, "Can't convert item to string. Error code: "<<returnValue);
336     }
337     if (NULL==vStrings) {
338         ThrowMsg(PlatformException, "No items converted.");
339     }
340
341     returnString = std::string(vStrings);
342     free(vStrings);
343     return returnString;
344 }
345
346 cal_struct* EventWrapper::getPlatformEvent() const
347 {
348     return m_platformEvent;
349 }
350
351 CalendarEventPtr EventWrapper::getAbstractEvent() const
352 {
353     return m_abstractEvent;
354 }
355
356 void EventWrapper::freePlatformEvent()
357 {
358     LogDebug("entered");
359     if (m_platformEvent != NULL) {
360         if (CAL_SUCCESS != calendar_svc_struct_free(&m_platformEvent)) {
361             LogError("Can't free calendar event struct.");
362         }
363         m_platformEvent = NULL;
364     }
365 }
366
367 cal_struct* EventWrapper::convertAbstractEventToPlatformEvent()
368 {
369     LogDebug("entered");
370     freePlatformEvent();
371     const char *dataType;
372
373     if(getType() == CalendarEvent::TASK_TYPE) {
374         dataType = CAL_STRUCT_TODO;
375     } else {
376         dataType = CAL_STRUCT_SCHEDULE;
377     }
378     m_platformEvent = calendar_svc_struct_new(dataType);
379     if (!m_platformEvent) {
380         ThrowMsg(UnknownException, "cannot create platform event");
381     }
382
383     setDescriptionToPlatformEvent();
384     setSummaryToPlatformEvent();
385     setStartTimeToPlatformEvent();
386     setEndTimeToPlatformEvent(); // replacement for duration
387     setLocationToPlatformEvent();
388     setCategoriesToPlatformEvent();
389     setStatusToPlatformEvent();
390     setAlarmsToPlatformEvent();
391     setIsAllDayToPlatformEvent();
392     setOrganizerToPlatformEvent();
393     setAttendeesToPlatformEvent();
394     setPositionToPlatformEvent();
395     setVisibilityToPlatformEvent();
396     //setLastModifiedDateToPlatformEvent(); // read-only in platform.
397     setAvailabilityToPlatformEvent();
398     setRecurrenceRuleToPlatformEvent();
399     setIdToPlatformEvent();
400     //setUIdToPlatformEvent(); // We don't set uid but use id field instead.
401     setParentIdToPlatformEvent();
402     setPriorityToPlatformEvent();
403     setCreatedDateToPlatformEvent();
404     setCompletedDateToPlatformEvent();
405     setProgressToPlatformEvent();
406
407     return getPlatformEvent();
408 }
409
410 void EventWrapper::setDescriptionToPlatformEvent()
411 {
412     if (!m_platformEvent) {
413         ThrowMsg(UnknownException, "Null platform pointer.");
414     }
415     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformEvent,
416                                                    CAL_VALUE_TXT_DESCRIPTION,
417                                                    m_abstractEvent->
418                                                        getDescription().c_str()))
419     {
420         ThrowMsg(PlatformException, "Can't set event description.");
421     }
422 }
423
424 void EventWrapper::setSummaryToPlatformEvent()
425 {
426     if (!m_platformEvent) {
427         ThrowMsg(UnknownException, "Null platform pointer.");
428     }
429     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformEvent,
430                                                    CAL_VALUE_TXT_SUMMARY,
431                                                    m_abstractEvent->getSubject()
432                                                        .c_str())) {
433         ThrowMsg(PlatformException, "Can't set event subject.");
434     }
435 }
436
437 void EventWrapper::setStartTimeToPlatformEvent()
438 {
439     if (!m_platformEvent) {
440         ThrowMsg(UnknownException, "Null platform pointer.");
441     }
442
443     long long int time = m_abstractEvent->getStartTime();
444     if (time == 0) {
445         time = m_abstractEvent->getEndTime();
446     }
447
448     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
449             CALS_VALUE_INT_DTSTART_TYPE,
450             CALS_TIME_UTIME)) {
451         ThrowMsg(PlatformException, "Can't set start time type.");
452     }
453
454     if (CAL_SUCCESS != calendar_svc_struct_set_lli(m_platformEvent,
455                                                     CALS_VALUE_LLI_DTSTART_UTIME,
456                                                     time)) {
457         ThrowMsg(PlatformException, "Can't set event start time.");
458     }
459
460     const char* timeZone = m_abstractEvent->getTimeZone().c_str();
461     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformEvent,
462                                                     CALS_VALUE_TXT_DTSTART_TZID,
463                                                     timeZone)) {
464         ThrowMsg(PlatformException, "Can't set event time zone.");
465     }
466 }
467
468 void EventWrapper::setEndTimeToPlatformEvent()
469 {
470     if (!m_platformEvent) {
471         ThrowMsg(UnknownException, "Null platform pointer.");
472     }
473
474     long long int time = m_abstractEvent->getEndTime();
475     if (time == 0) {
476         time = m_abstractEvent->getStartTime();
477     }
478
479     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
480             CALS_VALUE_INT_DTEND_TYPE,
481             CALS_TIME_UTIME)) {
482         ThrowMsg(PlatformException, "Can't set end time type.");
483     }
484
485     if (CAL_SUCCESS != calendar_svc_struct_set_lli(m_platformEvent,
486                                                     CALS_VALUE_LLI_DTEND_UTIME,
487                                                     time)) {
488         ThrowMsg(PlatformException, "Can't set event end time.");
489     }
490 }
491
492 void EventWrapper::setLocationToPlatformEvent()
493 {
494     if (!m_platformEvent) {
495         ThrowMsg(UnknownException, "Null platform pointer.");
496     }
497     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformEvent,
498                                                    CAL_VALUE_TXT_LOCATION,
499                                                    m_abstractEvent->getLocation()
500                                                        .c_str())) {
501         ThrowMsg(PlatformException, "Can't set event location.");
502     }
503 }
504
505 void EventWrapper::setRecurrenceRuleToPlatformEvent()
506 {
507     LogDebug("entered");
508     if (!m_platformEvent) {
509         ThrowMsg(UnknownException, "Null platform pointer.");
510     }
511
512     EventRecurrenceRulePtr rrule  = m_abstractEvent->getRecurrenceRule();
513     if (NULL==rrule) {
514         LogInfo("rrule is not set.");
515         return;
516     }
517
518     // set frequency
519     LogInfo("frequency "<<rrule->getFrequency());
520     switch (rrule->getFrequency()) {
521     case EventRecurrenceRule::NO_RECURRENCE:
522         if(CAL_SUCCESS!=calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ, CALS_FREQ_ONCE)) {
523             ThrowMsg(PlatformException, "Failed setting frequency.");
524         }
525         break;
526     case EventRecurrenceRule::DAILY_RECURRENCE:
527         if(CAL_SUCCESS!=calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ, CALS_FREQ_DAILY)) {
528             ThrowMsg(PlatformException, "Failed setting frequency.");
529         }
530         break;
531     case EventRecurrenceRule::WEEKLY_RECURRENCE:
532     {
533         if(CAL_SUCCESS!=calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ, CALS_FREQ_WEEKLY)) {
534             ThrowMsg(PlatformException, "Failed setting frequency.");
535         }
536         break;
537     }
538     case EventRecurrenceRule::MONTHLY_RECURRENCE:
539     {
540         if(CAL_SUCCESS!=calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ, CALS_FREQ_MONTHLY)) {
541             ThrowMsg(PlatformException, "Failed setting frequency.");
542         }
543         break;
544     }
545     case EventRecurrenceRule::YEARLY_RECURRENCE:
546     {
547         if(CAL_SUCCESS!=calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ, CALS_FREQ_YEARLY)) {
548             ThrowMsg(PlatformException, "Failed setting frequency.");
549         }
550         break;
551     }
552     case EventRecurrenceRule::UNDEFINED_RECURRENCE:
553     default:
554         ThrowMsg(UnknownException, "Invalid reccurence rule frequency "<<rrule->getFrequency());
555         break;
556     }
557
558     // set byday
559     StringArrayPtr daysOfTheWeek = rrule->getDaysOfTheWeek();
560     if( 0 != daysOfTheWeek->size() ) {
561         std::string byday = "";
562         for(unsigned int i=0; i<daysOfTheWeek->size(); i++) {
563             byday.append(daysOfTheWeek->at(i));
564             if(i!=daysOfTheWeek->size()-1) {
565                 byday.append(",");
566             }
567         }
568
569         LogInfo("byday: "<<byday);
570         if (CAL_SUCCESS!=calendar_svc_struct_set_str(m_platformEvent, CALS_VALUE_TXT_RRULE_BYDAY, byday.c_str())) {
571             ThrowMsg(PlatformException, "Can't set byday.");
572         }
573     }
574
575     // set the ocurrence count
576     if (-1 != rrule->getOccurrenceCount()) {
577         LogInfo("Set the occurrence count: "<<rrule->getOccurrenceCount());
578
579         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_RANGE_TYPE, CALS_RANGE_COUNT)) {
580             ThrowMsg(PlatformException, "Can't set rrule range type.");
581         }
582
583         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_COUNT, rrule->getOccurrenceCount())) {
584             ThrowMsg(PlatformException, "Can't set occurrence count.");
585         }
586     }
587
588     // set the exceptions
589     if ( !rrule->getExceptions()->empty() )
590     {
591         LogInfo("Set the exceptions of length: "<<rrule->getExceptions()->size());
592
593         std::string exdate = "";
594         for( unsigned int i=0; i<rrule->getExceptions()->size(); i++ )
595         {
596             std::stringstream ss;
597             ss<<rrule->getExceptions()->at(i);
598             exdate.append(ss.str());
599             if(i!=rrule->getExceptions()->size()-1) {
600                 exdate.append(",");
601             }
602         }
603         if (CAL_SUCCESS!=calendar_svc_struct_set_str(m_platformEvent, CAL_VALUE_TXT_EXDATE, exdate.c_str())) {
604             ThrowMsg(PlatformException, "Can't save exceptions.");
605         }
606     }
607
608     // set the recurrence interval
609     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
610                                                    CALS_VALUE_INT_RRULE_INTERVAL,
611                                                    rrule->getInterval()))
612     {
613         ThrowMsg(PlatformException, "Can't set interval.");
614     }
615
616     // set the recurrence end date. This is automatically calculated by platform if empty.
617     if (UNDEFINED_TIME!=rrule->getEndDate()) {
618         LogInfo("Setting the end date: "<<rrule->getEndDate());
619
620         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_RANGE_TYPE, CALS_RANGE_UNTIL)) {
621             ThrowMsg(PlatformException, "Can't set rrule range type.");
622         }
623
624         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent, CALS_VALUE_INT_RRULE_UNTIL_TYPE, CALS_TIME_UTIME)) {
625             ThrowMsg(PlatformException, "Can't set rrule until type.");
626         }
627
628         if (CAL_SUCCESS != calendar_svc_struct_set_lli(m_platformEvent, CALS_VALUE_LLI_RRULE_UNTIL_UTIME, m_abstractEvent->getRecurrenceRule()->getEndDate())) {
629             ThrowMsg(PlatformException, "Can't set recurrence end date.");
630         }
631     }
632 }
633
634 void EventWrapper::setAlarmsToPlatformEvent()
635 {
636     if (!m_platformEvent) {
637         ThrowMsg(UnknownException, "Null platform pointer.");
638     }
639
640     if( 0 != m_abstractEvent->getAlarms()->size() ) {
641         int errorCode;
642         GList* list = NULL;
643         calendar_svc_struct_get_list(m_platformEvent, CAL_VALUE_LST_ALARM, &list);
644
645         for( unsigned int i=0; i<m_abstractEvent->getAlarms()->size(); i++ )
646         {
647             LogInfo("Set the alarms #"<<i);
648             EventAlarmPtr theAlarm = m_abstractEvent->getAlarms()->at(i);
649
650             cal_value *value = calendar_svc_value_new(CAL_VALUE_LST_ALARM);
651
652             cal_sch_remind_tick_unit_t tickUnit = CAL_SCH_TIME_UNIT_OFF;
653             int tick = 0;
654             if( UNDEFINED_TIME!=theAlarm->getAbsoluteDate() ) {
655                 tickUnit = CAL_SCH_TIME_UNIT_SPECIFIC;
656             } else {
657                 if( TizenApis::Api::TimeUtil::MSECS_UNIT==theAlarm->getDuration().unit ) {
658                     tickUnit = CAL_SCH_TIME_UNIT_MIN; // minimum calendar time unit.
659                     tick = theAlarm->getDuration().length / 60000;
660                 } else if( TizenApis::Api::TimeUtil::SECONDS_UNIT==theAlarm->getDuration().unit ) {
661                     tickUnit = CAL_SCH_TIME_UNIT_MIN;
662                     tick = theAlarm->getDuration().length / 1000;
663                 } else if( TizenApis::Api::TimeUtil::MINUTES_UNIT==theAlarm->getDuration().unit ) {
664                     tickUnit = CAL_SCH_TIME_UNIT_MIN;
665                     tick = theAlarm->getDuration().length;
666                 } else if( TizenApis::Api::TimeUtil::HOURS_UNIT==theAlarm->getDuration().unit ) {
667                     tickUnit = CAL_SCH_TIME_UNIT_HOUR;
668                     tick = theAlarm->getDuration().length;
669                 } else if( TizenApis::Api::TimeUtil::DAYS_UNIT==theAlarm->getDuration().unit ) {
670                     tickUnit = CAL_SCH_TIME_UNIT_DAY;
671                     tick = theAlarm->getDuration().length;
672                 } else {
673                     LogError("Wrong alarm time unit: "<<theAlarm->getDuration().unit);
674                 }
675             }
676
677             if( CAL_SCH_TIME_UNIT_SPECIFIC==tickUnit ) {
678                 long long int time = theAlarm->getAbsoluteDate();
679                 LogInfo("Save absolute date: "<<time);
680                 errorCode = calendar_svc_value_set_lli(value,
681                         CAL_VALUE_LLI_ALARMS_TIME,
682                         time);
683                 if (CAL_SUCCESS != errorCode) {
684                     if (value) {
685                         calendar_svc_value_free(&value);
686                     }
687                     ThrowMsg(PlatformException, "Can't set alarm time: "<<errorCode);
688                 }
689             } else {
690                 errorCode = calendar_svc_value_set_int(
691                         value,
692                         CAL_VALUE_INT_ALARMS_TICK,
693                         tick);
694                 if (CAL_SUCCESS != errorCode) {
695                     if (value) {
696                         calendar_svc_value_free(&value);
697                     }
698                     ThrowMsg(PlatformException, "Can't set alarm tick: "<<errorCode);
699                 }
700             }
701
702             errorCode = calendar_svc_value_set_int(
703                     value,
704                     CAL_VALUE_INT_ALARMS_TICK_UNIT,
705                     tickUnit);
706             if (CAL_SUCCESS != errorCode) {
707                 if (value) {
708                     calendar_svc_value_free(&value);
709                 }
710                 ThrowMsg(PlatformException, "Can't set alarm tick unit: "<<errorCode);
711             }
712
713             // Set the alarm type.
714             cal_alert_type_t alarmType = CAL_ALERT_MELODY;
715             switch (theAlarm->getMethods().at(0)) {
716             case EventAlarm::NO_ALARM:
717                 alarmType = CAL_ALERT_MUTE;
718                 break;
719             case EventAlarm::SOUND_ALARM:
720                 alarmType = CAL_ALERT_MELODY;
721                 break;
722             case EventAlarm::SILENT_ALARM:
723                 alarmType = CAL_ALERT_VIBRATION;
724                 break;
725             default:
726                 LogDebug("Use the default alarm type");
727                 break;
728             }
729             errorCode = calendar_svc_value_set_int(
730                     value,
731                     CAL_VALUE_INT_ALARMS_TYPE,
732                     alarmType);
733             if (CAL_SUCCESS != errorCode) {
734                 if (value) {
735                     calendar_svc_value_free(&value);
736                 }
737                 ThrowMsg(PlatformException, "Can't set alarm type: "<<errorCode);
738             }
739
740             // Set the display text
741             errorCode = calendar_svc_value_set_str(
742                     value,
743                     CAL_VALUE_TXT_ALARMS_DESCRIPTION,
744                     theAlarm->getDescription().c_str());
745             if (CAL_SUCCESS != errorCode) {
746                 if (value) {
747                     calendar_svc_value_free(&value);
748                 }
749                 ThrowMsg(PlatformException, "Can't set alarm description: "<<errorCode);
750             }
751
752             list = g_list_append(list, value);
753         }
754
755         if (CAL_SUCCESS!=calendar_svc_struct_store_list(m_platformEvent, CAL_VALUE_LST_ALARM, list)) {
756             ThrowMsg(PlatformException, "cannot save the alarms");
757         }
758     }
759 }
760
761 void EventWrapper::setStatusToPlatformEvent()
762 {
763     if (!m_platformEvent) {
764         ThrowMsg(UnknownException, "Null platform pointer.");
765     }
766
767     cals_status_t status;
768
769     switch (m_abstractEvent->getStatus()) {
770     case CalendarEvent::TENTATIVE_STATUS:
771         status = CALS_EVENT_STATUS_TENTATIVE;
772         break;
773     case CalendarEvent::CONFIRMED_STATUS:
774         status = CALS_EVENT_STATUS_CONFIRMED;
775         break;
776     case CalendarEvent::CANCELLED_STATUS:
777         status = CALS_EVENT_STATUS_CANCELLED;
778         break;
779     case CalendarEvent::NEEDS_ACTION_STATUS:
780         status = CALS_TODO_STATUS_NEEDS_ACTION;
781         break;
782     case CalendarEvent::COMPLETED_STATUS:
783         status = CALS_TODO_STATUS_COMPLETED;
784         break;
785     case CalendarEvent::IN_PROCESS_STATUS:
786         status = CALS_TODO_STATUS_IN_PROCESS;
787         break;
788     default:
789         status = CALS_EVENT_STATUS_NONE;
790         break;
791     }
792
793     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
794                                                    CAL_VALUE_INT_TASK_STATUS,
795                                                    status)) {
796         ThrowMsg(PlatformException, "Can't set event status.");
797     }
798 }
799
800 void EventWrapper::setCategoriesToPlatformEvent()
801 {
802     if (!m_platformEvent) {
803         ThrowMsg(UnknownException, "Null platform pointer.");
804     }
805
806     std::string categories = "";
807     // Need to concatenate all categories into one string separated by a comma.
808     for (size_t i = 0; i < m_abstractEvent->getCategories()->size(); ++i) {
809         if(0==i) {
810             categories.append(m_abstractEvent->getCategories()->at(i));
811         } else {
812             categories.append("," + m_abstractEvent->getCategories()->at(i));
813         }
814     }
815
816     LogInfo("Saving categories: "<<categories);
817
818     if (CAL_SUCCESS !=
819         calendar_svc_struct_set_str(m_platformEvent, CAL_VALUE_TXT_CATEGORIES,
820                                    categories.c_str())) {
821         ThrowMsg(PlatformException, "Cannot save categories.");
822     }
823 }
824
825 void EventWrapper::setIdToPlatformEvent()
826 {
827     if (!m_platformEvent) {
828         ThrowMsg(UnknownException, "Null platform pointer.");
829     }
830     if (m_abstractEvent->getIdIsSet()) {
831         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
832                                                        CAL_VALUE_INT_INDEX,
833                                                        m_abstractEvent->getId()))
834         {
835             ThrowMsg(PlatformException, "Can't set event Id.");
836         }
837     } else {
838         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
839                                                        CAL_VALUE_INT_INDEX,
840                                                        NEW_EVENT_ID)) {
841             ThrowMsg(PlatformException, "Can't set event Id.");
842         }
843     }
844 }
845
846 void EventWrapper::setParentIdToPlatformEvent()
847 {
848     if (!m_platformEvent) {
849         ThrowMsg(UnknownException, "Null platform pointer.");
850     }
851
852     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
853                                                    CAL_VALUE_INT_ORIGINAL_EVENT_ID,
854                                                    m_abstractEvent->getParentId()))
855     {
856         ThrowMsg(PlatformException, "Can't set parent Id.");
857     }
858 }
859
860 void EventWrapper::setIsAllDayToPlatformEvent()
861 {
862     if (!m_platformEvent) {
863         ThrowMsg(UnknownException, "Null platform pointer.");
864     }
865
866     cals_time_type timeType = CALS_TIME_UTIME;
867     if(m_abstractEvent->getIsAllDay()) {
868         timeType = CALS_TIME_LOCALTIME;
869     }
870
871     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
872                                                    CALS_VALUE_INT_DTSTART_TYPE,
873                                                    timeType)) {
874         ThrowMsg(PlatformException, "Can't set event isAllDay.");
875     }
876 }
877
878 void EventWrapper::setOrganizerToPlatformEvent()
879 {
880     if (!m_platformEvent) {
881         ThrowMsg(UnknownException, "Null platform pointer.");
882     }
883     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformEvent,
884                                                    CAL_VALUE_TXT_ORGANIZER_NAME,
885                                                    m_abstractEvent->getOrganizer().c_str())) {
886         ThrowMsg(PlatformException, "Can't set event organizer.");
887     }
888 }
889
890 void EventWrapper::setLastModifiedDateToPlatformEvent()
891 {
892     if (!m_platformEvent) {
893         ThrowMsg(UnknownException, "Null platform pointer.");
894     }
895
896     long long int time = m_abstractEvent->getLastModifiedDate();
897
898     if (CAL_SUCCESS!=calendar_svc_struct_set_lli(m_platformEvent,
899                                                     CALS_VALUE_LLI_LASTMOD,
900                                                     time)) {
901         ThrowMsg(PlatformException, "Can't set event lastModifiedDate.");
902     }
903 }
904
905 void EventWrapper::setVisibilityToPlatformEvent()
906 {
907     if (!m_platformEvent) {
908         ThrowMsg(UnknownException, "Null platform pointer.");
909     }
910
911     int visibility = PUBLIC_VISIBILITY; // default value
912     switch (m_abstractEvent->getVisibility()) {
913     case CalendarEvent::PUBLIC_VISIBILITY:
914         visibility = PUBLIC_VISIBILITY;
915         break;
916     case CalendarEvent::PRIVATE_VISIBILITY:
917         visibility = PRIVATE_VISIBILITY;
918         break;
919     case CalendarEvent::CONFIDENTIAL_VISIBILITY:
920         visibility = CONFIDENTIAL_VISIBILITY;
921         break;
922     default:
923         LogDebug("Use the default visibility");
924         break;
925     }
926     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
927                                                    CAL_VALUE_INT_SENSITIVITY,
928                                                    visibility)) {
929         ThrowMsg(PlatformException, "Can't set visibility.");
930     }
931 }
932
933 void EventWrapper::setAvailabilityToPlatformEvent()
934 {
935     if (!m_platformEvent) {
936         ThrowMsg(UnknownException, "Null platform pointer.");
937     }
938
939     cal_event_availability_type_t availability = EVENT_FREE_FB;
940     switch (m_abstractEvent->getAvailability()) {
941     case CalendarEvent::BUSY_FB:
942         availability = EVENT_BUSY_FB;
943         break;
944     case CalendarEvent::BUSY_UNAVAILABLE_FB:
945         availability = EVENT_BUSY_UNAVAILABLE_FB;
946         break;
947     case CalendarEvent::FREE_FB:
948         availability = EVENT_FREE_FB;
949         break;
950     case CalendarEvent::BUSY_TENTATIVE_FB:
951         availability = EVENT_BUSY_TENTATIVE_FB;
952         break;
953     default:
954         LogDebug("Use the default availability");
955         break;
956     }
957     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
958                                                    CAL_VALUE_INT_AVAILABILITY,
959                                                    availability)) {
960         ThrowMsg(PlatformException, "Can't set availability.");
961     }
962 }
963
964 void EventWrapper::setUIdToPlatformEvent()
965 {
966     if (!m_platformEvent) {
967         ThrowMsg(UnknownException, "Null platform pointer.");
968     }
969     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformEvent,
970                                                    CAL_VALUE_TXT_UID,
971                                                    m_abstractEvent->getUId().c_str())) {
972         ThrowMsg(PlatformException, "Can't set event uid.");
973     }
974 }
975
976 void EventWrapper::setAttendeesToPlatformEvent()
977 {
978     if (!m_platformEvent) {
979         ThrowMsg(UnknownException, "Null platform pointer.");
980     }
981
982     EventAttendeeListPtr attendeeList  = m_abstractEvent->getAttendees();
983     if (NULL==attendeeList) {
984         LogInfo("attendeeList is not set.");
985         return;
986     }
987
988     GList *attendees = NULL;
989     cal_value* attendee = NULL;
990     Try
991     {
992         for (size_t i = 0; i < attendeeList->size(); ++i) {
993             LogDebug("adding attendee " << i+1 << " over " <<
994                      attendeeList->size());
995
996             attendee = calendar_svc_value_new(CAL_VALUE_LST_ATTENDEE_LIST);
997             if (NULL == attendee) {
998                 LogError("error during creating attendee");
999                 ThrowMsg(PlatformException,
1000                          "Cannot create attendee object");
1001             }
1002
1003             // save name
1004             if (CAL_SUCCESS !=
1005                 calendar_svc_value_set_str(attendee, CAL_VALUE_TXT_ATTENDEE_DETAIL_NAME,
1006                                            attendeeList->at(i)->getName().c_str())) {
1007                 LogError("error during setting attendee name");
1008                 calendar_svc_value_free(&attendee);
1009                 ThrowMsg(PlatformException, "Cannot set attendee name");
1010             }
1011
1012             // save uri
1013             if (CAL_SUCCESS !=
1014                 calendar_svc_value_set_str(attendee, CAL_VALUE_TXT_ATTENDEE_DETAIL_EMAIL,
1015                                            attendeeList->at(i)->getURI().c_str())) {
1016                 LogError("error during setting attendee URI");
1017                 calendar_svc_value_free(&attendee);
1018                 ThrowMsg(PlatformException, "Cannot set attendee URI");
1019             }
1020
1021             // save role
1022             cal_event_attendee_role_type_t role = EVENT_ATTENDEE_CHAIR_ROLE;
1023             switch (attendeeList->at(i)->getRole()) {
1024             case EventAttendee::REQ_PARTICIPANT_ROLE:
1025                 role = EVENT_ATTENDEE_REQ_PARTICIPANT_ROLE;
1026                 break;
1027             case EventAttendee::OPT_PARTICIPANT_ROLE:
1028                 role = EVENT_ATTENDEE_OPT_PARTICIPANT_ROLE;
1029                 break;
1030             case EventAttendee::NON_PARTICIPANT_ROLE:
1031                 role = EVENT_ATTENDEE_NON_PARTICIPANT_ROLE;
1032                 break;
1033             case EventAttendee::CHAIR_ROLE:
1034                 role = EVENT_ATTENDEE_CHAIR_ROLE;
1035                 break;
1036             default:
1037                 LogDebug("Use the default role");
1038                 break;
1039             }
1040             if (CAL_SUCCESS !=
1041                 calendar_svc_value_set_int(attendee, CAL_VALUE_INT_ATTENDEE_ROLE,
1042                                            role)) {
1043                 LogError("error during setting attendee role");
1044                 calendar_svc_value_free(&attendee);
1045                 ThrowMsg(PlatformException, "Cannot set attendee role");
1046             }
1047
1048             // save status
1049             cal_event_attendee_status_type_t status = EVENT_ATTENDEE_NEEDS_ACTION_AT_STATUS;
1050             switch (attendeeList->at(i)->getStatus()) {
1051             case EventAttendee::PENDING_AT_STATUS:
1052                 status = EVENT_ATTENDEE_NEEDS_ACTION_AT_STATUS;
1053                 break;
1054             case EventAttendee::ACCEPTED_AT_STATUS:
1055                 status = EVENT_ATTENDEE_ACCEPTED_AT_STATUS;
1056                 break;
1057             case EventAttendee::DECLINED_AT_STATUS:
1058                 status = EVENT_ATTENDEE_DECLINED_AT_STATUS;
1059                 break;
1060             case EventAttendee::TENTATIVE_AT_STATUS:
1061                 status = EVENT_ATTENDEE_TENTATIVE_AT_STATUS;
1062                 break;
1063             case EventAttendee::DELEGATED_AT_STATUS:
1064                 status = EVENT_ATTENDEE_DELEGATED_AT_STATUS;
1065                 break;
1066             case EventAttendee::COMPLETED_AT_STATUS:
1067                 status = EVENT_ATTENDEE_COMPLETED_AT_STATUS;
1068                 break;
1069             case EventAttendee::IN_PROCESS_AT_STATUS:
1070                 status = EVENT_ATTENDEE_IN_PROCESS_AT_STATUS;
1071                 break;
1072             default:
1073                 LogDebug("Use the default status");
1074                 break;
1075             }
1076             if (CAL_SUCCESS !=
1077                 calendar_svc_value_set_int(attendee, CAL_VALUE_INT_ATTENDEE_DETAIL_STATUS,
1078                                            status)) {
1079                 LogError("error during setting attendee status");
1080                 calendar_svc_value_free(&attendee);
1081                 ThrowMsg(PlatformException, "Cannot set attendee status");
1082             }
1083
1084             // save RSVP
1085             if (CAL_SUCCESS !=
1086                 calendar_svc_value_set_int(attendee, CAL_VALUE_INT_ATTENDEE_RSVP,
1087                                            attendeeList->at(i)->getRSVP())) {
1088                 LogError("error during setting attendee RSVP");
1089                 calendar_svc_value_free(&attendee);
1090                 ThrowMsg(PlatformException, "Cannot set attendee RSVP");
1091             }
1092
1093             // save type
1094             cal_event_attendee_type_t type = EVENT_ATTENDEE_INDIVIDUAL_TYPE;
1095             switch (attendeeList->at(i)->getType()) {
1096             case EventAttendee::INDIVIDUAL_TYPE:
1097                 type = EVENT_ATTENDEE_INDIVIDUAL_TYPE;
1098                 break;
1099             case EventAttendee::GROUP_TYPE:
1100                 type = EVENT_ATTENDEE_GROUP_TYPE;
1101                 break;
1102             case EventAttendee::RESOURCE_TYPE:
1103                 type = EVENT_ATTENDEE_RESOURCE_TYPE;
1104                 break;
1105             case EventAttendee::ROOM_TYPE:
1106                 type = EVENT_ATTENDEE_ROOM_TYPE;
1107                 break;
1108             case EventAttendee::UNKNOWN_TYPE:
1109                 type = EVENT_ATTENDEE_UNKNOWN_TYPE;
1110                 break;
1111             default:
1112                 LogDebug("Use the default type");
1113                 break;
1114             }
1115             if (CAL_SUCCESS !=
1116                 calendar_svc_value_set_int(attendee, CAL_VALUE_INT_ATTENDEE_DETAIL_TYPE,
1117                                            type)) {
1118                 LogError("error during setting attendee type");
1119                 calendar_svc_value_free(&attendee);
1120                 ThrowMsg(PlatformException, "Cannot set attendee type");
1121             }
1122
1123             // save group
1124             if (CAL_SUCCESS !=
1125                 calendar_svc_value_set_str(attendee, CAL_VALUE_TXT_ATTENDEE_GROUP,
1126                                            attendeeList->at(i)->getGroup().c_str())) {
1127                 LogError("error during setting attendee group");
1128                 calendar_svc_value_free(&attendee);
1129                 ThrowMsg(PlatformException, "Cannot set attendee group");
1130             }
1131
1132             // save delegatorURI
1133             if (CAL_SUCCESS !=
1134                 calendar_svc_value_set_str(attendee, CAL_VALUE_TXT_ATTENDEE_DELEGATOR_URI,
1135                                            attendeeList->at(i)->getDelegatorURI().c_str())) {
1136                 LogError("error during setting attendee delegator uri");
1137                 calendar_svc_value_free(&attendee);
1138                 ThrowMsg(PlatformException, "Cannot set attendee delegator uri");
1139             }
1140
1141             // save delegateURI
1142             if (CAL_SUCCESS !=
1143                 calendar_svc_value_set_str(attendee, CAL_VALUE_TXT_ATTENDEE_DELEGATE_URI,
1144                                            attendeeList->at(i)->getDelegateURI().c_str())) {
1145                 LogError("error during setting attendee delegate uri");
1146                 calendar_svc_value_free(&attendee);
1147                 ThrowMsg(PlatformException, "Cannot set attendee delegate uri");
1148             }
1149
1150             // save contactId
1151             if (CAL_SUCCESS !=
1152                 calendar_svc_value_set_str(attendee, CAL_VALUE_TXT_ATTENDEE_UID,
1153                                            attendeeList->at(i)->getContactId().c_str())) {
1154                 LogError("error during setting attendee uid");
1155                 calendar_svc_value_free(&attendee);
1156                 ThrowMsg(PlatformException, "Cannot set attendee uid");
1157             }
1158
1159             // save addressBookId
1160             std::stringstream ss(attendeeList->at(i)->getAddressBookId());
1161             int addressBookId;
1162             ss>>addressBookId;
1163             LogInfo("addressBookId: "<<addressBookId);
1164             if (CAL_SUCCESS !=
1165                 calendar_svc_value_set_int(attendee, CAL_VALUE_INT_ATTENDEE_DETAIL_CT_INDEX,
1166                                            addressBookId)) {
1167                 LogError("error during setting attendee address book id");
1168                 calendar_svc_value_free(&attendee);
1169                 ThrowMsg(PlatformException, "Cannot set attendee address book id");
1170             }
1171
1172             attendees = g_list_append(attendees, attendee);
1173         }
1174
1175         LogDebug("m_abstractEvent->getAttendees()->size() " << attendeeList->size());
1176
1177         calendar_svc_struct_store_list(m_platformEvent,
1178                                        CAL_VALUE_LST_ATTENDEE_LIST,
1179                                        attendees);
1180     }
1181     Catch(PlatformException)
1182     {
1183         LogError("error during setting attendees");
1184         calendar_svc_struct_store_list(m_platformEvent,
1185                                        CAL_VALUE_LST_ATTENDEE_LIST,
1186                                        NULL);
1187         for (; attendees; attendees = g_list_next(attendees)) {
1188             attendee = static_cast<cal_value*>(attendees->data);
1189             calendar_svc_value_free(&attendee);
1190         }
1191         g_list_free(attendees);
1192         ReThrow(PlatformException);
1193     }
1194 }
1195
1196 void EventWrapper::setPositionToPlatformEvent()
1197 {
1198     if (!m_platformEvent) {
1199         ThrowMsg(UnknownException, "Null platform pointer.");
1200     }
1201
1202     if (CAL_SUCCESS != calendar_svc_struct_set_double(m_platformEvent,
1203                                                    CAL_VALUE_DBL_LATITUDE,
1204                                                    m_abstractEvent->getLatitude())) {
1205         ThrowMsg(PlatformException, "Can't set latitude.");
1206     }
1207
1208     if (CAL_SUCCESS != calendar_svc_struct_set_double(m_platformEvent,
1209                                                    CAL_VALUE_DBL_LONGITUDE,
1210                                                    m_abstractEvent->getLongitude())) {
1211         ThrowMsg(PlatformException, "Can't set longitude.");
1212     }
1213 }
1214
1215 void EventWrapper::setPriorityToPlatformEvent()
1216 {
1217     if (!m_platformEvent) {
1218         ThrowMsg(UnknownException, "Null platform pointer.");
1219     }
1220
1221     cal_priority_type_t priority = (cal_priority_type_t)-1;
1222
1223     switch (m_abstractEvent->getPriority()) {
1224     case CalendarEvent::HIGH_PRIORITY:
1225         priority = EVENT_PRIORITY_HIGH;
1226         break;
1227     case CalendarEvent::MEDIUM_PRIORITY:
1228         priority = EVENT_PRIORITY_NORMAL;
1229         break;
1230     case CalendarEvent::LOW_PRIORITY:
1231         priority = EVENT_PRIORITY_LOW;
1232         break;
1233     default:
1234         break;
1235     }
1236
1237     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
1238                                                    CAL_VALUE_INT_PRIORITY,
1239                                                    priority))
1240     {
1241         ThrowMsg(PlatformException, "Can't set task priority.");
1242     }
1243 }
1244
1245 void EventWrapper::setCreatedDateToPlatformEvent()
1246 {
1247     if (!m_platformEvent) {
1248         ThrowMsg(UnknownException, "Null platform pointer.");
1249     }
1250
1251         long long int time = m_abstractEvent->getCreatedDate();
1252         if (CAL_SUCCESS != calendar_svc_struct_set_lli(m_platformEvent,
1253                                                          CAL_VALUE_LLI_CREATED_TIME,
1254                                                         time)) {
1255             ThrowMsg(PlatformException, "Can't set created time.");
1256         }
1257 }
1258
1259 void EventWrapper::setCompletedDateToPlatformEvent()
1260 {
1261     if (!m_platformEvent) {
1262         ThrowMsg(UnknownException, "Null platform pointer.");
1263     }
1264
1265         long long int time = m_abstractEvent->getCompletedDate();
1266         if (CAL_SUCCESS != calendar_svc_struct_set_lli(m_platformEvent,
1267                                                         CAL_VALUE_LLI_COMPLETED_TIME,
1268                                                         time)) {
1269             ThrowMsg(PlatformException, "Can't set completed time.");
1270         }
1271 }
1272
1273 void EventWrapper::setProgressToPlatformEvent()
1274 {
1275     if (!m_platformEvent) {
1276         ThrowMsg(UnknownException, "Null platform pointer.");
1277     }
1278
1279     int progress = m_abstractEvent->getProgress();
1280     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
1281         CAL_VALUE_INT_PROGRESS,
1282         progress)) {
1283         ThrowMsg(PlatformException, "Can't set visibility.");
1284     }
1285 }
1286
1287 CalendarEventPtr EventWrapper::convertPlatformEventToAbstractEvent()
1288 {
1289     LogDebug("entered");
1290     setDescriptionFromPlatformEvent();
1291     setSummaryFromPlatformEvent();
1292     setStartTimeFromPlatformEvent();
1293     setEndTimeFromPlatformEvent(); // replace for duration
1294     setLocationFromPlatformEvent();
1295     setCategoriesFromPlatformEvent();
1296     setStatusFromPlatformEvent();
1297     setAlarmsFromPlatformEvent();
1298     setIsAllDayFromPlatformEvent();
1299     setOrganizerFromPlatformEvent();
1300     setAttendeesFromPlatformEvent();
1301     setPositionFromPlatformEvent();
1302     setVisibilityFromPlatformEvent();
1303     setLastModifiedDateFromPlatformEvent();
1304     setAvailabilityFromPlatformEvent();
1305     setRecurrenceRuleFromPlatformEvent();
1306     setIdFromPlatformEvent();
1307     setParentIdFromPlatformEvent();
1308     //setUIdFromPlatformEvent(); // We set the uid value as the same with id.
1309     setPriorityFromPlatformEvent();
1310     setCreatedDateFromPlatformEvent();
1311     setCompletedDateFromPlatformEvent();
1312     setProgressFromPlatformEvent();
1313     setIsDetachedFromPlatformEvent();
1314
1315     return getAbstractEvent();
1316 }
1317
1318 void EventWrapper::setDescriptionFromPlatformEvent()
1319 {
1320     if (!m_platformEvent) {
1321         ThrowMsg(UnknownException, "Null platform pointer.");
1322     }
1323     const char *description = calendar_svc_struct_get_str(
1324             m_platformEvent,
1325             CAL_VALUE_TXT_DESCRIPTION);
1326     if (description) {
1327         m_abstractEvent->setDescription(description);
1328     }
1329 }
1330
1331 void EventWrapper::setSummaryFromPlatformEvent()
1332 {
1333     if (!m_platformEvent) {
1334         ThrowMsg(UnknownException, "Null platform pointer.");
1335     }
1336     const char *summary = calendar_svc_struct_get_str(m_platformEvent,
1337                                                       CAL_VALUE_TXT_SUMMARY);
1338     if (summary) {
1339         m_abstractEvent->setSubject(summary);
1340     }
1341 }
1342
1343 void EventWrapper::setStartTimeFromPlatformEvent()
1344 {
1345     if (!m_platformEvent) {
1346         ThrowMsg(UnknownException, "Null platform pointer.");
1347     }
1348     long long int startTime = calendar_svc_struct_get_lli(
1349             m_platformEvent,
1350             CALS_VALUE_LLI_DTSTART_UTIME);
1351     m_abstractEvent->setStartTime(startTime);
1352
1353     // Retrive the time zone info only when the start time is loaded.
1354     char* timeZone = calendar_svc_struct_get_str(
1355             m_platformEvent,
1356             CALS_VALUE_TXT_DTSTART_TZID);
1357     if( timeZone ) {
1358         LogInfo("timeZone: "<<timeZone);
1359         m_abstractEvent->setTimeZone(std::string(timeZone));
1360     } else {
1361         LogWarning("Cannot read timeZone from platform!");
1362     }
1363     LogInfo("startTime: "<<startTime);
1364 }
1365
1366 void EventWrapper::setEndTimeFromPlatformEvent()
1367 {
1368     if (!m_platformEvent) {
1369         ThrowMsg(UnknownException, "Null platform pointer.");
1370     }
1371     long long int endTime = calendar_svc_struct_get_lli(m_platformEvent,
1372                                                   CALS_VALUE_LLI_DTEND_UTIME);
1373     m_abstractEvent->setEndTime(endTime);
1374 }
1375
1376 void EventWrapper::setLocationFromPlatformEvent()
1377 {
1378     if (!m_platformEvent) {
1379         ThrowMsg(UnknownException, "Null platform pointer.");
1380     }
1381     const char* location = calendar_svc_struct_get_str(m_platformEvent,
1382                                                        CAL_VALUE_TXT_LOCATION);
1383     if (location) {
1384         m_abstractEvent->setLocation(location);
1385     }
1386 }
1387
1388 void EventWrapper::setRecurrenceRuleFromPlatformEvent()
1389 {
1390     LogDebug("entered");
1391     if (!m_platformEvent) {
1392         ThrowMsg(UnknownException, "Null platform pointer.");
1393     }
1394
1395     EventRecurrenceRulePtr rrule(new EventRecurrenceRule());
1396
1397     // load the recurrence frequency
1398     int frequency = calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ);
1399     LogInfo("frequency "<<frequency);
1400     switch (frequency) {
1401     case CALS_FREQ_ONCE:
1402         rrule->setFrequency(EventRecurrenceRule::NO_RECURRENCE);
1403         break;
1404     case CALS_FREQ_DAILY:
1405         rrule->setFrequency(EventRecurrenceRule::DAILY_RECURRENCE);
1406         break;
1407     case CALS_FREQ_WEEKLY:
1408         rrule->setFrequency(EventRecurrenceRule::WEEKLY_RECURRENCE);
1409         break;
1410     case CALS_FREQ_MONTHLY:
1411         rrule->setFrequency(EventRecurrenceRule::MONTHLY_RECURRENCE);
1412         break;
1413     case CALS_FREQ_YEARLY:
1414         rrule->setFrequency(EventRecurrenceRule::YEARLY_RECURRENCE);
1415         break;
1416     default:
1417         LogWarning("Unknown recurrence frequency.");
1418         rrule->setFrequency(EventRecurrenceRule::NO_RECURRENCE);
1419         break;
1420     }
1421
1422     // load the byday
1423     StringArrayPtr daysOfTheWeek(new StringArray());
1424     char* byday = calendar_svc_struct_get_str(m_platformEvent, CALS_VALUE_TXT_RRULE_BYDAY);
1425     LogInfo("Loaded byday: "<<byday);
1426     if (byday) {
1427         char *saveptr = NULL;
1428         char* pch = strtok_r(byday, ",", &saveptr);
1429         while (NULL != pch) {
1430             (*daysOfTheWeek).push_back(pch);
1431             pch = strtok_r(NULL, ",", &saveptr);
1432         }
1433     }
1434     rrule->setDaysOfTheWeek(daysOfTheWeek);
1435     LogInfo("Number of daysOfTheWeek: "<<rrule->getDaysOfTheWeek()->size());
1436
1437     // load the recurrence interval
1438     int interval = calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_RRULE_INTERVAL);
1439     rrule->setInterval(interval);
1440
1441     // load the ocurrence count
1442     int occurrrenceCount = calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_RRULE_COUNT);
1443     rrule->setOccurrenceCount(occurrrenceCount);
1444
1445     // load the recurrence end date
1446     long long int endDate = calendar_svc_struct_get_lli(m_platformEvent, CALS_VALUE_LLI_RRULE_UNTIL_UTIME);
1447     rrule->setEndDate(endDate);
1448     LogDebug("endDate from platform = " << endDate);
1449
1450     // load the exceptions
1451     NumberArrayPtr exceptions(new NumberArray());
1452     char* exdate = calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_EXDATE);
1453     LogInfo("Loaded exdate: "<<exdate);
1454     if (exdate) {
1455         char *saveptr = NULL;
1456         char *pch = strtok_r(exdate, ",", &saveptr);
1457         while (NULL != pch) {
1458             std::stringstream ss(pch);
1459             long long int oneException;
1460             ss>>oneException;
1461             (*exceptions).push_back(oneException);
1462             pch = strtok_r(NULL, ",", &saveptr);
1463         }
1464     }
1465     rrule->setExceptions(exceptions);
1466     LogInfo("Number of exceptions: "<<rrule->getExceptions()->size());
1467
1468     // set the loaded recurrence rule
1469     m_abstractEvent->setRecurrenceRule(rrule);
1470 }
1471
1472 void EventWrapper::setAlarmsFromPlatformEvent()
1473 {
1474     if (!m_platformEvent) {
1475         ThrowMsg(UnknownException, "Null platform pointer.");
1476     }
1477
1478     GList* alarmList = NULL;
1479     calendar_svc_struct_get_list(m_platformEvent, CAL_VALUE_LST_ALARM, &alarmList);
1480     cal_value* alarmValue = NULL;
1481     int tick, tickUnit;
1482     for (; alarmList; alarmList = g_list_next(alarmList)) {
1483         LogDebug("Processing alarm...");
1484         alarmValue = static_cast<cal_value*>(alarmList->data);
1485
1486         EventAlarmPtr theAlarm( new EventAlarm() );
1487
1488         tickUnit = calendar_svc_value_get_int(alarmValue, CAL_VALUE_INT_ALARMS_TICK_UNIT);
1489         if (CAL_ERR_FAIL == tickUnit) {
1490             LogError("Cannot read alarm tick unit. Stop processing alarms.");
1491             return;
1492         }
1493
1494         if( CAL_SCH_TIME_UNIT_SPECIFIC==tickUnit ) {
1495             long long int absoluteDate = calendar_svc_value_get_lli(
1496                     alarmValue,
1497                     CAL_VALUE_LLI_ALARMS_TIME);
1498             if (CAL_ERR_FAIL == absoluteDate) {
1499                 LogError("Cannot read alarm time. Stop processing alarms.");
1500                 return;
1501             }
1502             LogInfo("Load absolute date: "<<absoluteDate);
1503             theAlarm->setAbsoluteDate(absoluteDate);
1504         } else {
1505             tick = calendar_svc_value_get_int(alarmValue, CAL_VALUE_INT_ALARMS_TICK);
1506             if (CAL_ERR_FAIL == tick) {
1507                 LogError("Cannot read alarm tick. Stop processing alarms.");
1508                 return;
1509             }
1510
1511             TizenApis::Api::TimeUtil::DurationProperties duration;
1512             if( CAL_SCH_TIME_UNIT_MIN==tickUnit ) {
1513                 duration.unit = TizenApis::Api::TimeUtil::MINUTES_UNIT;
1514                 duration.length = tick;
1515             } else if( CAL_SCH_TIME_UNIT_HOUR==tickUnit ) {
1516                 duration.unit = TizenApis::Api::TimeUtil::HOURS_UNIT;
1517                 duration.length = tick;
1518             } else if( CAL_SCH_TIME_UNIT_DAY==tickUnit ) {
1519                 duration.unit = TizenApis::Api::TimeUtil::DAYS_UNIT;
1520                 duration.length = tick;
1521             } else if( CAL_SCH_TIME_UNIT_WEEK==tickUnit) {
1522                 duration.unit = TizenApis::Api::TimeUtil::DAYS_UNIT;
1523                 duration.length = tick*7;
1524             } else {
1525                 LogWarning("Wrong tick unit: "<<tickUnit);
1526             }
1527
1528             theAlarm->setDuration(duration);
1529         }
1530
1531         std::vector<EventAlarm::EventAlarmType> methods;
1532         cal_alert_type_t type = static_cast<cal_alert_type_t>(calendar_svc_value_get_int(alarmValue, CAL_VALUE_INT_ALARMS_TYPE));
1533         if ( 0 > type) {
1534             LogError("Cannot read alarm type. Setting the default value.");
1535         }
1536         switch (type) {
1537         case CAL_ALERT_VIBRATION:
1538             methods.push_back(EventAlarm::SILENT_ALARM);
1539             break;
1540         case CAL_ALERT_MELODY:
1541         case CAL_ALERT_INCREASING_MELODY:
1542         case CAL_ALERT_VIBRATION_THEN_MELODY:
1543         case CAL_ALERT_VIBMELODY:
1544         case CAL_ALERT_VIB_INCREASING_MELODY:
1545             methods.push_back(EventAlarm::SOUND_ALARM);
1546             break;
1547         case CAL_ALERT_MUTE:
1548         default:
1549             methods.push_back(EventAlarm::NO_ALARM);
1550             break;
1551         }
1552         theAlarm->setMethods(methods);
1553
1554         char* description = calendar_svc_value_get_str(alarmValue, CAL_VALUE_TXT_ALARMS_DESCRIPTION);
1555         if (NULL == description) {
1556             LogError("Cannot read alarm description. Setting empty string.");
1557             theAlarm->setDescription("");
1558         } else {
1559             theAlarm->setDescription(description);
1560         }
1561
1562         // Save the alarm.
1563         m_abstractEvent->getAlarms()->push_back(theAlarm);
1564     }
1565     LogInfo("Number of alarms: "<<m_abstractEvent->getAlarms()->size());
1566 }
1567
1568 void EventWrapper::setStatusFromPlatformEvent()
1569 {
1570     if (!m_platformEvent) {
1571         ThrowMsg(UnknownException, "Null platform pointer.");
1572     }
1573
1574     cals_status_t status = static_cast<cals_status_t>(calendar_svc_struct_get_int(m_platformEvent,
1575                                              CAL_VALUE_INT_TASK_STATUS));
1576     switch (status) {
1577     case CALS_EVENT_STATUS_TENTATIVE:
1578         m_abstractEvent->setStatus(CalendarEvent::TENTATIVE_STATUS);
1579         break;
1580     case CALS_EVENT_STATUS_CONFIRMED:
1581         m_abstractEvent->setStatus(CalendarEvent::CONFIRMED_STATUS);
1582         break;
1583     case CALS_EVENT_STATUS_CANCELLED:
1584     case CALS_TODO_STATUS_CANCELLED:
1585         m_abstractEvent->setStatus(CalendarEvent::CANCELLED_STATUS);
1586         break;
1587     case CALS_TODO_STATUS_NEEDS_ACTION:
1588         m_abstractEvent->setStatus(CalendarEvent::NEEDS_ACTION_STATUS);
1589         break;
1590     case CALS_TODO_STATUS_COMPLETED:
1591         m_abstractEvent->setStatus(CalendarEvent::COMPLETED_STATUS);
1592         break;
1593     case CALS_TODO_STATUS_IN_PROCESS:
1594         m_abstractEvent->setStatus(CalendarEvent::IN_PROCESS_STATUS);
1595         break;
1596     default:
1597         m_abstractEvent->setStatus(CalendarEvent::UNDEFINED_STATUS);
1598         break;
1599     }
1600 }
1601
1602 void EventWrapper::setCategoriesFromPlatformEvent()
1603 {
1604     if (!m_platformEvent) {
1605         ThrowMsg(UnknownException, "Null platform pointer.");
1606     }
1607     m_abstractEvent->getCategories()->clear();
1608
1609     char *categories = calendar_svc_struct_get_str(
1610             m_platformEvent,
1611             CAL_VALUE_TXT_CATEGORIES);
1612
1613     if( categories ) {
1614         LogInfo("Loaded categories: "<<categories);
1615
1616         char *saveptr = NULL;
1617         char* pch = strtok_r(categories, ",", &saveptr);
1618         while (NULL != pch) {
1619             m_abstractEvent->getCategories()->push_back(pch);
1620             pch = strtok_r(NULL, ",", &saveptr);
1621         }
1622     }
1623
1624     LogInfo("Number of categories: "<<m_abstractEvent->getCategories()->size());
1625 }
1626
1627 void EventWrapper::setIdFromPlatformEvent()
1628 {
1629     if (!m_platformEvent) {
1630         ThrowMsg(UnknownException, "Null platform pointer.");
1631     }
1632     m_abstractEvent->setId(getIdFromPlatformEvent());
1633
1634     // Set the uid also.
1635     std::stringstream ss;
1636     ss<<getIdFromPlatformEvent();
1637     m_abstractEvent->setUId(ss.str());
1638 }
1639
1640 void EventWrapper::setParentIdFromPlatformEvent()
1641 {
1642     if (!m_platformEvent) {
1643         ThrowMsg(UnknownException, "Null platform pointer.");
1644     }
1645
1646     int parentId = calendar_svc_struct_get_int(m_platformEvent,
1647         CAL_VALUE_INT_ORIGINAL_EVENT_ID);
1648
1649     m_abstractEvent->setParentId(parentId);
1650 }
1651
1652 void EventWrapper::setIsAllDayFromPlatformEvent()
1653 {
1654     if (!m_platformEvent) {
1655         ThrowMsg(UnknownException, "Null platform pointer.");
1656     }
1657
1658     int timeType = calendar_svc_struct_get_int(m_platformEvent,
1659                                              CALS_VALUE_INT_DTSTART_TYPE);
1660
1661     if(timeType==CALS_TIME_LOCALTIME) {
1662         m_abstractEvent->setIsAllDay(true);
1663     } else {
1664         m_abstractEvent->setIsAllDay(false);
1665     }
1666 }
1667
1668 void EventWrapper::setOrganizerFromPlatformEvent()
1669 {
1670     if (!m_platformEvent) {
1671         ThrowMsg(UnknownException, "Null platform pointer.");
1672     }
1673     const char* organizer = calendar_svc_struct_get_str(m_platformEvent,
1674                                                        CAL_VALUE_TXT_ORGANIZER_NAME);
1675     if (organizer) {
1676         m_abstractEvent->setOrganizer(organizer);
1677     }
1678 }
1679
1680 void EventWrapper::setLastModifiedDateFromPlatformEvent()
1681 {
1682     if (!m_platformEvent) {
1683         ThrowMsg(UnknownException, "Null platform pointer.");
1684     }
1685     long long int lastModifiedDate = calendar_svc_struct_get_lli(
1686             m_platformEvent,
1687             CALS_VALUE_LLI_LASTMOD);
1688     m_abstractEvent->setLastModifiedDate(lastModifiedDate);
1689 }
1690
1691 void EventWrapper::setVisibilityFromPlatformEvent()
1692 {
1693     if (!m_platformEvent) {
1694         ThrowMsg(UnknownException, "Null platform pointer.");
1695     }
1696
1697     int visibility = calendar_svc_struct_get_int(m_platformEvent,
1698                                                  CAL_VALUE_INT_SENSITIVITY);
1699     switch (visibility) {
1700     case PUBLIC_VISIBILITY:
1701         m_abstractEvent->setVisibility(CalendarEvent::PUBLIC_VISIBILITY);
1702         break;
1703     case PRIVATE_VISIBILITY:
1704         m_abstractEvent->setVisibility(CalendarEvent::PRIVATE_VISIBILITY);
1705         break;
1706     case CONFIDENTIAL_VISIBILITY:
1707         m_abstractEvent->setVisibility(CalendarEvent::CONFIDENTIAL_VISIBILITY);
1708         break;
1709     default:
1710         m_abstractEvent->setVisibility(CalendarEvent::PUBLIC_VISIBILITY);
1711         break;
1712     }
1713 }
1714
1715 void EventWrapper::setAvailabilityFromPlatformEvent()
1716 {
1717     if (!m_platformEvent) {
1718         ThrowMsg(UnknownException, "Null platform pointer.");
1719     }
1720
1721     cal_status_type_t availability =
1722         static_cast<cal_status_type_t>(calendar_svc_struct_get_int(
1723                                           m_platformEvent,
1724                                           CAL_VALUE_INT_AVAILABILITY));
1725     switch (availability) {
1726     case EVENT_BUSY_FB:
1727         m_abstractEvent->setAvailability(CalendarEvent::BUSY_FB);
1728         break;
1729     case EVENT_BUSY_UNAVAILABLE_FB:
1730         m_abstractEvent->setAvailability(CalendarEvent::BUSY_UNAVAILABLE_FB);
1731         break;
1732     case EVENT_FREE_FB:
1733         m_abstractEvent->setAvailability(CalendarEvent::FREE_FB);
1734         break;
1735     case EVENT_BUSY_TENTATIVE_FB:
1736         m_abstractEvent->setAvailability(CalendarEvent::BUSY_TENTATIVE_FB);
1737         break;
1738     default:
1739         m_abstractEvent->setAvailability(CalendarEvent::FREE_FB);
1740         break;
1741     }
1742 }
1743
1744 void EventWrapper::setUIdFromPlatformEvent()
1745 {
1746     if (!m_platformEvent) {
1747         ThrowMsg(UnknownException, "Null platform pointer.");
1748     }
1749     const char* uid = calendar_svc_struct_get_str(m_platformEvent,
1750                                                        CAL_VALUE_TXT_UID);
1751     if (uid) {
1752         m_abstractEvent->setUId(uid);
1753     }
1754 }
1755
1756 void EventWrapper::setAttendeesFromPlatformEvent()
1757 {
1758     LogDebug("entered");
1759
1760     if (!m_platformEvent) {
1761         ThrowMsg(UnknownException, "Null platform pointer.");
1762     }
1763
1764     if ( NULL != m_abstractEvent->getAttendees() )
1765         m_abstractEvent->getAttendees()->clear();
1766
1767     GList *attendees = NULL;
1768     if (CAL_SUCCESS !=
1769         calendar_svc_struct_get_list(m_platformEvent,
1770                                      CAL_VALUE_LST_ATTENDEE_LIST,
1771                                      &attendees)) {
1772         LogError("Cannot read attendees list");
1773         return;
1774     }
1775     cal_value* attendee = NULL;
1776     for (; attendees; attendees = g_list_next(attendees)) {
1777         LogDebug("Processing attendee...");
1778         attendee = static_cast<cal_value*>(attendees->data);
1779         EventAttendeePtr attendeePtr(new EventAttendee());
1780
1781         // load name
1782         char* attendeeName = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_DETAIL_NAME);
1783         if (NULL == attendeeName) {
1784             LogError("Cannot read attendee name. Setting an empty string.");
1785             attendeePtr->setName("");
1786         } else {
1787             attendeePtr->setName(attendeeName);
1788         }
1789
1790         // load URI
1791         char* attendeeURI = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_DETAIL_EMAIL);
1792         if (NULL == attendeeURI) {
1793             LogError("Cannot read attendee URI. Setting an empty string.");
1794             attendeePtr->setURI("");
1795         } else {
1796             attendeePtr->setURI(attendeeURI);
1797         }
1798
1799         // load role
1800         cal_event_attendee_role_type_t attendeeRole =
1801             static_cast<cal_event_attendee_role_type_t>(calendar_svc_value_get_int(
1802                                               attendee,
1803                                               CAL_VALUE_INT_ATTENDEE_ROLE));
1804         if (0 > attendeeRole) {
1805             LogError("Cannot read attendee role. Setting the default value.");
1806         }
1807         switch (attendeeRole) {
1808         case EVENT_ATTENDEE_REQ_PARTICIPANT_ROLE:
1809             attendeePtr->setRole(EventAttendee::REQ_PARTICIPANT_ROLE);
1810             break;
1811         case EVENT_ATTENDEE_OPT_PARTICIPANT_ROLE:
1812             attendeePtr->setRole(EventAttendee::OPT_PARTICIPANT_ROLE);
1813             break;
1814         case EVENT_ATTENDEE_NON_PARTICIPANT_ROLE:
1815             attendeePtr->setRole(EventAttendee::NON_PARTICIPANT_ROLE);
1816             break;
1817         case EVENT_ATTENDEE_CHAIR_ROLE:
1818             attendeePtr->setRole(EventAttendee::CHAIR_ROLE);
1819             break;
1820         default:
1821             attendeePtr->setRole(EventAttendee::REQ_PARTICIPANT_ROLE);
1822             break;
1823         }
1824
1825         // load status
1826         cal_event_attendee_status_type_t attendeeStatus =
1827             static_cast<cal_event_attendee_status_type_t>(calendar_svc_value_get_int(
1828                                               attendee,
1829                                               CAL_VALUE_INT_ATTENDEE_DETAIL_STATUS));
1830         if (0 > attendeeStatus) {
1831             LogError("Cannot read attendee status. Setting the default value.");
1832         }
1833         switch (attendeeStatus) {
1834         case EVENT_ATTENDEE_NEEDS_ACTION_AT_STATUS:
1835             attendeePtr->setStatus(EventAttendee::PENDING_AT_STATUS);
1836             break;
1837         case EVENT_ATTENDEE_ACCEPTED_AT_STATUS:
1838             attendeePtr->setStatus(EventAttendee::ACCEPTED_AT_STATUS);
1839             break;
1840         case EVENT_ATTENDEE_DECLINED_AT_STATUS:
1841             attendeePtr->setStatus(EventAttendee::DECLINED_AT_STATUS);
1842             break;
1843         case EVENT_ATTENDEE_TENTATIVE_AT_STATUS:
1844             attendeePtr->setStatus(EventAttendee::TENTATIVE_AT_STATUS);
1845             break;
1846         case EVENT_ATTENDEE_DELEGATED_AT_STATUS:
1847             attendeePtr->setStatus(EventAttendee::DELEGATED_AT_STATUS);
1848             break;
1849         case EVENT_ATTENDEE_COMPLETED_AT_STATUS:
1850             attendeePtr->setStatus(EventAttendee::COMPLETED_AT_STATUS);
1851             break;
1852         case EVENT_ATTENDEE_IN_PROCESS_AT_STATUS:
1853             attendeePtr->setStatus(EventAttendee::IN_PROCESS_AT_STATUS);
1854             break;
1855         default:
1856             attendeePtr->setStatus(EventAttendee::PENDING_AT_STATUS);
1857             break;
1858         }
1859
1860         // load RSVP
1861         int attendeeRSVP = calendar_svc_value_get_int(attendee, CAL_VALUE_INT_ATTENDEE_RSVP);
1862         if (0 > attendeeRSVP) {
1863             LogError("Cannot read attendee RSVP. Setting the default value.");
1864         }
1865         attendeePtr->setRSVP(attendeeRSVP>0 ? true : false);
1866
1867         // load type
1868         cal_event_attendee_type_t attendeeType =
1869             static_cast<cal_event_attendee_type_t>(calendar_svc_value_get_int(
1870                                               attendee,
1871                                               CAL_VALUE_INT_ATTENDEE_DETAIL_TYPE));
1872         if (0 > attendeeType) {
1873             LogError("Cannot read attendee type. Setting the default value.");
1874         }
1875         switch (attendeeType) {
1876         case EVENT_ATTENDEE_INDIVIDUAL_TYPE:
1877             attendeePtr->setType(EventAttendee::INDIVIDUAL_TYPE);
1878             break;
1879         case EVENT_ATTENDEE_GROUP_TYPE:
1880             attendeePtr->setType(EventAttendee::GROUP_TYPE);
1881             break;
1882         case EVENT_ATTENDEE_RESOURCE_TYPE:
1883             attendeePtr->setType(EventAttendee::RESOURCE_TYPE);
1884             break;
1885         case EVENT_ATTENDEE_ROOM_TYPE:
1886             attendeePtr->setType(EventAttendee::ROOM_TYPE);
1887             break;
1888         case EVENT_ATTENDEE_UNKNOWN_TYPE:
1889             attendeePtr->setType(EventAttendee::UNKNOWN_TYPE);
1890             break;
1891         default:
1892             attendeePtr->setType(EventAttendee::INDIVIDUAL_TYPE);
1893             break;
1894         }
1895
1896         // load group
1897         char* attendeeGroup = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_GROUP);
1898         if (NULL == attendeeGroup) {
1899             LogError("Cannot read attendee group. Setting an empty string.");
1900             attendeePtr->setGroup("");
1901         } else {
1902             attendeePtr->setGroup(attendeeGroup);
1903         }
1904
1905         // load delegatorURI
1906         char* attendeeDelegatorURI = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_DELEGATOR_URI);
1907         if (NULL == attendeeDelegatorURI) {
1908             LogError("Cannot read attendee delegatorURI. Setting an empty string.");
1909             attendeePtr->setDelegatorURI("");
1910         } else {
1911             attendeePtr->setDelegatorURI(attendeeDelegatorURI);
1912         }
1913
1914         // load delegateURI
1915         char* attendeeDelegateURI = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_DELEGATE_URI);
1916         if (NULL == attendeeDelegateURI) {
1917             LogError("Cannot read attendee delegateURI. Setting an empty string.");
1918             attendeePtr->setDelegateURI("");
1919         } else {
1920             attendeePtr->setDelegateURI(attendeeDelegateURI);
1921         }
1922
1923         // load contactId
1924         char* attendeeContactId = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_UID);
1925         if (NULL == attendeeContactId) {
1926             LogError("Cannot read attendee UId. Setting an empty string.");
1927             attendeePtr->setContactId("");
1928         } else {
1929             attendeePtr->setContactId(attendeeContactId);
1930         }
1931
1932         // load addressBookId
1933         int attendeeAddressBookId = calendar_svc_value_get_int(attendee, CAL_VALUE_INT_ATTENDEE_DETAIL_CT_INDEX);
1934         LogInfo("attendeeAddressBookId: "<<attendeeAddressBookId);
1935         if ( 0>attendeeAddressBookId) {
1936             LogError("Cannot read attendee address book id. Setting an empty string.");
1937             attendeeAddressBookId = -1;
1938         }
1939         std::stringstream ss;
1940         if (attendeeAddressBookId>=0) {
1941             ss<<attendeeAddressBookId;
1942         } else {
1943             ss<<"";
1944         }
1945         attendeePtr->setAddressBookId(ss.str());
1946
1947         m_abstractEvent->getAttendees()->push_back(attendeePtr);
1948     }
1949 }
1950
1951 void EventWrapper::setPositionFromPlatformEvent()
1952 {
1953     if (!m_platformEvent) {
1954         ThrowMsg(UnknownException, "Null platform pointer.");
1955     }
1956
1957     double value = calendar_svc_struct_get_double(
1958             m_platformEvent,
1959             CAL_VALUE_DBL_LATITUDE);
1960     m_abstractEvent->setLatitude(value);
1961
1962     value = calendar_svc_struct_get_double(
1963             m_platformEvent,
1964             CAL_VALUE_DBL_LONGITUDE);
1965     m_abstractEvent->setLongitude(value);
1966 }
1967
1968 void EventWrapper::setPriorityFromPlatformEvent()
1969 {
1970     if (!m_platformEvent) {
1971         ThrowMsg(UnknownException, "Null platform pointer.");
1972     }
1973
1974     cal_priority_type_t priority = static_cast<cal_priority_type_t>(calendar_svc_struct_get_int(m_platformEvent,
1975                                              CAL_VALUE_INT_PRIORITY));
1976     switch (priority) {
1977     case EVENT_PRIORITY_LOW:
1978         m_abstractEvent->setPriority(CalendarEvent::LOW_PRIORITY);
1979         break;
1980     case EVENT_PRIORITY_NORMAL:
1981         m_abstractEvent->setPriority(CalendarEvent::MEDIUM_PRIORITY);
1982         break;
1983     case EVENT_PRIORITY_HIGH:
1984         m_abstractEvent->setPriority(CalendarEvent::HIGH_PRIORITY);
1985         break;
1986     default:
1987         m_abstractEvent->setPriority(CalendarEvent::MEDIUM_PRIORITY);
1988         break;
1989     }
1990 }
1991
1992 void EventWrapper::setCreatedDateFromPlatformEvent()
1993 {
1994     if (!m_platformEvent) {
1995         ThrowMsg(UnknownException, "Null platform pointer.");
1996     }
1997
1998     long long int createdDate = 0;
1999     createdDate = calendar_svc_struct_get_lli(m_platformEvent,
2000                                                   CALS_VALUE_LLI_LASTMOD);
2001
2002     m_abstractEvent->setCreatedDate(createdDate);
2003 }
2004
2005 void EventWrapper::setCompletedDateFromPlatformEvent()
2006 {
2007     if (!m_platformEvent) {
2008         ThrowMsg(UnknownException, "Null platform pointer.");
2009     }
2010
2011     long long int completedDate = 0;
2012     completedDate = calendar_svc_struct_get_lli(m_platformEvent,
2013                                                   CAL_VALUE_LLI_COMPLETED_TIME);
2014
2015     LogInfo("completedDate: "<<completedDate);
2016     m_abstractEvent->setCompletedDate(completedDate);
2017 }
2018
2019 void EventWrapper::setProgressFromPlatformEvent()
2020 {
2021     if (!m_platformEvent) {
2022         ThrowMsg(UnknownException, "Null platform pointer.");
2023     }
2024
2025     int progress = 0;
2026     progress = calendar_svc_struct_get_int(m_platformEvent, CAL_VALUE_INT_PROGRESS);
2027
2028     LogInfo("progress: "<<progress);
2029     m_abstractEvent->setProgress(progress);
2030 }
2031
2032 void EventWrapper::setIsDetachedFromPlatformEvent()
2033 {
2034     if (!m_platformEvent) {
2035         ThrowMsg(UnknownException, "Null platform pointer.");
2036     }
2037
2038     int isDetached = calendar_svc_struct_get_int(m_platformEvent, CAL_VALUE_INT_ORIGINAL_EVENT_ID);
2039     if (isDetached>0) {
2040         m_abstractEvent->setIsDetached(true);
2041         LogInfo("This is a detached event.");
2042     } else {
2043         m_abstractEvent->setIsDetached(false);
2044     }
2045 }
2046
2047 void EventWrapper::displayAbstractEvent()
2048 {
2049     m_abstractEvent->display();
2050 }
2051
2052 void EventWrapper::displayPlatformEvent()
2053 {
2054     LogDebug("event id: " << calendar_svc_struct_get_int(m_platformEvent,
2055                                                          CAL_VALUE_INT_INDEX));
2056
2057     long long int startTime = calendar_svc_struct_get_lli(
2058                  m_platformEvent, CALS_VALUE_LLI_DTSTART_UTIME);
2059     LogDebug("event start time: " << startTime);
2060
2061     LogDebug("event end time: " << calendar_svc_struct_get_lli(m_platformEvent, CALS_VALUE_LLI_DTEND_UTIME));
2062     LogDebug("event location: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_LOCATION));
2063     LogDebug("event summary: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_SUMMARY));
2064     LogDebug("event description: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_DESCRIPTION));
2065     LogDebug("event isAllDay: " << calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_DTSTART_TYPE));
2066     LogDebug("event organizer: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_ORGANIZER_NAME));
2067     LogDebug("event lastModifiedDate: " << calendar_svc_struct_get_lli(m_platformEvent, CALS_VALUE_LLI_LASTMOD));
2068     LogDebug("event visibility: " << calendar_svc_struct_get_int(m_platformEvent, CAL_VALUE_INT_SENSITIVITY));
2069     LogDebug("event availability: " << calendar_svc_struct_get_int(m_platformEvent, CAL_VALUE_INT_BUSY_STATUS));
2070     LogDebug("event categories: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_CATEGORIES));
2071     LogDebug("event uid: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_UID));
2072
2073     GList *attendees = NULL;
2074     if (CAL_SUCCESS != calendar_svc_struct_get_list(m_platformEvent, CAL_VALUE_LST_ATTENDEE_LIST, &attendees)) {
2075         LogError("cannot read attendee list");
2076         return;
2077     }
2078     cal_value* attendee = NULL;
2079     for (; attendees; attendees = g_list_next(attendees)) {
2080         attendee = static_cast<cal_value*>(attendees->data);
2081         char* attendeeName = calendar_svc_value_get_str(attendee, CAL_VALUE_TXT_ATTENDEE_DETAIL_NAME);
2082         if (NULL == attendeeName) {
2083             LogError("cannot read attendee name");
2084             return;
2085         }
2086         LogDebug("event attendees: " << attendeeName);
2087     }
2088
2089     LogDebug("event repeat frequency: " << calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_RRULE_FREQ));
2090     LogDebug("event repeat interval: " << calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_RRULE_INTERVAL));
2091     LogDebug("event repeat occurrence count: " << calendar_svc_struct_get_int(m_platformEvent, CALS_VALUE_INT_RRULE_COUNT));
2092     LogDebug("event repeat end date: " << calendar_svc_struct_get_lli(m_platformEvent, CALS_VALUE_LLI_RRULE_UNTIL_UTIME));
2093     LogDebug("event exceptions: " << calendar_svc_struct_get_str(m_platformEvent, CAL_VALUE_TXT_EXDATE));
2094 }
2095
2096 CalendarEvent::CalendarType EventWrapper::getType()
2097 {
2098     return m_calendarType;
2099 }
2100
2101 }
2102 }
2103 }