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 <API/Calendar/ICalendar.h>
20 #include <API/Calendar/ICalendarManager.h>
21 #include <API/Calendar/CalendarFactory.h>
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/Validator.h>
26 #include <Tizen/Common/JSTizenException.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/SecurityExceptions.h>
30 #include "JSCalendarManager.h"
31 #include "JSCalendar.h"
32 #include "CalendarConverter.h"
33 #include "CalendarResponseDispatcher.h"
34 #include "plugin_config.h"
36 #include "CalendarAsyncCallbackManager.h"
38 using namespace TizenApis::Api::Calendar;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41 using namespace TizenApis::Commons;
43 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
49 JSClassDefinition JSCalendarManager::m_classInfo = {
51 kJSClassAttributeNone,
52 TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
61 NULL, //DeleteProperty,
62 NULL, //GetPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
69 JSStaticFunction JSCalendarManager::m_function[] = {
70 { CALENDAR_FUNCTION_API_GET_CALENDARS,
71 getCalendars, kJSPropertyAttributeNone },
73 { CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR,
74 getDefaultCalendar, kJSPropertyAttributeNone },
76 { CALENDAR_FUNCTION_API_GET_CALENDAR,
77 getCalendar, kJSPropertyAttributeNone },
82 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
83 JSCalendarManager::getClassInfo());
85 void JSCalendarManager::initialize(JSContextRef context,
88 if (!JSObjectGetPrivate(object)) {
89 LogDebug("Create calendar manager private object.");
90 ICalendarManagerPtr calendarManager = CalendarFactory::getInstance().createCalendarManagerObject();
91 CalendarManagerPrivObject *privateObject = new CalendarManagerPrivObject(context, calendarManager);
92 if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
96 LogDebug("Private object alrerady set.");
100 void JSCalendarManager::finalize(JSObjectRef object)
103 CalendarManagerPrivObject *priv = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
106 JSObjectSetPrivate(object, NULL);
110 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
112 JSObjectRef thisObject,
113 size_t argumentCount,
114 const JSValueRef arguments[],
115 JSValueRef* exception)
119 CalendarManagerPrivObject *privateObject =
120 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
122 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDARS);
124 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
127 if (!privateObject) {
128 ThrowMsg(ConversionException, "Object is null.");
131 CalendarConverter converter(context);
133 JSContextRef globalContext = privateObject->getContext();
135 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
137 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
138 if (argumentCount>=1) {
139 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
142 if (argumentCount>=2) {
143 cbm->setOnSuccess(converter.toFunction(arguments[1]));
145 ThrowMsg(ConversionException, "Wrong parameter count.");
148 if (argumentCount>=3) {
149 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
152 cbm->setObject(thisObject);
154 IEventGetCalendarsPtr dplEvent(new IEventGetCalendars());
155 dplEvent->setType(calendarType);
156 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
157 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
158 privateObject->getObject()->getCalendars(dplEvent);
160 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
162 return JSValueMakeUndefined(context);
164 Catch(UnsupportedException)
166 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
167 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
169 Catch(InvalidArgumentException)
171 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
172 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
174 Catch(ConversionException)
176 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
177 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
181 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
182 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
185 return JSValueMakeUndefined(context);
188 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
190 JSObjectRef thisObject,
191 size_t argumentCount,
192 const JSValueRef arguments[],
193 JSValueRef* exception)
195 CalendarManagerPrivObject *privateObject =
196 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
198 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
200 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
203 if (!privateObject) {
204 ThrowMsg(ConversionException, "Object is null.");
207 JSContextRef globalContext = privateObject->getContext();
209 CalendarConverter converter(globalContext);
210 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
211 if (argumentCount>=1) {
212 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
214 ThrowMsg(ConversionException, "Wrong parameter type.");
217 IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
218 dplEvent->setForSynchronousCall();
219 dplEvent->setType(calendarType);
220 privateObject->getObject()->getDefaultCalendar(dplEvent);
222 if (dplEvent->getResult()) {
223 if( dplEvent->getCalendar() ) {
224 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
226 LogError("Default calendar not found.");
229 ThrowMsg(UnknownException, "Getting default calendar failed by unknown reason.");
232 Catch(UnsupportedException)
234 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
235 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
237 Catch(InvalidArgumentException)
239 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
240 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
242 Catch(ConversionException)
244 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
245 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
247 Catch (NotFoundException)
249 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
250 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
254 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
255 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
258 return JSValueMakeUndefined(context);
261 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
263 JSObjectRef thisObject,
264 size_t argumentCount,
265 const JSValueRef arguments[],
266 JSValueRef* exception)
268 CalendarManagerPrivObject *privateObject =
269 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
271 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDAR);
273 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
276 if (!privateObject) {
277 ThrowMsg(ConversionException, "Object is null.");
280 JSContextRef globalContext = privateObject->getContext();
282 CalendarConverter converter(globalContext);
284 CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
285 if (argumentCount>=1) {
286 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
288 ThrowMsg(ConversionException, "Wrong parameter count.");
291 std::string calendarId;
292 if (argumentCount>=2) {
293 calendarId = converter.toString(arguments[1]);
296 IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
297 dplEvent->setForSynchronousCall();
298 dplEvent->setId(calendarId);
299 dplEvent->setType(calendarType);
300 privateObject->getObject()->getCalendar(dplEvent);
302 // Process the result.
303 if (dplEvent->getResult()) {
304 if( dplEvent->getCalendar() ) {
305 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
307 ThrowMsg(NotFoundException, "Calendar not found.");
310 ThrowMsg(UnknownException, "Getting calendar failed by unknown reason.");
313 Catch(UnsupportedException)
315 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
316 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
318 Catch(InvalidArgumentException)
320 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
321 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
323 Catch(ConversionException)
325 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
326 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
328 Catch (NotFoundException)
330 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
331 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
335 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
336 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
339 return JSValueMakeUndefined(context);
342 const JSClassRef JSCalendarManager::getClassRef()
345 m_jsClassRef = JSClassCreate(&m_classInfo);
350 const JSClassDefinition* JSCalendarManager::getClassInfo()