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"
43 #define TIZEN_CALENDAR_MANAGER_FUNCTION_GET_CALENDARS "getCalendars"
44 #define TIZEN_CALENDAR_MANAGER_FUNCTION_GET_DEFAULT_CALENDAR "getDefaultCalendar"
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 { TIZEN_CALENDAR_MANAGER_FUNCTION_GET_CALENDARS,
72 getCalendars, kJSPropertyAttributeNone },
74 { TIZEN_CALENDAR_MANAGER_FUNCTION_GET_DEFAULT_CALENDAR,
75 getDefaultCalendar, kJSPropertyAttributeNone },
80 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
81 JSCalendarManager::getClassInfo());
83 void JSCalendarManager::initialize(JSContextRef context,
87 CalendarManagerPrivObject *privateObject =
88 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
89 if (NULL == privateObject) {
90 ICalendarManagerPtr calendarManager =
91 Api::Calendar::CalendarFactory::getInstance().createCalendarManagerObject();
92 privateObject = new CalendarManagerPrivObject(context, calendarManager);
93 if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
99 void JSCalendarManager::finalize(JSObjectRef object)
102 CalendarManagerPrivObject *privateObject =
103 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
104 delete privateObject;
107 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
109 JSObjectRef thisObject,
110 size_t argumentCount,
111 const JSValueRef arguments[],
112 JSValueRef* exception)
114 CalendarManagerPrivObject *privateObject =
115 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
116 if (NULL == privateObject) {
117 LogError("private object is null");
118 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR);
121 JSContextRef globalContext = privateObject->getContext();
123 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_CALENDARS);
125 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
127 CalendarConverter converter(context);
130 if (argumentCount<2 || argumentCount>3) {
131 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
134 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
136 if (argumentCount > 2) {
137 cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
139 cbm->setOnSuccess(converter.toFunction(arguments[1]));
141 CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
142 if (!JSValueIsString(context, arguments[0])) {
143 ThrowMsg(ConversionException, "Wrong parameter type.");
145 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
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);
155 Catch(UnsupportedException)
157 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
158 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
160 Catch(InvalidArgumentException)
162 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
163 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
165 Catch(ConversionException)
167 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
168 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
172 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
173 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
176 return JSValueMakeNull(context);
179 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
181 JSObjectRef thisObject,
182 size_t argumentCount,
183 const JSValueRef arguments[],
184 JSValueRef* exception)
186 CalendarManagerPrivObject *privateObject =
187 static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
188 if (NULL == privateObject) {
189 LogError("private object is null");
190 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR);
193 JSContextRef globalContext = privateObject->getContext();
195 AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
197 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
200 if (argumentCount!=1) {
201 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
204 CalendarConverter converter(context);
205 CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
206 if (!JSValueIsString(context, arguments[0])) {
207 ThrowMsg(ConversionException, "Wrong parameter type.");
209 calendarType = converter.toCalendarType(converter.toString(arguments[0]));
212 IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
213 dplEvent->setForSynchronousCall();
214 dplEvent->setType(calendarType);
215 privateObject->getObject()->getDefaultCalendar(dplEvent);
217 if (dplEvent->getResult()) {
218 CalendarConverterFactory::ConverterType converter =
219 CalendarConverterFactory::getConverter(globalContext); // should be global!
220 return converter->toJSValueRefCalendar(dplEvent->getCalendar());
222 ThrowMsg(UnknownException, "Updating failed by unkown 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 return JSValueMakeNull(context);
254 const JSClassRef JSCalendarManager::getClassRef()
257 m_jsClassRef = JSClassCreate(&m_classInfo);
262 const JSClassDefinition* JSCalendarManager::getClassInfo()