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