2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dpl/log/log.h>
19 #include <API/Calendar/ICalendar.h>
20 #include <API/Calendar/EventId.h>
21 #include <API/Calendar/CalendarFactory.h>
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>
35 #include "JSCalendarManager.h"
36 #include "CalendarConverter.h"
37 #include "JSCalendar.h"
38 #include "JSCalendarEvent.h"
39 #include "JSCalendarTask.h"
40 #include "JSCalendarItemProperties.h"
41 #include "JSCalendarEventId.h"
42 #include "plugin_config.h"
43 #include "CalendarResponseDispatcher.h"
44 #include "CalendarMultiCallback.h"
46 #include "CalendarAsyncCallbackManager.h"
47 #include "CalendarListenerManager.h"
49 using namespace TizenApis::Api::Calendar;
50 using namespace WrtDeviceApis::Commons;
51 using namespace WrtDeviceApis::CommonsJavaScript;
52 using namespace TizenApis::Commons;
54 #define TIZEN_CALENDAR_ATTRIBUTENAME "Calendar"
60 JSClassDefinition JSCalendar::m_classInfo = {
62 kJSClassAttributeNone,
63 TIZEN_CALENDAR_ATTRIBUTENAME,
72 NULL, //DeleteProperty,
73 NULL, //GetPropertyNames,
74 NULL, //CallAsFunction,
75 NULL, //CallAsConstructor,
80 JSStaticValue JSCalendar::m_property[] = {
81 { TIZEN_CALENDAR_PROPERTY_NAME, JSCalendar::getPropertyName,
82 NULL, kJSPropertyAttributeReadOnly },
83 { TIZEN_CALENDAR_PROPERTY_ACCOUNT_SERVICE_ID, JSCalendar::getPropertyAccountServiceId,
84 NULL, kJSPropertyAttributeReadOnly },
85 { TIZEN_CALENDAR_PROPERTY_ID, JSCalendar::getPropertyId,
86 NULL, kJSPropertyAttributeReadOnly },
90 JSStaticFunction JSCalendar::m_function[] = {
91 { CALENDAR_FUNCTION_API_ADD, add, kJSPropertyAttributeNone },
92 { CALENDAR_FUNCTION_API_ADD_BATCH, addBatch, kJSPropertyAttributeNone },
93 { CALENDAR_FUNCTION_API_UPDATE, update, kJSPropertyAttributeNone },
94 { CALENDAR_FUNCTION_API_UPDATE_BATCH, updateBatch, kJSPropertyAttributeNone },
95 { CALENDAR_FUNCTION_API_REMOVE, remove, kJSPropertyAttributeNone },
96 { CALENDAR_FUNCTION_API_REMOVE_BATCH, removeBatch, kJSPropertyAttributeNone },
97 { CALENDAR_FUNCTION_API_FIND, find, kJSPropertyAttributeNone },
98 { CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER, addChangeListener, kJSPropertyAttributeNone },
99 { CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER, removeChangeListener, kJSPropertyAttributeNone },
100 { CALENDAR_FUNCTION_API_GET, get, kJSPropertyAttributeNone },
105 JSClassRef JSCalendar::m_jsClassRef = JSClassCreate(JSCalendar::getClassInfo());
107 void JSCalendar::initialize(JSContextRef context, JSObjectRef object)
109 if (!JSObjectGetPrivate(object)) {
110 LogDebug("Create calendar private object.");
111 ICalendarPtr calendar = CalendarFactory::getInstance().createCalendarObject();
112 CalendarPrivObject *priv = new CalendarPrivObject(context, calendar);
113 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
117 LogDebug("Private object already set.");
121 void JSCalendar::finalize(JSObjectRef object)
124 CalendarPrivObject *priv = static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
127 JSObjectSetPrivate(object, NULL);
131 const JSClassRef JSCalendar::getClassRef()
134 m_jsClassRef = JSClassCreate(&m_classInfo);
139 const JSClassDefinition* JSCalendar::getClassInfo()
144 JSValueRef JSCalendar::add(JSContextRef context,
146 JSObjectRef thisObject,
147 size_t argumentCount,
148 const JSValueRef arguments[],
149 JSValueRef* exception)
153 CalendarPrivObject *privateObject =
154 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
156 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_ADD);
158 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
162 if (!privateObject) {
163 ThrowMsg(ConversionException, "Object is null.");
166 ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
168 if (argumentCount<1) {
169 ThrowMsg(ConversionException, "Wrong parameter type.");
172 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
173 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef())) {
174 ThrowMsg(ConversionException, "Wrong first parameter type.");
176 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
177 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
178 ThrowMsg(ConversionException, "Wrong first parameter type.");
181 ThrowMsg(ConversionException, "Wrong calendar type.");
184 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
185 CalendarEventPtr event = JSCalendarEvent::getPrivateObject(arg);
187 ThrowMsg(ConversionException, "Parameter conversion failed.");
190 IEventAddEventPtr dplEvent(new IEventAddEvent());
191 dplEvent->setEvent(event);
192 dplEvent->setForSynchronousCall();
193 calendar->addEvent(dplEvent);
195 if (dplEvent->getResult()) {
196 LogDebug("Add result successful.");
197 return JSValueMakeUndefined(context);
199 ThrowMsg(UnknownException, "Adding failed by unknown reason.");
202 Catch(UnsupportedException)
204 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
205 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
207 Catch(InvalidArgumentException)
209 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
210 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
212 Catch(ConversionException)
214 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
215 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
219 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
223 return JSValueMakeUndefined(context);
226 JSValueRef JSCalendar::addBatch(JSContextRef context,
228 JSObjectRef thisObject,
229 size_t argumentCount,
230 const JSValueRef arguments[],
231 JSValueRef* exception)
235 CalendarPrivObject *privateObject =
236 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
238 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_ADD_BATCH);
240 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
244 if (!privateObject) {
245 ThrowMsg(ConversionException, "Object is null.");
248 ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
250 JSContextRef globalContext = privateObject->getContext();
251 CalendarConverter converter(context);
253 if (argumentCount<1) {
254 ThrowMsg(ConversionException, "Wrong parameter type.");
257 CalendarEventListPtr events;
258 if (!JSIsArrayValue(context, arguments[0])) {
259 ThrowMsg(ConversionException, "Wrong first parameter type.");
261 events = converter.toVectorOfEvents(arguments[0]);
263 ThrowMsg(ConversionException, "First parameter conversion failed.");
266 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
267 if (argumentCount>=2) {
268 cbm->setOnSuccess(converter.toFunctionOrNull(arguments[1]));
270 if (argumentCount>=3) {
271 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
274 cbm->setObject(thisObject);
276 LogDebug("Proceed the event to the platform.");
278 IEventAddEventsPtr dplEvent(new IEventAddEvents());
279 dplEvent->setCalendarType(calendar->getType());
280 dplEvent->setEvents(events);
281 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
282 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
283 calendar->addEvents(dplEvent);
285 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
287 return JSValueMakeUndefined(context);
289 Catch(UnsupportedException)
291 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
292 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
294 Catch(InvalidArgumentException)
296 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
297 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
299 Catch(ConversionException)
301 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
302 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
306 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
307 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
310 return JSValueMakeUndefined(context);
313 JSValueRef JSCalendar::update(JSContextRef context,
315 JSObjectRef thisObject,
316 size_t argumentCount,
317 const JSValueRef arguments[],
318 JSValueRef* exception)
322 CalendarPrivObject *privateObject =
323 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
325 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_UPDATE);
327 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
331 if (!privateObject) {
332 ThrowMsg(ConversionException, "Object is null.");
335 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
337 if (argumentCount<1) {
338 ThrowMsg(ConversionException, "Wrong parameter type.");
341 if (argumentCount>=1) {
342 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef()) &&
343 !JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
344 ThrowMsg(ConversionException, "Wrong first parameter type.");
347 ThrowMsg(ConversionException, "Wrong first parameter type.");
350 CalendarConverter converter(context);
352 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
353 CalendarEventPtr item = JSCalendarEvent::getPrivateObject(arg);
355 ThrowMsg(ConversionException, "Parameter conversion failed.");
358 bool updateAllInstances = true; // Set the default value.
359 if (argumentCount>=2) {
360 updateAllInstances = converter.toBool(arguments[1]);
363 IEventUpdateEventPtr dplEvent(new IEventUpdateEvent());
364 dplEvent->setEvent(item);
365 dplEvent->setUpdateAllInstances(updateAllInstances);
366 dplEvent->setForSynchronousCall();
367 calendar->updateEvent(dplEvent);
369 if (dplEvent->getResult()) {
370 return JSValueMakeUndefined(context);
372 ThrowMsg(UnknownException, "Updating failed by unknown reason.");
375 Catch(UnsupportedException)
377 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
378 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
380 Catch(InvalidArgumentException)
382 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
383 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
385 Catch(ConversionException)
387 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
388 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
390 Catch (NotFoundException)
392 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
393 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
397 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
398 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
401 return JSValueMakeUndefined(context);
404 JSValueRef JSCalendar::updateBatch(JSContextRef context,
406 JSObjectRef thisObject,
407 size_t argumentCount,
408 const JSValueRef arguments[],
409 JSValueRef* exception)
413 CalendarPrivObject *privateObject =
414 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
416 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_UPDATE_BATCH);
418 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
422 if (!privateObject) {
423 ThrowMsg(ConversionException, "Object is null.");
426 JSContextRef globalContext = privateObject->getContext();
428 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
430 CalendarConverter converter(context);
432 if (argumentCount>=1) {
433 if (!JSIsArrayValue(context, arguments[0])) {
434 ThrowMsg(ConversionException, "Wrong first parameter type.");
437 ThrowMsg(ConversionException, "Wrong parameter type.");
439 CalendarEventListPtr events;
440 events = converter.toVectorOfEvents(arguments[0]);
442 ThrowMsg(ConversionException, "Third parameter conversion failed.");
445 JSValueRef onSuccess = NULL;
446 if (argumentCount>=2) {
447 onSuccess = converter.toFunctionOrNull(arguments[1]);
449 JSValueRef onError = NULL;
450 if (argumentCount>=3) {
451 onError = converter.toFunctionOrNull(arguments[2]);
453 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
455 bool updateAllInstances = true; // Set the default value.
456 if( argumentCount>=4 ) {
457 updateAllInstances = converter.toBool(arguments[3]);
460 cbm->setObject(thisObject);
462 LogDebug("Proceed the event to the platform.");
464 IEventUpdateEventsPtr dplEvent(new IEventUpdateEvents());
465 dplEvent->setEvents(events);
466 dplEvent->setUpdateAllInstances(updateAllInstances);
467 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
468 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
469 calendar->updateEvents(dplEvent);
471 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
473 return JSValueMakeUndefined(context);
475 Catch(UnsupportedException)
477 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
478 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
480 Catch(InvalidArgumentException)
482 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
483 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
485 Catch(ConversionException)
487 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
488 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
490 Catch (NotFoundException)
492 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
493 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
497 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
498 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
501 return JSValueMakeUndefined(context);
504 JSValueRef JSCalendar::remove(JSContextRef context,
506 JSObjectRef thisObject,
507 size_t argumentCount,
508 const JSValueRef arguments[],
509 JSValueRef* exception)
512 CalendarPrivObject *privateObject =
513 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
515 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_REMOVE);
517 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
521 if (!privateObject) {
522 ThrowMsg(ConversionException, "Object is null.");
525 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
528 CalendarConverter converter(context);
530 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
531 if (argumentCount<1) {
532 ThrowMsg(ConversionException, "Wrong parameter type.");
535 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
536 ThrowMsg(ConversionException, "Wrong parameter type.");
538 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
540 ThrowMsg(ConversionException, "Parameter conversion failed.");
542 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
543 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
544 EventIdPtr result( new EventId() );
545 if (argumentCount>=1) {
546 result->setUId(converter.toString(arguments[0]));
549 itemId->setCalendarType(CalendarEvent::TASK_TYPE);
551 ThrowMsg(ConversionException, "Parameter conversion failed.");
554 IEventDeleteEventPtr dplEvent(new IEventDeleteEvent());
555 dplEvent->setEventId(itemId);
556 dplEvent->setForSynchronousCall();
557 calendar->deleteEvent(dplEvent);
559 if (dplEvent->getResult()) {
560 LogInfo("Successfully deleted.");
561 return JSValueMakeUndefined(context);
563 if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
564 ThrowMsg(NotFoundException, "Item not found.");
566 ThrowMsg(UnknownException, "Removing failed by unnkown reason.");
570 Catch(UnsupportedException)
572 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
573 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
575 Catch(InvalidArgumentException)
577 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
578 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
580 Catch(ConversionException)
582 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
583 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
585 Catch (NotFoundException)
587 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
588 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
592 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
593 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
596 return JSValueMakeUndefined(context);
599 JSValueRef JSCalendar::removeBatch(JSContextRef context,
601 JSObjectRef thisObject,
602 size_t argumentCount,
603 const JSValueRef arguments[],
604 JSValueRef* exception)
608 CalendarPrivObject *privateObject =
609 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
611 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_REMOVE_BATCH);
613 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
617 if (!privateObject) {
618 ThrowMsg(ConversionException, "Object is null.");
621 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
622 JSContextRef globalContext = privateObject->getContext();
624 CalendarConverter converter(context);
626 if (argumentCount>=1) {
627 if (!JSIsArrayValue(context, arguments[0])) {
628 ThrowMsg(ConversionException, "Wrong first parameter type.");
631 ThrowMsg(ConversionException, "Wrong parameter type.");
634 // Pick one array element to determine its type.
635 JSObjectRef objArg = converter.toJSObjectRef(arguments[0]);
637 if (JSGetArrayLength(context, objArg)>0) {
638 element = JSGetArrayElement(context, objArg, 0);
640 ThrowMsg(ConversionException, "Wrong first parameter type.");
643 EventIdListPtr itemIds;
644 if (JSValueIsObjectOfClass(context, element, JSCalendarEventId::getClassRef())) {
645 itemIds = converter.toVectorOfEventIds(arguments[0]);
647 ThrowMsg(ConversionException, "Parameter conversion failed.");
649 } else if (JSValueIsString(context, element)) {
650 std::vector<std::string> idStrings = converter.toVectorOfStrings(arguments[0]);
651 EventIdListPtr result(new EventIdList());
652 for (unsigned int i=0; i<idStrings.size(); i++) {
653 EventIdPtr idPtr( new EventId() );
654 idPtr->setUId(idStrings[i]);
655 idPtr->setCalendarType(CalendarEvent::TASK_TYPE);
656 result->push_back(idPtr);
660 ThrowMsg(ConversionException, "Wrong parameter type.");
663 JSValueRef onSuccess = NULL;
664 if (argumentCount>=2) {
665 onSuccess = converter.toFunctionOrNull(arguments[1]);
667 JSValueRef onError = NULL;
668 if (argumentCount>=3) {
669 onError = converter.toFunctionOrNull(arguments[2]);
671 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
673 cbm->setObject(thisObject);
675 LogDebug("Proceed the event to the platform.");
677 IEventDeleteEventsPtr dplEvent(new IEventDeleteEvents());
678 dplEvent->setEventIds(itemIds);
679 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
680 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
681 calendar->deleteEvents(dplEvent);
683 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
685 return JSValueMakeUndefined(context);
687 Catch(UnsupportedException)
689 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
690 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
692 Catch(InvalidArgumentException)
694 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
695 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
697 Catch(ConversionException)
699 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
700 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
702 Catch (NotFoundException)
704 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
705 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
709 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
710 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
713 return JSValueMakeUndefined(context);
716 JSValueRef JSCalendar::find(JSContextRef context,
718 JSObjectRef thisObject,
719 size_t argumentCount,
720 const JSValueRef arguments[],
721 JSValueRef* exception)
725 CalendarPrivObject *privateObject =
726 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
728 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_FIND);
730 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
734 if (!privateObject) {
735 ThrowMsg(ConversionException, "Object is null.");
738 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
740 JSContextRef globalContext = privateObject->getContext();
742 Validator validator(context, exception);
743 CalendarConverter converter(context);
744 TizenApis::Tizen1_0::Tizen::FilterConverterFactory::ConverterType filterConverter = TizenApis::Tizen1_0::Tizen::FilterConverterFactory::getConverter(context);
746 if (argumentCount<1) {
747 ThrowMsg(ConversionException, "Wrong parameter type.");
750 JSValueRef onSuccess = NULL;
751 if (argumentCount>=1) {
752 onSuccess = converter.toFunction(arguments[0]);
754 JSValueRef onError = NULL;
755 if (argumentCount>=2) {
756 onError = converter.toFunctionOrNull(arguments[1]);
758 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
760 cbm->setObject(thisObject);
762 LogDebug("Proceed the find event to the platform.");
764 IEventFindEventsPtr dplEvent(new IEventFindEvents());
765 dplEvent->setCalendarType(calendar->getType());
766 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
767 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
769 if (argumentCount>=3) {
770 if (JSValueIsObject(context, arguments[2])) {
771 dplEvent->setGenericFilter(filterConverter->toFilter(arguments[2]));
772 } else if (JSValueIsNull(context, arguments[2]) || JSValueIsUndefined(context, arguments[2])) {
773 LogDebug("Use default filter.");
775 ThrowMsg(ConversionException, "Wrong third parameter type.");
778 if (argumentCount>=4) {
779 if (JSValueIsObject(context, arguments[3])) {
780 // Though the sortMode is a single type, we save it in an array internally.
781 TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
782 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
783 dplEvent->setSortModes(sortModes);
784 } else if (JSValueIsNull(context, arguments[3]) || JSValueIsUndefined(context, arguments[3])) {
785 LogDebug("Use default sort mode.");
787 ThrowMsg(ConversionException, "Wrong fourth parameter type.");
791 calendar->findEvents(dplEvent);
793 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
795 return JSValueMakeUndefined(context);
797 Catch(UnsupportedException)
799 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
800 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
802 Catch(InvalidArgumentException)
804 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
805 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
807 Catch(ConversionException)
809 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
810 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
812 Catch (NotFoundException)
814 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
815 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
819 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
820 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
823 return JSValueMakeUndefined(context);
826 JSValueRef JSCalendar::get(JSContextRef context,
828 JSObjectRef thisObject,
829 size_t argumentCount,
830 const JSValueRef arguments[],
831 JSValueRef* exception)
835 CalendarPrivObject *privateObject =
836 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
838 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET);
840 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
844 if (!privateObject) {
845 ThrowMsg(ConversionException, "Object is null.");
848 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
849 JSContextRef globalContext = privateObject->getContext();
852 CalendarConverter converter(context);
853 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
854 if (argumentCount<1) {
855 ThrowMsg(ConversionException, "Wrong parameter type.");
858 if (JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
859 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
861 ThrowMsg(ConversionException, "Parameter conversion failed.");
863 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
865 ThrowMsg(ConversionException, "Wrong parameter type.");
867 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
868 EventIdPtr result( new EventId() );
870 if (argumentCount>=1) {
871 itemId->setUId(converter.toString(arguments[0]));
873 itemId->setCalendarType(CalendarEvent::TASK_TYPE);
875 ThrowMsg(ConversionException, "Wrong calendar type.");
878 IEventGetPtr dplEvent(new IEventGet());
879 dplEvent->setForSynchronousCall();
880 dplEvent->setItemId(itemId);
881 calendar->get(dplEvent);
883 // Process the found item.
884 if (dplEvent->getResult()) {
885 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
886 // Use global context for the expandRecurrence api in event.
887 return JSCalendarEvent::createJSCalendarEvent(globalContext, dplEvent->getItem());
888 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
889 return JSCalendarTask::createJSCalendarTask(context, dplEvent->getItem());
891 ThrowMsg(UnknownException, "Wrong calendar type.");
894 if(ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
895 ThrowMsg(NotFoundException, "Item not found.");
897 ThrowMsg(UnknownException, "Getting an item failed by unknown reason.");
901 Catch(UnsupportedException)
903 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
904 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
906 Catch(InvalidArgumentException)
908 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
909 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
911 Catch(ConversionException)
913 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
914 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
916 Catch (NotFoundException)
918 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
919 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
923 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
924 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
927 return JSValueMakeUndefined(context);
930 JSValueRef JSCalendar::addChangeListener(JSContextRef context,
932 JSObjectRef thisObject,
933 size_t argumentCount,
934 const JSValueRef arguments[],
935 JSValueRef* exception)
939 CalendarPrivObject *privateObject =
940 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
942 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER);
944 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
948 if (!privateObject) {
949 ThrowMsg(ConversionException, "Object is null.");
952 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
953 JSContextRef globalContext = privateObject->getContext();
955 CalendarConverter converter(context);
957 if (argumentCount>=1) {
958 if (!JSValueIsObject(context, arguments[0])) {
959 ThrowMsg(ConversionException, "Wrong first parameter type.");
962 ThrowMsg(ConversionException, "Wrong first parameter type.");
965 JSValueRef onError = NULL;
966 if (argumentCount>=2) {
967 onError = converter.toFunctionOrNull(arguments[1]);
969 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
971 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[0]);
972 CalendarChangeCallback result;
973 Validator validator(context);
975 result.onAdded = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsadded");
976 if (validator.isNullOrUndefined(result.onAdded)) {
977 result.onAdded = NULL;
978 } else if(!validator.isCallback(result.onAdded)) {
979 ThrowMsg(ConversionException, "Wrong first parameter type.");
982 result.onUpdated = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsupdated");
983 if (validator.isNullOrUndefined(result.onUpdated)) {
984 result.onUpdated = NULL;
985 } else if(!validator.isCallback(result.onUpdated)) {
986 ThrowMsg(ConversionException, "Wrong first parameter type.");
989 result.onDeleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsremoved");
990 if (validator.isNullOrUndefined(result.onDeleted)) {
991 result.onDeleted = NULL;
992 } else if(!validator.isCallback(result.onDeleted)) {
993 ThrowMsg(ConversionException, "Wrong first parameter type.");
996 if (!result.onAdded && !result.onUpdated && !result.onDeleted) {
997 ThrowMsg(ConversionException, "Wrong second parameter type.");
1000 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(globalContext, result.onAdded, NULL);
1001 JSCallbackManagerPtr onUpdatedCbm = JSCallbackManager::createObject(globalContext, result.onUpdated, NULL);
1002 JSCallbackManagerPtr onDeletedCbm = JSCallbackManager::createObject(globalContext, result.onDeleted, NULL);
1004 LogDebug("Make change emitter and process sync operation.");
1006 CalendarChangeCallbackPrivateDataPtr privData(new CalendarChangeCallbackPrivateData(
1007 onAddedCbm, onUpdatedCbm, onDeletedCbm));
1008 OnEventsChangedEmitterPtr emitter(new OnEventsChangedEmitter());
1009 emitter->setListener(&CalendarResponseDispatcher::getInstance());
1010 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
1012 IEventWatchChangesPtr dplEvent(new IEventWatchChanges());
1013 dplEvent->setEmitter(emitter);
1014 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
1015 dplEvent->setForSynchronousCall();
1016 calendar->watchChanges(dplEvent);
1019 if (dplEvent->getResult()) {
1020 watchId = dplEvent->getWatchId();
1022 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
1023 TizenApis::Commons::IListenerItemPtr listenerItem = DPL::StaticPointerCast<TizenApis::Commons::IListenerItem>(canceller);
1024 CalendarListenerManagerSingleton::Instance().registerListener(listenerItem, globalContext);
1026 LogDebug("Returning the watchId "<<watchId);
1027 return converter.toJSValueRefLong(watchId);
1029 ThrowMsg(UnknownException, "Adding change listener failed by unknown reason.");
1032 Catch(UnsupportedException)
1034 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1035 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1037 Catch(InvalidArgumentException)
1039 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1040 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1042 Catch(ConversionException)
1044 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1045 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1049 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1050 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1053 return JSValueMakeUndefined(context);
1056 JSValueRef JSCalendar::removeChangeListener(JSContextRef context,
1058 JSObjectRef thisObject,
1059 size_t argumentCount,
1060 const JSValueRef arguments[],
1061 JSValueRef* exception)
1063 LogDebug("entered");
1065 CalendarPrivObject *privateObject =
1066 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1068 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER);
1070 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1074 if (!privateObject) {
1075 ThrowMsg(ConversionException, "Object is null.");
1078 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1080 JSContextRef globalContext = privateObject->getContext();
1083 CalendarConverter converter(context);
1084 if (argumentCount>=1) {
1085 watchId = converter.toLong(arguments[0]);
1087 ThrowMsg(ConversionException, "Wrong parameter type.");
1090 LogDebug("Make sync operation with watchId: "<<watchId);
1092 IEventClearWatchPtr dplEvent(new IEventClearWatch());
1093 dplEvent->setWatchId(watchId);
1094 dplEvent->setForSynchronousCall();
1095 calendar->clearWatch(dplEvent);
1097 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
1098 TizenApis::Commons::IListenerItemPtr listenerItem = DPL::StaticPointerCast<TizenApis::Commons::IListenerItem>(canceller);
1099 CalendarListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1101 return JSValueMakeUndefined(context);
1103 Catch(UnsupportedException)
1105 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1106 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1108 Catch(InvalidArgumentException)
1110 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1111 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1113 Catch(ConversionException)
1115 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1116 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1118 Catch (NotFoundException)
1120 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1121 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1125 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1126 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1129 return JSValueMakeUndefined(context);
1132 JSValueRef JSCalendar::getPropertyName(JSContextRef context,
1134 JSStringRef propertyName,
1135 JSValueRef* exception)
1139 ICalendarPtr calendar = getCalendar(context, object, exception);
1140 Converter converter(context);
1141 return converter.toJSValueRef(calendar->getName());
1145 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1147 return JSValueMakeUndefined(context);
1150 JSValueRef JSCalendar::getPropertyAccountServiceId(JSContextRef context,
1152 JSStringRef propertyName,
1153 JSValueRef* exception)
1157 ICalendarPtr calendar = getCalendar(context, object, exception);
1158 Converter converter(context);
1159 return converter.toJSValueRefLong(calendar->getAccountId());
1163 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1165 return JSValueMakeUndefined(context);
1168 JSValueRef JSCalendar::getPropertyId(JSContextRef context,
1170 JSStringRef propertyName,
1171 JSValueRef* exception)
1175 ICalendarPtr calendar = getCalendar(context, object, exception);
1176 Converter converter(context);
1177 return converter.toJSValueRef(calendar->getId());
1181 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1183 return JSValueMakeUndefined(context);
1186 ICalendarPtr JSCalendar::getCalendar(JSContextRef ctx,
1187 const JSObjectRef object,
1188 JSValueRef* exception)
1190 CalendarPrivObject *priv =
1191 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
1193 return priv->getObject();
1195 ThrowMsg(NullPointerException, "Private object is NULL.");