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 "JSCalendarItem.h"
31 #include "JSCalendarManager.h"
32 #include "JSCalendar.h"
33 #include "CalendarConverter.h"
34 #include "CalendarResponseDispatcher.h"
35 #include "plugin_config.h"
37 using namespace TizenApis::Api::Calendar;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace TizenApis::Commons;
42 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
48 JSClassDefinition JSCalendarManager::m_classInfo = {
50 kJSClassAttributeNone,
51 TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
60 NULL, //DeleteProperty,
61 NULL, //GetPropertyNames,
62 NULL, //CallAsFunction,
63 NULL, //CallAsConstructor,
68 JSStaticFunction JSCalendarManager::m_function[] = {
69 { CALENDAR_FUNCTION_API_GET_CALENDARS,
70 getCalendars, kJSPropertyAttributeNone },
72 { CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR,
73 getDefaultCalendar, kJSPropertyAttributeNone },
75 { CALENDAR_FUNCTION_API_GET_CALENDAR,
76 getCalendar, kJSPropertyAttributeNone },
81 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
82 JSCalendarManager::getClassInfo());
84 void JSCalendarManager::initialize(JSContextRef context,
88 CalendarManagerPrivObject *privateObject =
89 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
90 if (NULL == privateObject) {
91 ICalendarManagerPtr calendarManager =
92 Api::Calendar::CalendarFactory::getInstance().createCalendarManagerObject();
93 privateObject = new CalendarManagerPrivObject(context, calendarManager);
94 if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
100 void JSCalendarManager::finalize(JSObjectRef object)
103 CalendarManagerPrivObject *privateObject =
104 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
105 delete privateObject;
108 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
110 JSObjectRef thisObject,
111 size_t argumentCount,
112 const JSValueRef arguments[],
113 JSValueRef* exception)
115 CalendarManagerPrivObject *privateObject =
116 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
117 if (NULL == privateObject) {
118 LogError("private object is null");
119 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR);
122 JSContextRef globalContext = privateObject->getContext();
124 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_CALENDARS);
126 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
128 CalendarConverter converter(context);
131 if (argumentCount<2 || argumentCount>3) {
132 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
135 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
137 if (argumentCount > 2) {
138 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
140 cbm->setOnSuccess(converter.toFunction(arguments[1]));
142 CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
143 if (!JSValueIsString(context, arguments[0])) {
144 ThrowMsg(ConversionException, "Wrong parameter type.");
146 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
149 IEventGetCalendarsPtr dplEvent(new IEventGetCalendars());
150 dplEvent->setType(calendarType);
151 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
152 dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
153 privateObject->getObject()->getCalendars(dplEvent);
156 Catch(UnsupportedException)
158 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
159 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
161 Catch(InvalidArgumentException)
163 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
164 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
166 Catch(ConversionException)
168 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
169 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
173 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
174 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
177 return JSValueMakeNull(context);
180 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
182 JSObjectRef thisObject,
183 size_t argumentCount,
184 const JSValueRef arguments[],
185 JSValueRef* exception)
187 CalendarManagerPrivObject *privateObject =
188 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
189 if (NULL == privateObject) {
190 LogError("private object is null");
191 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR);
194 JSContextRef globalContext = privateObject->getContext();
196 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
198 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
201 if (argumentCount!=1) {
202 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
205 CalendarConverter converter(globalContext); // Needs global context for subsequent object creation. E.g., TZDate.
206 CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
207 if (!JSValueIsString(context, arguments[0])) {
208 ThrowMsg(ConversionException, "Wrong parameter type.");
210 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
213 IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
214 dplEvent->setForSynchronousCall();
215 dplEvent->setType(calendarType);
216 privateObject->getObject()->getDefaultCalendar(dplEvent);
218 if (dplEvent->getResult()) {
219 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
221 ThrowMsg(UnknownException, "Getting default calendar failed by unkown reason.");
224 Catch(UnsupportedException)
226 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
227 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
229 Catch(InvalidArgumentException)
231 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
232 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
234 Catch(ConversionException)
236 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
237 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
239 Catch (NotFoundException)
241 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
242 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
246 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
247 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
250 return JSValueMakeNull(context);
253 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
255 JSObjectRef thisObject,
256 size_t argumentCount,
257 const JSValueRef arguments[],
258 JSValueRef* exception)
260 CalendarManagerPrivObject *privateObject =
261 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
262 if (NULL == privateObject) {
263 LogError("private object is null");
264 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR);
267 JSContextRef globalContext = privateObject->getContext();
269 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_CALENDAR);
271 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
274 if (argumentCount!=2) {
275 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
278 CalendarConverter converter(globalContext); // Needs global context for subsequent object creation. E.g., TZDate.
280 CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
281 if (!JSValueIsString(context, arguments[0])) {
282 ThrowMsg(ConversionException, "Wrong parameter type.");
284 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
287 std::string calendarId;
288 if (!JSValueIsString(context, arguments[1])) {
289 ThrowMsg(ConversionException, "Wrong parameter type.");
291 calendarId = converter.toString(arguments[1]);
294 IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
295 dplEvent->setForSynchronousCall();
296 dplEvent->setId(calendarId);
297 dplEvent->setType(calendarType);
298 privateObject->getObject()->getCalendar(dplEvent);
300 // Process the result.
301 if (dplEvent->getResult()) {
302 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
304 ThrowMsg(UnknownException, "Getting calendar failed by unkown reason.");
307 Catch(UnsupportedException)
309 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
310 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
312 Catch(InvalidArgumentException)
314 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
315 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
317 Catch(ConversionException)
319 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
320 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
322 Catch (NotFoundException)
324 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
325 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
329 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
330 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
333 return JSValueMakeNull(context);
336 const JSClassRef JSCalendarManager::getClassRef()
339 m_jsClassRef = JSClassCreate(&m_classInfo);
344 const JSClassDefinition* JSCalendarManager::getClassInfo()