2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <dpl/log/log.h>
20 #include "ICalendar.h"
22 #include "CalendarFactory.h"
24 #include <CommonsJavaScript/PrivateObject.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include <CommonsJavaScript/Utils.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <CommonsJavaScript/ScopedJSStringRef.h>
30 #include <JSTizenException.h>
31 #include <JSTizenExceptionFactory.h>
32 #include <SecurityExceptions.h>
33 #include <FilterConverter.h>
34 #include <TimeUtilConverter.h>
36 #include "JSCalendarManager.h"
37 #include "CalendarConverter.h"
38 #include "JSCalendar.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"
47 #include "CalendarAsyncCallbackManager.h"
48 #include "CalendarListenerManager.h"
51 using namespace WrtDeviceApis::Commons;
52 using namespace WrtDeviceApis::CommonsJavaScript;
53 using namespace DeviceAPI::Common;
55 #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)
123 CalendarPrivObject *priv = static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
126 JSObjectSetPrivate(object, NULL);
130 const JSClassRef JSCalendar::getClassRef()
133 m_jsClassRef = JSClassCreate(&m_classInfo);
138 const JSClassDefinition* JSCalendar::getClassInfo()
143 JSValueRef JSCalendar::add(JSContextRef context,
145 JSObjectRef thisObject,
146 size_t argumentCount,
147 const JSValueRef arguments[],
148 JSValueRef* exception)
150 CalendarPrivObject *privateObject =
151 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
153 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_ADD);
155 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
159 if (!privateObject) {
160 ThrowMsg(ConversionException, "Object is null.");
163 ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
165 if (argumentCount<1) {
166 ThrowMsg(ConversionException, "Wrong parameter type.");
169 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
170 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef())) {
171 ThrowMsg(ConversionException, "Wrong first parameter type.");
173 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
174 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
175 ThrowMsg(ConversionException, "Wrong first parameter type.");
178 ThrowMsg(ConversionException, "Wrong calendar type.");
181 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
182 CalendarEventPtr event = JSCalendarEvent::getPrivateObject(arg);
184 ThrowMsg(ConversionException, "Parameter conversion failed.");
187 IEventAddEventPtr dplEvent(new IEventAddEvent());
188 dplEvent->setEvent(event);
189 dplEvent->setForSynchronousCall();
190 calendar->addEvent(dplEvent);
192 if (dplEvent->getResult()) {
193 LogDebug("Add result successful.");
194 return JSValueMakeUndefined(context);
196 ThrowMsg(UnknownException, "Adding failed by unknown reason.");
199 Catch(UnsupportedException)
201 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
202 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
204 Catch(InvalidArgumentException)
206 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
207 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
209 Catch(ConversionException)
211 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
212 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
216 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
217 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
221 JSValueRef JSCalendar::addBatch(JSContextRef context,
223 JSObjectRef thisObject,
224 size_t argumentCount,
225 const JSValueRef arguments[],
226 JSValueRef* exception)
228 CalendarPrivObject *privateObject =
229 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
231 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_ADD_BATCH);
233 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
237 if (!privateObject) {
238 ThrowMsg(ConversionException, "Object is null.");
241 ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
243 JSContextRef globalContext = privateObject->getContext();
244 CalendarConverter converter(context);
246 if (argumentCount<1) {
247 ThrowMsg(ConversionException, "Wrong parameter type.");
250 CalendarEventListPtr events;
251 if (!JSIsArrayValue(context, arguments[0])) {
252 ThrowMsg(ConversionException, "Wrong first parameter type.");
254 events = converter.toVectorOfEvents(arguments[0]);
256 ThrowMsg(ConversionException, "First parameter conversion failed.");
259 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
260 if (argumentCount>=2) {
261 cbm->setOnSuccess(converter.toFunctionOrNull(arguments[1]));
263 if (argumentCount>=3) {
264 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
267 cbm->setObject(thisObject);
269 LogDebug("Proceed the event to the platform.");
271 IEventAddEventsPtr dplEvent(new IEventAddEvents());
272 dplEvent->setCalendarType(calendar->getType());
273 dplEvent->setEvents(events);
274 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
275 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
276 calendar->addEvents(dplEvent);
278 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
280 return JSValueMakeUndefined(context);
282 Catch(UnsupportedException)
284 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
285 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
287 Catch(InvalidArgumentException)
289 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
290 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
292 Catch(ConversionException)
294 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
295 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
299 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
300 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
304 JSValueRef JSCalendar::update(JSContextRef context,
306 JSObjectRef thisObject,
307 size_t argumentCount,
308 const JSValueRef arguments[],
309 JSValueRef* exception)
311 CalendarPrivObject *privateObject =
312 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
314 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_UPDATE);
316 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
320 if (!privateObject) {
321 ThrowMsg(ConversionException, "Object is null.");
324 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
326 if (argumentCount<1) {
327 ThrowMsg(ConversionException, "Wrong parameter type.");
330 if (argumentCount>=1) {
331 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEvent::getClassRef()) &&
332 !JSValueIsObjectOfClass(context, arguments[0], JSCalendarTask::getClassRef())) {
333 ThrowMsg(ConversionException, "Wrong first parameter type.");
336 ThrowMsg(ConversionException, "Wrong first parameter type.");
339 CalendarConverter converter(context);
341 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
342 CalendarEventPtr item = JSCalendarEvent::getPrivateObject(arg);
344 ThrowMsg(ConversionException, "Parameter conversion failed.");
347 bool updateAllInstances = true; // Set the default value.
348 if (argumentCount>=2) {
349 updateAllInstances = converter.toBool(arguments[1]);
352 IEventUpdateEventPtr dplEvent(new IEventUpdateEvent());
353 dplEvent->setEvent(item);
354 dplEvent->setUpdateAllInstances(updateAllInstances);
355 dplEvent->setForSynchronousCall();
356 calendar->updateEvent(dplEvent);
358 if (dplEvent->getResult()) {
359 return JSValueMakeUndefined(context);
361 ThrowMsg(UnknownException, "Updating failed by unknown reason.");
364 Catch(UnsupportedException)
366 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
367 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
369 Catch(InvalidArgumentException)
371 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
372 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
374 Catch(ConversionException)
376 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
377 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
379 Catch (NotFoundException)
381 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
382 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
386 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
387 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
391 JSValueRef JSCalendar::updateBatch(JSContextRef context,
393 JSObjectRef thisObject,
394 size_t argumentCount,
395 const JSValueRef arguments[],
396 JSValueRef* exception)
398 CalendarPrivObject *privateObject =
399 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
401 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_UPDATE_BATCH);
403 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
407 if (!privateObject) {
408 ThrowMsg(ConversionException, "Object is null.");
411 JSContextRef globalContext = privateObject->getContext();
413 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
415 CalendarConverter converter(context);
417 if (argumentCount>=1) {
418 if (!JSIsArrayValue(context, arguments[0])) {
419 ThrowMsg(ConversionException, "Wrong first parameter type.");
422 ThrowMsg(ConversionException, "Wrong parameter type.");
424 CalendarEventListPtr events;
425 events = converter.toVectorOfEvents(arguments[0]);
427 ThrowMsg(ConversionException, "Third parameter conversion failed.");
430 JSValueRef onSuccess = NULL;
431 if (argumentCount>=2) {
432 onSuccess = converter.toFunctionOrNull(arguments[1]);
434 JSValueRef onError = NULL;
435 if (argumentCount>=3) {
436 onError = converter.toFunctionOrNull(arguments[2]);
438 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
440 bool updateAllInstances = true; // Set the default value.
441 if( argumentCount>=4 ) {
442 updateAllInstances = converter.toBool(arguments[3]);
445 cbm->setObject(thisObject);
447 LogDebug("Proceed the event to the platform.");
449 IEventUpdateEventsPtr dplEvent(new IEventUpdateEvents());
450 dplEvent->setEvents(events);
451 dplEvent->setUpdateAllInstances(updateAllInstances);
452 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
453 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
454 calendar->updateEvents(dplEvent);
456 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
458 return JSValueMakeUndefined(context);
460 Catch(UnsupportedException)
462 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
463 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
465 Catch(InvalidArgumentException)
467 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
468 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
470 Catch(ConversionException)
472 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
473 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
475 Catch (NotFoundException)
477 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
478 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
482 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
483 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
487 JSValueRef JSCalendar::remove(JSContextRef context,
489 JSObjectRef thisObject,
490 size_t argumentCount,
491 const JSValueRef arguments[],
492 JSValueRef* exception)
494 CalendarPrivObject *privateObject =
495 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
497 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_REMOVE);
499 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
503 if (!privateObject) {
504 ThrowMsg(ConversionException, "Object is null.");
507 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
510 CalendarConverter converter(context);
512 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
513 if (argumentCount<1) {
514 ThrowMsg(ConversionException, "Wrong parameter type.");
517 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
518 ThrowMsg(ConversionException, "Wrong parameter type.");
520 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
522 ThrowMsg(ConversionException, "Parameter conversion failed.");
524 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
525 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
526 EventIdPtr result( new EventId() );
527 if (argumentCount>=1) {
528 result->setUId(converter.toString(arguments[0]));
531 itemId->setCalendarType(CalendarEvent::TASK_TYPE);
533 ThrowMsg(ConversionException, "Parameter conversion failed.");
536 IEventDeleteEventPtr dplEvent(new IEventDeleteEvent());
537 dplEvent->setEventId(itemId);
538 dplEvent->setForSynchronousCall();
539 calendar->deleteEvent(dplEvent);
541 if (dplEvent->getResult()) {
542 LogInfo("Successfully deleted.");
543 return JSValueMakeUndefined(context);
545 if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
546 ThrowMsg(NotFoundException, "Item not found.");
548 ThrowMsg(UnknownException, "Removing failed by unnkown reason.");
552 Catch(UnsupportedException)
554 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
555 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
557 Catch(InvalidArgumentException)
559 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
560 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
562 Catch(ConversionException)
564 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
565 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
567 Catch (NotFoundException)
569 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
570 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
574 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
575 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
579 JSValueRef JSCalendar::removeBatch(JSContextRef context,
581 JSObjectRef thisObject,
582 size_t argumentCount,
583 const JSValueRef arguments[],
584 JSValueRef* exception)
586 CalendarPrivObject *privateObject =
587 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
589 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_REMOVE_BATCH);
591 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
595 if (!privateObject) {
596 ThrowMsg(ConversionException, "Object is null.");
599 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
600 JSContextRef globalContext = privateObject->getContext();
602 CalendarConverter converter(context);
604 if (argumentCount>=1) {
605 if (!JSIsArrayValue(context, arguments[0])) {
606 ThrowMsg(ConversionException, "Wrong first parameter type.");
609 ThrowMsg(ConversionException, "Wrong parameter type.");
612 // Pick one array element to determine its type.
613 JSObjectRef objArg = converter.toJSObjectRef(arguments[0]);
615 if (JSGetArrayLength(context, objArg)>0) {
616 element = JSGetArrayElement(context, objArg, 0);
618 ThrowMsg(ConversionException, "Wrong first parameter type.");
621 EventIdListPtr itemIds;
622 if (JSValueIsObjectOfClass(context, element, JSCalendarEventId::getClassRef())) {
623 itemIds = converter.toVectorOfEventIds(arguments[0]);
625 ThrowMsg(ConversionException, "Parameter conversion failed.");
627 } else if (JSValueIsString(context, element)) {
628 std::vector<std::string> idStrings = converter.toVectorOfStrings(arguments[0]);
629 EventIdListPtr result(new EventIdList());
630 for (unsigned int i=0; i<idStrings.size(); i++) {
631 EventIdPtr idPtr( new EventId() );
632 idPtr->setUId(idStrings[i]);
633 idPtr->setCalendarType(CalendarEvent::TASK_TYPE);
634 result->push_back(idPtr);
638 ThrowMsg(ConversionException, "Wrong parameter type.");
641 JSValueRef onSuccess = NULL;
642 if (argumentCount>=2) {
643 onSuccess = converter.toFunctionOrNull(arguments[1]);
645 JSValueRef onError = NULL;
646 if (argumentCount>=3) {
647 onError = converter.toFunctionOrNull(arguments[2]);
649 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
651 cbm->setObject(thisObject);
653 LogDebug("Proceed the event to the platform.");
655 IEventDeleteEventsPtr dplEvent(new IEventDeleteEvents());
656 dplEvent->setEventIds(itemIds);
657 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
658 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
659 calendar->deleteEvents(dplEvent);
661 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
663 return JSValueMakeUndefined(context);
665 Catch(UnsupportedException)
667 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
668 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
670 Catch(InvalidArgumentException)
672 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
673 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
675 Catch(ConversionException)
677 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
678 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
680 Catch (NotFoundException)
682 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
683 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
687 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
688 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
692 JSValueRef JSCalendar::find(JSContextRef context,
694 JSObjectRef thisObject,
695 size_t argumentCount,
696 const JSValueRef arguments[],
697 JSValueRef* exception)
699 CalendarPrivObject *privateObject =
700 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
702 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_FIND);
704 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
708 if (!privateObject) {
709 ThrowMsg(ConversionException, "Object is null.");
712 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
714 JSContextRef globalContext = privateObject->getContext();
716 Validator validator(context, exception);
717 CalendarConverter converter(context);
718 DeviceAPI::Tizen::FilterConverterFactory::ConverterType filterConverter = DeviceAPI::Tizen::FilterConverterFactory::getConverter(context);
720 if (argumentCount<1) {
721 ThrowMsg(ConversionException, "Wrong parameter type.");
724 JSValueRef onSuccess = NULL;
725 if (argumentCount>=1) {
726 onSuccess = converter.toFunction(arguments[0]);
728 JSValueRef onError = NULL;
729 if (argumentCount>=2) {
730 onError = converter.toFunctionOrNull(arguments[1]);
732 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
734 cbm->setObject(thisObject);
736 LogDebug("Proceed the find event to the platform.");
738 IEventFindEventsPtr dplEvent(new IEventFindEvents());
739 dplEvent->setCalendarType(calendar->getType());
740 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
741 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
743 if (argumentCount>=3) {
744 if (JSValueIsObject(context, arguments[2])) {
745 dplEvent->setGenericFilter(filterConverter->toFilter(arguments[2]));
746 } else if (JSValueIsNull(context, arguments[2]) || JSValueIsUndefined(context, arguments[2])) {
747 LogDebug("Use default filter.");
749 ThrowMsg(ConversionException, "Wrong third parameter type.");
752 if (argumentCount>=4) {
753 if (JSValueIsObject(context, arguments[3])) {
754 // Though the sortMode is a single type, we save it in an array internally.
755 DeviceAPI::Tizen::SortModeArrayPtr sortModes(new DeviceAPI::Tizen::SortModeArray());
756 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
757 dplEvent->setSortModes(sortModes);
758 } else if (JSValueIsNull(context, arguments[3]) || JSValueIsUndefined(context, arguments[3])) {
759 LogDebug("Use default sort mode.");
761 ThrowMsg(ConversionException, "Wrong fourth parameter type.");
765 calendar->findEvents(dplEvent);
767 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
769 return JSValueMakeUndefined(context);
771 Catch(UnsupportedException)
773 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
774 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
776 Catch(InvalidArgumentException)
778 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
779 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
781 Catch(ConversionException)
783 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
784 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
786 Catch (NotFoundException)
788 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
789 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
793 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
794 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
798 JSValueRef JSCalendar::get(JSContextRef context,
800 JSObjectRef thisObject,
801 size_t argumentCount,
802 const JSValueRef arguments[],
803 JSValueRef* exception)
805 CalendarPrivObject *privateObject =
806 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
808 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET);
810 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
814 if (!privateObject) {
815 ThrowMsg(ConversionException, "Object is null.");
818 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
819 JSContextRef globalContext = privateObject->getContext();
822 CalendarConverter converter(context);
823 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
824 if (argumentCount<1) {
825 ThrowMsg(ConversionException, "Wrong parameter type.");
828 if (JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
829 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
831 ThrowMsg(ConversionException, "Parameter conversion failed.");
833 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
835 ThrowMsg(ConversionException, "Wrong parameter type.");
837 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
838 EventIdPtr result( new EventId() );
840 if (argumentCount>=1) {
841 itemId->setUId(converter.toString(arguments[0]));
843 itemId->setCalendarType(CalendarEvent::TASK_TYPE);
845 ThrowMsg(ConversionException, "Wrong calendar type.");
848 IEventGetPtr dplEvent(new IEventGet());
849 dplEvent->setForSynchronousCall();
850 dplEvent->setItemId(itemId);
851 calendar->get(dplEvent);
853 // Process the found item.
854 if (dplEvent->getResult()) {
855 LogDebug("Successfully got an item.");
856 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
857 // Use global context for the expandRecurrence api in event.
858 return JSCalendarEvent::createJSCalendarEvent(globalContext, dplEvent->getItem());
859 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
860 return JSCalendarTask::createJSCalendarTask(context, dplEvent->getItem());
862 ThrowMsg(UnknownException, "Wrong calendar type.");
865 if(ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
866 ThrowMsg(NotFoundException, "Item not found.");
868 ThrowMsg(UnknownException, "Getting an item failed by unknown reason.");
872 Catch(UnsupportedException)
874 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
875 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
877 Catch(InvalidArgumentException)
879 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
880 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
882 Catch(ConversionException)
884 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
885 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
887 Catch (NotFoundException)
889 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
890 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
894 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
895 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
899 JSValueRef JSCalendar::addChangeListener(JSContextRef context,
901 JSObjectRef thisObject,
902 size_t argumentCount,
903 const JSValueRef arguments[],
904 JSValueRef* exception)
906 CalendarPrivObject *privateObject =
907 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
909 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER);
911 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
915 if (!privateObject) {
916 ThrowMsg(ConversionException, "Object is null.");
919 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
920 JSContextRef globalContext = privateObject->getContext();
922 CalendarConverter converter(context);
924 if (argumentCount>=1) {
925 if (!JSValueIsObject(context, arguments[0])) {
926 ThrowMsg(ConversionException, "Wrong first parameter type.");
929 ThrowMsg(ConversionException, "Wrong first parameter type.");
932 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[0]);
933 CalendarChangeCallback result;
934 Validator validator(context);
936 result.onAdded = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsadded");
937 if (validator.isNullOrUndefined(result.onAdded)) {
938 result.onAdded = NULL;
939 } else if(!validator.isCallback(result.onAdded)) {
940 ThrowMsg(ConversionException, "Wrong first parameter type.");
943 result.onUpdated = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsupdated");
944 if (validator.isNullOrUndefined(result.onUpdated)) {
945 result.onUpdated = NULL;
946 } else if(!validator.isCallback(result.onUpdated)) {
947 ThrowMsg(ConversionException, "Wrong first parameter type.");
950 result.onDeleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsremoved");
951 if (validator.isNullOrUndefined(result.onDeleted)) {
952 result.onDeleted = NULL;
953 } else if(!validator.isCallback(result.onDeleted)) {
954 ThrowMsg(ConversionException, "Wrong first parameter type.");
957 if (!result.onAdded && !result.onUpdated && !result.onDeleted) {
958 ThrowMsg(ConversionException, "Wrong second parameter type.");
961 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(globalContext, result.onAdded, NULL);
962 JSCallbackManagerPtr onUpdatedCbm = JSCallbackManager::createObject(globalContext, result.onUpdated, NULL);
963 JSCallbackManagerPtr onDeletedCbm = JSCallbackManager::createObject(globalContext, result.onDeleted, NULL);
965 LogDebug("Make change emitter and process sync operation.");
967 CalendarChangeCallbackPrivateDataPtr privData(new CalendarChangeCallbackPrivateData(
968 onAddedCbm, onUpdatedCbm, onDeletedCbm));
969 OnEventsChangedEmitterPtr emitter(new OnEventsChangedEmitter());
970 emitter->setListener(&CalendarResponseDispatcher::getInstance());
971 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
973 IEventWatchChangesPtr dplEvent(new IEventWatchChanges());
974 dplEvent->setEmitter(emitter);
975 dplEvent->setForSynchronousCall();
976 calendar->watchChanges(dplEvent);
979 if (dplEvent->getResult()) {
980 watchId = dplEvent->getWatchId();
982 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
983 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
984 CalendarListenerManagerSingleton::Instance().registerListener(listenerItem, globalContext);
986 LogDebug("Returning the watchId "<<watchId);
987 return converter.toJSValueRefLong(watchId);
989 ThrowMsg(UnknownException, "Adding change listener failed by unknown reason.");
992 Catch(UnsupportedException)
994 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
995 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
997 Catch(InvalidArgumentException)
999 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1000 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1002 Catch(ConversionException)
1004 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1005 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1009 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1010 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1014 JSValueRef JSCalendar::removeChangeListener(JSContextRef context,
1016 JSObjectRef thisObject,
1017 size_t argumentCount,
1018 const JSValueRef arguments[],
1019 JSValueRef* exception)
1021 CalendarPrivObject *privateObject =
1022 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1024 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER);
1026 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1030 if (!privateObject) {
1031 ThrowMsg(ConversionException, "Object is null.");
1034 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1036 JSContextRef globalContext = privateObject->getContext();
1039 CalendarConverter converter(context);
1040 if (argumentCount>=1) {
1041 watchId = converter.toLong(arguments[0]);
1043 ThrowMsg(ConversionException, "Wrong parameter type.");
1046 LogDebug("Make sync operation with watchId: "<<watchId);
1048 IEventClearWatchPtr dplEvent(new IEventClearWatch());
1049 dplEvent->setWatchId(watchId);
1050 dplEvent->setForSynchronousCall();
1051 calendar->clearWatch(dplEvent);
1053 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
1054 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1055 CalendarListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1057 return JSValueMakeUndefined(context);
1059 Catch(UnsupportedException)
1061 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1062 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1064 Catch(InvalidArgumentException)
1066 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1067 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1069 Catch(ConversionException)
1071 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1072 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1074 Catch (NotFoundException)
1076 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1077 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1081 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1082 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1086 JSValueRef JSCalendar::getPropertyName(JSContextRef context,
1088 JSStringRef propertyName,
1089 JSValueRef* exception)
1093 ICalendarPtr calendar = getCalendar(context, object, exception);
1094 Converter converter(context);
1095 if(calendar->getIsUnified()) {
1096 return JSValueMakeNull(context);
1098 return converter.toJSValueRef(calendar->getName());
1103 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1105 return JSValueMakeUndefined(context);
1108 JSValueRef JSCalendar::getPropertyAccountServiceId(JSContextRef context,
1110 JSStringRef propertyName,
1111 JSValueRef* exception)
1115 ICalendarPtr calendar = getCalendar(context, object, exception);
1116 Converter converter(context);
1117 if(calendar->getIsUnified()) {
1118 return JSValueMakeNull(context);
1120 return converter.toJSValueRefLong(calendar->getAccountId());
1125 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1127 return JSValueMakeUndefined(context);
1130 JSValueRef JSCalendar::getPropertyId(JSContextRef context,
1132 JSStringRef propertyName,
1133 JSValueRef* exception)
1137 ICalendarPtr calendar = getCalendar(context, object, exception);
1138 Converter converter(context);
1139 if(calendar->getIsUnified()) {
1140 return JSValueMakeNull(context);
1142 return converter.toJSValueRef(calendar->getId());
1147 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1149 return JSValueMakeUndefined(context);
1152 ICalendarPtr JSCalendar::getCalendar(JSContextRef ctx,
1153 const JSObjectRef object,
1154 JSValueRef* exception)
1156 CalendarPrivObject *priv =
1157 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
1159 return priv->getObject();
1161 ThrowMsg(NullPointerException, "Private object is NULL.");