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