merge wrt-plugins-tizen_0.2.0-12
[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 JSValueMakeUndefined(context);
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 JSValueMakeUndefined(context);
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->setCalendarType(calendar->getType());
300         dplEvent->setEvents(events);
301         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
302         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
303         calendar->addEvents(dplEvent);
304
305         return JSValueMakeUndefined(context);
306     }
307     Catch(UnsupportedException)
308     {
309                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
310         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
311     }
312     Catch(InvalidArgumentException)
313     {
314                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
315         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
316     }
317     Catch(ConversionException)
318     {
319                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
320         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
321     }
322     Catch(Exception)
323     {
324                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
325         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
326     }
327
328     return JSValueMakeUndefined(context);
329 }
330
331 JSValueRef JSCalendar::update(JSContextRef context,
332         JSObjectRef object,
333         JSObjectRef thisObject,
334         size_t argumentCount,
335         const JSValueRef arguments[],
336         JSValueRef* exception)
337 {
338     LogDebug("entered");
339
340     CalendarPrivObject *privateObject =
341         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
342     assert(privateObject);
343
344     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_UPDATE);
345
346     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
347
348     Try
349     {
350         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
351
352         if (argumentCount>2 || argumentCount<1) {
353             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
354         }
355
356         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
357             ThrowMsg(ConversionException, "First parameter is undefined or null.");
358         } else if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarItem::getClassRef()) &&
359         !JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef()) &&
360         !JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
361             ThrowMsg(ConversionException, "Wrong parameter type.");
362         }
363
364         JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
365         CalendarEventPtr event = JSCalendarItem::getPrivateObject(arg);
366         if (!JSCalendarItem::validate(context, arg, exception)) {
367             ThrowMsg(InvalidArgumentException, "Wrong parameter value.");
368         }
369         if (!event) {
370             ThrowMsg(ConversionException, "Parameter conversion failed.");
371         }
372
373         bool updateAllInstances = true;
374         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
375         if( argumentCount > 1 ) {
376             if (JSValueIsBoolean(context, arguments[1])) {
377                 updateAllInstances = converter->toBool(arguments[1]);
378             } else {
379                 ThrowMsg(ConversionException, "Wrong parameter type.");
380             }
381         }
382
383         IEventUpdateEventPtr dplEvent(new IEventUpdateEvent());
384         dplEvent->setEvent(event);
385         dplEvent->setUpdateAllInstances(updateAllInstances);
386         dplEvent->setForSynchronousCall();
387         calendar->updateEvent(dplEvent);
388
389         if (dplEvent->getResult()) {
390             return JSValueMakeUndefined(context);
391         } else {
392             ThrowMsg(UnknownException, "Updating failed by unkown reason.");
393         }
394     }
395     Catch(UnsupportedException)
396     {
397                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
398         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
399     }
400     Catch(InvalidArgumentException)
401     {
402                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
403         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
404     }
405     Catch(ConversionException)
406     {
407                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
408         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
409     }
410     Catch (NotFoundException)
411     {
412                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
413         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
414     }
415     Catch(Exception)
416     {
417                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
418         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
419     }
420
421     return JSValueMakeUndefined(context);
422 }
423
424 JSValueRef JSCalendar::updateBatch(JSContextRef context,
425         JSObjectRef object,
426         JSObjectRef thisObject,
427         size_t argumentCount,
428         const JSValueRef arguments[],
429         JSValueRef* exception)
430 {
431     LogDebug("entered");
432
433     CalendarPrivObject *privateObject =
434         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
435     assert(privateObject);
436
437     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_UPDATE_BATCH);
438
439     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
440
441     JSCallbackManagerPtr cbm(NULL);
442
443     Try
444     {
445         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
446
447         if (argumentCount>4 || argumentCount<1) {
448             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
449         }
450
451         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
452
453         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
454             ThrowMsg(ConversionException, "Wrong parameter type.");
455         }
456
457         CalendarEventListPtr events;
458         events = converter->toVectorOfEvents(arguments[0]);
459         if (!events) {
460             ThrowMsg(ConversionException, "Parameter conversion failed.");
461         }
462
463         JSValueRef onError = NULL;
464         if (argumentCount > 2) {
465             onError = getFunctionOrNull(context, arguments[2]);
466         }
467         JSContextRef globalContext = privateObject->getContext();
468         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
469         CalendarEventsSuccessCallback result;
470         if (argumentCount > 1) {
471             result.onSuccess = getFunctionOrNull(context, arguments[1]);
472         }
473         cbm->setOnSuccess(result.onSuccess);
474
475         bool updateAllInstances = true;
476         if( argumentCount > 3 ) {
477             if (JSValueIsBoolean(context, arguments[3])) {
478                 updateAllInstances = converter->toBool(arguments[3]);
479             } else {
480                 ThrowMsg(ConversionException, "Wrong parameter type.");
481             }
482         }
483
484         LogDebug("Proceed the event to the platform.");
485
486         IEventUpdateEventsPtr dplEvent(new IEventUpdateEvents());
487         dplEvent->setEvents(events);
488         dplEvent->setUpdateAllInstances(updateAllInstances);
489         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
490         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
491         calendar->updateEvents(dplEvent);
492
493         return JSValueMakeUndefined(context);
494     }
495     Catch(UnsupportedException)
496     {
497                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
498         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
499     }
500     Catch(InvalidArgumentException)
501     {
502                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
503         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
504     }
505     Catch(ConversionException)
506     {
507                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
508         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
509     }
510     Catch (NotFoundException)
511     {
512                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
513         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
514     }
515     Catch(Exception)
516     {
517                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
518         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
519     }
520
521     return JSValueMakeUndefined(context);
522 }
523
524 JSValueRef JSCalendar::remove(JSContextRef context,
525         JSObjectRef object,
526         JSObjectRef thisObject,
527         size_t argumentCount,
528         const JSValueRef arguments[],
529         JSValueRef* exception)
530 {
531     LogDebug("entered");
532     CalendarPrivObject *privateObject =
533         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
534     assert(privateObject);
535
536     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_REMOVE);
537
538     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
539
540     Try
541     {
542         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
543
544         if (argumentCount!=1) {
545             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
546         }
547
548         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
549             ThrowMsg(ConversionException, "Wrong parameter type.");
550         }
551
552         if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
553             ThrowMsg(ConversionException, "Wrong parameter type.");
554         }
555
556         EventIdPtr itemId;
557         JSContextRef globalContext = privateObject->getContext();
558         CalendarConverter converter(globalContext);
559         if (JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
560             itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
561             if (!itemId) {
562                 ThrowMsg(ConversionException, "Parameter conversion failed.");
563             }
564             itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
565         } else if (JSValueIsString(context, arguments[0])) {
566             itemId->setUId(converter.toString(arguments[0]));
567             itemId->setCalendarType(CalendarEvent::TASK_TYPE);
568         } else {
569             ThrowMsg(ConversionException, "Wrong parameter type.");
570         }
571
572         IEventDeleteEventPtr dplEvent(new IEventDeleteEvent());
573         dplEvent->setEventId(itemId);
574         dplEvent->setForSynchronousCall();
575         calendar->deleteEvent(dplEvent);
576
577         if (dplEvent->getResult()) {
578             LogInfo("Successfully deleted.");
579             return JSValueMakeUndefined(context);
580         } else {
581             if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
582                 ThrowMsg(ConversionException, "Item not found.");
583             } else {
584                 ThrowMsg(UnknownException, "Removing failed by unkown reason.");
585             }
586         }
587     }
588     Catch(UnsupportedException)
589     {
590                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
591         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
592     }
593     Catch(InvalidArgumentException)
594     {
595                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
596         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
597     }
598     Catch(ConversionException)
599     {
600                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
601         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
602     }
603     Catch (NotFoundException)
604     {
605                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
606         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
607     }
608     Catch(Exception)
609     {
610                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
611         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
612     }
613
614     return JSValueMakeUndefined(context);
615 }
616
617 JSValueRef JSCalendar::removeBatch(JSContextRef context,
618         JSObjectRef object,
619         JSObjectRef thisObject,
620         size_t argumentCount,
621         const JSValueRef arguments[],
622         JSValueRef* exception)
623 {
624     LogDebug("entered");
625     CalendarPrivObject *privateObject =
626         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
627     assert(privateObject);
628
629     JSCallbackManagerPtr cbm(NULL);
630
631     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_REMOVE_BATCH);
632
633     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
634
635     Try
636     {
637         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
638
639         if (argumentCount>3 || argumentCount<1) {
640             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
641         }
642
643         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
644
645         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
646             ThrowMsg(ConversionException, "Wrong parameter type.");
647         }
648
649         // Pick one array element to determine its type.
650         JSObjectRef objArg = converter->toJSObjectRef(arguments[0]);
651         JSValueRef element;
652         if (JSGetArrayLength(context, objArg)>0) {
653             element = JSGetArrayElement(context, objArg, 0);
654         } else {
655             ThrowMsg(InvalidArgumentException, "Invalid length ID array length.");
656         }
657
658         EventIdListPtr itemIds;
659         if (JSValueIsObjectOfClass(context, element, JSCalendarEventId::getClassRef())) {
660             itemIds = converter->toVectorOfEventIds(arguments[0]);
661             if (!itemIds) {
662                 ThrowMsg(ConversionException, "Parameter conversion failed.");
663             }
664         } else if (JSValueIsString(context, element)) {
665             std::vector<std::string> idStrings = converter->toVectorOfStrings(arguments[0]);
666             for (unsigned int i=0; i<idStrings.size(); i++) {
667                 EventIdPtr idPtr( new EventId() );
668                 idPtr->setUId(idStrings[i]);
669                 idPtr->setCalendarType(CalendarEvent::TASK_TYPE);
670                 itemIds->push_back(idPtr);
671             }
672             if (!itemIds) {
673                 ThrowMsg(ConversionException, "Parameter conversion failed.");
674             }
675         } else {
676             ThrowMsg(ConversionException, "Wrong parameter type.");
677         }
678
679         JSValueRef onError = NULL;
680         if (argumentCount > 2) {
681             onError = getFunctionOrNull(context, arguments[2]);
682         }
683         JSContextRef globalContext = privateObject->getContext();
684         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
685         CalendarEventsSuccessCallback result;
686         if (argumentCount > 1) {
687             result.onSuccess = getFunctionOrNull(context, arguments[1]);
688         }
689         cbm->setOnSuccess(result.onSuccess);
690
691         LogDebug("Proceed the event to the platform.");
692
693         IEventDeleteEventsPtr dplEvent(new IEventDeleteEvents());
694         dplEvent->setEventIds(itemIds);
695         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
696         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
697         calendar->deleteEvents(dplEvent);
698
699         return JSValueMakeUndefined(context);
700     }
701     Catch(UnsupportedException)
702     {
703                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
704         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
705     }
706     Catch(InvalidArgumentException)
707     {
708                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
709         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
710     }
711     Catch(ConversionException)
712     {
713                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
714         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
715     }
716     Catch (NotFoundException)
717     {
718                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
719         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
720     }
721     Catch(Exception)
722     {
723                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
724         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
725     }
726
727     return JSValueMakeUndefined(context);
728 }
729
730 JSValueRef JSCalendar::findWithWACFilter(JSContextRef context,
731         JSObjectRef object,
732         JSObjectRef thisObject,
733         size_t argumentCount,
734         const JSValueRef arguments[],
735         JSValueRef* exception)
736 {
737     LogDebug("entered");
738     CalendarPrivObject *privateObject =
739         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
740     assert(privateObject);
741
742     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_FIND);
743
744     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
745
746     JSCallbackManagerPtr cbm(NULL);
747     Try
748     {
749         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
750         if (argumentCount<1 || argumentCount>4) {
751             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
752         }
753         JSValueRef onError = (argumentCount > 1 ? getFunctionOrNull(context, arguments[1]) : NULL);
754         JSContextRef globalContext = privateObject->getContext();
755         cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
756
757         Validator validator(context);
758         if (validator.isCallback(arguments[0])) {
759             cbm->setOnSuccess(arguments[0]);
760         } else if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])) {
761             ThrowMsg(ConversionException, "Wrong parameter type.");
762         } else {
763             ThrowMsg(ConversionException, "Wrong parameter type.");
764         }
765         //setup filters
766         EventFilterPtr filter(NULL);
767         if (argumentCount >= 3) {
768             LogDebug("setting some filters");
769             CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
770             if (!JSValueIsUndefined(context, arguments[2]) && !JSValueIsNull(context, arguments[2])) {
771                 filter = converter->toEventFilter(arguments[2]);
772             }
773         }
774         IEventFindEventsPtr dplEvent(new IEventFindEvents());
775         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
776         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
777         dplEvent->setFilter(filter);
778         calendar->findEvents(dplEvent);
779
780         return JSValueMakeUndefined(context);
781     }
782     Catch(UnsupportedException)
783     {
784                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
785         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
786     }
787     Catch(InvalidArgumentException)
788     {
789                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
790         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
791     }
792     Catch(ConversionException)
793     {
794                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
795         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
796     }
797     Catch (NotFoundException)
798     {
799                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
800         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
801     }
802     Catch(Exception)
803     {
804                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
805         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
806     }
807
808     return JSValueMakeUndefined(context);
809 }
810
811 JSValueRef JSCalendar::find(JSContextRef context,
812         JSObjectRef object,
813         JSObjectRef thisObject,
814         size_t argumentCount,
815         const JSValueRef arguments[],
816         JSValueRef* exception)
817 {
818     LogDebug("entered");
819
820     CalendarPrivObject *privateObject =
821         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
822     assert(privateObject);
823
824     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_FIND);
825
826     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
827
828     Try
829     {
830         if (argumentCount<1 || argumentCount>4) {
831             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
832         }
833
834         JSCallbackManagerPtr cbm(NULL);
835
836         Validator validator(context, exception);
837         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
838         TizenApis::Tizen1_0::Tizen::FilterConverterFactory::ConverterType filterConverter = TizenApis::Tizen1_0::Tizen::FilterConverterFactory::getConverter(context);
839         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
840
841         JSContextRef globalContext = privateObject->getContext();
842         cbm = JSCallbackManager::createObject(globalContext);
843
844         if ((!validator.isCallback(arguments[0])) ||
845             (argumentCount >= 2 && (!validator.isCallback(arguments[1]) && !validator.isNullOrUndefined(arguments[1]))) ||
846             (argumentCount >= 3 && (!JSValueIsObject(context, arguments[2]) && !validator.isNullOrUndefined(arguments[2]))) ||
847             (argumentCount >= 4 && (!JSValueIsObject(context, arguments[3]) && !validator.isNullOrUndefined(arguments[3])))) {
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->setCalendarType(calendar->getType());
863         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
864         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
865
866         if (argumentCount >= 3 && !validator.isNullOrUndefined(arguments[2])) {
867             dplEvent->setGenericFilter(filterConverter->toFilter(arguments[2]));
868         }
869         if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3])) {
870             // Though the sortMode is a single type, we save it in an array internally.
871             TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
872             sortModes->push_back(filterConverter->toSortMode(arguments[3]));
873             dplEvent->setSortModes(sortModes);
874         }
875         /*if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4])) {
876             dplEvent->setAttributesOfInterest(converter->toVectorOfStrings(arguments[4]));
877         }*/
878
879         calendar->findEvents(dplEvent);
880     }
881     Catch(UnsupportedException)
882     {
883                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
884         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
885     }
886     Catch(InvalidArgumentException)
887     {
888                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
889         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
890     }
891     Catch(ConversionException)
892     {
893                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
894         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
895     }
896     Catch (NotFoundException)
897     {
898                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
899         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
900     }
901     Catch(Exception)
902     {
903                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
904         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
905     }
906
907     return JSValueMakeUndefined(context);
908 }
909
910 JSValueRef JSCalendar::get(JSContextRef context,
911         JSObjectRef object,
912         JSObjectRef thisObject,
913         size_t argumentCount,
914         const JSValueRef arguments[],
915         JSValueRef* exception)
916 {
917     LogDebug("entered");
918
919     CalendarPrivObject *privateObject =
920         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
921     assert(privateObject);
922
923     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_GET);
924
925     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
926
927     Try
928     {
929         if (argumentCount!=1) {
930             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
931         }
932
933         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
934         JSContextRef globalContext = privateObject->getContext();
935
936         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
937             ThrowMsg(ConversionException, "Wrong parameter type.");
938         }
939
940         EventIdPtr itemId;
941         CalendarConverter converter(globalContext);
942         if (JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
943             itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
944             if (!itemId) {
945                 ThrowMsg(ConversionException, "Parameter conversion failed.");
946             }
947             itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
948         } else if (JSValueIsString(context, arguments[0])) {
949             itemId->setUId(converter.toString(arguments[0]));
950             itemId->setCalendarType(CalendarEvent::TASK_TYPE);
951         } else {
952             ThrowMsg(ConversionException, "Wrong parameter type.");
953         }
954
955         IEventGetPtr dplEvent(new IEventGet());
956         dplEvent->setForSynchronousCall();
957         dplEvent->setItemId(itemId);
958         calendar->get(dplEvent);
959
960         // Process the found item.
961         if (dplEvent->getResult()) {
962             LogInfo("Successfully found an item.");
963             return JSCalendarItem::createJSCalendarItem(globalContext, dplEvent->getItem());
964         } else {
965             ThrowMsg(UnknownException, "Getting an item failed by unkown reason.");
966         }
967     }
968     Catch(UnsupportedException)
969     {
970                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
971         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
972     }
973     Catch(InvalidArgumentException)
974     {
975                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
976         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
977     }
978     Catch(ConversionException)
979     {
980                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
981         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
982     }
983     Catch (NotFoundException)
984     {
985                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
986         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
987     }
988     Catch(Exception)
989     {
990                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
991         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
992     }
993
994     return JSValueMakeUndefined(context);
995 }
996
997 JSValueRef JSCalendar::convertFromString(JSContextRef context,
998         JSObjectRef object,
999         JSObjectRef thisObject,
1000         size_t argumentCount,
1001         const JSValueRef arguments[],
1002         JSValueRef* exception)
1003 {
1004     LogDebug("entered");
1005
1006     CalendarPrivObject *privateObject =
1007         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1008     assert(privateObject);
1009
1010     JSContextRef globalContext = privateObject->getContext();
1011
1012     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_CONVERT_FROM_STRING);
1013
1014     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1015
1016     Try
1017     {
1018         ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
1019         if (argumentCount!=2) {
1020             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
1021         }
1022
1023         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
1024             ThrowMsg(ConversionException, "Wrong parameter type.");
1025         }
1026         else if (!JSValueIsString(context, arguments[0])) {
1027             ThrowMsg(ConversionException, "First parameter is not a string type.");
1028         }
1029
1030         if (JSValueIsUndefined(context, arguments[1]) || JSValueIsNull(context, arguments[1])) {
1031             ThrowMsg(ConversionException, "Wrong parameter type.");
1032         }
1033         else if (!JSValueIsString(context, arguments[1])) {
1034             ThrowMsg(ConversionException, "Second parameter is not a CalendarTextFormat type.");
1035         }
1036
1037         CalendarConverterFactory::ConverterType converter =
1038             CalendarConverterFactory::getConverter(globalContext);
1039
1040         std::string eventStr;
1041         CalendarEvent::VObjectFormat format = CalendarEvent::ICALENDAR_20;
1042
1043         eventStr = converter->toString(arguments[0]);
1044
1045         if( argumentCount>1 )
1046         {
1047             if (JSValueIsString(context, arguments[1])) {
1048                 format = converter->toVObjectFormat(converter->toString(arguments[1]));
1049             } else {
1050                 ThrowMsg(ConversionException, "Second parameter is not a CalendarTextFormat type.");
1051             }
1052         }
1053
1054         IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
1055         dplEvent->setEventString(eventStr);
1056         dplEvent->setFormat(format);
1057         dplEvent->setForSynchronousCall();
1058         calendar->createEventFromString(dplEvent);
1059
1060         if (dplEvent->getResult()) {
1061             LogInfo("Successfully created an event.");
1062             return JSCalendarItemProperties::createJSCalendarItemProperties(globalContext, dplEvent->getEvent());
1063         } else {
1064             ThrowMsg(UnknownException, "Converting from string failed by unkown reason.");
1065         }
1066     }
1067     Catch(UnsupportedException)
1068     {
1069                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1070         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1071     }
1072     Catch(InvalidArgumentException)
1073     {
1074                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1075         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1076     }
1077     Catch(ConversionException)
1078     {
1079                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1080         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1081     }
1082     Catch(Exception)
1083     {
1084                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1085         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1086     }
1087
1088     return JSValueMakeUndefined(context);
1089 }
1090
1091 JSValueRef JSCalendar::addChangeListener(JSContextRef context,
1092         JSObjectRef object,
1093         JSObjectRef thisObject,
1094         size_t argumentCount,
1095         const JSValueRef arguments[],
1096         JSValueRef* exception)
1097 {
1098     LogDebug("entered");
1099     CalendarPrivObject *privateObject =
1100         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1101     assert(privateObject);
1102
1103     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER);
1104
1105     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1106
1107     JSCallbackManagerPtr cbm(NULL);
1108     Try
1109     {
1110         ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1111         if (argumentCount>2 || argumentCount<1) {
1112             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
1113         }
1114
1115         CalendarConverterFactory::ConverterType converter = CalendarConverterFactory::getConverter(context);
1116         JSValueRef onError = NULL;
1117         if (argumentCount > 1) {
1118             onError = getFunctionOrNull(context, arguments[1]);
1119         }
1120         JSContextRef globalContext = privateObject->getContext();
1121         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
1122         JSObjectRef objectCallbacks = converter->toJSObjectRef(arguments[0]);
1123         CalendarChangeCallback result;
1124         result.onAdded = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onItemsAdded");
1125         result.onUpdated = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onItemsUpdated");
1126         result.onDeleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onItemsRemoved");
1127         Validator validator(context);
1128         if ((!validator.isNullOrUndefined(result.onAdded) && !validator.isCallback(result.onAdded)) ||
1129            (!validator.isNullOrUndefined(result.onUpdated) && !validator.isCallback(result.onUpdated)) ||
1130            (!validator.isNullOrUndefined(result.onDeleted) && !validator.isCallback(result.onDeleted)))
1131         {
1132             ThrowMsg(InvalidArgumentException, "Wrong successCallback parameter value.");
1133         }
1134         JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(globalContext, result.onAdded, NULL);
1135         JSCallbackManagerPtr onUpdatedCbm = JSCallbackManager::createObject(globalContext, result.onUpdated, NULL);
1136         JSCallbackManagerPtr onDeletedCbm = JSCallbackManager::createObject(globalContext, result.onDeleted, NULL);
1137
1138         /* The interested attributes are not supported in platform.
1139         AttributeListPtr attributes;
1140         if( argumentCount >= 3 ) {
1141             try
1142             {
1143                 *attributes = converter->toVectorOfStrings(arguments[2]);
1144                 if (!attributes) {
1145                     LogError("watchChanges: Failed to get interested attributes");
1146                     Throw(InvalidArgumentException);
1147                 }
1148             }
1149             Catch(Exception)
1150             {
1151                 LogError("Error while converting the attributes");
1152                 Throw(InvalidArgumentException);
1153             }
1154         }*/
1155
1156         LogDebug("Make change emitter and sync operation");
1157
1158         CalendarChangeCallbackPrivateDataPtr privData(new CalendarChangeCallbackPrivateData(
1159             onAddedCbm, onUpdatedCbm, onDeletedCbm));
1160         OnEventsChangedEmitterPtr emitter(new OnEventsChangedEmitter());
1161         emitter->setListener(&CalendarResponseDispatcher::getInstance());
1162         emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
1163
1164         // return sync operation and process the events and emit results while processing them
1165         IEventWatchChangesPtr dplEvent(new IEventWatchChanges());
1166         dplEvent->setEmitter(emitter);
1167         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
1168         dplEvent->setForSynchronousCall();
1169         calendar->watchChanges(dplEvent);
1170
1171         long watchId;
1172         if (dplEvent->getResult()) {
1173             watchId = dplEvent->getWatchId();
1174             LogDebug("Returning the watchId "<<watchId);
1175             return converter->toJSValueRefLong(watchId);
1176         } else {
1177             ThrowMsg(UnknownException, "Adding change listener failed by unkown reason.");
1178         }
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 JSValueMakeUndefined(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         } else {
1232             ThrowMsg(ConversionException, "Wrong watchId type.");
1233         }
1234
1235         LogDebug("Make sync operation");
1236
1237         IEventClearWatchPtr dplEvent(new IEventClearWatch());
1238         dplEvent->setWatchId(watchId);
1239         dplEvent->setForSynchronousCall();
1240         calendar->clearWatch(dplEvent);
1241
1242         return JSValueMakeUndefined(context);
1243     }
1244     Catch(UnsupportedException)
1245     {
1246                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1247         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1248     }
1249     Catch(InvalidArgumentException)
1250     {
1251                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1252         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1253     }
1254     Catch(ConversionException)
1255     {
1256                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1257         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1258     }
1259     Catch (NotFoundException)
1260     {
1261                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1262         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1263     }
1264     Catch(Exception)
1265     {
1266                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1267         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1268     }
1269
1270     return JSValueMakeUndefined(context);
1271 }
1272
1273 JSValueRef JSCalendar::getPropertyName(JSContextRef context,
1274         JSObjectRef object,
1275         JSStringRef propertyName,
1276         JSValueRef* exception)
1277 {
1278     Try
1279     {
1280         ICalendarPtr calendar = getCalendar(context, object, exception);
1281         Converter converter(context);
1282         return converter.toJSValueRef(calendar->getName());
1283     }
1284     Catch(Exception)
1285     {
1286         LogError("error during executing a function");
1287     }
1288     return JSValueMakeUndefined(context);
1289 }
1290
1291 JSValueRef JSCalendar::getPropertyAccountId(JSContextRef context,
1292         JSObjectRef object,
1293         JSStringRef propertyName,
1294         JSValueRef* exception)
1295 {
1296     Try
1297     {
1298         ICalendarPtr calendar = getCalendar(context, object, exception);
1299         Converter converter(context);
1300         return converter.toJSValueRefLong(calendar->getAccountId());
1301     }
1302     Catch(Exception)
1303     {
1304         LogError("error during executing a function");
1305     }
1306     return JSValueMakeUndefined(context);
1307 }
1308
1309 JSValueRef JSCalendar::getPropertyId(JSContextRef context,
1310         JSObjectRef object,
1311         JSStringRef propertyName,
1312         JSValueRef* exception)
1313 {
1314     Try
1315     {
1316         ICalendarPtr calendar = getCalendar(context, object, exception);
1317         Converter converter(context);
1318         return converter.toJSValueRef(calendar->getId());
1319     }
1320     Catch(Exception)
1321     {
1322         LogError("error during executing a function");
1323     }
1324     return JSValueMakeUndefined(context);
1325 }
1326
1327 ICalendarPtr JSCalendar::getCalendar(JSContextRef ctx,
1328         const JSObjectRef object,
1329         JSValueRef* exception)
1330 {
1331     CalendarPrivObject *priv =
1332         static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
1333     if (priv) {
1334         return priv->getObject();
1335     }
1336     ThrowMsg(NullPointerException, "Private object is NULL.");
1337 }
1338
1339 }
1340 }
1341 }