656aefdefbe505d6dd29a14e4ef697ae19d5e6f0
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendar.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 <dpl/log/log.h>
19 #include <API/Calendar/ICalendar.h>
20 #include <API/Calendar/EventId.h>
21 #include <API/Calendar/CalendarFactory.h>
22 #include <API/Calendar/IEventAddEvent.h>
23 #include <API/Calendar/IEventAddEvents.h>
24 #include <API/Calendar/IEventDeleteEvent.h>
25 #include <API/Calendar/IEventDeleteEvents.h>
26 #include <API/Calendar/IEventFindEvents.h>
27 #include <API/Calendar/IEventUpdateEvent.h>
28 #include <API/Calendar/IEventUpdateEvents.h>
29 #include <API/Calendar/IEventCreateEventFromString.h>
30 #include <API/Calendar/IEventExportEventToString.h>
31 #include <API/Calendar/OnAddEventsChanged.h>
32 #include <API/Calendar/OnUpdateEventsChanged.h>
33 #include <API/Calendar/OnDeleteEventsChanged.h>
34 #include <API/Calendar/OnEventsChanged.h>
35
36 #include <CommonsJavaScript/PrivateObject.h>
37 #include <CommonsJavaScript/Converter.h>
38 #include <CommonsJavaScript/JSUtils.h>
39 #include <CommonsJavaScript/Utils.h>
40 #include <CommonsJavaScript/Validator.h>
41 #include <CommonsJavaScript/ScopedJSStringRef.h>
42 #include <Tizen/Common/JSTizenException.h>
43 #include <Tizen/Common/JSTizenExceptionFactory.h>
44 #include <Tizen/Common/SecurityExceptions.h>
45 #include <Tizen/Tizen/FilterConverter.h>
46 #include <Tizen/TimeUtil/TimeUtilConverter.h>
47
48 #include "JSCalendarManager.h"
49 #include "CalendarConverter.h"
50 #include "JSCalendar.h"
51 #include "JSCalendarItem.h"
52 #include "JSCalendarEvent.h"
53 #include "JSCalendarTask.h"
54 #include "JSCalendarItemProperties.h"
55 #include "JSEventId.h"
56 #include "plugin_config.h"
57 #include "CalendarResponseDispatcher.h"
58 #include "CalendarMultiCallback.h"
59
60 using namespace TizenApis::Api::Calendar;
61 using namespace WrtDeviceApis::Commons;
62 using namespace WrtDeviceApis::CommonsJavaScript;
63 using namespace TizenApis::Commons;
64
65 namespace {
66 /**
67  * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
68  */
69 JSValueRef getFunctionOrNull(JSContextRef ctx,
70         JSValueRef arg)
71 {
72     if (Validator(ctx).isCallback(arg)) {
73         return arg;
74     } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
75         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
76     }
77     return NULL;
78 }
79 }
80
81 #define TIZEN_CALENDAR_ATTRIBUTENAME "Calendar"
82
83 namespace TizenApis {
84 namespace Tizen1_0 { 
85 namespace Calendar {
86
87 JSClassDefinition JSCalendar::m_classInfo = {
88     0,
89     kJSClassAttributeNone,
90     TIZEN_CALENDAR_ATTRIBUTENAME,
91     NULL,
92     m_property,
93     m_function,
94     initialize,
95     finalize,
96     NULL, //HasProperty,
97     NULL, //GetProperty,
98     NULL, //SetProperty,
99     NULL, //DeleteProperty,
100     NULL, //GetPropertyNames,
101     NULL, //CallAsFunction,
102     NULL, //CallAsConstructor,
103     NULL, //HasInstance,
104     NULL //ConvertToType
105 };
106
107 JSStaticValue JSCalendar::m_property[] = {
108     { TIZEN_CALENDAR_PROPERTY_NAME, JSCalendar::getPropertyName,
109       NULL, kJSPropertyAttributeReadOnly },
110     { TIZEN_CALENDAR_PROPERTY_ACCOUNT_ID, JSCalendar::getPropertyAccountId,
111       NULL, kJSPropertyAttributeReadOnly },
112     { TIZEN_CALENDAR_PROPERTY_ID, JSCalendar::getPropertyId,
113       NULL, kJSPropertyAttributeReadOnly },
114     { 0, 0, 0, 0 }
115 };
116
117 JSStaticFunction JSCalendar::m_function[] = {
118     { "add", add, kJSPropertyAttributeNone },
119     { "addBatch", addBatch, kJSPropertyAttributeNone },
120     { "update", update, kJSPropertyAttributeNone },
121     { "updateBatch", updateBatch, kJSPropertyAttributeNone },
122     { "remove", remove, kJSPropertyAttributeNone },
123     { "removeBatch", removeBatch, kJSPropertyAttributeNone },
124     { "find", find, kJSPropertyAttributeNone },
125     { "convertFromString", convertFromString, kJSPropertyAttributeNone },
126     { "convertToString", convertToString, kJSPropertyAttributeNone },
127     { "addChangeListener", addChangeListener, kJSPropertyAttributeNone },
128     { "removeChangeListener", removeChangeListener, kJSPropertyAttributeNone },
129     { "expandEventRecurrence", expandEventRecurrence, kJSPropertyAttributeNone },
130
131     { 0, 0, 0 }
132 };
133
134 JSClassRef JSCalendar::m_jsClassRef = JSClassCreate(JSCalendar::getClassInfo());
135
136 void JSCalendar::initialize(JSContextRef context,
137         JSObjectRef object)
138 {
139     LogDebug("entered");
140     CalendarPrivObject *priv =
141         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
142     if (!priv) {
143         LogWarning("create default instance");
144         ICalendarPtr calendar =
145             Api::Calendar::CalendarFactory::getInstance().createCalendarObject();
146         priv = new CalendarPrivObject(context, calendar);
147         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
148             delete priv;
149         }
150     } else {
151         //can be set by JSMakeObject inside getCalendars method
152         LogDebug("private object alrerady exists");
153     }
154 }
155
156 void JSCalendar::finalize(JSObjectRef object)
157 {
158     LogDebug("entered");
159     CalendarPrivObject *priv =
160         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
161     delete priv;
162 }
163
164 const JSClassRef JSCalendar::getClassRef()
165 {
166     if (!m_jsClassRef) {
167         m_jsClassRef = JSClassCreate(&m_classInfo);
168     }
169     return m_jsClassRef;
170 }
171
172 const JSClassDefinition* JSCalendar::getClassInfo()
173 {
174     return &m_classInfo;
175 }
176
177 JSValueRef JSCalendar::add(JSContextRef context,
178         JSObjectRef object,
179         JSObjectRef thisObject,
180         size_t argumentCount,
181         const JSValueRef arguments[],
182         JSValueRef* exception)
183 {
184     LogDebug("entered");
185
186     CalendarPrivObject *privateObject =
187         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
188     assert(privateObject);
189
190     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD);
191
192     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
193
194     Try
195     {
196         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
197
198         if (argumentCount!=1) {
199             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
200         }
201
202         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
203             ThrowMsg(ConversionException, "Wrong parameter type.");
204         }
205
206         CalendarConverterFactory::ConverterType converter =
207             CalendarConverterFactory::getConverter(privateObject->getContext());
208         CalendarEventPtr event = converter->toEvent(arguments[0]);
209         if (!event) {
210             ThrowMsg(ConversionException, "Parameter conversion failed.");
211         }
212
213         IEventAddEventPtr dplEvent(new IEventAddEvent());
214         dplEvent->setEvent(event);
215         dplEvent->setForSynchronousCall();
216         calendar->addEvent(dplEvent);
217
218         if (dplEvent->getResult()) {
219             return converter->toJSValueRefItem(dplEvent->getEvent());
220         } else {
221             ThrowMsg(UnknownException, "Adding failed by unkown reason.");
222         }
223     }
224     Catch(UnsupportedException)
225     {
226                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
227         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
228     }
229     Catch(InvalidArgumentException)
230     {
231                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
232         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
233     }
234     Catch(ConversionException)
235     {
236                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
237         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
238     }
239     Catch(Exception)
240     {
241                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
242         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
243     }
244
245     return JSValueMakeNull(context);
246 }
247
248 JSValueRef JSCalendar::addBatch(JSContextRef context,
249         JSObjectRef object,
250         JSObjectRef thisObject,
251         size_t argumentCount,
252         const JSValueRef arguments[],
253         JSValueRef* exception)
254 {
255     LogDebug("entered");
256
257     CalendarPrivObject *privateObject =
258         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
259     assert(privateObject);
260
261     JSCallbackManagerPtr cbm(NULL);
262
263     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD_BATCH);
264
265     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
266
267     Try
268     {
269         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
270
271         if (argumentCount>3 || argumentCount<1) {
272             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
273         }
274
275         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
276
277         CalendarEventListPtr events;
278         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
279             ThrowMsg(ConversionException, "Wrong parameter type.");
280         }
281         events = converter->toVectorOfEventsFromProperty(arguments[0]);
282         if (!events) {
283             LogError("Failed to create events.");
284             ThrowMsg(ConversionException, "Parameter conversion failed.");
285         }
286
287         JSValueRef onError = NULL;
288         if (argumentCount > 2) {
289             onError = getFunctionOrNull(context, arguments[2]);
290         }
291         JSContextRef globalContext = privateObject->getContext();
292         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
293         CalendarEventsSuccessCallback result;
294         if (argumentCount > 1) {
295             result.onSuccess = getFunctionOrNull(context, arguments[1]);
296         }
297         cbm->setOnSuccess(result.onSuccess);
298
299         LogDebug("Proceed the event to the platform.");
300
301         //CalendarEventsCallbackPrivateDataPtr privData(new CalendarEventsCallbackPrivateData(cbm, onAddEventCbm));
302         //OnAddEventsChangedEmitterPtr emitter(new OnAddEventsChangedEmitter());
303         //emitter->setListener(&CalendarResponseDispatcher::getInstance());
304         //emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
305         //calendar->setAddEmitter(emitter);
306
307         IEventAddEventsPtr dplEvent(new IEventAddEvents());
308         dplEvent->setEvents(events);
309         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
310         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
311         calendar->addEvents(dplEvent);
312
313         return JSValueMakeNull(context);
314     }
315     Catch(UnsupportedException)
316     {
317                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
318         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
319     }
320     Catch(InvalidArgumentException)
321     {
322                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
323         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
324     }
325     Catch(ConversionException)
326     {
327                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
328         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
329     }
330     Catch(Exception)
331     {
332                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
333         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
334     }
335
336     return JSValueMakeNull(context);
337 }
338
339 JSValueRef JSCalendar::update(JSContextRef context,
340         JSObjectRef object,
341         JSObjectRef thisObject,
342         size_t argumentCount,
343         const JSValueRef arguments[],
344         JSValueRef* exception)
345 {
346     LogDebug("entered");
347
348     CalendarPrivObject *privateObject =
349         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
350     assert(privateObject);
351
352     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_UPDATE);
353
354     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
355
356     Try
357     {
358         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
359
360         if (argumentCount>2 || argumentCount<1) {
361             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
362         }
363
364         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
365             ThrowMsg(ConversionException, "Wrong parameter type.");
366         } else if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarItem::getClassRef())) {
367             ThrowMsg(ConversionException, "Wrong parameter type.");
368         }
369
370         JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
371         CalendarEventPtr event = JSCalendarItem::getPrivateObject(arg);
372         if (!JSCalendarItem::validate(context, arg, exception)) {
373             ThrowMsg(InvalidArgumentException, "Wrong parameter value.");
374         }
375         if (!event) {
376             ThrowMsg(ConversionException, "Parameter conversion failed.");
377         }
378
379         bool updateAllInstances = true;
380         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
381         if( argumentCount > 1 ) {
382             if (JSValueIsBoolean(context, arguments[1])) {
383                 updateAllInstances = converter->toBool(arguments[1]);
384             } else {
385                 ThrowMsg(ConversionException, "Wrong parameter type.");
386             }
387         }
388
389         IEventUpdateEventPtr dplEvent(new IEventUpdateEvent());
390         dplEvent->setEvent(event);
391         dplEvent->setUpdateAllInstances(updateAllInstances);
392         dplEvent->setForSynchronousCall();
393         calendar->updateEvent(dplEvent);
394
395         if (dplEvent->getResult()) {
396             return JSValueMakeNull(context);
397         } else {
398             ThrowMsg(UnknownException, "Updating failed by unkown reason.");
399         }
400     }
401     Catch(UnsupportedException)
402     {
403                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
404         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
405     }
406     Catch(InvalidArgumentException)
407     {
408                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
409         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
410     }
411     Catch(ConversionException)
412     {
413                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
414         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
415     }
416     Catch (NotFoundException)
417     {
418                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
419         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
420     }
421     Catch(Exception)
422     {
423                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
424         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
425     }
426
427     return JSValueMakeNull(context);
428 }
429
430 JSValueRef JSCalendar::updateBatch(JSContextRef context,
431         JSObjectRef object,
432         JSObjectRef thisObject,
433         size_t argumentCount,
434         const JSValueRef arguments[],
435         JSValueRef* exception)
436 {
437     LogDebug("entered");
438
439     CalendarPrivObject *privateObject =
440         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
441     assert(privateObject);
442
443     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_UPDATE_BATCH);
444
445     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
446
447     JSCallbackManagerPtr cbm(NULL);
448
449     Try
450     {
451         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
452
453         if (argumentCount>4 || argumentCount<1) {
454             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
455         }
456
457         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
458
459         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
460             ThrowMsg(ConversionException, "Wrong parameter type.");
461         }
462
463         CalendarEventListPtr events;
464         events = converter->toVectorOfEvents(arguments[0]);
465         if (!events) {
466             ThrowMsg(ConversionException, "Parameter conversion failed.");
467         }
468
469         JSValueRef onError = NULL;
470         if (argumentCount > 2) {
471             onError = getFunctionOrNull(context, arguments[2]);
472         }
473         JSContextRef globalContext = privateObject->getContext();
474         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
475         CalendarEventsSuccessCallback result;
476         if (argumentCount > 1) {
477             result.onSuccess = getFunctionOrNull(context, arguments[1]);
478         }
479         cbm->setOnSuccess(result.onSuccess);
480
481         bool updateAllInstances = true;
482         if( argumentCount > 3 ) {
483             if (JSValueIsBoolean(context, arguments[3])) {
484                 updateAllInstances = converter->toBool(arguments[3]);
485             } else {
486                 ThrowMsg(ConversionException, "Wrong parameter type.");
487             }
488         }
489
490         LogDebug("Proceed the event to the platform.");
491
492         IEventUpdateEventsPtr dplEvent(new IEventUpdateEvents());
493         dplEvent->setEvents(events);
494         dplEvent->setUpdateAllInstances(updateAllInstances);
495         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
496         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
497         calendar->updateEvents(dplEvent);
498
499         return makePendingOperation(cbm->getContext(), dplEvent);
500     }
501     Catch(UnsupportedException)
502     {
503                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
504         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
505     }
506     Catch(InvalidArgumentException)
507     {
508                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
509         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
510     }
511     Catch(ConversionException)
512     {
513                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
514         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
515     }
516     Catch (NotFoundException)
517     {
518                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
519         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
520     }
521     Catch(Exception)
522     {
523                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
524         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
525     }
526
527     return JSValueMakeNull(context);
528 }
529
530 JSValueRef JSCalendar::remove(JSContextRef context,
531         JSObjectRef object,
532         JSObjectRef thisObject,
533         size_t argumentCount,
534         const JSValueRef arguments[],
535         JSValueRef* exception)
536 {
537     LogDebug("entered");
538     CalendarPrivObject *privateObject =
539         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
540     assert(privateObject);
541
542     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_REMOVE);
543
544     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
545
546     Try
547     {
548         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
549
550         if (argumentCount!=1) {
551             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
552         }
553
554         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
555             ThrowMsg(ConversionException, "Wrong parameter type.");
556         }
557
558         if (!JSValueIsObjectOfClass(context, arguments[0], JSEventId::getClassRef())) {
559             ThrowMsg(ConversionException, "Wrong parameter type.");
560         }
561
562         EventIdPtr itemId;
563         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
564         itemId = converter->toEventId(arguments[0]);
565         if (!itemId) {
566             ThrowMsg(ConversionException, "Parameter conversion failed.");
567         }
568
569         IEventDeleteEventPtr dplEvent(new IEventDeleteEvent());
570         dplEvent->setEventId(itemId);
571         dplEvent->setForSynchronousCall();
572         calendar->deleteEvent(dplEvent);
573
574         if (dplEvent->getResult()) {
575             LogInfo("Successfully deleted.");
576             return JSValueMakeNull(context);
577         } else {
578             if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
579                 ThrowMsg(ConversionException, "Item not found.");
580             } else {
581                 ThrowMsg(UnknownException, "Removing failed by unkown reason.");
582             }
583         }
584     }
585     Catch(UnsupportedException)
586     {
587                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
588         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
589     }
590     Catch(InvalidArgumentException)
591     {
592                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
593         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
594     }
595     Catch(ConversionException)
596     {
597                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
598         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
599     }
600     Catch (NotFoundException)
601     {
602                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
603         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
604     }
605     Catch(Exception)
606     {
607                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
608         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
609     }
610
611     return JSValueMakeNull(context);
612 }
613
614 JSValueRef JSCalendar::removeBatch(JSContextRef context,
615         JSObjectRef object,
616         JSObjectRef thisObject,
617         size_t argumentCount,
618         const JSValueRef arguments[],
619         JSValueRef* exception)
620 {
621     LogDebug("entered");
622     CalendarPrivObject *privateObject =
623         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
624     assert(privateObject);
625
626     JSCallbackManagerPtr cbm(NULL);
627
628     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_REMOVE_BATCH);
629
630     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
631
632     Try
633     {
634         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
635
636         if (argumentCount>3 || argumentCount<1) {
637             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
638         }
639
640         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
641
642         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
643             ThrowMsg(ConversionException, "Wrong parameter type.");
644         }
645
646         EventIdListPtr itemIds;
647         itemIds = converter->toVectorOfEventIds(arguments[0]);
648         if (!itemIds) {
649             ThrowMsg(ConversionException, "Parameter conversion failed.");
650         }
651
652         JSValueRef onError = NULL;
653         if (argumentCount > 2) {
654             onError = getFunctionOrNull(context, arguments[2]);
655         }
656         JSContextRef globalContext = privateObject->getContext();
657         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
658         CalendarEventsSuccessCallback result;
659         if (argumentCount > 1) {
660             result.onSuccess = getFunctionOrNull(context, arguments[1]);
661         }
662         cbm->setOnSuccess(result.onSuccess);
663
664         LogDebug("Proceed the event to the platform.");
665
666         IEventDeleteEventsPtr dplEvent(new IEventDeleteEvents());
667         dplEvent->setEventIds(itemIds);
668         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
669         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
670         calendar->deleteEvents(dplEvent);
671
672         return makePendingOperation(cbm->getContext(), dplEvent);
673     }
674     Catch(UnsupportedException)
675     {
676                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
677         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
678     }
679     Catch(InvalidArgumentException)
680     {
681                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
682         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
683     }
684     Catch(ConversionException)
685     {
686                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
687         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
688     }
689     Catch (NotFoundException)
690     {
691                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
692         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
693     }
694     Catch(Exception)
695     {
696                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
697         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
698     }
699
700     return JSValueMakeNull(context);
701 }
702
703 JSValueRef JSCalendar::findWithWACFilter(JSContextRef context,
704         JSObjectRef object,
705         JSObjectRef thisObject,
706         size_t argumentCount,
707         const JSValueRef arguments[],
708         JSValueRef* exception)
709 {
710     LogDebug("entered");
711     CalendarPrivObject *privateObject =
712         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
713     assert(privateObject);
714
715     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_FIND);
716
717     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
718
719     JSCallbackManagerPtr cbm(NULL);
720     Try
721     {
722         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
723         if (argumentCount<1 || argumentCount>4) {
724             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
725         }
726         JSValueRef onError = (argumentCount > 1 ? getFunctionOrNull(context, arguments[1]) : NULL);
727         JSContextRef globalContext = privateObject->getContext();
728         cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
729
730         Validator validator(context);
731         if (validator.isCallback(arguments[0])) {
732             cbm->setOnSuccess(arguments[0]);
733         } else if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])) {
734             ThrowMsg(ConversionException, "Wrong parameter type.");
735         } else {
736             ThrowMsg(ConversionException, "Wrong parameter type.");
737         }
738         //setup filters
739         EventFilterPtr filter(NULL);
740         if (argumentCount >= 3) {
741             LogDebug("setting some filters");
742             CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
743             if (!JSValueIsUndefined(context, arguments[2]) && !JSValueIsNull(context, arguments[2])) {
744                 filter = converter->toEventFilter(arguments[2]);
745             }
746         }
747         IEventFindEventsPtr dplEvent(new IEventFindEvents());
748         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
749         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
750         dplEvent->setFilter(filter);
751         calendar->findEvents(dplEvent);
752
753         return JSValueMakeNull(context);
754     }
755     Catch(UnsupportedException)
756     {
757                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
758         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
759     }
760     Catch(InvalidArgumentException)
761     {
762                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
763         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
764     }
765     Catch(ConversionException)
766     {
767                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
768         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
769     }
770     Catch (NotFoundException)
771     {
772                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
773         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
774     }
775     Catch(Exception)
776     {
777                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
778         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
779     }
780
781     return JSValueMakeNull(context);
782 }
783
784 JSValueRef JSCalendar::find(JSContextRef context,
785         JSObjectRef object,
786         JSObjectRef thisObject,
787         size_t argumentCount,
788         const JSValueRef arguments[],
789         JSValueRef* exception)
790 {
791     LogDebug("entered");
792
793     CalendarPrivObject *privateObject =
794         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
795     assert(privateObject);
796
797     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_FIND);
798
799     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
800
801     Try
802     {
803         if (argumentCount<1 || argumentCount>4) {
804             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
805         }
806
807         JSCallbackManagerPtr cbm(NULL);
808
809         Validator validator(context, exception);
810         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
811         TizenApis::Tizen1_0::Tizen::FilterConverterFactory::ConverterType filterConverter = TizenApis::Tizen1_0::Tizen::FilterConverterFactory::getConverter(context);
812         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
813
814         JSContextRef globalContext = privateObject->getContext();
815         cbm = JSCallbackManager::createObject(globalContext);
816
817         if ((!validator.isCallback(arguments[0])) ||
818             (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) ||
819             (argumentCount >= 3 && (!JSValueIsObject(context, arguments[2]) && !validator.isNullOrUndefined(arguments[2]))) ||
820             (argumentCount >= 4 && (!JSValueIsObject(context, arguments[3]) && !validator.isNullOrUndefined(arguments[3]))) ||
821             (argumentCount >= 5 && (!JSValueIsObject(context, arguments[4]) && !validator.isNullOrUndefined(arguments[4])))) {
822             ThrowMsg(ConversionException, "Wrong parameter type.");
823         }
824
825         if (cbm) {
826             JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
827             onSuccessForCbm = arguments[0];
828             if (argumentCount >= 2) {
829                 onErrorForCbm = arguments[1];
830             }
831             cbm->setOnSuccess(onSuccessForCbm);
832             cbm->setOnError(onErrorForCbm);
833         }
834
835         IEventFindEventsPtr dplEvent(new IEventFindEvents());
836         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
837         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
838
839         if (argumentCount >= 3 && !validator.isNullOrUndefined(arguments[2])) {
840             dplEvent->setGenericFilter(filterConverter->toFilter(arguments[2]));
841         }
842         if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3])) {
843             // Though the sortMode is a single type, we save it in an array internally.
844             TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
845             sortModes->push_back(filterConverter->toSortMode(arguments[3]));
846             dplEvent->setSortModes(sortModes);
847         }
848         /*if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4])) {
849             dplEvent->setAttributesOfInterest(converter->toVectorOfStrings(arguments[4]));
850         }*/
851
852         calendar->findEvents(dplEvent);
853     }
854     Catch(UnsupportedException)
855     {
856                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
857         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
858     }
859     Catch(InvalidArgumentException)
860     {
861                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
862         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
863     }
864     Catch(ConversionException)
865     {
866                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
867         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
868     }
869     Catch (NotFoundException)
870     {
871                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
872         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
873     }
874     Catch(Exception)
875     {
876                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
877         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
878     }
879
880     return JSValueMakeNull(context);
881 }
882
883 JSValueRef JSCalendar::expandEventRecurrence(JSContextRef context,
884         JSObjectRef object,
885         JSObjectRef thisObject,
886         size_t argumentCount,
887         const JSValueRef arguments[],
888         JSValueRef* exception)
889 {
890     LogDebug("entered");
891     CalendarPrivObject *privateObject =
892         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
893     assert(privateObject);
894
895     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_EXPAND_EVENT_RECURRENCE);
896
897     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
898
899     JSCallbackManagerPtr cbm(NULL);
900
901     Try
902     {
903         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
904         if (argumentCount>5 || argumentCount<4) {
905             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
906         }
907
908         JSValueRef onError =(argumentCount > 4 ? getFunctionOrNull(context, arguments[4]) : NULL);
909         JSContextRef globalContext = privateObject->getContext();
910         cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
911
912         Validator validator(context);
913         if (validator.isCallback(arguments[3])) {
914             cbm->setOnSuccess(arguments[3]);
915         } else if (JSValueIsNull(context, arguments[3]) || JSValueIsUndefined(context, arguments[3])) {
916             ThrowMsg(ConversionException, "Wrong parameter type.");
917         } else {
918             ThrowMsg(ConversionException, "Wrong parameter type.");
919         }
920
921         JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
922         CalendarEventPtr event = JSCalendarItem::getPrivateObject(arg);
923         if (!event) {
924             ThrowMsg(ConversionException, "Parameter conversion failed.");
925         }
926
927         std::time_t startDate = 0;
928         std::time_t endDate = INT_MAX; // about 60 years in 4 bytes system.
929         Converter converter(context);
930         TimeUtilConverter timeConverter(context);
931         if (argumentCount>1 ) {
932             startDate = timeConverter.toTZDateTimeT(arguments[1]);
933         }
934         if (argumentCount>2 ) {
935             endDate = timeConverter.toDateTimeT(arguments[2]);
936         }
937
938         IEventExpandEventRecurrencePtr dplEvent(new IEventExpandEventRecurrence());
939         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
940         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
941         dplEvent->setEvent(event);
942         dplEvent->setStartDate(startDate);
943         dplEvent->setEndDate(endDate);
944         calendar->expandEventRecurrence(dplEvent);
945
946         return JSValueMakeNull(context);
947     }
948     Catch(UnsupportedException)
949     {
950                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
951         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
952     }
953     Catch(InvalidArgumentException)
954     {
955                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
956         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
957     }
958     Catch(ConversionException)
959     {
960                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
961         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
962     }
963     Catch (NotFoundException)
964     {
965                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
966         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
967     }
968     Catch(Exception)
969     {
970                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
971         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
972     }
973
974     return JSValueMakeNull(context);
975 }
976
977 JSValueRef JSCalendar::convertFromString(JSContextRef context,
978         JSObjectRef object,
979         JSObjectRef thisObject,
980         size_t argumentCount,
981         const JSValueRef arguments[],
982         JSValueRef* exception)
983 {
984     LogDebug("entered");
985
986     CalendarPrivObject *privateObject =
987         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
988     assert(privateObject);
989
990     JSContextRef globalContext = privateObject->getContext();
991
992     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_CONVERT_FROM_STRING);
993
994     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
995
996     Try
997     {
998         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
999         if (argumentCount!=2) {
1000             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
1001         }
1002
1003         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
1004             ThrowMsg(ConversionException, "Wrong parameter type.");
1005         }
1006         else if (!JSValueIsString(context, arguments[0])) {
1007             ThrowMsg(ConversionException, "First parameter is not a string type.");
1008         }
1009
1010         if (JSValueIsUndefined(context, arguments[1]) || JSValueIsNull(context, arguments[1])) {
1011             ThrowMsg(ConversionException, "Wrong parameter type.");
1012         }
1013         else if (!JSValueIsString(context, arguments[1])) {
1014             ThrowMsg(ConversionException, "Second parameter is not a CalendarTextFormat type.");
1015         }
1016
1017         CalendarConverterFactory::ConverterType converter =
1018             CalendarConverterFactory::getConverter(globalContext);
1019
1020         std::string eventStr;
1021         CalendarEvent::VObjectFormat format = CalendarEvent::ICALENDAR_20;
1022
1023         eventStr = converter->toString(arguments[0]);
1024
1025         if( argumentCount>1 )
1026         {
1027             if (JSValueIsString(context, arguments[1])) {
1028                 format = converter->toVObjectFormat(converter->toString(arguments[1]));
1029             } else {
1030                 ThrowMsg(ConversionException, "Second parameter is not a CalendarTextFormat type.");
1031             }
1032         }
1033
1034         IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
1035         dplEvent->setEventString(eventStr);
1036         dplEvent->setFormat(format);
1037         dplEvent->setForSynchronousCall();
1038         calendar->createEventFromString(dplEvent);
1039
1040         if (dplEvent->getResult()) {
1041             LogInfo("Successfully created an event.");
1042             return converter->toJSValueRefItemProperties(dplEvent->getEvent());
1043         } else {
1044             ThrowMsg(UnknownException, "Converting from string failed by unkown reason.");
1045         }
1046     }
1047     Catch(UnsupportedException)
1048     {
1049                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1050         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1051     }
1052     Catch(InvalidArgumentException)
1053     {
1054                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1055         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1056     }
1057     Catch(ConversionException)
1058     {
1059                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1060         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1061     }
1062     Catch(Exception)
1063     {
1064                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1065         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1066     }
1067
1068     return JSValueMakeNull(context);
1069 }
1070
1071 JSValueRef JSCalendar::convertToString(JSContextRef context,
1072         JSObjectRef object,
1073         JSObjectRef thisObject,
1074         size_t argumentCount,
1075         const JSValueRef arguments[],
1076         JSValueRef* exception)
1077 {
1078     LogDebug("entered");
1079
1080     CalendarPrivObject *privateObject =
1081         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1082     assert(privateObject);
1083
1084     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(),
1085             CALENDAR_FUNCTION_API_CONVERT_TO_STRING);
1086
1087     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1088
1089     Try
1090     {
1091         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
1092         if (argumentCount!=2) {
1093             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
1094         }
1095
1096         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
1097             ThrowMsg(ConversionException, "Wrong parameter type.");
1098         }
1099
1100         if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
1101             if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarItem::getClassRef())) {
1102                 ThrowMsg(ConversionException, "First parameter is not a CalendarItem type.");
1103             }
1104             if (!JSCalendarEvent::validate(context, Converter(context).toJSObjectRef(arguments[0]), NULL)) {
1105                 ThrowMsg(InvalidArgumentException, "Wrong parameter value.");
1106             }
1107         } else if(CalendarEvent::TASK_TYPE==calendar->getType()) {
1108             if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarItem::getClassRef())) {
1109                 ThrowMsg(ConversionException, "First parameter is not a CalendarItem type.");
1110             }
1111             if (!JSCalendarTask::validate(context, Converter(context).toJSObjectRef(arguments[0]), NULL)) {
1112                 ThrowMsg(InvalidArgumentException, "Wrong parameter value.");
1113             }
1114         }
1115
1116         JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
1117         CalendarEventPtr event = JSCalendarItem::getPrivateObject(arg);
1118         if (!event) {
1119             ThrowMsg(ConversionException, "Parameter conversion failed.");
1120         }
1121
1122         CalendarEvent::VObjectFormat format = CalendarEvent::ICALENDAR_20;
1123         CalendarConverter converter(context);
1124         if( argumentCount>1 )
1125         {
1126             if (JSValueIsString(context, arguments[1])) {
1127                 format = converter.toVObjectFormat(converter.toString(arguments[1]));
1128             }
1129         }
1130
1131         IEventExportEventToStringPtr dplEvent(new IEventExportEventToString());
1132         dplEvent->setEvent(event);
1133         dplEvent->setFormat(format);
1134         dplEvent->setForSynchronousCall();
1135         calendar->exportEventToString(dplEvent);
1136
1137         if (dplEvent->getResult()) {
1138             return converter.toJSValueRef(dplEvent->getEventString());
1139         } else {
1140             ThrowMsg(UnknownException, "Converting to string failed by unkown reason.");
1141         }
1142     }
1143     Catch(UnsupportedException)
1144     {
1145                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1146         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1147     }
1148     Catch(InvalidArgumentException)
1149     {
1150                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1151         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1152     }
1153     Catch(ConversionException)
1154     {
1155                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1156         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1157     }
1158     Catch (NotFoundException)
1159     {
1160                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1161         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1162     }
1163     Catch(Exception)
1164     {
1165                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1166         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1167     }
1168
1169     return JSValueMakeNull(context);
1170 }
1171
1172 JSValueRef JSCalendar::addChangeListener(JSContextRef context,
1173         JSObjectRef object,
1174         JSObjectRef thisObject,
1175         size_t argumentCount,
1176         const JSValueRef arguments[],
1177         JSValueRef* exception)
1178 {
1179     LogDebug("entered");
1180     CalendarPrivObject *privateObject =
1181         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1182     assert(privateObject);
1183
1184     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER);
1185
1186     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1187
1188     JSCallbackManagerPtr cbm(NULL);
1189     Try
1190     {
1191         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1192         if (argumentCount>2 || argumentCount<1) {
1193             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
1194         }
1195
1196         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
1197         JSValueRef onError = NULL;
1198         if (argumentCount > 1) {
1199             onError = getFunctionOrNull(context, arguments[1]);
1200         }
1201         JSContextRef globalContext = privateObject->getContext();
1202         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
1203         JSObjectRef objectCallbacks = converter->toJSObjectRef(arguments[0]);
1204         CalendarChangeCallback result;
1205         result.onAdded = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onItemsAdded");
1206         result.onUpdated = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onItemsUpdated");
1207         result.onDeleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onItemsRemoved");
1208         Validator validator(context);
1209         if ((!validator.isNullOrUndefined(result.onAdded) && !validator.isCallback(result.onAdded)) ||
1210            (!validator.isNullOrUndefined(result.onUpdated) && !validator.isCallback(result.onUpdated)) ||
1211            (!validator.isNullOrUndefined(result.onDeleted) && !validator.isCallback(result.onDeleted)))
1212         {
1213             ThrowMsg(InvalidArgumentException, "Wrong successCallback parameter value.");
1214         }
1215         JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(globalContext, result.onAdded, NULL);
1216         JSCallbackManagerPtr onUpdatedCbm = JSCallbackManager::createObject(globalContext, result.onUpdated, NULL);
1217         JSCallbackManagerPtr onDeletedCbm = JSCallbackManager::createObject(globalContext, result.onDeleted, NULL);
1218
1219         /* The interested attributes are not supported in platform.
1220         AttributeListPtr attributes;
1221         if( argumentCount >= 3 ) {
1222             try
1223             {
1224                 *attributes = converter->toVectorOfStrings(arguments[2]);
1225                 if (!attributes) {
1226                     LogError("watchChanges: Failed to get interested attributes");
1227                     Throw(InvalidArgumentException);
1228                 }
1229             }
1230             Catch(Exception)
1231             {
1232                 LogError("Error while converting the attributes");
1233                 Throw(InvalidArgumentException);
1234             }
1235         }*/
1236
1237         LogDebug("Make change emitter and sync operation");
1238
1239         CalendarChangeCallbackPrivateDataPtr privData(new CalendarChangeCallbackPrivateData(
1240             onAddedCbm, onUpdatedCbm, onDeletedCbm));
1241         OnEventsChangedEmitterPtr emitter(new OnEventsChangedEmitter());
1242         emitter->setListener(&CalendarResponseDispatcher::getInstance());
1243         emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
1244
1245         // return sync operation and process the events and emit results while processing them
1246         IEventWatchChangesPtr dplEvent(new IEventWatchChanges());
1247         dplEvent->setEmitter(emitter);
1248         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
1249         dplEvent->setForSynchronousCall();
1250         calendar->watchChanges(dplEvent);
1251
1252         long watchId;
1253         if (dplEvent->getResult()) {
1254             watchId = dplEvent->getWatchId();
1255             LogDebug("Returning the watchId "<<watchId);
1256             return converter->toJSValueRefLong(watchId);
1257         } else {
1258             ThrowMsg(UnknownException, "Adding change listener failed by unkown reason.");
1259         }
1260         return JSValueMakeNull(context);
1261     }
1262     Catch(UnsupportedException)
1263     {
1264                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1265         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1266     }
1267     Catch(InvalidArgumentException)
1268     {
1269                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1270         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1271     }
1272     Catch(ConversionException)
1273     {
1274                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1275         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1276     }
1277     Catch(Exception)
1278     {
1279                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1280         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1281     }
1282
1283     return JSValueMakeNull(context);
1284 }
1285
1286 JSValueRef JSCalendar::removeChangeListener(JSContextRef context,
1287         JSObjectRef object,
1288         JSObjectRef thisObject,
1289         size_t argumentCount,
1290         const JSValueRef arguments[],
1291         JSValueRef* exception)
1292 {
1293     LogDebug("entered");
1294     CalendarPrivObject *privateObject =
1295         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1296     assert(privateObject);
1297
1298     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER);
1299
1300     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1301
1302     Try
1303     {
1304         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1305         if (argumentCount!=1) {
1306             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
1307         }
1308
1309         long watchId = -1;
1310             CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
1311             if (JSValueIsNumber(context, arguments[0])) {
1312                 watchId = converter->toLong(arguments[0]);
1313             }
1314
1315         LogDebug("Make sync operation");
1316
1317         IEventClearWatchPtr dplEvent(new IEventClearWatch());
1318         dplEvent->setWatchId(watchId);
1319         dplEvent->setForSynchronousCall();
1320         calendar->clearWatch(dplEvent);
1321
1322         return JSValueMakeNull(context);
1323     }
1324     Catch(UnsupportedException)
1325     {
1326                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1327         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1328     }
1329     Catch(InvalidArgumentException)
1330     {
1331                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1332         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1333     }
1334     Catch(ConversionException)
1335     {
1336                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1337         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1338     }
1339     Catch (NotFoundException)
1340     {
1341                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1342         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1343     }
1344     Catch(Exception)
1345     {
1346                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1347         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1348     }
1349
1350     return JSValueMakeNull(context);
1351 }
1352
1353 JSValueRef JSCalendar::getPropertyName(JSContextRef context,
1354         JSObjectRef object,
1355         JSStringRef propertyName,
1356         JSValueRef* exception)
1357 {
1358     Try
1359     {
1360         ICalendarPtr calendar = getCalendar(context, object, exception);
1361         Converter converter(context);
1362         return converter.toJSValueRef(calendar->getName());
1363     }
1364     Catch(Exception)
1365     {
1366         LogError("error during executing a function");
1367     }
1368     return JSValueMakeUndefined(context);
1369 }
1370
1371 JSValueRef JSCalendar::getPropertyAccountId(JSContextRef context,
1372         JSObjectRef object,
1373         JSStringRef propertyName,
1374         JSValueRef* exception)
1375 {
1376     Try
1377     {
1378         ICalendarPtr calendar = getCalendar(context, object, exception);
1379         Converter converter(context);
1380         return converter.toJSValueRefLong(calendar->getAccountId());
1381     }
1382     Catch(Exception)
1383     {
1384         LogError("error during executing a function");
1385     }
1386     return JSValueMakeUndefined(context);
1387 }
1388
1389 JSValueRef JSCalendar::getPropertyId(JSContextRef context,
1390         JSObjectRef object,
1391         JSStringRef propertyName,
1392         JSValueRef* exception)
1393 {
1394     Try
1395     {
1396         ICalendarPtr calendar = getCalendar(context, object, exception);
1397         Converter converter(context);
1398         return converter.toJSValueRef(calendar->getId());
1399     }
1400     Catch(Exception)
1401     {
1402         LogError("error during executing a function");
1403     }
1404     return JSValueMakeUndefined(context);
1405 }
1406
1407 ICalendarPtr JSCalendar::getCalendar(JSContextRef ctx,
1408         const JSObjectRef object,
1409         JSValueRef* exception)
1410 {
1411     CalendarPrivObject *priv =
1412         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
1413     if (priv) {
1414         return priv->getObject();
1415     }
1416     ThrowMsg(NullPointerException, "Private object is NULL.");
1417 }
1418
1419 }
1420 }
1421 }