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,
60 NULL, //CallAsConstructor,
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 void JSCalendarTask::initialize(JSContextRef context,
81 CalendarTaskPrivObject *priv =
82 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
84 CalendarEventPtr privateData(new CalendarEvent());
85 priv = new CalendarTaskPrivObject(context, privateData);
86 JSObjectSetPrivate(object, static_cast<void*>(priv));
87 LogDebug("new event is created");
89 LogDebug("private object already exists");
93 void JSCalendarTask::finalize(JSObjectRef object)
98 JSObjectRef JSCalendarTask::constructor(JSContextRef context,
99 JSObjectRef constructor,
100 size_t argumentCount,
101 const JSValueRef arguments[],
102 JSValueRef* exception)
108 CalendarTaskPrivObject* privateObject = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(constructor));
109 JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
111 CalendarConverter converter(globalContext);
112 CalendarEventPtr task;
114 if (argumentCount==0) {
115 CalendarEventPtr result(new CalendarEvent());
117 } else if (argumentCount==1) { // eventInitDict case
118 task = converter.toEvent(arguments[0]);
120 ThrowMsg(ConversionException, "Parameter conversion failed.");
122 } else if (argumentCount==2) { // stringRepresentation case
123 if (!JSValueIsString(context, arguments[0]) || !JSValueIsString(context, arguments[1])) {
124 ThrowMsg(ConversionException, "Wrong parameter type.");
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]));
132 ICalendarPtr calendar = CalendarFactory::getInstance().createCalendarObject();
133 calendar->setType(CalendarEvent::TASK_TYPE);
135 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
136 dplEvent->setEventString(eventStr);
137 dplEvent->setFormat(format);
138 dplEvent->setForSynchronousCall();
139 calendar->createEventFromString(dplEvent);
141 // Process the returned object.
142 if (dplEvent->getResult()) {
143 LogInfo("Successfully created an item.");
144 task = dplEvent->getEvent();
146 ThrowMsg(UnknownException, "Converting from string failed by unkown reason.");
149 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
152 return createJSCalendarTask(globalContext, task);
154 Catch(UnsupportedException)
156 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
157 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
159 Catch(InvalidArgumentException)
161 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
162 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
164 Catch(ConversionException)
166 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
167 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
171 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
172 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
178 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
180 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
181 return JSObjectMake(context, getClassRef(), priv);
184 const JSClassRef JSCalendarTask::getClassRef()
187 m_jsClassRef = JSClassCreate(&m_classInfo);
192 const JSClassDefinition* JSCalendarTask::getClassInfo()
197 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
200 CalendarTaskPrivObject *priv =
201 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
203 ThrowMsg(NullPointerException, "Private object is null");
205 CalendarEventPtr result = priv->getObject();
207 ThrowMsg(NullPointerException, "Private object is null");
212 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
214 const JSObjectRef object)
219 CalendarTaskPrivObject *priv =
220 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
222 priv = new CalendarTaskPrivObject(ctx, event);
223 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
229 LogError("Error during replacing event object");
233 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
235 JSStringRef propertyName,
236 JSValueRef* exception)
241 CalendarTaskPrivObject *privateObject =
242 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
243 CalendarEventPtr task = privateObject->getObject();
245 return Converter(context).toJSValueRef(task->getUId());
249 LogWarning("trying to get incorrect value");
251 return JSValueMakeUndefined(context);
254 JSValueRef JSCalendarTask::getPropertyLastModificationDate(JSContextRef context,
256 JSStringRef propertyName,
257 JSValueRef* exception)
262 CalendarTaskPrivObject *privateObject =
263 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
264 CalendarEventPtr task = privateObject->getObject();
267 Throw(NullPointerException);
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());
273 return JSValueMakeUndefined(context);
278 LogWarning("trying to get incorrect value");
280 return JSValueMakeUndefined(context);
283 bool JSCalendarTask::validate(JSContextRef ctx,
284 const JSObjectRef object,
285 JSValueRef* exception)
288 CalendarTaskPrivObject *priv =
289 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
293 CalendarEventPtr event = priv->getObject();
297 return event->validate();