77804c1bc8025d952c314cd5e4c2a4664a11b420
[platform/framework/web/wrt-plugins-tizen.git] / src / Calendar / JSCalendarTask.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 #include <ctime>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include "EventId.h"
23 #include "ICalendar.h"
24 #include "CalendarFactory.h"
25 #include "JSCalendarTask.h"
26 #include "JSCalendarItemProperties.h"
27 #include "CalendarConverter.h"
28 #include "JSCalendarEventId.h"
29 #include <TimeUtilConverter.h>
30 #include <JSTZDate.h>
31 #include <JSWebAPIErrorFactory.h>
32 #include <GlobalContextManager.h>
33 #include <Logger.h>
34 #include <Export.h>
35
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace DeviceAPI::Common;
39 using namespace DeviceAPI::Time;
40
41 namespace DeviceAPI {
42 namespace Calendar {
43
44 using WrtDeviceApis::Commons::UnknownException;
45
46 JSClassDefinition JSCalendarTask::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     TIZEN_INTERFACE_CALENDAR_TASK,
50     JSCalendarItemProperties::getClassRef(),
51     m_property,
52     NULL, //m_function,
53     initialize,
54     finalize,
55     NULL, //hasProperty,
56     NULL, //getProperty,
57     NULL, //setProperty,
58     NULL, //DeleteProperty,
59     NULL, //GetPropertyNames,
60     NULL, //CallAsFunction,
61     NULL, //constructor,
62     NULL, //HasInstance,
63     NULL  //ConvertToType
64 };
65
66 JSStaticValue JSCalendarTask::m_property[] = {
67     //Task properties
68     { TIZEN_CALENDAR_TASK_ID, getPropertyId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
69
70     { 0, 0, 0, 0 }
71 };
72
73 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
74
75 ICalendarPtr JSCalendarTask::m_calendar;
76
77 void JSCalendarTask::initialize(JSContextRef context,
78         JSObjectRef object)
79 {
80     if (!JSObjectGetPrivate(object)) {
81         LoggerD("Create calendar task private object.");
82         CalendarEventPtr task( new CalendarEvent() );
83         CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
84         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
85             delete priv;
86         }
87     } else {
88         LoggerD("Private object already set.");
89     }
90
91         if (NULL==m_calendar) {
92                 m_calendar = CalendarFactory::getInstance().createCalendarObject();
93         LoggerD("Static calendar for task created.");
94         }
95
96     if (m_calendar) {
97         m_calendar->setType(CalendarEvent::TASK_TYPE);
98         LoggerD("Calendar object type is set to task.");
99     }
100 }
101
102 void JSCalendarTask::finalize(JSObjectRef object)
103 {
104     CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
105     if (priv) {
106         delete priv;
107         JSObjectSetPrivate(object, NULL);
108     }
109 }
110
111 JSObjectRef DLL_EXPORT JSCalendarTask::constructor(JSContextRef context,
112     JSObjectRef constructor,
113     size_t argumentCount,
114     const JSValueRef arguments[],
115     JSValueRef* exception)
116 {
117     Try
118     {
119                 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
120         CalendarConverter converter(globalContext);
121         CalendarEventPtr task;
122
123         if (argumentCount==0) {
124             CalendarEventPtr result(new CalendarEvent());
125             task = result;
126         } else if (argumentCount==1) {
127             LoggerI("taskInitDict case");
128             if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
129                 CalendarEventPtr result(new CalendarEvent());
130                 task = result;
131             } else if (JSValueIsObject(context, arguments[0])) {
132                 task = converter.toItem(arguments[0], false);
133                 if (!task) {
134                     ThrowMsg(ConversionException, "Parameter conversion failed.");
135                 }
136             } else {
137                 ThrowMsg(ConversionException, "Parameter conversion failed.");
138             }
139         } else if (argumentCount>=2) {
140             LoggerI("task stringRepresentation case");
141             std::string eventStr;
142             CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
143             eventStr = converter.toString(arguments[0]);
144             format = converter.toVObjectFormat(converter.toString(arguments[1]));
145
146             IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
147             dplEvent->setEventString(eventStr);
148             dplEvent->setFormat(format);
149             dplEvent->setForSynchronousCall();
150             m_calendar->createEventFromString(dplEvent);
151
152             // Process the returned object.
153             if (dplEvent->getResult()) {
154                 LoggerI("Successfully created a task.");
155                 task = dplEvent->getEvent();
156             } else {
157                 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
158                     ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
159                 } else {
160                     ThrowMsg(UnknownException, "Converting string failed.");
161                 }
162             }
163         }
164
165         task->setCalendarType(CalendarEvent::TASK_TYPE);
166         return createJSCalendarTask(context, task);
167     }
168     Catch(UnsupportedException)
169     {
170         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
172     }
173     Catch(InvalidArgumentException)
174     {
175         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
177     }
178     Catch(ConversionException)
179     {
180         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
182     }
183     Catch(Exception)
184     {
185         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
186         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
187     }
188 }
189
190 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
191 {
192     CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
193     return JSObjectMake(context, getClassRef(), priv);
194 }
195
196 const JSClassRef DLL_EXPORT JSCalendarTask::getClassRef()
197 {
198     if (!m_jsClassRef) {
199         m_jsClassRef = JSClassCreate(&m_classInfo);
200     }
201     return m_jsClassRef;
202 }
203
204 const JSClassDefinition* JSCalendarTask::getClassInfo()
205 {
206     return &m_classInfo;
207 }
208
209 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
210 {
211     CalendarTaskPrivObject *priv =
212         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
213     if (!priv) {
214         ThrowMsg(NullPointerException, "Private object is null");
215     }
216     CalendarEventPtr result = priv->getObject();
217     if (!result) {
218         ThrowMsg(NullPointerException, "Private object is null");
219     }
220     return result;
221 }
222
223 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
224         JSContextRef ctx,
225         const JSObjectRef object)
226 {
227     Try
228     {
229         CalendarTaskPrivObject *priv =
230             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
231         delete priv;
232         priv = new CalendarTaskPrivObject(ctx, event);
233         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
234             delete priv;
235         }
236     }
237     Catch(Exception)
238     {
239         LoggerE("Error during replacing task object");
240     }
241 }
242
243 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
244         JSObjectRef object,
245         JSStringRef propertyName,
246         JSValueRef* exception)
247 {
248     Try
249     {
250         CalendarTaskPrivObject *privateObject =
251             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
252         CalendarEventPtr task = privateObject->getObject();
253
254         if (task->getUId()==UNDEFINED_ID) {
255             return JSValueMakeNull(context);
256         } else {
257             return Converter(context).toJSValueRef(task->getUId());
258         }
259     }
260     Catch(Exception)
261     {
262                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
263     }
264     return JSValueMakeUndefined(context);
265 }
266
267 }
268 }