Update change log and spec for wrt-plugins-tizen_0.4.57
[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     { TIZEN_CALENDAR_TASK_COMPLETED_DATE, getPropertyCompletedDate, setPropertyCompletedDate, kJSPropertyAttributeNone | kJSPropertyAttributeDontDelete },
70     { TIZEN_CALENDAR_TASK_PROGRESS, getPropertyProgress, setPropertyProgress, kJSPropertyAttributeNone | kJSPropertyAttributeDontDelete },
71
72     { 0, 0, 0, 0 }
73 };
74
75 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
76
77 ICalendarPtr JSCalendarTask::m_calendar;
78
79 void JSCalendarTask::initialize(JSContextRef context,
80         JSObjectRef object)
81 {
82     if (!JSObjectGetPrivate(object)) {
83         LoggerD("Create calendar task private object.");
84         CalendarEventPtr task( new CalendarEvent() );
85         CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
86         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
87             delete priv;
88         }
89     } else {
90         LoggerD("Private object already set.");
91     }
92
93         if (NULL==m_calendar) {
94                 m_calendar = CalendarFactory::getInstance().createCalendarObject();
95                 m_calendar->setType(CalendarEvent::TASK_TYPE);
96                 LoggerD("Static calendar for task created.");
97         }
98 }
99
100 void JSCalendarTask::finalize(JSObjectRef object)
101 {
102     CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
103     if (priv) {
104         delete priv;
105         JSObjectSetPrivate(object, NULL);
106     }
107 }
108
109 JSObjectRef DLL_EXPORT JSCalendarTask::constructor(JSContextRef context,
110     JSObjectRef constructor,
111     size_t argumentCount,
112     const JSValueRef arguments[],
113     JSValueRef* exception)
114 {
115     Try
116     {
117                 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
118         CalendarConverter converter(globalContext);
119         CalendarEventPtr task;
120
121         if (argumentCount==0) {
122             CalendarEventPtr result(new CalendarEvent());
123             task = result;
124         } else if (argumentCount==1) {
125             LoggerI("taskInitDict case");
126             if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
127                 CalendarEventPtr result(new CalendarEvent());
128                 task = result;
129             } else if (JSValueIsObject(context, arguments[0])) {
130                 task = converter.toItem(arguments[0], false);
131                 if (!task) {
132                     ThrowMsg(ConversionException, "Parameter conversion failed.");
133                 }
134             } else {
135                 ThrowMsg(ConversionException, "Parameter conversion failed.");
136             }
137         } else if (argumentCount>=2) {
138             LoggerI("task stringRepresentation case");
139             std::string eventStr;
140             CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
141             eventStr = converter.toString(arguments[0]);
142             format = converter.toVObjectFormat(converter.toString(arguments[1]));
143
144                         if (NULL==m_calendar) {
145                                 m_calendar = CalendarFactory::getInstance().createCalendarObject();
146                                 m_calendar->setType(CalendarEvent::TASK_TYPE);
147                                 LoggerD("Static calendar for task created.");
148                         }
149
150             IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
151             dplEvent->setEventString(eventStr);
152             dplEvent->setFormat(format);
153             dplEvent->setForSynchronousCall();
154             m_calendar->createEventFromString(dplEvent);
155
156             // Process the returned object.
157             if (dplEvent->getResult()) {
158                 LoggerI("Successfully created a task.");
159                 task = dplEvent->getEvent();
160             } else {
161                 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
162                     ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
163                 } else {
164                     ThrowMsg(UnknownException, "Converting string failed.");
165                 }
166             }
167         }
168
169         task->setCalendarType(CalendarEvent::TASK_TYPE);
170         return createJSCalendarTask(context, task);
171     }
172     Catch(UnsupportedException)
173     {
174         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
175         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
176     }
177     Catch(InvalidArgumentException)
178     {
179         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
180         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
181     }
182     Catch(ConversionException)
183     {
184         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
185         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
186     }
187     Catch(Exception)
188     {
189         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
190         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
191     }
192 }
193
194 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
195 {
196     CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
197     return JSObjectMake(context, getClassRef(), priv);
198 }
199
200 const JSClassRef DLL_EXPORT JSCalendarTask::getClassRef()
201 {
202     if (!m_jsClassRef) {
203         m_jsClassRef = JSClassCreate(&m_classInfo);
204     }
205     return m_jsClassRef;
206 }
207
208 const JSClassDefinition* JSCalendarTask::getClassInfo()
209 {
210     return &m_classInfo;
211 }
212
213 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
214 {
215     CalendarTaskPrivObject *priv =
216         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
217     if (!priv) {
218         ThrowMsg(NullPointerException, "Private object is null");
219     }
220     CalendarEventPtr result = priv->getObject();
221     if (!result) {
222         ThrowMsg(NullPointerException, "Private object is null");
223     }
224     return result;
225 }
226
227 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
228         JSContextRef ctx,
229         const JSObjectRef object)
230 {
231     Try
232     {
233         CalendarTaskPrivObject *priv =
234             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
235         delete priv;
236         priv = new CalendarTaskPrivObject(ctx, event);
237         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
238             delete priv;
239         }
240     }
241     Catch(Exception)
242     {
243         LoggerE("Error during replacing task object");
244     }
245 }
246
247 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
248         JSObjectRef object,
249         JSStringRef propertyName,
250         JSValueRef* exception)
251 {
252     Try
253     {
254         CalendarTaskPrivObject *privateObject =
255             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
256         CalendarEventPtr task = privateObject->getObject();
257
258         if (task->getUId()==UNDEFINED_ID) {
259             return JSValueMakeNull(context);
260         } else {
261             return Converter(context).toJSValueRef(task->getUId());
262         }
263     }
264     Catch(Exception)
265     {
266                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
267     }
268     return JSValueMakeUndefined(context);
269 }
270
271 JSValueRef JSCalendarTask::getPropertyCompletedDate(JSContextRef context,
272         JSObjectRef object,
273         JSStringRef propertyName,
274         JSValueRef* exception)
275 {
276     Try
277     {
278         CalendarTaskPrivObject *privateObject =
279             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
280         CalendarEventPtr task = privateObject->getObject();
281         if (!task) {
282             ThrowMsg(NullPointerException, "Task object is NULL.");
283         }
284
285         if (UNDEFINED_TIME==task->getCompletedDate()) {
286             return JSValueMakeUndefined(context);
287         } else {
288             TimeUtilConverter timeConverter(context);
289             return timeConverter.toJSValueRefTZDate((double)(task->getCompletedDate()*1000.0), task->getTimeZone());
290         }
291     }
292     Catch(Exception)
293     {
294                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
295     }
296     return JSValueMakeUndefined(context);
297 }
298
299 bool JSCalendarTask::setPropertyCompletedDate(JSContextRef context,
300         JSObjectRef object,
301         JSStringRef propertyName,
302         JSValueRef value,
303         JSValueRef* exception)
304 {
305     Try
306     {
307         CalendarEventPtr task = getPrivateObject(object);
308         if (!task) {
309             ThrowMsg(NullPointerException, "Task object is NULL.");
310         }
311
312         if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
313             ThrowMsg(ConversionException, "Wrong parameter type.");
314         }
315
316         TimeUtilConverter converter(context);
317         long long int completedDate = (long long int) (converter.getTimeInMilliseconds(value)/1000);
318
319         task->setCompletedDate(completedDate);
320
321         if( task->getTimeZone().empty() ) {
322             std::string timeZone = converter.getPropertiesInTZDate(value).timezone;
323             task->setTimeZone(timeZone);
324         }
325         return true;
326     }
327     Catch(Exception)
328     {
329                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
330     }
331
332     return true;
333 }
334
335 JSValueRef JSCalendarTask::getPropertyProgress(JSContextRef context,
336         JSObjectRef object,
337         JSStringRef propertyName,
338         JSValueRef* exception)
339 {
340     Try
341     {
342         CalendarEventPtr task = getPrivateObject(object);
343         Converter converter(context);
344         return converter.toJSValueRef(task->getProgress());
345     }
346     Catch(Exception)
347     {
348                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
349     }
350     return JSValueMakeUndefined(context);
351 }
352
353 bool JSCalendarTask::setPropertyProgress(JSContextRef context,
354         JSObjectRef object,
355         JSStringRef propertyName,
356         JSValueRef value,
357         JSValueRef* exception)
358 {
359     Try
360     {
361         CalendarEventPtr task = getPrivateObject(object);
362         Converter converter(context);
363         int progress = converter.toInt(value);
364         task->setProgress(progress);
365         return true;
366     }
367     Catch(Exception)
368     {
369                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
370     }
371
372     return true;
373 }
374 }
375 }