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