Add DRAFT stubs for Vehicle plugin
[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     constructor,
61     NULL, //HasInstance,
62     NULL  //ConvertToType
63 };
64
65 JSStaticValue JSCalendarTask::m_property[] = {
66     //Task properties
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 ICalendarPtr JSCalendarTask::m_calendar = CalendarFactory::getInstance().createCalendarObject();
78
79 void JSCalendarTask::initialize(JSContextRef context,
80         JSObjectRef object)
81 {
82     if (!JSObjectGetPrivate(object)) {
83         LogDebug("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         LogDebug("Private object alrerady set.");
91     }
92
93     if (m_calendar) {
94         m_calendar->setType(CalendarEvent::TASK_TYPE);
95         LogDebug("Calendar object type is set to task.");
96     }
97 }
98
99 void JSCalendarTask::finalize(JSObjectRef object)
100 {
101     LogDebug("entered");
102     CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
103     if (priv) {
104         delete priv;
105         JSObjectSetPrivate(object, NULL);
106     }
107 }
108
109 JSObjectRef JSCalendarTask::constructor(JSContextRef context,
110     JSObjectRef constructor,
111     size_t argumentCount,
112     const JSValueRef arguments[],
113     JSValueRef* exception)
114 {
115         LogDebug("entered");
116
117     Try
118     {
119         CalendarConverter converter(context);
120         CalendarEventPtr task;
121
122         if (argumentCount==0) {
123             CalendarEventPtr result(new CalendarEvent());
124             task = result;
125         } else if (argumentCount==1) {
126             LogInfo("taskInitDict case");
127             if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
128                 CalendarEventPtr result(new CalendarEvent());
129                 task = result;
130             } else if (JSValueIsObject(context, arguments[0])) {
131                 task = converter.toEvent(arguments[0]);
132                 if (!task) {
133                     ThrowMsg(ConversionException, "Parameter conversion failed.");
134                 }
135             } else {
136                 ThrowMsg(ConversionException, "Parameter conversion failed.");
137             }
138         } else if (argumentCount>=2) {
139             LogInfo("task stringRepresentation case");
140             std::string eventStr;
141             CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
142             eventStr = converter.toString(arguments[0]);
143             format = converter.toVObjectFormat(converter.toString(arguments[1]));
144
145             IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
146             dplEvent->setEventString(eventStr);
147             dplEvent->setFormat(format);
148             dplEvent->setForSynchronousCall();
149             m_calendar->createEventFromString(dplEvent);
150
151             // Process the returned object.
152             if (dplEvent->getResult()) {
153                 LogInfo("Successfully created a task.");
154                 task = dplEvent->getEvent();
155             } else {
156                 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
157                     ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
158                 } else {
159                     ThrowMsg(UnknownException, "Converting string failed.");
160                 }
161             }
162         }
163
164         task->setCalendarType(CalendarEvent::TASK_TYPE);
165         return createJSCalendarTask(context, task);
166     }
167     Catch(UnsupportedException)
168     {
169         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
170         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
171     }
172     Catch(InvalidArgumentException)
173     {
174         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
175         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
176     }
177     Catch(ConversionException)
178     {
179         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
180         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
181     }
182     Catch(Exception)
183     {
184         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
185         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
186     }
187
188     return NULL;
189 }
190
191 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
192 {
193     CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
194     return JSObjectMake(context, getClassRef(), priv);
195 }
196
197 const JSClassRef JSCalendarTask::getClassRef()
198 {
199     if (!m_jsClassRef) {
200         m_jsClassRef = JSClassCreate(&m_classInfo);
201     }
202     return m_jsClassRef;
203 }
204
205 const JSClassDefinition* JSCalendarTask::getClassInfo()
206 {
207     return &m_classInfo;
208 }
209
210 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
211 {
212     LogDebug("entered");
213     CalendarTaskPrivObject *priv =
214         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
215     if (!priv) {
216         ThrowMsg(NullPointerException, "Private object is null");
217     }
218     CalendarEventPtr result = priv->getObject();
219     if (!result) {
220         ThrowMsg(NullPointerException, "Private object is null");
221     }
222     return result;
223 }
224
225 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
226         JSContextRef ctx,
227         const JSObjectRef object)
228 {
229     LogDebug("entered");
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         LogError("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     LogDebug("entered");
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 JSValueMakeUndefined(context);
260         } else {
261             return Converter(context).toJSValueRef(task->getUId());
262         }
263     }
264     Catch(Exception)
265     {
266                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
267     }
268     return JSValueMakeUndefined(context);
269 }
270
271 JSValueRef JSCalendarTask::getPropertyLastModificationDate(JSContextRef context,
272         JSObjectRef object,
273         JSStringRef propertyName,
274         JSValueRef* exception)
275 {
276     LogDebug("entered");
277     Try
278     {
279         CalendarTaskPrivObject *privateObject =
280             static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
281         CalendarEventPtr task = privateObject->getObject();
282         if (!task) {
283             ThrowMsg(NullPointerException, "Task object is NULL.");
284         }
285
286         if (UNDEFINED_TIME==task->getLastModifiedDate()) {
287             return JSValueMakeNull(context);
288         } else {
289             TimeUtilConverter timeConverter(context);
290             return timeConverter.toJSValueRefTZDate((double)(task->getLastModifiedDate()*1000.0), task->getTimeZone());
291         }
292     }
293     Catch(Exception)
294     {
295                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
296     }
297     return JSValueMakeUndefined(context);
298 }
299
300 bool JSCalendarTask::validate(JSContextRef ctx,
301         const JSObjectRef object,
302         JSValueRef* exception)
303 {
304     LogDebug("entered");
305     CalendarTaskPrivObject *priv =
306         static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
307     if (priv == NULL) {
308         return false;
309     }
310     CalendarEventPtr event = priv->getObject();
311     if (!event) {
312         return false;
313     }
314     return event->validate();
315 }
316
317 }
318 }
319 }