b4327839973c8d963aee08190d0c697efaab799f
[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 "JSCalendarManager.h"
31 #include "JSCalendar.h"
32 #include "CalendarConverter.h"
33 #include "CalendarResponseDispatcher.h"
34 #include "plugin_config.h"
35
36 #include "CalendarAsyncCallbackManager.h"
37
38 using namespace TizenApis::Api::Calendar;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41 using namespace TizenApis::Commons;
42
43 #define TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME "calendar"
44
45 namespace TizenApis {
46 namespace Tizen1_0 {
47 namespace Calendar {
48
49 JSClassDefinition JSCalendarManager::m_classInfo = {
50     0,
51     kJSClassAttributeNone,
52     TIZEN_CALENDAR_MANAGER_ATTRIBUTENAME,
53     0,
54     NULL,
55     m_function,
56     initialize,
57     finalize,
58     NULL, //HasProperty,
59     NULL, //GetProperty,
60     NULL, //SetProperty,
61     NULL, //DeleteProperty,
62     NULL, //GetPropertyNames,
63     NULL, //CallAsFunction,
64     NULL, //CallAsConstructor,
65     NULL, //HasInstance,
66     NULL  //ConvertToType
67 };
68
69 JSStaticFunction JSCalendarManager::m_function[] = {
70     { CALENDAR_FUNCTION_API_GET_CALENDARS,
71       getCalendars, kJSPropertyAttributeNone },
72
73     { CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR,
74       getDefaultCalendar, kJSPropertyAttributeNone },
75
76     { CALENDAR_FUNCTION_API_GET_CALENDAR,
77       getCalendar, kJSPropertyAttributeNone },
78
79     { 0, 0, 0 }
80 };
81
82 JSClassRef JSCalendarManager::m_jsClassRef = JSClassCreate(
83         JSCalendarManager::getClassInfo());
84
85 void JSCalendarManager::initialize(JSContextRef context,
86         JSObjectRef object)
87 {
88     if (!JSObjectGetPrivate(object)) {
89         LogDebug("Create calendar manager private object.");
90         ICalendarManagerPtr calendarManager = CalendarFactory::getInstance().createCalendarManagerObject();
91         CalendarManagerPrivObject *privateObject = new CalendarManagerPrivObject(context, calendarManager);
92         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
93             delete privateObject;
94         }
95     } else {
96         LogDebug("Private object alrerady set.");
97     }
98 }
99
100 void JSCalendarManager::finalize(JSObjectRef object)
101 {
102     LogDebug("entered");
103     CalendarManagerPrivObject *priv = static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(object));
104     if (priv) {
105         delete priv;
106         JSObjectSetPrivate(object, NULL);
107     }
108 }
109
110 JSValueRef JSCalendarManager::getCalendars(JSContextRef context,
111         JSObjectRef object,
112         JSObjectRef thisObject,
113         size_t argumentCount,
114         const JSValueRef arguments[],
115         JSValueRef* exception)
116 {
117     LogDebug("entered");
118
119     CalendarManagerPrivObject *privateObject =
120         static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
121
122     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDARS);
123
124     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
125
126     Try {
127         if (!privateObject) {
128             ThrowMsg(ConversionException, "Object is null.");
129         }
130
131         CalendarConverter converter(context);
132
133         JSContextRef globalContext = privateObject->getContext();
134
135         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
136
137         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
138         if (argumentCount>=1) {
139             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
140         }
141
142         if (argumentCount>=2) {
143             cbm->setOnSuccess(converter.toFunction(arguments[1]));
144         } else {
145             ThrowMsg(ConversionException, "Wrong parameter count.");
146         }
147
148         if (argumentCount>=3) {
149             cbm->setOnError(converter.toFunctionOrNull(arguments[2]));
150         }
151
152                 cbm->setObject(thisObject);
153
154         IEventGetCalendarsPtr dplEvent(new IEventGetCalendars());
155         dplEvent->setType(calendarType);
156         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
157         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
158         privateObject->getObject()->getCalendars(dplEvent);
159
160                 CalendarAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
161
162         return JSValueMakeUndefined(context);
163     }
164     Catch(UnsupportedException)
165     {
166                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
167         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
168     }
169     Catch(InvalidArgumentException)
170     {
171                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
172         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
173     }
174     Catch(ConversionException)
175     {
176                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
177         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
178     }
179     Catch(Exception)
180     {
181                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
182         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
183     }
184
185     return JSValueMakeUndefined(context);
186 }
187
188 JSValueRef JSCalendarManager::getDefaultCalendar(JSContextRef context,
189         JSObjectRef object,
190         JSObjectRef thisObject,
191         size_t argumentCount,
192         const JSValueRef arguments[],
193         JSValueRef* exception)
194 {
195     CalendarManagerPrivObject *privateObject =
196         static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
197
198     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_DEFAULT_CALENDAR);
199
200     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
201
202     Try {
203         if (!privateObject) {
204             ThrowMsg(ConversionException, "Object is null.");
205         }
206
207         JSContextRef globalContext = privateObject->getContext();
208
209         CalendarConverter converter(globalContext);
210         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
211         if (argumentCount>=1) {
212             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
213         } else {
214             ThrowMsg(ConversionException, "Wrong parameter type.");
215         }
216
217         IEventGetDefaultCalendarPtr dplEvent(new IEventGetDefaultCalendar());
218         dplEvent->setForSynchronousCall();
219         dplEvent->setType(calendarType);
220         privateObject->getObject()->getDefaultCalendar(dplEvent);
221
222         if (dplEvent->getResult()) {
223             if( dplEvent->getCalendar() ) {
224                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
225             } else {
226                 LogError("Default calendar not found.");
227             }
228         } else {
229             ThrowMsg(UnknownException, "Getting default calendar failed by unknown reason.");
230         }
231     }
232     Catch(UnsupportedException)
233     {
234                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
235         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
236     }
237     Catch(InvalidArgumentException)
238     {
239                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
240         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
241     }
242     Catch(ConversionException)
243     {
244                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
245         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
246     }
247     Catch (NotFoundException)
248     {
249                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
250         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
251     }
252     Catch(Exception)
253     {
254                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
255         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
256     }
257
258     return JSValueMakeUndefined(context);
259 }
260
261 JSValueRef JSCalendarManager::getCalendar(JSContextRef context,
262         JSObjectRef object,
263         JSObjectRef thisObject,
264         size_t argumentCount,
265         const JSValueRef arguments[],
266         JSValueRef* exception)
267 {
268     CalendarManagerPrivObject *privateObject =
269         static_cast<CalendarManagerPrivObject*>(JSObjectGetPrivate(thisObject));
270
271     AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_GET_CALENDAR);
272
273     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
274
275     Try {
276         if (!privateObject) {
277             ThrowMsg(ConversionException, "Object is null.");
278         }
279
280         JSContextRef globalContext = privateObject->getContext();
281
282         CalendarConverter converter(globalContext);
283
284         CalendarEvent::CalendarType calendarType = CalendarEvent::UNDEFINED_TYPE;
285         if (argumentCount>=1) {
286             calendarType = converter.toCalendarType(converter.toString(arguments[0]));
287         } else {
288             ThrowMsg(ConversionException, "Wrong parameter count.");
289         }
290
291         std::string calendarId;
292         if (argumentCount>=2) {
293             calendarId = converter.toString(arguments[1]);
294         }
295
296         IEventGetCalendarPtr dplEvent(new IEventGetCalendar());
297         dplEvent->setForSynchronousCall();
298         dplEvent->setId(calendarId);
299         dplEvent->setType(calendarType);
300         privateObject->getObject()->getCalendar(dplEvent);
301
302         // Process the result.
303         if (dplEvent->getResult()) {
304             if( dplEvent->getCalendar() ) {
305                 return converter.toJSValueRefCalendar(dplEvent->getCalendar());
306             } else {
307                 ThrowMsg(NotFoundException, "Calendar not found.");
308             }
309         } else {
310             ThrowMsg(UnknownException, "Getting calendar failed by unknown reason.");
311         }
312     }
313     Catch(UnsupportedException)
314     {
315                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
316         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
317     }
318     Catch(InvalidArgumentException)
319     {
320                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
321         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
322     }
323     Catch(ConversionException)
324     {
325                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
326         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
327     }
328     Catch (NotFoundException)
329     {
330                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
331         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
332     }
333     Catch(Exception)
334     {
335                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
336         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
337     }
338
339     return JSValueMakeUndefined(context);
340 }
341
342 const JSClassRef JSCalendarManager::getClassRef()
343 {
344     if (!m_jsClassRef) {
345         m_jsClassRef = JSClassCreate(&m_classInfo);
346     }
347     return m_jsClassRef;
348 }
349
350 const JSClassDefinition* JSCalendarManager::getClassInfo()
351 {
352     return &m_classInfo;
353 }
354
355 }
356 }
357 }