d57df1cfbaf421f1b8083ff58cbdc10c05d968a3
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <string>
19 #include <API/Calendar/ICalendar.h>
20 #include <API/Calendar/ICalendarManager.h>
21 #include <API/Calendar/CalendarFactory.h>
22
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>
29
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"
36
37 using namespace TizenApis::Api::Calendar;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace TizenApis::Commons;
41
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"
45
46 namespace TizenApis {
47 namespace Tizen1_0 {
48 namespace Calendar {
49
50 JSClassDefinition JSCalendarManager::m_classInfo = {
51     0,
52     kJSClassAttributeNone,
53     TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
54     0,
55     NULL,
56     m_function,
57     initialize,
58     finalize,
59     NULL, //HasProperty,
60     NULL, //GetProperty,
61     NULL, //SetProperty,
62     NULL, //DeleteProperty,
63     NULL, //GetPropertyNames,
64     NULL, //CallAsFunction,
65     NULL, //CallAsConstructor,
66     NULL, //HasInstance,
67     NULL  //ConvertToType
68 };
69
70 JSStaticFunction JSCalendarManager::m_function[] = {
71     { TIZEN_CALENDAR_MANAGER_FUNCTION_GET_CALENDARS,
72       getCalendars, kJSPropertyAttributeNone },
73
74     { TIZEN_CALENDAR_MANAGER_FUNCTION_GET_DEFAULT_CALENDAR,
75       getDefaultCalendar, kJSPropertyAttributeNone },
76
77     { 0, 0, 0 }
78 };
79
80 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
81         JSCalendarManager::getClassInfo());
82
83 void JSCalendarManager::initialize(JSContextRef context,
84         JSObjectRef object)
85 {
86     LogDebug("entered");
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))) {
94             delete privateObject;
95         }
96     }
97 }
98
99 void JSCalendarManager::finalize(JSObjectRef object)
100 {
101     LogDebug("entered");
102     CalendarManagerPrivObject *privateObject =
103         static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
104     delete privateObject;
105 }
106
107 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
108         JSObjectRef object,
109         JSObjectRef thisObject,
110         size_t argumentCount,
111         const JSValueRef arguments[],
112         JSValueRef* exception)
113 {
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);
119     }
120
121     JSContextRef globalContext = privateObject->getContext();
122
123     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_CALENDARS);
124
125     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
126
127     CalendarConverter converter(context);
128
129     Try {
130         if (argumentCount<2 || argumentCount>3) {
131             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
132         }
133
134         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
135
136         if (argumentCount > 2) {
137             cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
138         }
139         cbm->setOnSuccess(converter.toFunction(arguments[1]));
140
141         CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
142         if (!JSValueIsString(context, arguments[0])) {
143             ThrowMsg(ConversionException, "Wrong parameter type.");
144         } else {
145             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
146         }
147
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);
153
154     }
155     Catch(UnsupportedException)
156     {
157                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
158         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
159     }
160     Catch(InvalidArgumentException)
161     {
162                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
163         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
164     }
165     Catch(ConversionException)
166     {
167                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
168         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
169     }
170     Catch(Exception)
171     {
172                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
173         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
174     }
175
176     return JSValueMakeNull(context);
177 }
178
179 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
180         JSObjectRef object,
181         JSObjectRef thisObject,
182         size_t argumentCount,
183         const JSValueRef arguments[],
184         JSValueRef* exception)
185 {
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);
191     }
192
193     JSContextRef globalContext = privateObject->getContext();
194
195     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(globalContext, CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
196
197     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
198
199     Try {
200         if (argumentCount!=1) {
201             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
202         }
203
204         CalendarConverter converter(context);
205         CalendarEvent::CalendarType calendarType = CalendarEvent::EVENT_TYPE;
206         if (!JSValueIsString(context, arguments[0])) {
207             ThrowMsg(ConversionException, "Wrong parameter type.");
208         } else {
209             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
210         }
211
212         IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
213         dplEvent->setForSynchronousCall();
214         dplEvent->setType(calendarType);
215         privateObject->getObject()->getDefaultCalendar(dplEvent);
216
217         if (dplEvent->getResult()) {
218             CalendarConverterFactory::ConverterType converter =
219                 CalendarConverterFactory::getConverter(globalContext); // should be global!
220             return converter->toJSValueRefCalendar(dplEvent->getCalendar());
221         } else {
222             ThrowMsg(UnknownException, "Updating failed by unkown reason.");
223         }
224     }
225     Catch(UnsupportedException)
226     {
227                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
228         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
229     }
230     Catch(InvalidArgumentException)
231     {
232                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
233         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
234     }
235     Catch(ConversionException)
236     {
237                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
238         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
239     }
240     Catch (NotFoundException)
241     {
242                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
243         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
244     }
245     Catch(Exception)
246     {
247                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
248         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
249     }
250
251     return JSValueMakeNull(context);
252 }
253
254 const JSClassRef JSCalendarManager::getClassRef()
255 {
256     if (!m_jsClassRef) {
257         m_jsClassRef = JSClassCreate(&m_classInfo);
258     }
259     return m_jsClassRef;
260 }
261
262 const JSClassDefinition* JSCalendarManager::getClassInfo()
263 {
264     return &m_classInfo;
265 }
266
267 }
268 }
269 }