Update change log and spec for wrt-plugins-tizen_0.4.70
[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 <JSWebAPIErrorFactory.h>
28 #include <SecurityExceptions.h>
29 #include <TimeTracer.h>
30 #include <Export.h>
31 #include <Logger.h>
32 #include <GlobalContextManager.h>
33
34 #include "JSCalendarManager.h"
35 #include "JSCalendar.h"
36 #include "CalendarConverter.h"
37 #include "CalendarResponseDispatcher.h"
38 #include "plugin_config.h"
39
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace DeviceAPI::Common;
43
44 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
45
46 namespace DeviceAPI {
47 namespace Calendar {
48
49 using WrtDeviceApis::Commons::UnknownException;
50 using WrtDeviceApis::Commons::NotFoundException;
51
52 JSClassDefinition JSCalendarManager::m_classInfo = {
53     0,
54     kJSClassAttributeNone,
55     TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
56     0,
57     NULL,
58     m_function,
59     initialize,
60     finalize,
61     NULL, //HasProperty,
62     NULL, //GetProperty,
63     NULL, //SetProperty,
64     NULL, //DeleteProperty,
65     NULL, //GetPropertyNames,
66     NULL, //CallAsFunction,
67     NULL, //CallAsConstructor,
68     NULL, //HasInstance,
69     NULL  //ConvertToType
70 };
71
72 JSStaticFunction JSCalendarManager::m_function[] = {
73     { CALENDAR_FUNCTION_API_GET_CALENDARS, getCalendars, kJSPropertyAttributeNone },
74     { CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR, getDefaultCalendar, kJSPropertyAttributeNone },
75     { CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR, getUnifiedCalendar, kJSPropertyAttributeNone },
76     { CALENDAR_FUNCTION_API_GET_CALENDAR, getCalendar, kJSPropertyAttributeNone },
77
78     { 0, 0, 0 }
79 };
80
81 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
82         JSCalendarManager::getClassInfo());
83
84 void JSCalendarManager::initialize(JSContextRef context,
85         JSObjectRef object)
86 {
87     if (!JSObjectGetPrivate(object)) {
88         LoggerD("Create calendar manager private object.");
89         ICalendarManagerPtr calendarManager = CalendarFactory::getInstance().createCalendarManagerObject();
90         CalendarManagerPrivObject *privateObject = new CalendarManagerPrivObject(context, calendarManager);
91         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
92             delete privateObject;
93         }
94     } else {
95         LoggerD("Private object already set.");
96     }
97 }
98
99 void JSCalendarManager::finalize(JSObjectRef object)
100 {
101     CalendarManagerPrivObject *priv = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
102     if (priv) {
103         delete priv;
104         JSObjectSetPrivate(object, NULL);
105     }
106 }
107
108 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
109         JSObjectRef object,
110         JSObjectRef thisObject,
111         size_t argumentCount,
112         const JSValueRef arguments[],
113         JSValueRef* exception)
114 {
115         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
116     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
117
118     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_CALENDARS);
119
120     Try {
121         if (!privateObject) {
122             ThrowMsg(ConversionException, "Object is null.");
123         }
124
125         CalendarConverter converter(context);
126
127         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
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         dplEvent->copyAceCheckAccessFunction(privateObject);
153         privateObject->getObject()->getCalendars(dplEvent);
154
155                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
156         return JSValueMakeUndefined(context);
157     }
158     Catch(UnsupportedException)
159     {
160                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
161         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
162     }
163     Catch(InvalidArgumentException)
164     {
165                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
166         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
167     }
168     Catch(ConversionException)
169     {
170                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
172     }
173     Catch(Exception)
174     {
175                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::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     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
191
192     Try {
193         if (!privateObject) {
194             ThrowMsg(ConversionException, "Object is null.");
195         }
196
197                 // Global context should be passed to the calendar object.
198         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
199
200         CalendarConverter converter(globalContext, privateObject);
201         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
202         if (argumentCount>=1) {
203             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
204         } else {
205             ThrowMsg(ConversionException, "Wrong parameter type.");
206         }
207
208         IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
209         dplEvent->setForSynchronousCall();
210         dplEvent->setType(calendarType);
211         privateObject->getObject()->getDefaultCalendar(dplEvent);
212
213         if (dplEvent->getResult()) {
214             if( dplEvent->getCalendar() ) {
215                                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
216                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
217             } else {
218                 LoggerE("Default calendar not found.");
219             }
220         } else {
221             ThrowMsg(UnknownException, "Getting a default calendar failed by unknown reason.");
222         }
223     }
224     Catch(UnsupportedException)
225     {
226                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
227         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
228     }
229     Catch(InvalidArgumentException)
230     {
231                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
232         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
233     }
234     Catch(ConversionException)
235     {
236                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
237         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
238     }
239     Catch (NotFoundException)
240     {
241                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
242         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
243     }
244     Catch(Exception)
245     {
246                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
247         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
248     }
249
250         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
251     return JSValueMakeUndefined(context);
252 }
253
254 JSValueRef JSCalendarManager::getUnifiedCalendar(JSContextRef context,
255         JSObjectRef object,
256         JSObjectRef thisObject,
257         size_t argumentCount,
258         const JSValueRef arguments[],
259         JSValueRef* exception)
260 {
261         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
262     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
263
264     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_UNIFIED_CALENDAR);
265
266     Try {
267         if (!privateObject) {
268             ThrowMsg(ConversionException, "Object is null.");
269         }
270
271                 // Global context should be passed to the calendar object.
272         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
273
274         CalendarConverter converter(globalContext, privateObject);
275         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
276         if (argumentCount>=1) {
277             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
278         } else {
279             ThrowMsg(ConversionException, "Wrong parameter type.");
280         }
281
282         IEventGetUnifiedCalendarPtr dplEvent(new IEventGetUnifiedCalendar());
283         dplEvent->setForSynchronousCall();
284         dplEvent->setType(calendarType);
285         privateObject->getObject()->getUnifiedCalendar(dplEvent);
286
287         if (dplEvent->getResult()) {
288             if( dplEvent->getCalendar() ) {
289                                 LoggerD("Successfully got a unified calendar.");
290                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
291             } else {
292                 LoggerE("Unified calendar not found.");
293             }
294         } else {
295             ThrowMsg(UnknownException, "Getting a unified calendar failed by unknown reason.");
296         }
297     }
298     Catch(UnsupportedException)
299     {
300                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
301         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
302     }
303     Catch(InvalidArgumentException)
304     {
305                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
306         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
307     }
308     Catch(ConversionException)
309     {
310                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
311         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
312     }
313     Catch (NotFoundException)
314     {
315                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
316         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
317     }
318     Catch(Exception)
319     {
320                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
321         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
322     }
323
324         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
325     return JSValueMakeUndefined(context);
326 }
327
328 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
329         JSObjectRef object,
330         JSObjectRef thisObject,
331         size_t argumentCount,
332         const JSValueRef arguments[],
333         JSValueRef* exception)
334 {
335         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
336     CalendarManagerPrivObject *privateObject = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
337
338     TIZEN_CHECK_ACCESS(context, exception, privateObject, CALENDAR_FUNCTION_API_GET_CALENDAR);
339
340     Try {
341         if (!privateObject) {
342             ThrowMsg(ConversionException, "Object is null.");
343         }
344
345                 // Global context should be passed to the calendar object.
346         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
347
348         CalendarConverter converter(globalContext, privateObject);
349
350         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
351         if (argumentCount>=1) {
352             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
353         } else {
354             ThrowMsg(ConversionException, "Wrong parameter count.");
355         }
356
357         std::string calendarId;
358         if (argumentCount>=2) {
359             calendarId = converter.toString(arguments[1]);
360         }
361
362         IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
363         dplEvent->setForSynchronousCall();
364         dplEvent->setId(calendarId);
365         dplEvent->setType(calendarType);
366         privateObject->getObject()->getCalendar(dplEvent);
367
368         // Process the result.
369         if (dplEvent->getResult()) {
370             if( dplEvent->getCalendar() ) {
371                                         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
372                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
373             } else {
374                 ThrowMsg(NotFoundException, "Calendar not found.");
375             }
376         } else {
377             ThrowMsg(UnknownException, "Getting a calendar failed by unknown reason.");
378         }
379     }
380     Catch(UnsupportedException)
381     {
382                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
383         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
384     }
385     Catch(InvalidArgumentException)
386     {
387                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
388         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
389     }
390     Catch(ConversionException)
391     {
392                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
393         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
394     }
395     Catch (NotFoundException)
396     {
397                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
398         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
399     }
400     Catch(Exception)
401     {
402                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
403         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
404     }
405 }
406
407 const JSClassRef DLL_EXPORT JSCalendarManager::getClassRef()
408 {
409     if (!m_jsClassRef) {
410         m_jsClassRef = JSClassCreate(&m_classInfo);
411     }
412     return m_jsClassRef;
413 }
414
415 const JSClassDefinition* JSCalendarManager::getClassInfo()
416 {
417     return &m_classInfo;
418 }
419
420 }
421 }