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