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.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <CommonsJavaScript/Validator.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <API/Calendar/EventId.h>
27 #include "JSCalendarEvent.h"
28 #include "JSCalendarItemProperties.h"
29 #include "CalendarConverter.h"
30 #include "JSCalendarEventId.h"
31 #include "plugin_config.h"
32 #include "CalendarResponseDispatcher.h"
33 #include <Tizen/TimeUtil/TimeUtilConverter.h>
34 #include <Tizen/TimeUtil/JSTZDate.h>
35 #include <Tizen/Common/JSTizenException.h>
36 #include <Tizen/Common/JSTizenExceptionFactory.h>
37 #include <Tizen/Common/SecurityExceptions.h>
39 using namespace TizenApis::Api::Calendar;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace TizenApis::Commons;
48 JSClassDefinition JSCalendarEvent::m_classInfo = {
50 kJSClassAttributeNone,
51 TIZEN_INTERFACE_CALENDAR_EVENT,
52 JSCalendarItemProperties::getClassRef(),
60 NULL, //DeleteProperty,
61 NULL, //GetPropertyNames,
62 NULL, //CallAsFunction,
68 JSStaticValue JSCalendarEvent::m_property[] = {
69 { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
70 NULL, kJSPropertyAttributeReadOnly },
71 { TIZEN_CALENDAR_EVENT_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
72 NULL, kJSPropertyAttributeReadOnly },
73 { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
74 NULL, kJSPropertyAttributeReadOnly },
79 JSStaticFunction JSCalendarEvent::m_function[] = {
80 { CALENDAR_FUNCTION_API_EXPAND_RECURRENCE, expandRecurrence, kJSPropertyAttributeNone },
85 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
87 ICalendarPtr JSCalendarEvent::m_calendar = CalendarFactory::getInstance().createCalendarObject();
89 void JSCalendarEvent::initialize(JSContextRef context,
92 if (!JSObjectGetPrivate(object)) {
93 LogDebug("Create calendar event private object.");
94 CalendarEventPtr event( new CalendarEvent() );
95 CalendarEventPrivObject *priv = new CalendarEventPrivObject(context, event);
96 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
100 LogDebug("Private object alrerady set.");
104 m_calendar->setType(CalendarEvent::EVENT_TYPE);
105 LogDebug("Calendar object type is set to event.");
109 void JSCalendarEvent::finalize(JSObjectRef object)
112 CalendarEventPrivObject* priv = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
115 JSObjectSetPrivate(object, NULL);
119 JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
120 JSObjectRef constructor,
121 size_t argumentCount,
122 const JSValueRef arguments[],
123 JSValueRef* exception)
129 CalendarEventPrivObject* privateObject = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(constructor));
130 JSContextRef globalContext = privateObject->getContext();
132 CalendarConverter converter(globalContext);
133 CalendarEventPtr event;
135 if (argumentCount==0) {
136 CalendarEventPtr result(new CalendarEvent());
138 } else if (argumentCount==1) {
139 LogInfo("eventInitDict case");
140 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
141 CalendarEventPtr result(new CalendarEvent());
143 } else if (JSValueIsObject(context, arguments[0])) {
144 event = converter.toEvent(arguments[0]);
146 ThrowMsg(ConversionException, "Parameter conversion failed.");
149 ThrowMsg(ConversionException, "Parameter conversion failed.");
151 } else if (argumentCount>=2) {
152 LogInfo("event stringRepresentation case");
153 std::string eventStr;
154 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
155 eventStr = converter.toString(arguments[0]);
156 format = converter.toVObjectFormat(converter.toString(arguments[1]));
158 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
159 dplEvent->setEventString(eventStr);
160 dplEvent->setFormat(format);
161 dplEvent->setForSynchronousCall();
162 m_calendar->createEventFromString(dplEvent);
164 // Process the returned object.
165 if (dplEvent->getResult()) {
166 LogInfo("Successfully created an event.");
167 event = dplEvent->getEvent();
169 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
170 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
172 ThrowMsg(UnknownException, "Converting string failed.");
177 return createJSCalendarEvent(globalContext, event);
179 Catch(UnsupportedException)
181 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
182 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
184 Catch(InvalidArgumentException)
186 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
187 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
189 Catch(ConversionException)
191 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
192 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
196 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
197 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
203 JSObjectRef JSCalendarEvent::createJSCalendarEvent(JSContextRef context, CalendarEventPtr event)
205 CalendarEventPrivObject *priv = new CalendarEventPrivObject(context, event);
206 return JSObjectMake(context, getClassRef(), priv);
209 const JSClassRef JSCalendarEvent::getClassRef()
212 m_jsClassRef = JSClassCreate(&m_classInfo);
217 const JSClassDefinition* JSCalendarEvent::getClassInfo()
222 CalendarEventPtr JSCalendarEvent::getPrivateObject(JSObjectRef object)
225 CalendarEventPrivObject *priv =
226 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
228 ThrowMsg(NullPointerException, "Private object is null");
230 CalendarEventPtr result = priv->getObject();
232 ThrowMsg(NullPointerException, "Private object is null");
237 void JSCalendarEvent::setPrivateObject(const CalendarEventPtr &event,
239 const JSObjectRef object)
244 CalendarEventPrivObject *priv =
245 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
247 priv = new CalendarEventPrivObject(ctx, event);
248 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
254 LogError("Error during replacing event object");
258 JSValueRef JSCalendarEvent::expandRecurrence(JSContextRef context,
260 JSObjectRef thisObject,
261 size_t argumentCount,
262 const JSValueRef arguments[],
263 JSValueRef* exception)
267 CalendarEventPrivObject *privateObject =
268 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(thisObject));
270 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_EXPAND_RECURRENCE);
272 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
276 if (!privateObject) {
277 ThrowMsg(ConversionException, "Object is null.");
280 CalendarEventPtr event = privateObject->getObject();
282 ThrowMsg(ConversionException, "Parameter conversion failed.");
285 JSContextRef globalContext = privateObject->getContext();
286 CalendarConverter converter(context);
288 std::time_t startDate = 0;
289 std::time_t endDate = INT_MAX; // about 60 years in 4 bytes system.
290 TimeUtilConverter timeConverter(context);
291 if (argumentCount>=1) {
292 if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())) {
293 startDate = timeConverter.toTZDateTimeT(arguments[0]);
294 LogInfo("startDate: "<<startDate);
296 ThrowMsg(ConversionException, "Wrong first parameter type.");
299 if (argumentCount>=2) {
300 if (JSValueIsObjectOfClass(context, arguments[1], JSTZDate::getClassRef())) {
301 endDate = timeConverter.toTZDateTimeT(arguments[1]);
302 LogInfo("endDate: "<<endDate);
304 ThrowMsg(ConversionException, "Wrong second parameter type.");
308 JSValueRef onError = argumentCount > 1 ? converter.toFunctionOrNull(arguments[3]) : NULL;
310 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
312 Validator validator(context);
313 if (validator.isCallback(arguments[2])) {
314 cbm->setOnSuccess(arguments[2]);
316 ThrowMsg(ConversionException, "Wrong third parameter type.");
319 // Protect the super object until the callback operation is finished.
320 JSValueProtect(globalContext, thisObject);
322 LogDebug("Proceed the expand event to the platform.");
324 IEventExpandEventRecurrencePtr dplEvent(new IEventExpandEventRecurrence());
325 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
326 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
327 dplEvent->setEvent(event);
328 dplEvent->setStartDate(startDate);
329 dplEvent->setEndDate(endDate);
330 m_calendar->expandEventRecurrence(dplEvent);
332 return JSValueMakeUndefined(context);
334 Catch(UnsupportedException)
336 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
337 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
339 Catch(InvalidArgumentException)
341 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
342 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
344 Catch(ConversionException)
346 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
347 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
349 Catch (NotFoundException)
351 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
352 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
356 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
357 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
360 return JSValueMakeUndefined(context);
363 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
365 JSStringRef propertyName,
366 JSValueRef* exception)
371 CalendarEventPrivObject *privateObject =
372 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
373 CalendarEventPtr event = privateObject->getObject();
375 EventIdPtr eventId( new EventId() );
376 if (0<event->getParentId()) {
377 std::stringstream ss;
378 ss<<event->getParentId();
379 eventId->setUId(ss.str());
381 eventId->setUId(event->getUId());
383 std::stringstream ss;
384 std::time_t rid = event->getRecurrenceId();
386 eventId->setRecurrenceId(ss.str());
387 return JSCalendarEventId::createJSCalendarEventId(context, eventId);
391 LogWarning("trying to get incorrect value");
393 return JSValueMakeUndefined(context);
396 JSValueRef JSCalendarEvent::getPropertyLastModificationDate(JSContextRef context,
398 JSStringRef propertyName,
399 JSValueRef* exception)
404 CalendarEventPrivObject *privateObject =
405 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
406 CalendarEventPtr event = privateObject->getObject();
408 Throw(NullPointerException);
411 if (UNDEFINED_TIME==event->getLastModifiedDate()) {
412 return JSValueMakeNull(context);
414 return JSTZDate::createJSObject(context, event->getLastModifiedDate(), event->getTimeZone());
419 LogWarning("trying to get incorrect value");
421 return JSValueMakeUndefined(context);
424 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
426 JSStringRef propertyName,
427 JSValueRef* exception)
432 CalendarEventPtr event = getPrivateObject(object);
433 if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
434 return JSValueMakeUndefined(context);
437 Converter converter(context);
438 return converter.toJSValueRef(event->getIsDetached());
442 LogWarning("trying to get incorrect value");
444 return JSValueMakeUndefined(context);
447 bool JSCalendarEvent::validate(JSContextRef ctx,
448 const JSObjectRef object,
449 JSValueRef* exception)
452 CalendarEventPrivObject *priv =
453 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
457 CalendarEventPtr event = priv->getObject();
461 return event->validate();