2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.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>
31 #include <JSWebAPIErrorFactory.h>
32 #include <GlobalContextManager.h>
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace DeviceAPI::Common;
39 using namespace DeviceAPI::Time;
44 using WrtDeviceApis::Commons::UnknownException;
46 JSClassDefinition JSCalendarTask::m_classInfo = {
48 kJSClassAttributeNone,
49 TIZEN_INTERFACE_CALENDAR_TASK,
50 JSCalendarItemProperties::getClassRef(),
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
66 JSStaticValue JSCalendarTask::m_property[] = {
68 { TIZEN_CALENDAR_TASK_ID, getPropertyId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
73 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
75 ICalendarPtr JSCalendarTask::m_calendar;
77 void JSCalendarTask::initialize(JSContextRef context,
80 if (!JSObjectGetPrivate(object)) {
81 LoggerD("Create calendar task private object.");
82 CalendarEventPtr task( new CalendarEvent() );
83 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
84 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
88 LoggerD("Private object already set.");
91 if (NULL==m_calendar) {
92 m_calendar = CalendarFactory::getInstance().createCalendarObject();
93 LoggerD("Static calendar for task created.");
97 m_calendar->setType(CalendarEvent::TASK_TYPE);
98 LoggerD("Calendar object type is set to task.");
102 void JSCalendarTask::finalize(JSObjectRef object)
104 CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
107 JSObjectSetPrivate(object, NULL);
111 JSObjectRef DLL_EXPORT JSCalendarTask::constructor(JSContextRef context,
112 JSObjectRef constructor,
113 size_t argumentCount,
114 const JSValueRef arguments[],
115 JSValueRef* exception)
119 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
120 CalendarConverter converter(globalContext);
121 CalendarEventPtr task;
123 if (argumentCount==0) {
124 CalendarEventPtr result(new CalendarEvent());
126 } else if (argumentCount==1) {
127 LoggerI("taskInitDict case");
128 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
129 CalendarEventPtr result(new CalendarEvent());
131 } else if (JSValueIsObject(context, arguments[0])) {
132 task = converter.toItem(arguments[0], false);
134 ThrowMsg(ConversionException, "Parameter conversion failed.");
137 ThrowMsg(ConversionException, "Parameter conversion failed.");
139 } else if (argumentCount>=2) {
140 LoggerI("task stringRepresentation case");
141 std::string eventStr;
142 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
143 eventStr = converter.toString(arguments[0]);
144 format = converter.toVObjectFormat(converter.toString(arguments[1]));
146 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
147 dplEvent->setEventString(eventStr);
148 dplEvent->setFormat(format);
149 dplEvent->setForSynchronousCall();
150 m_calendar->createEventFromString(dplEvent);
152 // Process the returned object.
153 if (dplEvent->getResult()) {
154 LoggerI("Successfully created a task.");
155 task = dplEvent->getEvent();
157 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
158 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
160 ThrowMsg(UnknownException, "Converting string failed.");
165 task->setCalendarType(CalendarEvent::TASK_TYPE);
166 return createJSCalendarTask(context, task);
168 Catch(UnsupportedException)
170 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
173 Catch(InvalidArgumentException)
175 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
178 Catch(ConversionException)
180 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
185 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
186 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
190 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
192 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
193 return JSObjectMake(context, getClassRef(), priv);
196 const JSClassRef DLL_EXPORT JSCalendarTask::getClassRef()
199 m_jsClassRef = JSClassCreate(&m_classInfo);
204 const JSClassDefinition* JSCalendarTask::getClassInfo()
209 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
211 CalendarTaskPrivObject *priv =
212 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
214 ThrowMsg(NullPointerException, "Private object is null");
216 CalendarEventPtr result = priv->getObject();
218 ThrowMsg(NullPointerException, "Private object is null");
223 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
225 const JSObjectRef object)
229 CalendarTaskPrivObject *priv =
230 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
232 priv = new CalendarTaskPrivObject(ctx, event);
233 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
239 LoggerE("Error during replacing task object");
243 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
245 JSStringRef propertyName,
246 JSValueRef* exception)
250 CalendarTaskPrivObject *privateObject =
251 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
252 CalendarEventPtr task = privateObject->getObject();
254 if (task->getUId()==UNDEFINED_ID) {
255 return JSValueMakeNull(context);
257 return Converter(context).toJSValueRef(task->getUId());
262 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
264 return JSValueMakeUndefined(context);