wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Calendar / JSCalendarManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19 #include <string>
20 #include "ICalendar.h"
21 #include "ICalendarManager.h"
22 #include "CalendarFactory.h"
23
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>
31
32 #include "JSCalendarManager.h"
33 #include "JSCalendar.h"
34 #include "CalendarConverter.h"
35 #include "CalendarResponseDispatcher.h"
36 #include "plugin_config.h"
37
38 #include "CalendarAsyncCallbackManager.h"
39
40
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace DeviceAPI::Common;
44
45 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
46
47 namespace DeviceAPI {
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     { 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 },
75
76     { 0, 0, 0 }
77 };
78
79 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
80         JSCalendarManager::getClassInfo());
81
82 void JSCalendarManager::initialize(JSContextRef context,
83         JSObjectRef object)
84 {
85     if (!JSObjectGetPrivate(object)) {
86         LoggerD("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))) {
90             delete privateObject;
91         }
92     } else {
93         LoggerD("Private object already set.");
94     }
95 }
96
97 void JSCalendarManager::finalize(JSObjectRef object)
98 {
99     CalendarManagerPrivObject *priv = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
100     if (priv) {
101         delete priv;
102         JSObjectSetPrivate(object, NULL);
103     }
104 }
105
106 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
107         JSObjectRef object,
108         JSObjectRef thisObject,
109         size_t argumentCount,
110         const JSValueRef arguments[],
111         JSValueRef* exception)
112 {
113         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
114     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
115
116     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDARS);
117
118     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
119
120     Try {
121         if (!privateObject) {
122             ThrowMsg(ConversionException, "Object is null.");
123         }
124
125         CalendarConverter converter(context);
126
127         JSContextRef globalContext = privateObject->getContext();
128
129         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
130
131         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
132         if (argumentCount>=1) {
133             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
134         }
135
136         if (argumentCount>=2) {
137             cbm->setOnSuccess(converter.toFunction(arguments[1]));
138         } else {
139             ThrowMsg(ConversionException, "Wrong parameter count.");
140         }
141
142         if (argumentCount>=3) {
143             cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
144         }
145
146                 cbm->setObject(thisObject);
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                 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
155                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
156         return JSValueMakeUndefined(context);
157     }
158     Catch(UnsupportedException)
159     {
160                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
161         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
162     }
163     Catch(InvalidArgumentException)
164     {
165                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
166         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
167     }
168     Catch(ConversionException)
169     {
170                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
172     }
173     Catch(Exception)
174     {
175                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
177     }
178 }
179
180 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
181         JSObjectRef object,
182         JSObjectRef thisObject,
183         size_t argumentCount,
184         const JSValueRef arguments[],
185         JSValueRef* exception)
186 {
187         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
188     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
189
190     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
191
192     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
193
194     Try {
195         if (!privateObject) {
196             ThrowMsg(ConversionException, "Object is null.");
197         }
198
199                 // Global context should be passed to the calendar object.
200         JSContextRef globalContext = privateObject->getContext();
201
202         CalendarConverter converter(globalContext);
203         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
204         if (argumentCount>=1) {
205             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
206         } else {
207             ThrowMsg(ConversionException, "Wrong parameter type.");
208         }
209
210         IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
211         dplEvent->setForSynchronousCall();
212         dplEvent->setType(calendarType);
213         privateObject->getObject()->getDefaultCalendar(dplEvent);
214
215         if (dplEvent->getResult()) {
216             if( dplEvent->getCalendar() ) {
217                                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
218                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
219             } else {
220                 LoggerE("Default calendar not found.");
221             }
222         } else {
223             ThrowMsg(UnknownException, "Getting a default calendar failed by unknown reason.");
224         }
225     }
226     Catch(UnsupportedException)
227     {
228                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
229         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
230     }
231     Catch(InvalidArgumentException)
232     {
233                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
234         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
235     }
236     Catch(ConversionException)
237     {
238                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
239         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
240     }
241     Catch (NotFoundException)
242     {
243                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
244         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
245     }
246     Catch(Exception)
247     {
248                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
249         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
250     }
251
252         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
253     return JSValueMakeUndefined(context);
254 }
255
256 JSValueRef JSCalendarManager::getUnifiedCalendar(JSContextRef context,
257         JSObjectRef object,
258         JSObjectRef thisObject,
259         size_t argumentCount,
260         const JSValueRef arguments[],
261         JSValueRef* exception)
262 {
263         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
264     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
265
266     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR);
267
268     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
269
270     Try {
271         if (!privateObject) {
272             ThrowMsg(ConversionException, "Object is null.");
273         }
274
275                 // Global context should be passed to the calendar object.
276         JSContextRef globalContext = privateObject->getContext();
277
278         CalendarConverter converter(globalContext);
279         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
280         if (argumentCount>=1) {
281             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
282         } else {
283             ThrowMsg(ConversionException, "Wrong parameter type.");
284         }
285
286         IEventGetUnifiedCalendarPtr dplEvent(new IEventGetUnifiedCalendar());
287         dplEvent->setForSynchronousCall();
288         dplEvent->setType(calendarType);
289         privateObject->getObject()->getUnifiedCalendar(dplEvent);
290
291         if (dplEvent->getResult()) {
292             if( dplEvent->getCalendar() ) {
293                                 LoggerD("Successfully got a unified calendar.");
294                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
295             } else {
296                 LoggerE("Unified calendar not found.");
297             }
298         } else {
299             ThrowMsg(UnknownException, "Getting a unified calendar failed by unknown reason.");
300         }
301     }
302     Catch(UnsupportedException)
303     {
304                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
305         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
306     }
307     Catch(InvalidArgumentException)
308     {
309                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
310         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
311     }
312     Catch(ConversionException)
313     {
314                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
315         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
316     }
317     Catch (NotFoundException)
318     {
319                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
320         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
321     }
322     Catch(Exception)
323     {
324                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
325         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
326     }
327
328         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
329     return JSValueMakeUndefined(context);
330 }
331
332 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
333         JSObjectRef object,
334         JSObjectRef thisObject,
335         size_t argumentCount,
336         const JSValueRef arguments[],
337         JSValueRef* exception)
338 {
339         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
340     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
341
342     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDAR);
343
344     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
345
346     Try {
347         if (!privateObject) {
348             ThrowMsg(ConversionException, "Object is null.");
349         }
350
351                 // Global context should be passed to the calendar object.
352         JSContextRef globalContext = privateObject->getContext();
353
354         CalendarConverter converter(globalContext);
355
356         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
357         if (argumentCount>=1) {
358             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
359         } else {
360             ThrowMsg(ConversionException, "Wrong parameter count.");
361         }
362
363         std::string calendarId;
364         if (argumentCount>=2) {
365             calendarId = converter.toString(arguments[1]);
366         }
367
368         IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
369         dplEvent->setForSynchronousCall();
370         dplEvent->setId(calendarId);
371         dplEvent->setType(calendarType);
372         privateObject->getObject()->getCalendar(dplEvent);
373
374         // Process the result.
375         if (dplEvent->getResult()) {
376             if( dplEvent->getCalendar() ) {
377                                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
378                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
379             } else {
380                 ThrowMsg(NotFoundException, "Calendar not found.");
381             }
382         } else {
383             ThrowMsg(UnknownException, "Getting a calendar failed by unknown reason.");
384         }
385     }
386     Catch(UnsupportedException)
387     {
388                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
389         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
390     }
391     Catch(InvalidArgumentException)
392     {
393                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
394         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
395     }
396     Catch(ConversionException)
397     {
398                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
399         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
400     }
401     Catch (NotFoundException)
402     {
403                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
404         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
405     }
406     Catch(Exception)
407     {
408                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
409         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
410     }
411 }
412
413 const JSClassRef JSCalendarManager::getClassRef()
414 {
415     if (!m_jsClassRef) {
416         m_jsClassRef = JSClassCreate(&m_classInfo);
417     }
418     return m_jsClassRef;
419 }
420
421 const JSClassDefinition* JSCalendarManager::getClassInfo()
422 {
423     return &m_classInfo;
424 }
425
426 }
427 }