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