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 "ICalendar.h"
21 #include "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 <JSWebAPIErrorFactory.h>
30 #include <SecurityExceptions.h>
31 #include <TimeUtilConverter.h>
32 #include <TimeTracer.h>
35 #include <GlobalContextManager.h>
37 #include "JSCalendarManager.h"
38 #include "CalendarConverter.h"
39 #include "JSCalendar.h"
40 #include "JSCalendarEvent.h"
41 #include "JSCalendarTask.h"
42 #include "JSCalendarItemProperties.h"
43 #include "JSCalendarEventId.h"
44 #include "plugin_config.h"
45 #include "CalendarResponseDispatcher.h"
46 #include "CalendarMultiCallback.h"
47 #include "CalendarFilterConverter.h"
49 #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 using WrtDeviceApis::Commons::UnknownException;
61 using WrtDeviceApis::Commons::NotFoundException;
63 JSClassDefinition JSCalendar::m_classInfo = {
65 kJSClassAttributeNone,
66 TIZEN_CALENDAR_ATTRIBUTENAME,
75 NULL, //DeleteProperty,
76 NULL, //GetPropertyNames,
77 NULL, //CallAsFunction,
78 NULL, //CallAsConstructor,
83 JSStaticValue JSCalendar::m_property[] = {
84 { TIZEN_CALENDAR_PROPERTY_NAME, JSCalendar::getPropertyName, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
85 /*{ TIZEN_CALENDAR_PROPERTY_ACCOUNT_SERVICE_ID, JSCalendar::getPropertyAccountServiceId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },*/
86 { TIZEN_CALENDAR_PROPERTY_ID, JSCalendar::getPropertyId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
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 LoggerD("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 LoggerD("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 DLL_EXPORT 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 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
151 CalendarPrivObject *privateObject =
152 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
155 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_ADD);
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 CalendarConverter converter(context);
183 CalendarEventPtr item = converter.toItem(arguments[0], true);
185 ThrowMsg(ConversionException, "Parameter conversion failed.");
188 IEventAddEventPtr dplEvent(new IEventAddEvent());
189 dplEvent->setEvent(item);
190 dplEvent->setForSynchronousCall();
191 calendar->addEvent(dplEvent);
193 if (dplEvent->getResult()) {
194 LoggerD("Add result successful.");
195 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
196 return JSValueMakeUndefined(context);
198 ThrowMsg(UnknownException, "Adding failed by unknown reason.");
201 Catch(UnsupportedException)
203 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
204 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
206 Catch(InvalidArgumentException)
208 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
209 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
211 Catch(ConversionException)
213 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
214 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
218 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
219 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
223 JSValueRef JSCalendar::addBatch(JSContextRef context,
225 JSObjectRef thisObject,
226 size_t argumentCount,
227 const JSValueRef arguments[],
228 JSValueRef* exception)
230 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
231 CalendarPrivObject *privateObject =
232 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
234 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_ADD_BATCH);
238 if (!privateObject) {
239 ThrowMsg(ConversionException, "Object is null.");
242 ICalendarPtr calendar = getCalendar(context, thisObject, NULL);
244 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
245 CalendarConverter converter(context);
247 if (argumentCount<1) {
248 ThrowMsg(ConversionException, "Wrong parameter type.");
251 CalendarEventListPtr events;
252 if (!JSIsArrayValue(context, arguments[0])) {
253 ThrowMsg(ConversionException, "Wrong first parameter type.");
255 events = converter.toVectorOfItemsFromDictionary(arguments[0], true);
257 ThrowMsg(ConversionException, "First parameter conversion failed.");
260 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
261 if (argumentCount>=2) {
262 cbm->setOnSuccess(converter.toFunctionOrNull(arguments[1]));
264 if (argumentCount>=3) {
265 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
268 cbm->setObject(thisObject);
270 LoggerD("Proceed the event to the platform.");
272 IEventAddEventsPtr dplEvent(new IEventAddEvents());
273 dplEvent->setCalendarType(calendar->getType());
274 dplEvent->setEvents(events);
275 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
276 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
277 dplEvent->copyAceCheckAccessFunction(privateObject);
278 calendar->addEvents(dplEvent);
280 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
281 return JSValueMakeUndefined(context);
283 Catch(UnsupportedException)
285 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
286 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
288 Catch(InvalidArgumentException)
290 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
291 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
293 Catch(ConversionException)
295 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
296 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
300 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
301 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
305 JSValueRef JSCalendar::update(JSContextRef context,
307 JSObjectRef thisObject,
308 size_t argumentCount,
309 const JSValueRef arguments[],
310 JSValueRef* exception)
312 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
313 CalendarPrivObject *privateObject =
314 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
316 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_UPDATE);
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 CalendarEventPtr item = converter.toItem(arguments[0], true);
343 ThrowMsg(ConversionException, "Parameter conversion failed.");
346 bool updateAllInstances = true; // Set the default value.
347 if (argumentCount>=2) {
348 updateAllInstances = converter.toBool(arguments[1]);
351 IEventUpdateEventPtr dplEvent(new IEventUpdateEvent());
352 dplEvent->setEvent(item);
353 dplEvent->setUpdateAllInstances(updateAllInstances);
354 dplEvent->setForSynchronousCall();
355 calendar->updateEvent(dplEvent);
357 if (dplEvent->getResult()) {
358 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
359 return JSValueMakeUndefined(context);
361 ThrowMsg(UnknownException, "Updating failed by unknown reason.");
364 Catch(UnsupportedException)
366 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
367 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
369 Catch(InvalidArgumentException)
371 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
372 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
374 Catch(ConversionException)
376 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
377 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
379 Catch (NotFoundException)
381 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
382 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
386 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
387 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::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 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
399 CalendarPrivObject *privateObject =
400 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
402 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_UPDATE_BATCH);
406 if (!privateObject) {
407 ThrowMsg(ConversionException, "Object is null.");
410 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
412 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
414 CalendarConverter converter(context);
416 if (argumentCount>=1) {
417 if (!JSIsArrayValue(context, arguments[0])) {
418 ThrowMsg(ConversionException, "Wrong first parameter type.");
421 ThrowMsg(ConversionException, "Wrong parameter type.");
423 CalendarEventListPtr items;
424 items = converter.toVectorOfItemsFromDictionary(arguments[0], true);
426 ThrowMsg(ConversionException, "Third parameter conversion failed.");
429 JSValueRef onSuccess = NULL;
430 if (argumentCount>=2) {
431 onSuccess = converter.toFunctionOrNull(arguments[1]);
433 JSValueRef onError = NULL;
434 if (argumentCount>=3) {
435 onError = converter.toFunctionOrNull(arguments[2]);
437 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
439 bool updateAllInstances = true; // Set the default value.
440 if( argumentCount>=4 ) {
441 updateAllInstances = converter.toBool(arguments[3]);
444 cbm->setObject(thisObject);
446 LoggerD("Proceed the event to the platform.");
448 IEventUpdateEventsPtr dplEvent(new IEventUpdateEvents());
449 dplEvent->setEvents(items);
450 dplEvent->setUpdateAllInstances(updateAllInstances);
451 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
452 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
453 calendar->updateEvents(dplEvent);
455 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
456 return JSValueMakeUndefined(context);
458 Catch(UnsupportedException)
460 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
461 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
463 Catch(InvalidArgumentException)
465 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
466 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
468 Catch(ConversionException)
470 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
471 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
473 Catch (NotFoundException)
475 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
476 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
480 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
481 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
485 JSValueRef JSCalendar::remove(JSContextRef context,
487 JSObjectRef thisObject,
488 size_t argumentCount,
489 const JSValueRef arguments[],
490 JSValueRef* exception)
492 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
493 CalendarPrivObject *privateObject =
494 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
496 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_REMOVE);
500 if (!privateObject) {
501 ThrowMsg(ConversionException, "Object is null.");
504 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
507 CalendarConverter converter(context);
509 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
510 if (argumentCount<1) {
511 ThrowMsg(ConversionException, "Wrong parameter type.");
514 if (!JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
515 ThrowMsg(ConversionException, "Wrong parameter type.");
517 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
519 ThrowMsg(ConversionException, "Parameter conversion failed.");
521 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
522 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
523 EventIdPtr result( new EventId() );
524 if (argumentCount>=1) {
525 result->setUId(converter.toString(arguments[0]));
528 itemId->setCalendarType(CalendarEvent::TASK_TYPE);
530 ThrowMsg(ConversionException, "Parameter conversion failed.");
533 IEventDeleteEventPtr dplEvent(new IEventDeleteEvent());
534 dplEvent->setEventId(itemId);
535 dplEvent->setForSynchronousCall();
536 calendar->deleteEvent(dplEvent);
538 if (dplEvent->getResult()) {
539 LoggerI("Successfully deleted.");
540 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
541 return JSValueMakeUndefined(context);
543 if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
544 ThrowMsg(NotFoundException, "Item not found.");
546 ThrowMsg(UnknownException, "Removing failed by unnkown reason.");
550 Catch(UnsupportedException)
552 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
553 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
555 Catch(InvalidArgumentException)
557 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
558 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
560 Catch(ConversionException)
562 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
563 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
565 Catch (NotFoundException)
567 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
568 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
572 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
573 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
577 JSValueRef JSCalendar::removeBatch(JSContextRef context,
579 JSObjectRef thisObject,
580 size_t argumentCount,
581 const JSValueRef arguments[],
582 JSValueRef* exception)
584 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
585 CalendarPrivObject *privateObject =
586 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
588 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_REMOVE_BATCH);
592 if (!privateObject) {
593 ThrowMsg(ConversionException, "Object is null.");
596 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
597 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
599 CalendarConverter converter(context);
601 if (argumentCount>=1) {
602 if (!JSIsArrayValue(context, arguments[0])) {
603 ThrowMsg(ConversionException, "Wrong first parameter type.");
606 ThrowMsg(ConversionException, "Wrong parameter type.");
609 // Pick one array element to determine its type.
610 JSObjectRef objArg = converter.toJSObjectRef(arguments[0]);
612 if (JSGetArrayLength(context, objArg)>0) {
613 element = JSGetArrayElement(context, objArg, 0);
615 ThrowMsg(ConversionException, "Wrong first parameter type.");
618 EventIdListPtr itemIds;
619 if (JSValueIsObjectOfClass(context, element, JSCalendarEventId::getClassRef())) {
620 itemIds = converter.toVectorOfEventIds(arguments[0]);
622 ThrowMsg(ConversionException, "Parameter conversion failed.");
624 LoggerD("Array length: "<<itemIds->size());
626 } else if (JSValueIsString(context, element)) {
627 std::vector<std::string> idStrings = converter.toVectorOfStrings(arguments[0]);
628 EventIdListPtr result(new EventIdList());
629 for (unsigned int i=0; i<idStrings.size(); i++) {
630 EventIdPtr idPtr( new EventId() );
631 idPtr->setUId(idStrings[i]);
632 idPtr->setCalendarType(CalendarEvent::TASK_TYPE);
633 result->push_back(idPtr);
637 ThrowMsg(ConversionException, "Wrong parameter type.");
640 JSValueRef onSuccess = NULL;
641 if (argumentCount>=2) {
642 onSuccess = converter.toFunctionOrNull(arguments[1]);
644 JSValueRef onError = NULL;
645 if (argumentCount>=3) {
646 onError = converter.toFunctionOrNull(arguments[2]);
648 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
650 cbm->setObject(thisObject);
652 LoggerD("Proceed the event to the platform.");
654 IEventDeleteEventsPtr dplEvent(new IEventDeleteEvents());
655 dplEvent->setEventIds(itemIds);
656 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
657 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
658 calendar->deleteEvents(dplEvent);
660 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
661 return JSValueMakeUndefined(context);
663 Catch(UnsupportedException)
665 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
666 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
668 Catch(InvalidArgumentException)
670 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
671 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
673 Catch(ConversionException)
675 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
676 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
678 Catch (NotFoundException)
680 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
681 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
685 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
686 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
690 JSValueRef JSCalendar::find(JSContextRef context,
692 JSObjectRef thisObject,
693 size_t argumentCount,
694 const JSValueRef arguments[],
695 JSValueRef* exception)
697 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
698 CalendarPrivObject *privateObject =
699 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
701 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_FIND);
705 if (!privateObject) {
706 ThrowMsg(ConversionException, "Object is null.");
709 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
711 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
713 Validator validator(context, exception);
714 CalendarConverter converter(context);
715 CalendarFilterConverterFactory::ConverterType filterConverter =
716 CalendarFilterConverterFactory::ConverterType(new CalendarFilterConverter(context));
718 if (argumentCount<1) {
719 ThrowMsg(ConversionException, "Wrong parameter type.");
722 JSValueRef onSuccess = NULL;
723 if (argumentCount>=1) {
724 onSuccess = converter.toFunction(arguments[0]);
726 JSValueRef onError = NULL;
727 if (argumentCount>=2) {
728 onError = converter.toFunctionOrNull(arguments[1]);
730 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, onSuccess, onError);
732 cbm->setObject(thisObject);
734 LoggerD("Proceed the find event to the platform.");
736 IEventFindEventsPtr dplEvent(new IEventFindEvents());
737 dplEvent->setCalendarType(calendar->getType());
738 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
739 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
740 dplEvent->copyAceCheckAccessFunction(privateObject);
742 if (argumentCount>=3) {
743 if (JSValueIsObject(context, arguments[2])) {
744 dplEvent->setGenericFilter(filterConverter->toFilter(arguments[2]));
745 } else if (JSValueIsNull(context, arguments[2])) {
746 LoggerD("Use default filter.");
748 ThrowMsg(ConversionException, "Wrong third parameter type.");
751 if (argumentCount>=4) {
752 if (JSValueIsObject(context, arguments[3])) {
753 // Though the sortMode is a single type, we save it in an array internally.
754 DeviceAPI::Tizen::SortModeArrayPtr sortModes(new DeviceAPI::Tizen::SortModeArray());
755 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
756 dplEvent->setSortModes(sortModes);
757 } else if (JSValueIsNull(context, arguments[3])) {
758 LoggerD("Use default sort mode.");
760 ThrowMsg(ConversionException, "Wrong fourth parameter type.");
764 calendar->findEvents(dplEvent);
766 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
767 return JSValueMakeUndefined(context);
769 Catch(UnsupportedException)
771 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
772 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
774 Catch(InvalidArgumentException)
776 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
777 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
779 Catch(ConversionException)
781 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
782 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
784 Catch (NotFoundException)
786 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
787 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
791 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
792 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
796 JSValueRef JSCalendar::get(JSContextRef context,
798 JSObjectRef thisObject,
799 size_t argumentCount,
800 const JSValueRef arguments[],
801 JSValueRef* exception)
803 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
804 CalendarPrivObject *privateObject =
805 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
807 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET);
811 if (!privateObject) {
812 ThrowMsg(ConversionException, "Object is null.");
815 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
816 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
819 CalendarConverter converter(context);
820 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
821 if (argumentCount<1) {
822 ThrowMsg(ConversionException, "Wrong parameter type.");
825 if (JSValueIsObjectOfClass(context, arguments[0], JSCalendarEventId::getClassRef())) {
826 itemId = JSCalendarEventId::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
828 ThrowMsg(ConversionException, "Parameter conversion failed.");
830 itemId->setCalendarType(CalendarEvent::EVENT_TYPE);
832 ThrowMsg(ConversionException, "Wrong parameter type.");
834 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
835 EventIdPtr result( new EventId() );
837 if (argumentCount>=1) {
838 itemId->setUId(converter.toString(arguments[0]));
840 itemId->setCalendarType(CalendarEvent::TASK_TYPE);
842 ThrowMsg(ConversionException, "Wrong calendar type.");
845 IEventGetPtr dplEvent(new IEventGet());
846 dplEvent->setForSynchronousCall();
847 dplEvent->setItemId(itemId);
848 calendar->get(dplEvent);
850 // Process the found item.
851 if (dplEvent->getResult()) {
852 LoggerD("Successfully got an item.");
853 if (CalendarEvent::EVENT_TYPE==calendar->getType()) {
854 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
855 // Use global context for the expandRecurrence api in event.
856 return JSCalendarEvent::createJSCalendarEvent(globalContext, dplEvent->getItem(), privateObject);
857 } else if (CalendarEvent::TASK_TYPE==calendar->getType()) {
858 return JSCalendarTask::createJSCalendarTask(context, dplEvent->getItem(), privateObject);
860 ThrowMsg(UnknownException, "Wrong calendar type.");
863 if(ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
864 ThrowMsg(NotFoundException, "Item not found.");
866 ThrowMsg(UnknownException, "Getting an item failed by unknown reason.");
870 Catch(UnsupportedException)
872 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
873 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
875 Catch(InvalidArgumentException)
877 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
878 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
880 Catch(ConversionException)
882 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
883 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
885 Catch (NotFoundException)
887 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
888 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
892 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
893 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
897 JSValueRef JSCalendar::addChangeListener(JSContextRef context,
899 JSObjectRef thisObject,
900 size_t argumentCount,
901 const JSValueRef arguments[],
902 JSValueRef* exception)
904 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
905 CalendarPrivObject *privateObject =
906 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
908 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_ADD_CHANGE_LISTENER);
912 if (!privateObject) {
913 ThrowMsg(ConversionException, "Object is null.");
916 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
917 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
919 CalendarConverter converter(context);
921 if (argumentCount>=1) {
922 if (!JSValueIsObject(context, arguments[0])) {
923 ThrowMsg(ConversionException, "Wrong first parameter type.");
926 ThrowMsg(ConversionException, "Wrong first parameter type.");
929 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[0]);
930 CalendarChangeCallback result;
931 Validator validator(context);
933 result.onAdded = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsadded");
934 if (validator.isNullOrUndefined(result.onAdded)) {
935 result.onAdded = NULL;
936 } else if(!validator.isCallback(result.onAdded)) {
937 ThrowMsg(ConversionException, "Wrong first parameter type.");
940 result.onUpdated = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsupdated");
941 if (validator.isNullOrUndefined(result.onUpdated)) {
942 result.onUpdated = NULL;
943 } else if(!validator.isCallback(result.onUpdated)) {
944 ThrowMsg(ConversionException, "Wrong first parameter type.");
947 result.onDeleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onitemsremoved");
948 if (validator.isNullOrUndefined(result.onDeleted)) {
949 result.onDeleted = NULL;
950 } else if(!validator.isCallback(result.onDeleted)) {
951 ThrowMsg(ConversionException, "Wrong first parameter type.");
954 if (!result.onAdded && !result.onUpdated && !result.onDeleted) {
955 ThrowMsg(ConversionException, "Wrong second parameter type.");
958 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(globalContext, result.onAdded, NULL);
959 JSCallbackManagerPtr onUpdatedCbm = JSCallbackManager::createObject(globalContext, result.onUpdated, NULL);
960 JSCallbackManagerPtr onDeletedCbm = JSCallbackManager::createObject(globalContext, result.onDeleted, NULL);
962 LoggerD("Make change emitter and process sync operation.");
964 CalendarChangeCallbackPrivateDataPtr privData(new CalendarChangeCallbackPrivateData(
965 onAddedCbm, onUpdatedCbm, onDeletedCbm));
966 privData->copyAceCheckAccessFunction(privateObject);
967 OnEventsChangedEmitterPtr emitter(new OnEventsChangedEmitter());
968 emitter->setListener(&CalendarResponseDispatcher::getInstance());
969 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
971 IEventWatchChangesPtr dplEvent(new IEventWatchChanges());
972 dplEvent->setEmitter(emitter);
973 dplEvent->setForSynchronousCall();
974 calendar->watchChanges(dplEvent);
977 if (dplEvent->getResult()) {
978 watchId = dplEvent->getWatchId();
980 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
981 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
982 CalendarListenerManagerSingleton::Instance().registerListener(listenerItem, globalContext);
984 LoggerD("Returning the watchId "<<watchId);
985 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
986 return converter.toJSValueRefLong(watchId);
988 ThrowMsg(UnknownException, "Adding change listener failed by unknown reason.");
991 Catch(UnsupportedException)
993 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
994 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
996 Catch(InvalidArgumentException)
998 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
999 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1001 Catch(ConversionException)
1003 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1004 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1008 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1009 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1013 JSValueRef JSCalendar::removeChangeListener(JSContextRef context,
1015 JSObjectRef thisObject,
1016 size_t argumentCount,
1017 const JSValueRef arguments[],
1018 JSValueRef* exception)
1020 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1021 CalendarPrivObject *privateObject =
1022 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(thisObject));
1024 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_REMOVE_CHANGE_LISTENER);
1028 if (!privateObject) {
1029 ThrowMsg(ConversionException, "Object is null.");
1032 ICalendarPtr calendar = getCalendar(context, thisObject, exception);
1034 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
1037 CalendarConverter converter(context);
1038 if (argumentCount>=1) {
1039 watchId = converter.toLong(arguments[0]);
1041 watchId = converter.toLong(JSValueMakeUndefined(context));
1044 LoggerD("Make sync operation with watchId: "<<watchId);
1046 IEventClearWatchPtr dplEvent(new IEventClearWatch());
1047 dplEvent->setWatchId(watchId);
1048 dplEvent->setForSynchronousCall();
1049 calendar->clearWatch(dplEvent);
1051 CalendarListenerCancellerPtr canceller = CalendarListenerCancellerPtr(new CalendarListenerCanceller(globalContext, thisObject, watchId));
1052 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1053 CalendarListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1055 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1056 return JSValueMakeUndefined(context);
1058 Catch(UnsupportedException)
1060 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1061 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
1063 Catch(InvalidArgumentException)
1065 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1066 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1068 Catch(ConversionException)
1070 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1071 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1073 Catch (NotFoundException)
1075 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1076 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1080 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1081 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1085 JSValueRef JSCalendar::getPropertyName(JSContextRef context,
1087 JSStringRef propertyName,
1088 JSValueRef* exception)
1092 ICalendarPtr calendar = getCalendar(context, object, exception);
1093 Converter converter(context);
1094 if(calendar->getIsUnified()) {
1095 return JSValueMakeNull(context);
1097 return converter.toJSValueRef(calendar->getName());
1102 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1104 return JSValueMakeUndefined(context);
1107 JSValueRef JSCalendar::getPropertyAccountServiceId(JSContextRef context,
1109 JSStringRef propertyName,
1110 JSValueRef* exception)
1114 ICalendarPtr calendar = getCalendar(context, object, exception);
1115 Converter converter(context);
1116 if(calendar->getIsUnified()) {
1117 return JSValueMakeNull(context);
1119 return converter.toJSValueRefLong(calendar->getAccountId());
1124 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1126 return JSValueMakeUndefined(context);
1129 JSValueRef JSCalendar::getPropertyId(JSContextRef context,
1131 JSStringRef propertyName,
1132 JSValueRef* exception)
1136 ICalendarPtr calendar = getCalendar(context, object, exception);
1137 Converter converter(context);
1138 if(calendar->getIsUnified()) {
1139 return JSValueMakeNull(context);
1141 return converter.toJSValueRef(calendar->getId());
1146 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1148 return JSValueMakeUndefined(context);
1151 ICalendarPtr JSCalendar::getCalendar(JSContextRef ctx,
1152 const JSObjectRef object,
1153 JSValueRef* exception)
1155 CalendarPrivObject *priv =
1156 static_cast<CalendarPrivObject*>(JSObjectGetPrivate(object));
1158 return priv->getObject();
1160 ThrowMsg(NullPointerException, "Private object is NULL.");