79636d4ca4d1b9f450dabc07e06617dcf02daaea
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarItemProperties.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 <ctime>
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenException.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <Tizen/TimeUtil/TimeUtilConverter.h>
26 #include <Tizen/TimeUtil/JSTZDate.h>
27 #include <API/Calendar/EventAlarm.h>
28 #include <API/Calendar/EventId.h>
29 #include "API/TimeUtil/DurationProperties.h"
30 #include "JSCalendarItemProperties.h"
31 #include "CalendarConverter.h"
32 #include "JSEventId.h"
33 #include "JSEventAlarm.h"
34 #include "JSRecurrenceRule.h"
35 #include "JSAttendee.h"
36
37 using namespace TizenApis::Api::Calendar;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40
41 namespace TizenApis {
42 namespace Tizen1_0 {
43 namespace Calendar {
44
45 #define TIZEN_CALENDAR_ITEM_PROPERTIES_ATTRIBUTENAME "CalendarItemProperties"
46
47 JSClassDefinition JSCalendarItemProperties::m_classInfo = {
48     0,
49     kJSClassAttributeNone,
50     TIZEN_CALENDAR_ITEM_PROPERTIES_ATTRIBUTENAME,
51     0,
52     m_property,
53     NULL, //m_function,
54     initialize,
55     finalize,
56     NULL, //hasProperty,
57     NULL, //getProperty,
58     NULL, //setProperty,
59     NULL, //DeleteProperty,
60     NULL, //GetPropertyNames,
61     NULL, //CallAsFunction,
62     NULL, //CallAsConstructor,
63     NULL, //HasInstance,
64     NULL  //ConvertToType
65 };
66
67 JSStaticValue JSCalendarItemProperties::m_property[] = {
68     // Item Properties
69     { TIZEN_CALENDAR_ITEM_DESCRIPTION, getPropertyDescription,
70       setPropertyDescription, kJSPropertyAttributeNone },
71     { TIZEN_CALENDAR_ITEM_SUMMARY, getPropertySummary,
72       setPropertySummary, kJSPropertyAttributeNone },
73     { TIZEN_CALENDAR_ITEM_START_DATE, getPropertyStartTime,
74       setPropertyStartTime, kJSPropertyAttributeNone },
75     { TIZEN_CALENDAR_ITEM_LOCATION, getPropertyLocation,
76       setPropertyLocation, kJSPropertyAttributeNone },
77     { TIZEN_CALENDAR_ITEM_GEOLOCATION, getPropertyGeolocation,
78       setPropertyGeolocation, kJSPropertyAttributeNone },
79     { TIZEN_CALENDAR_ITEM_ORGANIZER, getPropertyOrganizer,
80       setPropertyOrganizer, kJSPropertyAttributeNone },
81     { TIZEN_CALENDAR_ITEM_VISIBILITY, getPropertyVisibility,
82       setPropertyVisibility, kJSPropertyAttributeNone },
83     { TIZEN_CALENDAR_ITEM_STATUS, getPropertyStatus,
84       setPropertyStatus, kJSPropertyAttributeNone },
85     { TIZEN_CALENDAR_ITEM_ALARMS, getPropertyAlarms,
86       setPropertyAlarms, kJSPropertyAttributeNone },
87     { TIZEN_CALENDAR_ITEM_CATEGORIES, getPropertyCategories,
88       setPropertyCategories, kJSPropertyAttributeNone },
89     { TIZEN_CALENDAR_ITEM_DURATION, getPropertyDuration,
90       setPropertyDuration, kJSPropertyAttributeNone },
91     { TIZEN_CALENDAR_ITEM_IS_ALL_DAY, getPropertyIsAllDay,
92       setPropertyIsAllDay, kJSPropertyAttributeNone },
93     { TIZEN_CALENDAR_ITEM_ATTENDEES, getPropertyAttendees,
94       setPropertyAttendees, kJSPropertyAttributeNone },
95     { TIZEN_CALENDAR_EVENT_RECURRENCE_RULE, getPropertyRecurrenceRule,
96       setPropertyRecurrenceRule, kJSPropertyAttributeNone },
97     { TIZEN_CALENDAR_EVENT_AVAILABILITY, getPropertyAvailability,
98       setPropertyAvailability, kJSPropertyAttributeNone },
99     { TIZEN_CALENDAR_TASK_DUE_DATE, getPropertyDueDate,
100       setPropertyDueDate, kJSPropertyAttributeNone },
101     { TIZEN_CALENDAR_TASK_COMPLETED_DATE, getPropertyCompletedDate,
102       setPropertyCompletedDate, kJSPropertyAttributeNone },
103     { TIZEN_CALENDAR_TASK_PROGRESS, getPropertyProgress,
104       setPropertyProgress, kJSPropertyAttributeNone },
105     { TIZEN_CALENDAR_ITEM_PRIORITY, getPropertyPriority,
106       setPropertyPriority, kJSPropertyAttributeNone },
107     { TIZEN_CALENDAR_EVENT_END_DATE, getPropertyEndDate,
108       setPropertyEndDate, kJSPropertyAttributeNone },
109
110     { 0, 0, 0, 0 }
111 };
112
113 JSClassRef JSCalendarItemProperties::m_jsClassRef = JSClassCreate(JSCalendarItemProperties::getClassInfo());
114
115 void JSCalendarItemProperties::initialize(JSContextRef context,
116         JSObjectRef object)
117 {
118     LogDebug("entered");
119     CalendarItemPropertiesPrivObject *priv =
120         static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
121     if (!priv) {
122         CalendarEventPtr privateData(new CalendarEvent());
123         priv = new CalendarItemPropertiesPrivObject(context, privateData);
124         JSObjectSetPrivate(object, static_cast<void*>(priv));
125         LogDebug("New CalendarItemPropertiesPrivObject is created.");
126     } else {
127         LogDebug("CalendarItemPropertiesPrivObject already exists.");
128     }
129 }
130
131 void JSCalendarItemProperties::finalize(JSObjectRef object)
132 {
133     LogDebug("entered");
134     CalendarItemPropertiesPrivObject *priv =
135         static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
136     delete priv;
137 }
138
139 const JSClassRef JSCalendarItemProperties::getClassRef()
140 {
141     if (!m_jsClassRef) {
142         m_jsClassRef = JSClassCreate(&m_classInfo);
143     }
144     return m_jsClassRef;
145 }
146
147 const JSClassDefinition* JSCalendarItemProperties::getClassInfo()
148 {
149     return &m_classInfo;
150 }
151
152 CalendarEventPtr JSCalendarItemProperties::getPrivateObject(JSObjectRef object)
153 {
154     LogDebug("entered");
155     CalendarItemPropertiesPrivObject *priv =
156         static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
157     if (!priv) {
158         ThrowMsg(NullPointerException, "Private object is null");
159     }
160     CalendarEventPtr result = priv->getObject();
161     if (!result) {
162         ThrowMsg(NullPointerException, "Private object is null");
163     }
164     return result;
165 }
166
167 void JSCalendarItemProperties::setPrivateObject(const CalendarEventPtr &event,
168         JSContextRef ctx,
169         const JSObjectRef object)
170 {
171     LogDebug("entered");
172     Try
173     {
174         CalendarItemPropertiesPrivObject *priv =
175             static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
176         delete priv;
177         priv = new CalendarItemPropertiesPrivObject(ctx, event);
178         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
179             delete priv;
180         }
181     }
182     Catch(Exception)
183     {
184         LogError("Error during replacing event object");
185     }
186 }
187
188 JSValueRef JSCalendarItemProperties::getPropertyDescription(JSContextRef context,
189         JSObjectRef object,
190         JSStringRef propertyName,
191         JSValueRef* exception)
192 {
193     LogDebug("entered");
194     Try
195     {
196         Converter converter(context);
197         CalendarEventPtr event = getPrivateObject(object);
198         return converter.toJSValueRef(event->getDescription());
199     }
200     Catch(Exception)
201     {
202         LogWarning("trying to get incorrect value");
203     }
204     return JSValueMakeUndefined(context);
205 }
206
207 bool JSCalendarItemProperties::setPropertyDescription(JSContextRef context,
208         JSObjectRef object,
209         JSStringRef propertyName,
210         JSValueRef value,
211         JSValueRef* exception)
212 {
213     LogDebug("entered");
214     Try
215     {
216         if (!JSValueIsString(context, value)) {
217             Throw(InvalidArgumentException);
218         }
219
220         Converter converter(context);
221         CalendarEventPtr event = getPrivateObject(object);
222         std::string description = converter.toString(value);
223         event->setDescription(description);
224         return true;
225     }
226     Catch(Exception)
227     {
228         LogWarning("trying to set incorrect value");
229         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
230     }
231
232     return false;
233 }
234
235 JSValueRef JSCalendarItemProperties::getPropertySummary(JSContextRef context,
236         JSObjectRef object,
237         JSStringRef propertyName,
238         JSValueRef* exception)
239 {
240     LogDebug("entered");
241     Try
242     {
243         Converter converter(context);
244         CalendarEventPtr event = getPrivateObject(object);
245         return converter.toJSValueRef(event->getSubject());
246     }
247     Catch(Exception)
248     {
249         LogWarning("trying to get incorrect value");
250     }
251     return JSValueMakeUndefined(context);
252 }
253
254 bool JSCalendarItemProperties::setPropertySummary(JSContextRef context,
255         JSObjectRef object,
256         JSStringRef propertyName,
257         JSValueRef value,
258         JSValueRef* exception)
259 {
260     LogDebug("entered");
261     Try
262     {
263         if (!JSValueIsString(context, value)) {
264             Throw(InvalidArgumentException);
265         }
266
267         Converter converter(context);
268         CalendarEventPtr event = getPrivateObject(object);
269         std::string subject = converter.toString(value);
270         event->setSubject(subject);
271         return true;
272     }
273     Catch(Exception)
274     {
275         LogWarning("trying to set incorrect value");
276         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
277     }
278
279     return false;
280 }
281
282 JSValueRef JSCalendarItemProperties::getPropertyStartTime(JSContextRef context,
283         JSObjectRef object,
284         JSStringRef propertyName,
285         JSValueRef* exception)
286 {
287     LogDebug("entered");
288     Try
289     {
290         CalendarItemPropertiesPrivObject *privateObject =
291             static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
292         TimeUtilConverter timeConverter(privateObject->getContext());
293         CalendarEventPtr event = privateObject->getObject();
294
295         LogInfo("start time before converted to TZDate: "<<event->getStartTime()<<", time zone: "<<event->getTimeZone());
296         return timeConverter.FromUTCTimeTToTZDate(event->getStartTime(), event->getTimeZone());
297     }
298     Catch(Exception)
299     {
300         LogWarning("trying to get incorrect value");
301     }
302     return JSValueMakeUndefined(context);
303 }
304
305 bool JSCalendarItemProperties::setPropertyStartTime(JSContextRef context,
306         JSObjectRef object,
307         JSStringRef propertyName,
308         JSValueRef value,
309         JSValueRef* exception)
310 {
311     LogDebug("entered");
312     Try
313     {
314         if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
315             Throw(InvalidArgumentException);
316         }
317
318         CalendarEventPtr event = getPrivateObject(object);
319         TimeUtilConverter converter(context);
320         std::time_t duration = event->getEndTime() - event->getStartTime();
321         if (duration<0) {
322             duration = 0;
323         }
324
325         std::time_t startTime = converter.toTZDateTimeT(value);
326         event->setStartTime(startTime);
327         event->setEndTime(startTime + duration);
328
329         std::string timeZone = converter.getPropertiesInTZDate(value).timezone;
330         event->setTimeZone(timeZone);
331         return true;
332     }
333     Catch(Exception)
334     {
335         LogWarning("trying to set incorrect value");
336         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
337     }
338
339     return false;
340 }
341
342 JSValueRef JSCalendarItemProperties::getPropertyLocation(JSContextRef context,
343         JSObjectRef object,
344         JSStringRef propertyName,
345         JSValueRef* exception)
346 {
347     LogDebug("entered");
348     Try
349     {
350         Converter converter(context);
351         CalendarEventPtr event = getPrivateObject(object);
352         return converter.toJSValueRef(event->getLocation());
353     }
354     Catch(Exception)
355     {
356         LogWarning("trying to get incorrect value");
357     }
358     return JSValueMakeUndefined(context);
359 }
360
361 bool JSCalendarItemProperties::setPropertyLocation(JSContextRef context,
362         JSObjectRef object,
363         JSStringRef propertyName,
364         JSValueRef value,
365         JSValueRef* exception)
366 {
367     LogDebug("entered");
368     Try
369     {
370         if (!JSValueIsString(context, value)) {
371             Throw(InvalidArgumentException);
372         }
373
374         Converter converter(context);
375         CalendarEventPtr event = getPrivateObject(object);
376         std::string location = converter.toString(value);
377         event->setLocation(location);
378         return true;
379     }
380     Catch(Exception)
381     {
382         LogWarning("trying to set incorrect value");
383         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
384     }
385
386     return false;
387 }
388
389 JSValueRef JSCalendarItemProperties::getPropertyCategories(JSContextRef context,
390         JSObjectRef object,
391         JSStringRef propertyName,
392         JSValueRef* exception)
393 {
394     LogDebug("entered");
395     Try
396     {
397         Converter converter(context);
398         CalendarEventPtr event = getPrivateObject(object);
399         CategoryListPtr categories = event->getCategories();
400         if (categories) {
401             return converter.toJSValueRef(*categories);
402         }
403     }
404     Catch(Exception)
405     {
406         LogWarning("trying to get incorrect value");
407     }
408     return JSValueMakeUndefined(context);
409 }
410
411 bool JSCalendarItemProperties::setPropertyCategories(JSContextRef context,
412         JSObjectRef object,
413         JSStringRef propertyName,
414         JSValueRef value,
415         JSValueRef* exception)
416 {
417     LogDebug("entered");
418     Try
419     {
420         CalendarEventPtr event = getPrivateObject(object);
421         CalendarConverterFactory::ConverterType converter =
422             CalendarConverterFactory::getConverter(context);
423         event->setCategories(converter->toCategories(value));
424         return true;
425     }
426     Catch(Exception)
427     {
428         LogWarning("trying to set incorrect value");
429         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
430     }
431
432     return false;
433 }
434
435 JSValueRef JSCalendarItemProperties::getPropertyStatus(JSContextRef context,
436         JSObjectRef object,
437         JSStringRef propertyName,
438         JSValueRef* exception)
439 {
440     LogDebug("entered");
441     Try
442     {
443         CalendarConverterFactory::ConverterType converter =
444             CalendarConverterFactory::getConverter(context);
445         CalendarEventPtr event = getPrivateObject(object);
446         std::string status = converter->toTizenValue(event->getStatus());
447         return converter->toJSValueRef(status);
448     }
449     Catch(Exception)
450     {
451         LogWarning("trying to get incorrect value");
452     }
453     return JSValueMakeUndefined(context);
454 }
455
456 bool JSCalendarItemProperties::setPropertyStatus(JSContextRef context,
457         JSObjectRef object,
458         JSStringRef propertyName,
459         JSValueRef value,
460         JSValueRef* exception)
461 {
462     LogDebug("entered");
463     CalendarEventPtr event(NULL);
464     Try
465     {
466         if (!JSValueIsString(context, value)) {
467             Throw(InvalidArgumentException);
468         }
469
470         event = getPrivateObject(object);
471         CalendarConverterFactory::ConverterType converter =
472             CalendarConverterFactory::getConverter(context);
473         CalendarEvent::EventStatus status =
474             converter->toEventStatus(converter->toString(value));
475         event->setStatus(status);
476         return true;
477     }
478     Catch(Exception)
479     {
480         LogWarning("trying to set incorrect value");
481         event->setStatus(CalendarEvent::INVALID_STATUS);
482         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
483     }
484
485     return false;
486 }
487
488 JSValueRef JSCalendarItemProperties::getPropertyAlarms(JSContextRef context,
489         JSObjectRef object,
490         JSStringRef propertyName,
491         JSValueRef* exception)
492 {
493     LogDebug("entered");
494     Try
495     {
496         CalendarConverterFactory::ConverterType converter =
497             CalendarConverterFactory::getConverter(context);
498         CalendarEventPtr event = getPrivateObject(object);
499
500         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
501         if (NULL == jsResult) {
502             ThrowMsg(NullPointerException, "Could not create js array object");
503         }
504         for( unsigned int i=0; i<event->getAlarmsTick().size(); i++) {
505             EventAlarmPtr alarm(new EventAlarm());
506             TizenApis::Api::TimeUtil::DurationProperties duration;
507             duration.unit = TizenApis::Api::TimeUtil::MINUTES_UNIT;
508             duration.length = event->getAlarmsTick().at(i);
509             alarm->setDuration(duration); // Default unit is minute.
510             std::vector<CalendarEvent::EventAlarmType> methodVector;
511             methodVector.push_back(event->getAlarmsType().at(i)); // Only one alarm type is saved.
512             alarm->setMethods(methodVector);
513             alarm->setTimeZone(event->getTimeZone());
514
515             if (!JSSetArrayElement(context, jsResult, i, JSEventAlarm::createJSObject(context, alarm))) {
516                ThrowMsg(UnknownException, "Could not insert value into js array");
517             }
518         }
519         return jsResult;
520     }
521     Catch(Exception)
522     {
523         LogWarning("trying to get incorrect value");
524     }
525     return JSValueMakeUndefined(context);
526 }
527
528 bool JSCalendarItemProperties::setPropertyAlarms(JSContextRef context,
529         JSObjectRef object,
530         JSStringRef propertyName,
531         JSValueRef value,
532         JSValueRef* exception)
533 {
534     LogDebug("entered");
535     CalendarEventPtr event(NULL);
536     Try
537     {
538         event = getPrivateObject(object);
539         CalendarConverterFactory::ConverterType converter =
540             CalendarConverterFactory::getConverter(context);
541
542         EventAlarmListPtr alarms = converter->toVectorOfEventAlarms(value);
543         std::vector<CalendarEvent::EventAlarmType> alarmsType;
544         std::vector<long> alarmsTick;
545         // Set the multiple alarms.
546         for( unsigned int i=0; i<alarms->size(); i++) {
547             EventAlarmPtr theAlarm = alarms->at(i);
548             alarmsType.push_back(theAlarm->getMethods().at(0));
549             if( 0 < theAlarm->getAbsoluteDate() ) {
550                 alarmsTick.push_back(theAlarm->getAbsoluteDate()/60);
551             } else {
552                 long tick;
553                 long length = theAlarm->getDuration().length;
554                 switch(theAlarm->getDuration().unit) {
555                 case TizenApis::Api::TimeUtil::MSECS_UNIT:
556                     tick = length/(1000*60);
557                     break;
558                 case TizenApis::Api::TimeUtil::SECONDS_UNIT:
559                     tick = length/60;
560                     break;
561                 case TizenApis::Api::TimeUtil::MINUTES_UNIT:
562                     tick = length;
563                     break;
564                 case TizenApis::Api::TimeUtil::HOURS_UNIT:
565                     tick = length*60;
566                     break;
567                 case TizenApis::Api::TimeUtil::DAYS_UNIT:
568                     tick = length*60*24;
569                     break;
570                 default:
571                     ThrowMsg(ConversionException, "Wrong alarm unit type.");
572                 }
573
574                 alarmsTick.push_back(tick);
575             }
576         }
577         event->setAlarmsType(alarmsType);
578         event->setAlarmsTick(alarmsTick);
579         return true;
580     }
581     Catch(Exception)
582     {
583         LogWarning("trying to set incorrect value");
584         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
585     }
586
587     return false;
588 }
589
590 JSValueRef JSCalendarItemProperties::getPropertyOrganizer(JSContextRef context,
591         JSObjectRef object,
592         JSStringRef propertyName,
593         JSValueRef* exception)
594 {
595     LogDebug("entered");
596     Try
597     {
598         Converter converter(context);
599         CalendarEventPtr event = getPrivateObject(object);
600         return converter.toJSValueRef(event->getOrganizer());
601     }
602     Catch(Exception)
603     {
604         LogWarning("trying to get incorrect value");
605     }
606     return JSValueMakeUndefined(context);
607 }
608
609 bool JSCalendarItemProperties::setPropertyOrganizer(JSContextRef context,
610         JSObjectRef object,
611         JSStringRef propertyName,
612         JSValueRef value,
613         JSValueRef* exception)
614 {
615     LogDebug("entered");
616     Try
617     {
618         if (!JSValueIsString(context, value)) {
619             Throw(InvalidArgumentException);
620         }
621
622         Converter converter(context);
623         CalendarEventPtr event = getPrivateObject(object);
624         std::string organizer = converter.toString(value);
625         event->setOrganizer(organizer);
626         return true;
627     }
628     Catch(Exception)
629     {
630         LogWarning("trying to set incorrect value");
631         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
632     }
633
634     return false;
635 }
636
637 JSValueRef JSCalendarItemProperties::getPropertyVisibility(JSContextRef context,
638         JSObjectRef object,
639         JSStringRef propertyName,
640         JSValueRef* exception)
641 {
642     LogDebug("entered");
643     Try
644     {
645         CalendarConverterFactory::ConverterType converter =
646             CalendarConverterFactory::getConverter(context);
647         CalendarEventPtr event = getPrivateObject(object);
648         std::string visibility = converter->toTizenValue(event->getVisibility());
649         return converter->toJSValueRef(visibility);
650     }
651     Catch(Exception)
652     {
653         LogWarning("trying to get incorrect value");
654     }
655     return JSValueMakeUndefined(context);
656 }
657
658 bool JSCalendarItemProperties::setPropertyVisibility(JSContextRef context,
659         JSObjectRef object,
660         JSStringRef propertyName,
661         JSValueRef value,
662         JSValueRef* exception)
663 {
664     LogDebug("entered");
665     CalendarEventPtr event(NULL);
666     Try
667     {
668         if (!JSValueIsString(context, value)) {
669             Throw(InvalidArgumentException);
670         }
671
672         event = getPrivateObject(object);
673         CalendarConverterFactory::ConverterType converter =
674             CalendarConverterFactory::getConverter(context);
675         CalendarEvent::EventVisibility visibility =
676             converter->toEventVisibility(converter->toString(value));
677         event->setVisibility(visibility);
678         return true;
679     }
680     Catch(Exception)
681     {
682         LogError("Error during setting a value");
683         event->setVisibility(CalendarEvent::INVALID_VISIBILITY);
684         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
685     }
686
687     return false;
688 }
689
690 JSValueRef JSCalendarItemProperties::getPropertyGeolocation(JSContextRef context,
691         JSObjectRef object,
692         JSStringRef propertyName,
693         JSValueRef* exception)
694 {
695     LogDebug("entered");
696     Try
697     {
698         CalendarConverter converter(context);
699         CalendarEventPtr event = getPrivateObject(object);
700         CalendarItemGeoPtr geoInfo = event->getGeolocation();
701
702         return converter.toJSValueRef(geoInfo);
703     }
704     Catch(Exception)
705     {
706         LogWarning("trying to get incorrect value");
707     }
708
709     return JSValueMakeUndefined(context);
710 }
711
712 bool JSCalendarItemProperties::setPropertyGeolocation(JSContextRef context,
713         JSObjectRef object,
714         JSStringRef propertyName,
715         JSValueRef value,
716         JSValueRef* exception)
717 {
718     LogDebug("entered");
719     CalendarEventPtr event(NULL);
720     Try
721     {
722         event = getPrivateObject(object);
723         CalendarConverter converter(context);
724         event->setGeolocation(converter.toGeolocation(value));
725         return true;
726     }
727     Catch(Exception)
728     {
729         LogWarning("trying to set incorrect value");
730         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
731     }
732
733     return false;
734 }
735
736 JSValueRef JSCalendarItemProperties::getPropertyDuration(JSContextRef context,
737         JSObjectRef object,
738         JSStringRef propertyName,
739         JSValueRef* exception)
740 {
741     LogDebug("entered");
742     Try
743     {
744         CalendarEventPtr event = getPrivateObject(object);
745         CalendarItemPropertiesPrivObject *priv =
746             static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
747
748         // Use the global context saved in the event struct.
749         TimeUtilConverter converter(priv->getContext());
750         long length = event->getEndTime() - event->getStartTime(); // in seconds only
751         LogDebug("event->getStartTime():"<< event->getStartTime() << ", length:" << length);
752         return converter.makeMillisecondDurationObject( length*1000 );
753     }
754     Catch(Exception)
755     {
756         LogWarning("trying to get incorrect value");
757     }
758     return JSValueMakeUndefined(context);
759 }
760
761 bool JSCalendarItemProperties::setPropertyDuration(JSContextRef context,
762         JSObjectRef object,
763         JSStringRef propertyName,
764         JSValueRef value,
765         JSValueRef* exception)
766 {
767     LogDebug("entered");
768     Try
769     {
770         CalendarEventPtr event = getPrivateObject(object);
771         TimeUtilConverter converter(context);
772         long length = converter.getDurationLength(value);
773         int unit = converter.getDurationUnit(value);
774         if (length < 0) {
775             TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
776             return false;
777         }
778         if( SECONDS_UNIT==unit ) {
779             event->setEndTime(event->getStartTime() + length);
780         } else if ( MINUTES_UNIT==unit ) {
781             event->setEndTime(event->getStartTime() + length*60);
782         } else if ( HOURS_UNIT==unit ) {
783             event->setEndTime(event->getStartTime() + length*60*60);
784         } else if ( DAYS_UNIT==unit ) {
785             event->setEndTime(event->getStartTime() + length*24*60*60);
786         } else if ( MSECS_UNIT==unit ) {
787             event->setEndTime(event->getStartTime() + length/1000);
788         } else {
789             TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
790             return false;
791         }
792
793         return true;
794     }
795     Catch(Exception)
796     {
797         LogWarning("trying to set incorrect value");
798         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
799     }
800
801     return false;
802 }
803
804 JSValueRef JSCalendarItemProperties::getPropertyRecurrenceRule(JSContextRef context,
805         JSObjectRef object,
806         JSStringRef propertyName,
807         JSValueRef* exception)
808 {
809     LogDebug("entered");
810     Try
811     {
812         CalendarConverterFactory::ConverterType converter =
813             CalendarConverterFactory::getConverter(context);
814         CalendarEventPtr event = getPrivateObject(object);
815         EventRecurrenceRulePtr rrule = event->getRecurrenceRule();
816
817         if (rrule) {
818             return JSRecurrenceRule::createJSRecurrenceRule(context, rrule);
819         }
820     }
821     Catch(Exception)
822     {
823         LogWarning("trying to get incorrect value");
824     }
825     return JSValueMakeUndefined(context);
826 }
827
828 bool JSCalendarItemProperties::setPropertyRecurrenceRule(JSContextRef context,
829         JSObjectRef object,
830         JSStringRef propertyName,
831         JSValueRef value,
832         JSValueRef* exception)
833 {
834     LogDebug("entered");
835     CalendarEventPtr event(NULL);
836     Try
837     {
838         event = getPrivateObject(object);
839         CalendarConverterFactory::ConverterType converter =
840             CalendarConverterFactory::getConverter(context);
841         event->setRecurrenceRule(converter->toEventRecurrenceRule(value));
842         return true;
843     }
844     Catch(Exception)
845     {
846         LogWarning("trying to set incorrect value");
847         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
848     }
849
850     return false;
851 }
852
853 JSValueRef JSCalendarItemProperties::getPropertyIsAllDay(JSContextRef context,
854         JSObjectRef object,
855         JSStringRef propertyName,
856         JSValueRef* exception)
857 {
858     LogDebug("entered");
859     Try
860     {
861         CalendarConverterFactory::ConverterType converter =
862             CalendarConverterFactory::getConverter(context);
863         CalendarEventPtr event = getPrivateObject(object);
864         return converter->toJSValueRef(event->getIsAllDay());
865     }
866     Catch(Exception)
867     {
868         LogWarning("trying to get incorrect value");
869     }
870     return JSValueMakeUndefined(context);
871 }
872
873 bool JSCalendarItemProperties::setPropertyIsAllDay(JSContextRef context,
874         JSObjectRef object,
875         JSStringRef propertyName,
876         JSValueRef value,
877         JSValueRef* exception)
878 {
879     Try
880     {
881         if (!JSValueIsBoolean(context, value)) {
882             Throw(InvalidArgumentException);
883         }
884
885         CalendarEventPtr event = getPrivateObject(object);
886         Converter converter(context);
887         event->setIsAllDay(converter.toBool(value));
888         return true;
889     }
890     Catch(Exception)
891     {
892         LogWarning("trying to get incorrect value");
893         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
894     }
895
896     return false;
897 }
898
899 JSValueRef JSCalendarItemProperties::getPropertyAvailability(JSContextRef context,
900         JSObjectRef object,
901         JSStringRef propertyName,
902         JSValueRef* exception)
903 {
904     LogDebug("entered");
905     Try
906     {
907         CalendarConverterFactory::ConverterType converter =
908             CalendarConverterFactory::getConverter(context);
909         CalendarEventPtr event = getPrivateObject(object);
910         std::string availability = converter->toTizenValue(event->getAvailability());
911         return converter->toJSValueRef(availability);
912     }
913     Catch(Exception)
914     {
915         LogWarning("trying to get incorrect value");
916     }
917     return JSValueMakeUndefined(context);
918 }
919
920 bool JSCalendarItemProperties::setPropertyAvailability(JSContextRef context,
921         JSObjectRef object,
922         JSStringRef propertyName,
923         JSValueRef value,
924         JSValueRef* exception)
925 {
926     LogDebug("entered");
927     CalendarEventPtr event(NULL);
928     Try
929     {
930         if (!JSValueIsString(context, value)) {
931             Throw(InvalidArgumentException);
932         }
933
934         event = getPrivateObject(object);
935         CalendarConverterFactory::ConverterType converter =
936             CalendarConverterFactory::getConverter(context);
937         CalendarEvent::EventAvailability availability =
938             converter->toEventAvailability(converter->toString(value));
939         event->setAvailability(availability);
940         return true;
941     }
942     Catch(Exception)
943     {
944         LogError("Error during setting a value");
945         event->setAvailability(CalendarEvent::INVALID_AVAILABILITY);
946         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
947     }
948
949     return false;
950 }
951
952 JSValueRef JSCalendarItemProperties::getPropertyAttendees(JSContextRef context,
953         JSObjectRef object,
954         JSStringRef propertyName,
955         JSValueRef* exception)
956 {
957     LogDebug("entered");
958     Try
959     {
960         CalendarEventPtr event = getPrivateObject(object);
961         EventAttendeeListPtr attendees = event->getAttendees();
962         if (attendees) {
963             JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
964             if (NULL == jsResult) {
965                 ThrowMsg(NullPointerException, "Could not create js array object");
966             }
967             for(unsigned int i=0; i<attendees->size(); i++) {
968                 if (!JSSetArrayElement(context, jsResult, i, JSAttendee::createJSAttendee(context, attendees->at(i)))) {
969                    ThrowMsg(UnknownException, "Could not insert value into js array");
970                 }
971             }
972             return jsResult;
973         }
974     }
975     Catch(Exception)
976     {
977         LogWarning("trying to get incorrect value");
978     }
979     return JSValueMakeUndefined(context);
980 }
981
982 bool JSCalendarItemProperties::setPropertyAttendees(JSContextRef context,
983         JSObjectRef object,
984         JSStringRef propertyName,
985         JSValueRef value,
986         JSValueRef* exception)
987 {
988     LogDebug("entered");
989     Try
990     {
991         CalendarEventPtr event = getPrivateObject(object);
992         CalendarConverterFactory::ConverterType converter =
993             CalendarConverterFactory::getConverter(context);
994         event->setAttendees(converter->toVectorOfAttendees(value));
995         return true;
996     }
997     Catch(Exception)
998     {
999         LogWarning("trying to set incorrect attendee value");
1000         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
1001     }
1002
1003     return false;
1004 }
1005
1006 JSValueRef JSCalendarItemProperties::getPropertyDueDate(JSContextRef context,
1007         JSObjectRef object,
1008         JSStringRef propertyName,
1009         JSValueRef* exception)
1010 {
1011     LogDebug("entered");
1012     Try
1013         {
1014         CalendarItemPropertiesPrivObject *privateObject =
1015             static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
1016         CalendarEventPtr task = privateObject->getObject();
1017         if(CalendarEvent::TASK_TYPE != task->getCalendarType()) {
1018             return JSValueMakeUndefined(context);
1019         }
1020         if (!task) {
1021             Throw(NullPointerException);
1022         }
1023         if (task->getEndTime() != 0) {
1024             // Use the global context saved in the event struct.
1025             return JSTZDate::createJSObject(privateObject->getContext(), task->getEndTime(), task->getTimeZone());
1026         } else {
1027             return JSValueMakeUndefined(context);
1028         }
1029     }
1030     Catch(Exception)
1031     {
1032         LogWarning("trying to get incorrect value");
1033     }
1034     return JSValueMakeUndefined(context);
1035 }
1036
1037 bool JSCalendarItemProperties::setPropertyDueDate(JSContextRef context,
1038         JSObjectRef object,
1039         JSStringRef propertyName,
1040         JSValueRef value,
1041         JSValueRef* exception)
1042 {
1043     LogDebug("entered");
1044     Try
1045     {
1046         CalendarEventPtr task = getPrivateObject(object);
1047         if (!task) {
1048             Throw(NullPointerException);
1049         }
1050         if(CalendarEvent::TASK_TYPE != task->getCalendarType()) {
1051             Throw(InvalidArgumentException);
1052         }
1053         if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
1054             Throw(InvalidArgumentException);
1055         }
1056
1057         TimeUtilConverter converter(context);
1058         std::time_t dueDate = converter.toTZDateTimeT(value);
1059
1060         task->setEndTime(dueDate);
1061
1062         if( task->getTimeZone().empty() ) {
1063             std::string timeZone = converter.getPropertiesInTZDate(value).timezone;
1064             task->setTimeZone(timeZone);
1065         }
1066         return true;
1067     }
1068     Catch(Exception)
1069     {
1070         LogWarning("trying to set incorrect value");
1071         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
1072     }
1073
1074     return false;
1075 }
1076
1077 JSValueRef JSCalendarItemProperties::getPropertyCompletedDate(JSContextRef context,
1078         JSObjectRef object,
1079         JSStringRef propertyName,
1080         JSValueRef* exception)
1081 {
1082     LogDebug("entered");
1083     Try
1084     {
1085         CalendarItemPropertiesPrivObject *privateObject =
1086             static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
1087         CalendarEventPtr task = privateObject->getObject();
1088         if(CalendarEvent::TASK_TYPE != task->getCalendarType()) {
1089             return JSValueMakeUndefined(context);
1090         }
1091         if (!task) {
1092             Throw(NullPointerException);
1093         }
1094         if (task->getEndTime() != 0) {
1095             // Use the global context saved in the event struct.
1096             return JSTZDate::createJSObject(privateObject->getContext(), task->getEndTime(), task->getTimeZone());
1097         } else {
1098             return JSValueMakeUndefined(context);
1099         }
1100     }
1101     Catch(Exception)
1102     {
1103         LogWarning("trying to get incorrect value");
1104     }
1105     return JSValueMakeUndefined(context);
1106 }
1107
1108 bool JSCalendarItemProperties::setPropertyCompletedDate(JSContextRef context,
1109         JSObjectRef object,
1110         JSStringRef propertyName,
1111         JSValueRef value,
1112         JSValueRef* exception)
1113 {
1114     LogDebug("entered");
1115     Try
1116     {
1117         CalendarEventPtr task = getPrivateObject(object);
1118         if (!task) {
1119             Throw(NullPointerException);
1120         }
1121         if(CalendarEvent::TASK_TYPE != task->getCalendarType()) {
1122             Throw(InvalidArgumentException);
1123         }
1124         if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
1125             Throw(InvalidArgumentException);
1126         }
1127
1128         TimeUtilConverter converter(context);
1129         std::time_t completedDate = converter.toTZDateTimeT(value);
1130
1131         task->setEndTime(completedDate);
1132
1133         if( task->getTimeZone().empty() ) {
1134             std::string timeZone = converter.getPropertiesInTZDate(value).timezone;
1135             task->setTimeZone(timeZone);
1136         }
1137         return true;
1138     }
1139     Catch(Exception)
1140     {
1141         LogWarning("trying to set incorrect value");
1142         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
1143     }
1144
1145     return false;
1146 }
1147
1148 JSValueRef JSCalendarItemProperties::getPropertyProgress(JSContextRef context,
1149         JSObjectRef object,
1150         JSStringRef propertyName,
1151         JSValueRef* exception)
1152 {
1153     LogDebug("entered");
1154     Try
1155     {
1156         CalendarEventPtr task = getPrivateObject(object);
1157         if(CalendarEvent::TASK_TYPE != task->getCalendarType()) {
1158             return JSValueMakeUndefined(context);
1159         }
1160
1161         Converter converter(context);
1162         return converter.toJSValueRef(task->getProgress());
1163     }
1164     Catch(Exception)
1165     {
1166         LogWarning("trying to get incorrect value");
1167     }
1168     return JSValueMakeUndefined(context);
1169 }
1170
1171 bool JSCalendarItemProperties::setPropertyProgress(JSContextRef context,
1172         JSObjectRef object,
1173         JSStringRef propertyName,
1174         JSValueRef value,
1175         JSValueRef* exception)
1176 {
1177     LogDebug("entered");
1178     Try
1179     {
1180         if (!JSValueIsNumber(context, value)) {
1181             Throw(InvalidArgumentException);
1182         }
1183
1184         CalendarEventPtr task = getPrivateObject(object);
1185         if(CalendarEvent::TASK_TYPE != task->getCalendarType()) {
1186             return JSValueMakeUndefined(context);
1187         }
1188
1189         Converter converter(context);
1190         int progress = converter.toInt(value);
1191         task->setProgress(progress);
1192         return true;
1193     }
1194     Catch(Exception)
1195     {
1196         LogWarning("trying to set incorrect value");
1197         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
1198     }
1199
1200     return false;
1201 }
1202
1203 JSValueRef JSCalendarItemProperties::getPropertyPriority(JSContextRef context,
1204         JSObjectRef object,
1205         JSStringRef propertyName,
1206         JSValueRef* exception)
1207 {
1208     LogDebug("entered");
1209     Try
1210     {
1211         CalendarConverterFactory::ConverterType converter =
1212             CalendarConverterFactory::getConverter(context);
1213         CalendarEventPtr item = getPrivateObject(object);
1214         if (!item) {
1215             Throw(NullPointerException);
1216         }
1217         if(CalendarEvent::LOW_PRIORITY <= item->getPriority() ||
1218             CalendarEvent::HIGH_PRIORITY >= item->getPriority()) {
1219             return JSValueMakeUndefined(context);
1220         }   
1221             
1222         std::string priority = converter->toTizenValue(item->getPriority());
1223         return converter->toJSValueRef(priority);
1224     }
1225     Catch(Exception)
1226     {
1227         LogWarning("trying to get incorrect value");
1228     }
1229     return JSValueMakeUndefined(context);
1230 }
1231
1232 bool JSCalendarItemProperties::setPropertyPriority(JSContextRef context,
1233         JSObjectRef object,
1234         JSStringRef propertyName,
1235         JSValueRef value,
1236         JSValueRef* exception)
1237 {
1238     LogDebug("entered");
1239     CalendarEventPtr item = getPrivateObject(object);
1240     Try
1241     {
1242
1243         if (!JSValueIsString(context, value)) {
1244             Throw(InvalidArgumentException);
1245         }
1246
1247         CalendarConverterFactory::ConverterType converter =
1248             CalendarConverterFactory::getConverter(context);
1249         CalendarEvent::TaskPriority priority =
1250             converter->toTaskPriority(converter->toString(value));
1251         item->setPriority(priority);
1252         return true;
1253     }
1254     Catch(Exception)
1255     {
1256         LogWarning("trying to set incorrect value");
1257         if (item) {
1258             item->setPriority(CalendarEvent::INVALID_PRIORITY);
1259         }
1260         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
1261     }
1262
1263     return false;
1264 }
1265
1266 JSValueRef JSCalendarItemProperties::getPropertyEndDate(JSContextRef context,
1267         JSObjectRef object,
1268         JSStringRef propertyName,
1269         JSValueRef* exception)
1270 {
1271     LogDebug("entered");
1272     Try
1273     {
1274         CalendarItemPropertiesPrivObject *privateObject =
1275             static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
1276         CalendarEventPtr event = privateObject->getObject();
1277         if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
1278             return JSValueMakeUndefined(context);
1279         }
1280         if (!event) {
1281             Throw(NullPointerException);
1282         }
1283         if (event->getEndTime() != 0) {
1284             // Use the global context saved in the event struct.
1285             return JSTZDate::createJSObject(privateObject->getContext(), event->getEndTime(), event->getTimeZone());
1286         } else {
1287             return JSValueMakeUndefined(context);
1288         }
1289     }
1290     Catch(Exception)
1291     {
1292         LogWarning("trying to get incorrect value");
1293     }
1294     return JSValueMakeUndefined(context);
1295 }
1296
1297 bool JSCalendarItemProperties::setPropertyEndDate(JSContextRef context,
1298         JSObjectRef object,
1299         JSStringRef propertyName,
1300         JSValueRef value,
1301         JSValueRef* exception)
1302 {
1303     LogDebug("entered");
1304     Try
1305     {
1306         CalendarEventPtr event = getPrivateObject(object);
1307         if (!event) {
1308             Throw(NullPointerException);
1309         }
1310         if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
1311             Throw(InvalidArgumentException);
1312         }
1313         if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
1314             Throw(InvalidArgumentException);
1315         }
1316
1317         TimeUtilConverter converter(context);
1318         std::time_t endDate = converter.toTZDateTimeT(value);
1319
1320         event->setEndTime(endDate);
1321
1322         if( event->getTimeZone().empty() ) {
1323             std::string timeZone = converter.getPropertiesInTZDate(value).timezone;
1324             event->setTimeZone(timeZone);
1325         }
1326         return true;
1327     }
1328     Catch(Exception)
1329     {
1330         LogWarning("trying to set incorrect value");
1331         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
1332     }
1333
1334     return false;
1335 }
1336
1337 bool JSCalendarItemProperties::validate(JSContextRef ctx,
1338         const JSObjectRef object,
1339         JSValueRef* exception)
1340 {
1341     LogDebug("entered");
1342     CalendarItemPropertiesPrivObject *priv =
1343         static_cast<CalendarItemPropertiesPrivObject*>(JSObjectGetPrivate(object));
1344     if (priv == NULL) {
1345         return false;
1346     }
1347     CalendarEventPtr event = priv->getObject();
1348     if (!event) {
1349         return false;
1350     }
1351     return event->validate();
1352 }
1353
1354 }
1355 }
1356 }