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.
20 #include "ICalendar.h"
21 #include "ICalendarManager.h"
22 #include "CalendarFactory.h"
24 #include <CommonsJavaScript/PrivateObject.h>
25 #include <CommonsJavaScript/Utils.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include <SecurityExceptions.h>
29 #include <TimeTracer.h>
32 #include <GlobalContextManager.h>
34 #include "JSCalendarManager.h"
35 #include "JSCalendar.h"
36 #include "CalendarConverter.h"
37 #include "CalendarResponseDispatcher.h"
38 #include "plugin_config.h"
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace DeviceAPI::Common;
44 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
49 using WrtDeviceApis::Commons::UnknownException;
50 using WrtDeviceApis::Commons::NotFoundException;
52 JSClassDefinition JSCalendarManager::m_classInfo = {
54 kJSClassAttributeNone,
55 TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
64 NULL, //DeleteProperty,
65 NULL, //GetPropertyNames,
66 NULL, //CallAsFunction,
67 NULL, //CallAsConstructor,
72 JSStaticFunction JSCalendarManager::m_function[] = {
73 { CALENDAR_FUNCTION_API_GET_CALENDARS, getCalendars, kJSPropertyAttributeNone },
74 { CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR, getDefaultCalendar, kJSPropertyAttributeNone },
75 { CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR, getUnifiedCalendar, kJSPropertyAttributeNone },
76 { CALENDAR_FUNCTION_API_GET_CALENDAR, getCalendar, kJSPropertyAttributeNone },
81 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
82 JSCalendarManager::getClassInfo());
84 void JSCalendarManager::initialize(JSContextRef context,
87 if (!JSObjectGetPrivate(object)) {
88 LoggerD("Create calendar manager private object.");
89 ICalendarManagerPtr calendarManager = CalendarFactory::getInstance().createCalendarManagerObject();
90 CalendarManagerPrivObject *privateObject = new CalendarManagerPrivObject(context, calendarManager);
91 if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
95 LoggerD("Private object already set.");
99 void JSCalendarManager::finalize(JSObjectRef object)
101 CalendarManagerPrivObject *priv = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
104 JSObjectSetPrivate(object, NULL);
108 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
110 JSObjectRef thisObject,
111 size_t argumentCount,
112 const JSValueRef arguments[],
113 JSValueRef* exception)
115 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
116 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
118 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_CALENDARS);
121 if (!privateObject) {
122 ThrowMsg(ConversionException, "Object is null.");
125 CalendarConverter converter(context);
127 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
129 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
131 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
132 if (argumentCount>=1) {
133 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
136 if (argumentCount>=2) {
137 cbm->setOnSuccess(converter.toFunction(arguments[1]));
139 ThrowMsg(ConversionException, "Wrong parameter count.");
142 if (argumentCount>=3) {
143 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
146 cbm->setObject(thisObject);
148 IEventGetCalendarsPtr dplEvent(new IEventGetCalendars());
149 dplEvent->setType(calendarType);
150 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
151 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
152 dplEvent->copyAceCheckAccessFunction(privateObject);
153 privateObject->getObject()->getCalendars(dplEvent);
155 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
156 return JSValueMakeUndefined(context);
158 Catch(UnsupportedException)
160 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
161 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
163 Catch(InvalidArgumentException)
165 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
166 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
168 Catch(ConversionException)
170 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
175 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
180 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
182 JSObjectRef thisObject,
183 size_t argumentCount,
184 const JSValueRef arguments[],
185 JSValueRef* exception)
187 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
188 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
190 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
193 if (!privateObject) {
194 ThrowMsg(ConversionException, "Object is null.");
197 // Global context should be passed to the calendar object.
198 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
200 CalendarConverter converter(globalContext, privateObject);
201 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
202 if (argumentCount>=1) {
203 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
205 ThrowMsg(ConversionException, "Wrong parameter type.");
208 IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
209 dplEvent->setForSynchronousCall();
210 dplEvent->setType(calendarType);
211 privateObject->getObject()->getDefaultCalendar(dplEvent);
213 if (dplEvent->getResult()) {
214 if( dplEvent->getCalendar() ) {
215 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
216 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
218 LoggerE("Default calendar not found.");
221 ThrowMsg(UnknownException, "Getting a default calendar failed by unknown reason.");
224 Catch(UnsupportedException)
226 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
227 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
229 Catch(InvalidArgumentException)
231 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
232 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
234 Catch(ConversionException)
236 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
237 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
239 Catch (NotFoundException)
241 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
242 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
246 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
247 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
250 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
251 return JSValueMakeUndefined(context);
254 JSValueRef JSCalendarManager::getUnifiedCalendar(JSContextRef context,
256 JSObjectRef thisObject,
257 size_t argumentCount,
258 const JSValueRef arguments[],
259 JSValueRef* exception)
261 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
262 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
264 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR);
267 if (!privateObject) {
268 ThrowMsg(ConversionException, "Object is null.");
271 // Global context should be passed to the calendar object.
272 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
274 CalendarConverter converter(globalContext, privateObject);
275 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
276 if (argumentCount>=1) {
277 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
279 ThrowMsg(ConversionException, "Wrong parameter type.");
282 IEventGetUnifiedCalendarPtr dplEvent(new IEventGetUnifiedCalendar());
283 dplEvent->setForSynchronousCall();
284 dplEvent->setType(calendarType);
285 privateObject->getObject()->getUnifiedCalendar(dplEvent);
287 if (dplEvent->getResult()) {
288 if( dplEvent->getCalendar() ) {
289 LoggerD("Successfully got a unified calendar.");
290 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
292 LoggerE("Unified calendar not found.");
295 ThrowMsg(UnknownException, "Getting a unified calendar failed by unknown reason.");
298 Catch(UnsupportedException)
300 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
301 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
303 Catch(InvalidArgumentException)
305 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
306 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
308 Catch(ConversionException)
310 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
311 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
313 Catch (NotFoundException)
315 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
316 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
320 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
321 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
324 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
325 return JSValueMakeUndefined(context);
328 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
330 JSObjectRef thisObject,
331 size_t argumentCount,
332 const JSValueRef arguments[],
333 JSValueRef* exception)
335 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
336 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
338 TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_CALENDAR);
341 if (!privateObject) {
342 ThrowMsg(ConversionException, "Object is null.");
345 // Global context should be passed to the calendar object.
346 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
348 CalendarConverter converter(globalContext, privateObject);
350 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
351 if (argumentCount>=1) {
352 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
354 ThrowMsg(ConversionException, "Wrong parameter count.");
357 std::string calendarId;
358 if (argumentCount>=2) {
359 calendarId = converter.toString(arguments[1]);
362 IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
363 dplEvent->setForSynchronousCall();
364 dplEvent->setId(calendarId);
365 dplEvent->setType(calendarType);
366 privateObject->getObject()->getCalendar(dplEvent);
368 // Process the result.
369 if (dplEvent->getResult()) {
370 if( dplEvent->getCalendar() ) {
371 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
372 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
374 ThrowMsg(NotFoundException, "Calendar not found.");
377 ThrowMsg(UnknownException, "Getting a calendar failed by unknown reason.");
380 Catch(UnsupportedException)
382 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
383 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
385 Catch(InvalidArgumentException)
387 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
388 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
390 Catch(ConversionException)
392 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
393 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
395 Catch (NotFoundException)
397 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
398 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
402 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
403 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
407 const JSClassRef DLL_EXPORT JSCalendarManager::getClassRef()
410 m_jsClassRef = JSClassCreate(&m_classInfo);
415 const JSClassDefinition* JSCalendarManager::getClassInfo()