221548e88d593417fd21142833df206ac8428688
[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 <JSTizenExceptionFactory.h>
23 #include "EventId.h"
24 #include "ICalendar.h"
25 #include "CalendarFactory.h"
26 #include "JSCalendarTask.h"
27 #include "JSCalendarItemProperties.h"
28 #include "CalendarConverter.h"
29 #include "JSCalendarEventId.h"
30 #include <TimeUtilConverter.h>
31 #include <JSTZDate.h>
32 #include <JSTizenException.h>
33 #include <JSTizenExceptionFactory.h>
34 #include <GlobalContextManager.h>
35 #include <Logger.h>
36 #include <Export.h>
37
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace DeviceAPI::Common;
41 using namespace DeviceAPI::Time;
42
43 namespace DeviceAPI {
44 namespace Calendar {
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 = CalendarFactory::getInstance().createCalendarObject();
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 (m_calendar) {
92         m_calendar->setType(CalendarEvent::TASK_TYPE);
93         LoggerD("Calendar object type is set to task.");
94     }
95 }
96
97 void JSCalendarTask::finalize(JSObjectRef object)
98 {
99     CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
100     if (priv) {
101         delete priv;
102         JSObjectSetPrivate(object, NULL);
103     }
104 }
105
106 JSObjectRef DLL_EXPORT JSCalendarTask::constructor(JSContextRef context,
107     JSObjectRef constructor,
108     size_t argumentCount,
109     const JSValueRef arguments[],
110     JSValueRef* exception)
111 {
112     Try
113     {
114                 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
115         CalendarConverter converter(globalContext);
116         CalendarEventPtr task;
117
118         if (argumentCount==0) {
119             CalendarEventPtr result(new CalendarEvent());
120             task = result;
121         } else if (argumentCount==1) {
122             LoggerI("taskInitDict case");
123             if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
124                 CalendarEventPtr result(new CalendarEvent());
125                 task = result;
126             } else if (JSValueIsObject(context, arguments[0])) {
127                 task = converter.toItem(arguments[0]);
128                 if (!task) {
129                     ThrowMsg(ConversionException, "Parameter conversion failed.");
130                 }
131             } else {
132                 ThrowMsg(ConversionException, "Parameter conversion failed.");
133             }
134         } else if (argumentCount>=2) {
135             LoggerI("task stringRepresentation case");
136             std::string eventStr;
137             CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
138             eventStr = converter.toString(arguments[0]);
139             format = converter.toVObjectFormat(converter.toString(arguments[1]));
140
141             IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
142             dplEvent->setEventString(eventStr);
143             dplEvent->setFormat(format);
144             dplEvent->setForSynchronousCall();
145             m_calendar->createEventFromString(dplEvent);
146
147             // Process the returned object.
148             if (dplEvent->getResult()) {
149                 LoggerI("Successfully created a task.");
150                 task = dplEvent->getEvent();
151             } else {
152                 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
153                     ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
154                 } else {
155                     ThrowMsg(UnknownException, "Converting string failed.");
156                 }
157             }
158         }
159
160         task->setCalendarType(CalendarEvent::TASK_TYPE);
161         return createJSCalendarTask(context, task);
162     }
163     Catch(UnsupportedException)
164     {
165         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
166                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
167         *exception = error;
168                 return error;
169     }
170     Catch(InvalidArgumentException)
171     {
172         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
173                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
174         *exception = error;
175                 return error;
176     }
177     Catch(ConversionException)
178     {
179         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
180                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
181         *exception = error;
182                 return error;
183     }
184     Catch(Exception)
185     {
186         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
187                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
188         *exception = error;
189                 return error;
190     }
191 }
192
193 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
194 {
195     CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
196     return JSObjectMake(context, getClassRef(), priv);
197 }
198
199 const JSClassRef DLL_EXPORT JSCalendarTask::getClassRef()
200 {
201     if (!m_jsClassRef) {
202         m_jsClassRef = JSClassCreate(&m_classInfo);
203     }
204     return m_jsClassRef;
205 }
206
207 const JSClassDefinition* JSCalendarTask::getClassInfo()
208 {
209     return &m_classInfo;
210 }
211
212 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
213 {
214     CalendarTaskPrivObject *priv =
215         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
216     if (!priv) {
217         ThrowMsg(NullPointerException, "Private object is null");
218     }
219     CalendarEventPtr result = priv->getObject();
220     if (!result) {
221         ThrowMsg(NullPointerException, "Private object is null");
222     }
223     return result;
224 }
225
226 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
227         JSContextRef ctx,
228         const JSObjectRef object)
229 {
230     Try
231     {
232         CalendarTaskPrivObject *priv =
233             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
234         delete priv;
235         priv = new CalendarTaskPrivObject(ctx, event);
236         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
237             delete priv;
238         }
239     }
240     Catch(Exception)
241     {
242         LoggerE("Error during replacing task object");
243     }
244 }
245
246 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
247         JSObjectRef object,
248         JSStringRef propertyName,
249         JSValueRef* exception)
250 {
251     Try
252     {
253         CalendarTaskPrivObject *privateObject =
254             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
255         CalendarEventPtr task = privateObject->getObject();
256
257         if (task->getUId()==UNDEFINED_ID) {
258             return JSValueMakeNull(context);
259         } else {
260             return Converter(context).toJSValueRef(task->getUId());
261         }
262     }
263     Catch(Exception)
264     {
265                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
266     }
267     return JSValueMakeUndefined(context);
268 }
269
270 }
271 }