Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarTask.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 <ctime>
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <API/Calendar/EventId.h>
25 #include <API/Calendar/ICalendar.h>
26 #include <API/Calendar/CalendarFactory.h>
27 #include "JSCalendarTask.h"
28 #include "JSCalendarItemProperties.h"
29 #include "CalendarConverter.h"
30 #include "JSCalendarEventId.h"
31 #include <Tizen/TimeUtil/TimeUtilConverter.h>
32 #include <Tizen/TimeUtil/JSTZDate.h>
33 #include <Tizen/Common/JSTizenException.h>
34 #include <Tizen/Common/JSTizenExceptionFactory.h>
35
36 using namespace TizenApis::Api::Calendar;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
40
41 namespace TizenApis {
42 namespace Tizen1_0 {
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     NULL, //CallAsConstructor,
61     NULL, //HasInstance,
62     NULL  //ConvertToType
63 };
64
65 JSStaticValue JSCalendarTask::m_property[] = {
66     //EventProperties
67     { TIZEN_CALENDAR_TASK_ID, getPropertyId,
68       NULL, kJSPropertyAttributeReadOnly },
69     { TIZEN_CALENDAR_TASK_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
70       NULL, kJSPropertyAttributeReadOnly },
71
72     { 0, 0, 0, 0 }
73 };
74
75 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
76
77 void JSCalendarTask::initialize(JSContextRef context,
78         JSObjectRef object)
79 {
80     LogDebug("entered");
81     CalendarTaskPrivObject *priv =
82         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
83     if (!priv) {
84         CalendarEventPtr privateData(new CalendarEvent());
85         priv = new CalendarTaskPrivObject(context, privateData);
86         JSObjectSetPrivate(object, static_cast<void*>(priv));
87         LogDebug("new event is created");
88     } else {
89         LogDebug("private object already exists");
90     }
91 }
92
93 void JSCalendarTask::finalize(JSObjectRef object)
94 {
95     LogDebug("entered");
96 }
97
98 JSObjectRef JSCalendarTask::constructor(JSContextRef context,
99     JSObjectRef constructor,
100     size_t argumentCount,
101     const JSValueRef arguments[],
102     JSValueRef* exception)
103 {
104         LogDebug("entered");
105
106     Try
107     {
108                 CalendarTaskPrivObject* privateObject = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(constructor));
109                 JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
110
111         CalendarConverter converter(globalContext);
112         CalendarEventPtr task;
113
114         if (argumentCount==0) {
115             CalendarEventPtr result(new CalendarEvent());
116             task = result;
117         } else if (argumentCount==1) { // eventInitDict case
118             task = converter.toEvent(arguments[0]);
119             if (!task) {
120                 ThrowMsg(ConversionException, "Parameter conversion failed.");
121             }
122         } else if (argumentCount==2) { // stringRepresentation case
123             if (!JSValueIsString(context, arguments[0]) || !JSValueIsString(context, arguments[1])) {
124                 ThrowMsg(ConversionException, "Wrong parameter type.");
125             }
126
127             std::string eventStr;
128             CalendarEvent::VObjectFormat format = CalendarEvent::ICALENDAR_20;
129             eventStr = converter.toString(arguments[0]);
130             format = converter.toVObjectFormat(converter.toString(arguments[1]));
131
132             ICalendarPtr calendar = CalendarFactory::getInstance().createCalendarObject();
133             calendar->setType(CalendarEvent::TASK_TYPE);
134
135             IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
136             dplEvent->setEventString(eventStr);
137             dplEvent->setFormat(format);
138             dplEvent->setForSynchronousCall();
139             calendar->createEventFromString(dplEvent);
140
141             // Process the returned object.
142             if (dplEvent->getResult()) {
143                 LogInfo("Successfully created an item.");
144                 task = dplEvent->getEvent();
145             } else {
146                 ThrowMsg(UnknownException, "Converting from string failed by unkown reason.");
147             }
148         } else {
149             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
150         }
151
152         return createJSCalendarTask(globalContext, task);
153     }
154     Catch(UnsupportedException)
155     {
156         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
157         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
158     }
159     Catch(InvalidArgumentException)
160     {
161         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
162         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
163     }
164     Catch(ConversionException)
165     {
166         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
167         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
168     }
169     Catch(Exception)
170     {
171         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
172         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
173     }
174
175     return NULL;
176 }
177
178 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
179 {
180     CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
181     return JSObjectMake(context, getClassRef(), priv);
182 }
183
184 const JSClassRef JSCalendarTask::getClassRef()
185 {
186     if (!m_jsClassRef) {
187         m_jsClassRef = JSClassCreate(&m_classInfo);
188     }
189     return m_jsClassRef;
190 }
191
192 const JSClassDefinition* JSCalendarTask::getClassInfo()
193 {
194     return &m_classInfo;
195 }
196
197 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
198 {
199     LogDebug("entered");
200     CalendarTaskPrivObject *priv =
201         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
202     if (!priv) {
203         ThrowMsg(NullPointerException, "Private object is null");
204     }
205     CalendarEventPtr result = priv->getObject();
206     if (!result) {
207         ThrowMsg(NullPointerException, "Private object is null");
208     }
209     return result;
210 }
211
212 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
213         JSContextRef ctx,
214         const JSObjectRef object)
215 {
216     LogDebug("entered");
217     Try
218     {
219         CalendarTaskPrivObject *priv =
220             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
221         delete priv;
222         priv = new CalendarTaskPrivObject(ctx, event);
223         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
224             delete priv;
225         }
226     }
227     Catch(Exception)
228     {
229         LogError("Error during replacing event object");
230     }
231 }
232
233 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
234         JSObjectRef object,
235         JSStringRef propertyName,
236         JSValueRef* exception)
237 {
238     LogDebug("entered");
239     Try
240     {
241         CalendarTaskPrivObject *privateObject =
242             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
243         CalendarEventPtr task = privateObject->getObject();
244
245         return Converter(context).toJSValueRef(task->getUId());
246     }
247     Catch(Exception)
248     {
249         LogWarning("trying to get incorrect value");
250     }
251     return JSValueMakeUndefined(context);
252 }
253
254 JSValueRef JSCalendarTask::getPropertyLastModificationDate(JSContextRef context,
255         JSObjectRef object,
256         JSStringRef propertyName,
257         JSValueRef* exception)
258 {
259     LogDebug("entered");
260     Try
261     {
262         CalendarTaskPrivObject *privateObject =
263             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
264         CalendarEventPtr task = privateObject->getObject();
265
266         if (!task) {
267             Throw(NullPointerException);
268         }
269         if (task->getLastModifiedDate() != 0) {
270             // Use the global context saved in the event struct.
271             return JSTZDate::createJSObject(privateObject->getContext(), task->getLastModifiedDate(), task->getTimeZone());
272         } else {
273             return JSValueMakeUndefined(context);
274         }
275     }
276     Catch(Exception)
277     {
278         LogWarning("trying to get incorrect value");
279     }
280     return JSValueMakeUndefined(context);
281 }
282
283 bool JSCalendarTask::validate(JSContextRef ctx,
284         const JSObjectRef object,
285         JSValueRef* exception)
286 {
287     LogDebug("entered");
288     CalendarTaskPrivObject *priv =
289         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
290     if (priv == NULL) {
291         return false;
292     }
293     CalendarEventPtr event = priv->getObject();
294     if (!event) {
295         return false;
296     }
297     return event->validate();
298 }
299
300 }
301 }
302 }