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 LogDebug("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 LogDebug("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 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
161 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
163 Catch(InvalidArgumentException)
165 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
166 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
168 Catch(ConversionException)
170 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
171 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
175 LogWarning("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 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
219 LogError("Default calendar not found.");
222 ThrowMsg(UnknownException, "Getting a default calendar failed by unknown reason.");
225 Catch(UnsupportedException)
227 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
228 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
230 Catch(InvalidArgumentException)
232 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
233 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
235 Catch(ConversionException)
237 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
238 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
240 Catch (NotFoundException)
242 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
243 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
247 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
248 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
251 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
252 return JSValueMakeUndefined(context);
255 JSValueRef JSCalendarManager::getUnifiedCalendar(JSContextRef context,
257 JSObjectRef thisObject,
258 size_t argumentCount,
259 const JSValueRef arguments[],
260 JSValueRef* exception)
262 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
263 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
265 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR);
267 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
270 if (!privateObject) {
271 ThrowMsg(ConversionException, "Object is null.");
274 // Global context should be passed to the calendar object.
275 JSContextRef globalContext = privateObject->getContext();
277 CalendarConverter converter(globalContext);
278 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
279 if (argumentCount>=1) {
280 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
282 ThrowMsg(ConversionException, "Wrong parameter type.");
285 IEventGetUnifiedCalendarPtr dplEvent(new IEventGetUnifiedCalendar());
286 dplEvent->setForSynchronousCall();
287 dplEvent->setType(calendarType);
288 privateObject->getObject()->getUnifiedCalendar(dplEvent);
290 if (dplEvent->getResult()) {
291 if( dplEvent->getCalendar() ) {
292 LogDebug("Successfully got a unified calendar.");
293 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
295 LogError("Unified calendar not found.");
298 ThrowMsg(UnknownException, "Getting a unified calendar failed by unknown reason.");
301 Catch(UnsupportedException)
303 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
304 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
306 Catch(InvalidArgumentException)
308 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
309 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
311 Catch(ConversionException)
313 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
314 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
316 Catch (NotFoundException)
318 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
319 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
323 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
324 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
327 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
328 return JSValueMakeUndefined(context);
331 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
333 JSObjectRef thisObject,
334 size_t argumentCount,
335 const JSValueRef arguments[],
336 JSValueRef* exception)
338 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
339 CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
341 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDAR);
343 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
346 if (!privateObject) {
347 ThrowMsg(ConversionException, "Object is null.");
350 // Global context should be passed to the calendar object.
351 JSContextRef globalContext = privateObject->getContext();
353 CalendarConverter converter(globalContext);
355 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
356 if (argumentCount>=1) {
357 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
359 ThrowMsg(ConversionException, "Wrong parameter count.");
362 std::string calendarId;
363 if (argumentCount>=2) {
364 calendarId = converter.toString(arguments[1]);
367 IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
368 dplEvent->setForSynchronousCall();
369 dplEvent->setId(calendarId);
370 dplEvent->setType(calendarType);
371 privateObject->getObject()->getCalendar(dplEvent);
373 // Process the result.
374 if (dplEvent->getResult()) {
375 if( dplEvent->getCalendar() ) {
376 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
377 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
379 ThrowMsg(NotFoundException, "Calendar not found.");
382 ThrowMsg(UnknownException, "Getting a calendar failed by unknown reason.");
385 Catch(UnsupportedException)
387 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
388 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
390 Catch(InvalidArgumentException)
392 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
393 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
395 Catch(ConversionException)
397 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
398 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
400 Catch (NotFoundException)
402 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
403 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
407 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
408 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
412 const JSClassRef JSCalendarManager::getClassRef()
415 m_jsClassRef = JSClassCreate(&m_classInfo);
420 const JSClassDefinition* JSCalendarManager::getClassInfo()