0defb46969951616b9bea953ef48b39f26a6cec6
[platform/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         LogDebug("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         LogDebug("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                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
161         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
162     }
163     Catch(InvalidArgumentException)
164     {
165                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
166         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
167     }
168     Catch(ConversionException)
169     {
170                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
171         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
172     }
173     Catch(Exception)
174     {
175                 LogWarning("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                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
218             } else {
219                 LogError("Default calendar not found.");
220             }
221         } else {
222             ThrowMsg(UnknownException, "Getting a default calendar failed by unknown 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         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
252     return JSValueMakeUndefined(context);
253 }
254
255 JSValueRef JSCalendarManager::getUnifiedCalendar(JSContextRef context,
256         JSObjectRef object,
257         JSObjectRef thisObject,
258         size_t argumentCount,
259         const JSValueRef arguments[],
260         JSValueRef* exception)
261 {
262         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
263     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
264
265     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR);
266
267     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
268
269     Try {
270         if (!privateObject) {
271             ThrowMsg(ConversionException, "Object is null.");
272         }
273
274                 // Global context should be passed to the calendar object.
275         JSContextRef globalContext = privateObject->getContext();
276
277         CalendarConverter converter(globalContext);
278         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
279         if (argumentCount>=1) {
280             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
281         } else {
282             ThrowMsg(ConversionException, "Wrong parameter type.");
283         }
284
285         IEventGetUnifiedCalendarPtr dplEvent(new IEventGetUnifiedCalendar());
286         dplEvent->setForSynchronousCall();
287         dplEvent->setType(calendarType);
288         privateObject->getObject()->getUnifiedCalendar(dplEvent);
289
290         if (dplEvent->getResult()) {
291             if( dplEvent->getCalendar() ) {
292                                 LogDebug("Successfully got a unified calendar.");
293                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
294             } else {
295                 LogError("Unified calendar not found.");
296             }
297         } else {
298             ThrowMsg(UnknownException, "Getting a unified calendar failed by unknown reason.");
299         }
300     }
301     Catch(UnsupportedException)
302     {
303                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
304         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
305     }
306     Catch(InvalidArgumentException)
307     {
308                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
309         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
310     }
311     Catch(ConversionException)
312     {
313                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
314         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
315     }
316     Catch (NotFoundException)
317     {
318                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
319         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
320     }
321     Catch(Exception)
322     {
323                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
324         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
325     }
326
327         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
328     return JSValueMakeUndefined(context);
329 }
330
331 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
332         JSObjectRef object,
333         JSObjectRef thisObject,
334         size_t argumentCount,
335         const JSValueRef arguments[],
336         JSValueRef* exception)
337 {
338         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
339     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
340
341     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDAR);
342
343     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
344
345     Try {
346         if (!privateObject) {
347             ThrowMsg(ConversionException, "Object is null.");
348         }
349
350                 // Global context should be passed to the calendar object.
351         JSContextRef globalContext = privateObject->getContext();
352
353         CalendarConverter converter(globalContext);
354
355         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
356         if (argumentCount>=1) {
357             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
358         } else {
359             ThrowMsg(ConversionException, "Wrong parameter count.");
360         }
361
362         std::string calendarId;
363         if (argumentCount>=2) {
364             calendarId = converter.toString(arguments[1]);
365         }
366
367         IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
368         dplEvent->setForSynchronousCall();
369         dplEvent->setId(calendarId);
370         dplEvent->setType(calendarType);
371         privateObject->getObject()->getCalendar(dplEvent);
372
373         // Process the result.
374         if (dplEvent->getResult()) {
375             if( dplEvent->getCalendar() ) {
376                                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
377                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
378             } else {
379                 ThrowMsg(NotFoundException, "Calendar not found.");
380             }
381         } else {
382             ThrowMsg(UnknownException, "Getting a calendar failed by unknown reason.");
383         }
384     }
385     Catch(UnsupportedException)
386     {
387                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
388         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
389     }
390     Catch(InvalidArgumentException)
391     {
392                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
393         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
394     }
395     Catch(ConversionException)
396     {
397                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
398         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
399     }
400     Catch (NotFoundException)
401     {
402                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
403         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
404     }
405     Catch(Exception)
406     {
407                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
408         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
409     }
410 }
411
412 const JSClassRef JSCalendarManager::getClassRef()
413 {
414     if (!m_jsClassRef) {
415         m_jsClassRef = JSClassCreate(&m_classInfo);
416     }
417     return m_jsClassRef;
418 }
419
420 const JSClassDefinition* JSCalendarManager::getClassInfo()
421 {
422     return &m_classInfo;
423 }
424
425 }
426 }