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