2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
36 using namespace TizenApis::Api::Calendar;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
45 JSClassDefinition JSCalendarTask::m_classInfo = {
47 kJSClassAttributeNone,
48 TIZEN_INTERFACE_CALENDAR_TASK,
49 JSCalendarItemProperties::getClassRef(),
57 NULL, //DeleteProperty,
58 NULL, //GetPropertyNames,
59 NULL, //CallAsFunction,
65 JSStaticValue JSCalendarTask::m_property[] = {
67 { TIZEN_CALENDAR_TASK_ID, getPropertyId,
68 NULL, kJSPropertyAttributeReadOnly },
69 { TIZEN_CALENDAR_TASK_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
70 NULL, kJSPropertyAttributeReadOnly },
75 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
77 ICalendarPtr JSCalendarTask::m_calendar = CalendarFactory::getInstance().createCalendarObject();
79 void JSCalendarTask::initialize(JSContextRef context,
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))) {
90 LogDebug("Private object alrerady set.");
94 m_calendar->setType(CalendarEvent::TASK_TYPE);
95 LogDebug("Calendar object type is set to task.");
99 void JSCalendarTask::finalize(JSObjectRef object)
102 CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
105 JSObjectSetPrivate(object, NULL);
109 JSObjectRef JSCalendarTask::constructor(JSContextRef context,
110 JSObjectRef constructor,
111 size_t argumentCount,
112 const JSValueRef arguments[],
113 JSValueRef* exception)
119 CalendarConverter converter(context);
120 CalendarEventPtr task;
122 if (argumentCount==0) {
123 CalendarEventPtr result(new CalendarEvent());
125 } else if (argumentCount==1) {
126 LogInfo("taskInitDict case");
127 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
128 CalendarEventPtr result(new CalendarEvent());
130 } else if (JSValueIsObject(context, arguments[0])) {
131 task = converter.toEvent(arguments[0]);
133 ThrowMsg(ConversionException, "Parameter conversion failed.");
136 ThrowMsg(ConversionException, "Parameter conversion failed.");
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]));
145 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
146 dplEvent->setEventString(eventStr);
147 dplEvent->setFormat(format);
148 dplEvent->setForSynchronousCall();
149 m_calendar->createEventFromString(dplEvent);
151 // Process the returned object.
152 if (dplEvent->getResult()) {
153 LogInfo("Successfully created a task.");
154 task = dplEvent->getEvent();
156 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
157 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
159 ThrowMsg(UnknownException, "Converting string failed.");
164 task->setCalendarType(CalendarEvent::TASK_TYPE);
165 return createJSCalendarTask(context, task);
167 Catch(UnsupportedException)
169 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
170 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
172 Catch(InvalidArgumentException)
174 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
175 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
177 Catch(ConversionException)
179 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
180 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
184 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
185 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
191 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
193 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
194 return JSObjectMake(context, getClassRef(), priv);
197 const JSClassRef JSCalendarTask::getClassRef()
200 m_jsClassRef = JSClassCreate(&m_classInfo);
205 const JSClassDefinition* JSCalendarTask::getClassInfo()
210 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
213 CalendarTaskPrivObject *priv =
214 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
216 ThrowMsg(NullPointerException, "Private object is null");
218 CalendarEventPtr result = priv->getObject();
220 ThrowMsg(NullPointerException, "Private object is null");
225 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
227 const JSObjectRef object)
232 CalendarTaskPrivObject *priv =
233 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
235 priv = new CalendarTaskPrivObject(ctx, event);
236 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
242 LogError("Error during replacing task object");
246 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
248 JSStringRef propertyName,
249 JSValueRef* exception)
254 CalendarTaskPrivObject *privateObject =
255 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
256 CalendarEventPtr task = privateObject->getObject();
258 if (task->getUId()==UNDEFINED_ID) {
259 return JSValueMakeUndefined(context);
261 return Converter(context).toJSValueRef(task->getUId());
266 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
268 return JSValueMakeUndefined(context);
271 JSValueRef JSCalendarTask::getPropertyLastModificationDate(JSContextRef context,
273 JSStringRef propertyName,
274 JSValueRef* exception)
279 CalendarTaskPrivObject *privateObject =
280 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
281 CalendarEventPtr task = privateObject->getObject();
283 ThrowMsg(NullPointerException, "Task object is NULL.");
286 if (UNDEFINED_TIME==task->getLastModifiedDate()) {
287 return JSValueMakeNull(context);
289 TimeUtilConverter timeConverter(context);
290 return timeConverter.toJSValueRefTZDate((double)(task->getLastModifiedDate()*1000.0), task->getTimeZone());
295 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
297 return JSValueMakeUndefined(context);
300 bool JSCalendarTask::validate(JSContextRef ctx,
301 const JSObjectRef object,
302 JSValueRef* exception)
305 CalendarTaskPrivObject *priv =
306 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
310 CalendarEventPtr event = priv->getObject();
314 return event->validate();