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/Validator.h>
24 #include <CommonsJavaScript/JSCallbackManager.h>
25 #include <API/Calendar/EventId.h>
26 #include <API/Calendar/ICalendar.h>
27 #include <API/Calendar/CalendarFactory.h>
28 #include "JSCalendarEvent.h"
29 #include "JSCalendarItemProperties.h"
30 #include "CalendarConverter.h"
31 #include "JSCalendarEventId.h"
32 #include "plugin_config.h"
33 #include "CalendarResponseDispatcher.h"
34 #include <Tizen/TimeUtil/TimeUtilConverter.h>
35 #include <Tizen/TimeUtil/JSTZDate.h>
36 #include <Tizen/Common/JSTizenException.h>
37 #include <Tizen/Common/JSTizenExceptionFactory.h>
38 #include <Tizen/Common/SecurityExceptions.h>
40 using namespace TizenApis::Api::Calendar;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace TizenApis::Commons;
47 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
49 JSValueRef getFunctionOrNull(JSContextRef ctx,
52 if (Validator(ctx).isCallback(arg)) {
54 } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
55 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
65 JSClassDefinition JSCalendarEvent::m_classInfo = {
67 kJSClassAttributeNone,
68 TIZEN_INTERFACE_CALENDAR_EVENT,
69 JSCalendarItemProperties::getClassRef(),
77 NULL, //DeleteProperty,
78 NULL, //GetPropertyNames,
79 NULL, //CallAsFunction,
85 JSStaticValue JSCalendarEvent::m_property[] = {
86 { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
87 NULL, kJSPropertyAttributeReadOnly },
88 { TIZEN_CALENDAR_EVENT_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
89 NULL, kJSPropertyAttributeReadOnly },
90 { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
91 NULL, kJSPropertyAttributeReadOnly },
96 JSStaticFunction JSCalendarEvent::m_function[] = {
97 { CALENDAR_FUNCTION_API_EXPAND_RECURRENCE, expandRecurrence, kJSPropertyAttributeNone },
102 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
104 void JSCalendarEvent::initialize(JSContextRef context,
108 CalendarEventPrivObject *priv =
109 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
111 CalendarEventPtr privateData(new CalendarEvent());
112 priv = new CalendarEventPrivObject(context, privateData);
113 JSObjectSetPrivate(object, static_cast<void*>(priv));
114 LogDebug("new event is created");
116 LogDebug("private object already exists");
120 void JSCalendarEvent::finalize(JSObjectRef object)
125 JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
126 JSObjectRef constructor,
127 size_t argumentCount,
128 const JSValueRef arguments[],
129 JSValueRef* exception)
135 CalendarEventPrivObject* privateObject = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(constructor));
136 JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
138 CalendarConverter converter(globalContext);
139 CalendarEventPtr event;
141 if (argumentCount==0) {
142 CalendarEventPtr result(new CalendarEvent());
144 } else if (argumentCount==1) { // eventInitDict case
145 event = converter.toEvent(arguments[0]);
147 ThrowMsg(ConversionException, "Parameter conversion failed.");
149 } else if (argumentCount==2) { // stringRepresentation case
150 if (!JSValueIsString(context, arguments[0]) || !JSValueIsString(context, arguments[1])) {
151 ThrowMsg(ConversionException, "Wrong parameter type.");
154 std::string eventStr;
155 CalendarEvent::VObjectFormat format = CalendarEvent::ICALENDAR_20;
156 eventStr = converter.toString(arguments[0]);
157 format = converter.toVObjectFormat(converter.toString(arguments[1]));
159 ICalendarPtr calendar = CalendarFactory::getInstance().createCalendarObject();
160 calendar->setType(CalendarEvent::EVENT_TYPE);
162 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
163 dplEvent->setEventString(eventStr);
164 dplEvent->setFormat(format);
165 dplEvent->setForSynchronousCall();
166 calendar->createEventFromString(dplEvent);
168 // Process the returned object.
169 if (dplEvent->getResult()) {
170 LogInfo("Successfully created an item.");
171 event = dplEvent->getEvent();
173 ThrowMsg(UnknownException, "Converting from string failed by unkown reason.");
176 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
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)
268 CalendarEventPrivObject *privateObject =
269 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(thisObject));
270 assert(privateObject);
272 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_EXPAND_RECURRENCE);
274 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
276 JSCallbackManagerPtr cbm(NULL);
280 if (argumentCount<3 || argumentCount>4) {
281 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
284 CalendarEventPtr event = privateObject->getObject();
286 ThrowMsg(ConversionException, "Parameter conversion failed.");
289 JSValueRef onError =(argumentCount > 3 ? getFunctionOrNull(context, arguments[3]) : NULL);
290 JSContextRef globalContext = privateObject->getContext();
291 cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
293 Validator validator(context);
294 if (validator.isCallback(arguments[2])) {
295 cbm->setOnSuccess(arguments[2]);
296 } else if (JSValueIsNull(context, arguments[2]) || JSValueIsUndefined(context, arguments[2])) {
297 ThrowMsg(ConversionException, "Wrong parameter type.");
299 ThrowMsg(ConversionException, "Wrong parameter type.");
302 std::time_t startDate = 0;
303 std::time_t endDate = INT_MAX; // about 60 years in 4 bytes system.
304 Converter converter(context);
305 TimeUtilConverter timeConverter(context);
306 if (argumentCount>0 ) {
307 startDate = timeConverter.toTZDateTimeT(arguments[0]);
309 if (argumentCount>1 ) {
310 endDate = timeConverter.toDateTimeT(arguments[1]);
313 ICalendarPtr calendar = CalendarFactory::getInstance().createCalendarObject();
314 calendar->setType(event->getCalendarType());
316 IEventExpandEventRecurrencePtr dplEvent(new IEventExpandEventRecurrence());
317 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
318 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
319 dplEvent->setEvent(event);
320 dplEvent->setStartDate(startDate);
321 dplEvent->setEndDate(endDate);
322 calendar->expandEventRecurrence(dplEvent);
324 return JSValueMakeNull(context);
326 Catch(UnsupportedException)
328 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
329 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
331 Catch(InvalidArgumentException)
333 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
334 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
336 Catch(ConversionException)
338 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
339 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
341 Catch (NotFoundException)
343 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
344 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
348 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
349 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
352 return JSValueMakeNull(context);
355 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
357 JSStringRef propertyName,
358 JSValueRef* exception)
363 CalendarEventPrivObject *privateObject =
364 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
365 CalendarEventPtr event = privateObject->getObject();
367 EventIdPtr eventId( new EventId() );
368 eventId->setUId(event->getUId());
369 eventId->setRecurrenceId(event->getRecurrenceId());
370 eventId->setTimeZone(event->getTimeZone());
371 return JSCalendarEventId::createJSCalendarEventId(privateObject->getContext(), eventId);
375 LogWarning("trying to get incorrect value");
377 return JSValueMakeUndefined(context);
380 JSValueRef JSCalendarEvent::getPropertyLastModificationDate(JSContextRef context,
382 JSStringRef propertyName,
383 JSValueRef* exception)
388 CalendarEventPrivObject *privateObject =
389 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
390 CalendarEventPtr event = privateObject->getObject();
393 Throw(NullPointerException);
395 if (event->getLastModifiedDate() != 0) {
396 // Use the global context saved in the event struct.
397 return JSTZDate::createJSObject(privateObject->getContext(), event->getLastModifiedDate(), event->getTimeZone());
399 return JSValueMakeUndefined(context);
404 LogWarning("trying to get incorrect value");
406 return JSValueMakeUndefined(context);
409 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
411 JSStringRef propertyName,
412 JSValueRef* exception)
417 CalendarEventPtr event = getPrivateObject(object);
418 if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
419 return JSValueMakeUndefined(context);
422 Converter converter(context);
423 return converter.toJSValueRef(event->getIsDetached());
427 LogWarning("trying to get incorrect value");
429 return JSValueMakeUndefined(context);
432 bool JSCalendarEvent::validate(JSContextRef ctx,
433 const JSObjectRef object,
434 JSValueRef* exception)
437 CalendarEventPrivObject *priv =
438 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
442 CalendarEventPtr event = priv->getObject();
446 return event->validate();