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