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 #include "CalendarAsyncCallbackManager.h"
41 using namespace TizenApis::Api::Calendar;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
44 using namespace TizenApis::Commons;
50 JSClassDefinition JSCalendarEvent::m_classInfo = {
52 kJSClassAttributeNone,
53 TIZEN_INTERFACE_CALENDAR_EVENT,
54 JSCalendarItemProperties::getClassRef(),
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
70 JSStaticValue JSCalendarEvent::m_property[] = {
71 { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
72 NULL, kJSPropertyAttributeReadOnly },
73 { TIZEN_CALENDAR_EVENT_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
74 NULL, kJSPropertyAttributeReadOnly },
75 { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
76 NULL, kJSPropertyAttributeReadOnly },
81 JSStaticFunction JSCalendarEvent::m_function[] = {
82 { CALENDAR_FUNCTION_API_EXPAND_RECURRENCE, expandRecurrence, kJSPropertyAttributeNone },
87 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
89 ICalendarPtr JSCalendarEvent::m_calendar = CalendarFactory::getInstance().createCalendarObject();
91 void JSCalendarEvent::initialize(JSContextRef context,
94 if (!JSObjectGetPrivate(object)) {
95 LogDebug("Create calendar event private object.");
96 CalendarEventPtr event( new CalendarEvent() );
97 CalendarEventPrivObject *priv = new CalendarEventPrivObject(context, event);
98 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
102 LogDebug("Private object alrerady set.");
106 m_calendar->setType(CalendarEvent::EVENT_TYPE);
107 LogDebug("Calendar object type is set to event.");
111 void JSCalendarEvent::finalize(JSObjectRef object)
114 CalendarEventPrivObject* priv = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
117 JSObjectSetPrivate(object, NULL);
121 JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
122 JSObjectRef constructor,
123 size_t argumentCount,
124 const JSValueRef arguments[],
125 JSValueRef* exception)
131 CalendarEventPrivObject* privateObject = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(constructor));
132 JSContextRef globalContext = privateObject->getContext();
134 CalendarConverter converter(globalContext);
135 CalendarEventPtr event;
137 if (argumentCount==0) {
138 CalendarEventPtr result(new CalendarEvent());
140 } else if (argumentCount==1) {
141 LogInfo("eventInitDict case");
142 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
143 CalendarEventPtr result(new CalendarEvent());
145 } else if (JSValueIsObject(context, arguments[0])) {
146 event = converter.toEvent(arguments[0]);
148 ThrowMsg(ConversionException, "Parameter conversion failed.");
151 ThrowMsg(ConversionException, "Parameter conversion failed.");
153 } else if (argumentCount>=2) {
154 LogInfo("event stringRepresentation case");
155 std::string eventStr;
156 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
157 eventStr = converter.toString(arguments[0]);
158 format = converter.toVObjectFormat(converter.toString(arguments[1]));
160 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
161 dplEvent->setEventString(eventStr);
162 dplEvent->setFormat(format);
163 dplEvent->setForSynchronousCall();
164 m_calendar->createEventFromString(dplEvent);
166 // Process the returned object.
167 if (dplEvent->getResult()) {
168 LogInfo("Successfully created an event.");
169 event = dplEvent->getEvent();
171 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
172 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
174 ThrowMsg(UnknownException, "Converting string failed.");
179 return createJSCalendarEvent(globalContext, event);
181 Catch(UnsupportedException)
183 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
184 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
186 Catch(InvalidArgumentException)
188 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
189 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
191 Catch(ConversionException)
193 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
194 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
198 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
199 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
205 JSObjectRef JSCalendarEvent::createJSCalendarEvent(JSContextRef context, CalendarEventPtr event)
207 CalendarEventPrivObject *priv = new CalendarEventPrivObject(context, event);
208 return JSObjectMake(context, getClassRef(), priv);
211 const JSClassRef JSCalendarEvent::getClassRef()
214 m_jsClassRef = JSClassCreate(&m_classInfo);
219 const JSClassDefinition* JSCalendarEvent::getClassInfo()
224 CalendarEventPtr JSCalendarEvent::getPrivateObject(JSObjectRef object)
227 CalendarEventPrivObject *priv =
228 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
230 ThrowMsg(NullPointerException, "Private object is null");
232 CalendarEventPtr result = priv->getObject();
234 ThrowMsg(NullPointerException, "Private object is null");
239 void JSCalendarEvent::setPrivateObject(const CalendarEventPtr &event,
241 const JSObjectRef object)
246 CalendarEventPrivObject *priv =
247 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
249 priv = new CalendarEventPrivObject(ctx, event);
250 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
256 LogError("Error during replacing event object");
260 JSValueRef JSCalendarEvent::expandRecurrence(JSContextRef context,
262 JSObjectRef thisObject,
263 size_t argumentCount,
264 const JSValueRef arguments[],
265 JSValueRef* exception)
269 CalendarEventPrivObject *privateObject =
270 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(thisObject));
272 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_EXPAND_RECURRENCE);
274 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
278 if (!privateObject) {
279 ThrowMsg(ConversionException, "Object is null.");
282 CalendarEventPtr event = privateObject->getObject();
284 ThrowMsg(ConversionException, "Parameter conversion failed.");
287 JSContextRef globalContext = privateObject->getContext();
288 CalendarConverter converter(context);
290 std::time_t startDate = 0;
291 std::time_t endDate = INT_MAX; // about 60 years in 4 bytes system.
292 TimeUtilConverter timeConverter(context);
293 if (argumentCount>=1) {
294 if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())) {
295 startDate = (long long int) (timeConverter.getTimeInMilliseconds(arguments[0])/1000);
296 LogInfo("startDate: "<<startDate);
298 ThrowMsg(ConversionException, "Wrong first parameter type.");
301 if (argumentCount>=2) {
302 if (JSValueIsObjectOfClass(context, arguments[1], JSTZDate::getClassRef())) {
303 endDate = (long long int) (timeConverter.getTimeInMilliseconds(arguments[1])/1000);
304 LogInfo("endDate: "<<endDate);
306 ThrowMsg(ConversionException, "Wrong second parameter type.");
310 JSValueRef onError = argumentCount > 3 ? converter.toFunctionOrNull(arguments[3]) : NULL;
312 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
314 Validator validator(context);
315 if (validator.isCallback(arguments[2])) {
316 cbm->setOnSuccess(arguments[2]);
318 ThrowMsg(ConversionException, "Wrong third parameter type.");
321 cbm->setObject(thisObject);
323 LogDebug("Proceed the expand event to the platform.");
325 IEventExpandEventRecurrencePtr dplEvent(new IEventExpandEventRecurrence());
326 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
327 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
328 dplEvent->setEvent(event);
329 dplEvent->setStartDate(startDate);
330 dplEvent->setEndDate(endDate);
331 m_calendar->expandEventRecurrence(dplEvent);
333 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
335 return JSValueMakeUndefined(context);
337 Catch(UnsupportedException)
339 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
340 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
342 Catch(InvalidArgumentException)
344 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
345 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
347 Catch(ConversionException)
349 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
350 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
352 Catch (NotFoundException)
354 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
355 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
359 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
360 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
363 return JSValueMakeUndefined(context);
366 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
368 JSStringRef propertyName,
369 JSValueRef* exception)
374 CalendarEventPrivObject *privateObject =
375 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
376 CalendarEventPtr event = privateObject->getObject();
378 EventIdPtr eventId( new EventId() );
379 if (0<event->getParentId()) {
380 std::stringstream ss;
381 ss<<event->getParentId();
382 eventId->setUId(ss.str());
384 if (UNDEFINED_ITEM_ID==event->getId()) {
385 return JSValueMakeNull(context);
387 eventId->setUId(event->getUId());
390 std::stringstream ss;
391 std::time_t rid = event->getRecurrenceId();
393 eventId->setRecurrenceId(ss.str());
394 return JSCalendarEventId::createJSCalendarEventId(context, eventId);
398 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
400 return JSValueMakeUndefined(context);
403 JSValueRef JSCalendarEvent::getPropertyLastModificationDate(JSContextRef context,
405 JSStringRef propertyName,
406 JSValueRef* exception)
411 CalendarEventPrivObject *privateObject =
412 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
413 CalendarEventPtr event = privateObject->getObject();
415 ThrowMsg(NullPointerException, "Event object is NULL.");
418 if (UNDEFINED_TIME==event->getLastModifiedDate()) {
419 return JSValueMakeNull(context);
421 TimeUtilConverter timeConverter(context);
422 return timeConverter.toJSValueRefTZDate((double)(event->getLastModifiedDate()*1000.0), event->getTimeZone());
427 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
429 return JSValueMakeUndefined(context);
432 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
434 JSStringRef propertyName,
435 JSValueRef* exception)
440 CalendarEventPtr event = getPrivateObject(object);
441 if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
442 return JSValueMakeUndefined(context);
445 Converter converter(context);
446 return converter.toJSValueRef(event->getIsDetached());
450 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
452 return JSValueMakeUndefined(context);
455 bool JSCalendarEvent::validate(JSContextRef ctx,
456 const JSObjectRef object,
457 JSValueRef* exception)
460 CalendarEventPrivObject *priv =
461 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
465 CalendarEventPtr event = priv->getObject();
469 return event->validate();