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 <JSTizenException.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <SecurityExceptions.h>
30 #include <TimeTracer.h>
32 #include "JSCalendarManager.h"
33 #include "JSCalendar.h"
34 #include "CalendarConverter.h"
35 #include "CalendarResponseDispatcher.h"
36 #include "plugin_config.h"
38 #include "CalendarAsyncCallbackManager.h"
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace DeviceAPI::Common;
45 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
50 JSClassDefinition JSCalendarManager::m_classInfo = {
52 kJSClassAttributeNone,
53 TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 NULL, //CallAsConstructor,
70 JSStaticFunction JSCalendarManager::m_function[] = {
71 { CALENDAR_FUNCTION_API_GET_CALENDARS, getCalendars, kJSPropertyAttributeNone },
72 { CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR, getDefaultCalendar, kJSPropertyAttributeNone },
73 { CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR, getUnifiedCalendar, kJSPropertyAttributeNone },
74 { CALENDAR_FUNCTION_API_GET_CALENDAR, getCalendar, kJSPropertyAttributeNone },
79 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
80 JSCalendarManager::getClassInfo());
82 void JSCalendarManager::initialize(JSContextRef context,
85 if (!JSObjectGetPrivate(object)) {
86 LoggerD("Create calendar manager private object.");
87 ICalendarManagerPtr calendarManager = CalendarFactory::getInstance().createCalendarManagerObject();
88 CalendarManagerPrivObject *privateObject = new CalendarManagerPrivObject(context, calendarManager);
89 if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
93 LoggerD("Private object already set.");
97 void JSCalendarManager::finalize(JSObjectRef object)
99 CalendarManagerPrivObject *priv = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
102 JSObjectSetPrivate(object, NULL);
106 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
108 JSObjectRef thisObject,
109 size_t argumentCount,
110 const JSValueRef arguments[],
111 JSValueRef* exception)
113 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
114 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
116 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDARS);
118 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
121 if (!privateObject) {
122 ThrowMsg(ConversionException, "Object is null.");
125 CalendarConverter converter(context);
127 JSContextRef globalContext = privateObject->getContext();
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 privateObject->getObject()->getCalendars(dplEvent);
154 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
155 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
156 return JSValueMakeUndefined(context);
158 Catch(UnsupportedException)
160 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
161 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
163 Catch(InvalidArgumentException)
165 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
166 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
168 Catch(ConversionException)
170 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
175 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::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 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
192 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
195 if (!privateObject) {
196 ThrowMsg(ConversionException, "Object is null.");
199 // Global context should be passed to the calendar object.
200 JSContextRef globalContext = privateObject->getContext();
202 CalendarConverter converter(globalContext);
203 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
204 if (argumentCount>=1) {
205 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
207 ThrowMsg(ConversionException, "Wrong parameter type.");
210 IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
211 dplEvent->setForSynchronousCall();
212 dplEvent->setType(calendarType);
213 privateObject->getObject()->getDefaultCalendar(dplEvent);
215 if (dplEvent->getResult()) {
216 if( dplEvent->getCalendar() ) {
217 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
218 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
220 LoggerE("Default calendar not found.");
223 ThrowMsg(UnknownException, "Getting a default calendar failed by unknown reason.");
226 Catch(UnsupportedException)
228 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
229 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
231 Catch(InvalidArgumentException)
233 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
234 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
236 Catch(ConversionException)
238 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
239 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
241 Catch (NotFoundException)
243 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
244 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
248 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
249 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
252 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
253 return JSValueMakeUndefined(context);
256 JSValueRef JSCalendarManager::getUnifiedCalendar(JSContextRef context,
258 JSObjectRef thisObject,
259 size_t argumentCount,
260 const JSValueRef arguments[],
261 JSValueRef* exception)
263 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
264 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
266 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR);
268 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
271 if (!privateObject) {
272 ThrowMsg(ConversionException, "Object is null.");
275 // Global context should be passed to the calendar object.
276 JSContextRef globalContext = privateObject->getContext();
278 CalendarConverter converter(globalContext);
279 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
280 if (argumentCount>=1) {
281 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
283 ThrowMsg(ConversionException, "Wrong parameter type.");
286 IEventGetUnifiedCalendarPtr dplEvent(new IEventGetUnifiedCalendar());
287 dplEvent->setForSynchronousCall();
288 dplEvent->setType(calendarType);
289 privateObject->getObject()->getUnifiedCalendar(dplEvent);
291 if (dplEvent->getResult()) {
292 if( dplEvent->getCalendar() ) {
293 LoggerD("Successfully got a unified calendar.");
294 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
296 LoggerE("Unified calendar not found.");
299 ThrowMsg(UnknownException, "Getting a unified calendar failed by unknown reason.");
302 Catch(UnsupportedException)
304 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
305 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
307 Catch(InvalidArgumentException)
309 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
310 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
312 Catch(ConversionException)
314 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
315 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
317 Catch (NotFoundException)
319 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
320 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
324 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
325 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
328 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
329 return JSValueMakeUndefined(context);
332 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
334 JSObjectRef thisObject,
335 size_t argumentCount,
336 const JSValueRef arguments[],
337 JSValueRef* exception)
339 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
340 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
342 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDAR);
344 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
347 if (!privateObject) {
348 ThrowMsg(ConversionException, "Object is null.");
351 // Global context should be passed to the calendar object.
352 JSContextRef globalContext = privateObject->getContext();
354 CalendarConverter converter(globalContext);
356 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
357 if (argumentCount>=1) {
358 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
360 ThrowMsg(ConversionException, "Wrong parameter count.");
363 std::string calendarId;
364 if (argumentCount>=2) {
365 calendarId = converter.toString(arguments[1]);
368 IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
369 dplEvent->setForSynchronousCall();
370 dplEvent->setId(calendarId);
371 dplEvent->setType(calendarType);
372 privateObject->getObject()->getCalendar(dplEvent);
374 // Process the result.
375 if (dplEvent->getResult()) {
376 if( dplEvent->getCalendar() ) {
377 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
378 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
380 ThrowMsg(NotFoundException, "Calendar not found.");
383 ThrowMsg(UnknownException, "Getting a calendar failed by unknown reason.");
386 Catch(UnsupportedException)
388 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
389 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
391 Catch(InvalidArgumentException)
393 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
394 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
396 Catch(ConversionException)
398 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
399 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
401 Catch (NotFoundException)
403 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
404 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
408 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
409 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
413 const JSClassRef JSCalendarManager::getClassRef()
416 m_jsClassRef = JSClassCreate(&m_classInfo);
421 const JSClassDefinition* JSCalendarManager::getClassInfo()