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>
22 #include <JSTizenExceptionFactory.h>
24 #include "ICalendar.h"
25 #include "CalendarFactory.h"
26 #include "JSCalendarTask.h"
27 #include "JSCalendarItemProperties.h"
28 #include "CalendarConverter.h"
29 #include "JSCalendarEventId.h"
30 #include <TimeUtilConverter.h>
32 #include <JSTizenException.h>
33 #include <JSTizenExceptionFactory.h>
34 #include <GlobalContextManager.h>
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace DeviceAPI::Common;
41 using namespace DeviceAPI::Time;
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 = CalendarFactory::getInstance().createCalendarObject();
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.");
92 m_calendar->setType(CalendarEvent::TASK_TYPE);
93 LoggerD("Calendar object type is set to task.");
97 void JSCalendarTask::finalize(JSObjectRef object)
99 CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
102 JSObjectSetPrivate(object, NULL);
106 JSObjectRef DLL_EXPORT JSCalendarTask::constructor(JSContextRef context,
107 JSObjectRef constructor,
108 size_t argumentCount,
109 const JSValueRef arguments[],
110 JSValueRef* exception)
114 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
115 CalendarConverter converter(globalContext);
116 CalendarEventPtr task;
118 if (argumentCount==0) {
119 CalendarEventPtr result(new CalendarEvent());
121 } else if (argumentCount==1) {
122 LoggerI("taskInitDict case");
123 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
124 CalendarEventPtr result(new CalendarEvent());
126 } else if (JSValueIsObject(context, arguments[0])) {
127 task = converter.toItem(arguments[0]);
129 ThrowMsg(ConversionException, "Parameter conversion failed.");
132 ThrowMsg(ConversionException, "Parameter conversion failed.");
134 } else if (argumentCount>=2) {
135 LoggerI("task stringRepresentation case");
136 std::string eventStr;
137 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
138 eventStr = converter.toString(arguments[0]);
139 format = converter.toVObjectFormat(converter.toString(arguments[1]));
141 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
142 dplEvent->setEventString(eventStr);
143 dplEvent->setFormat(format);
144 dplEvent->setForSynchronousCall();
145 m_calendar->createEventFromString(dplEvent);
147 // Process the returned object.
148 if (dplEvent->getResult()) {
149 LoggerI("Successfully created a task.");
150 task = dplEvent->getEvent();
152 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
153 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
155 ThrowMsg(UnknownException, "Converting string failed.");
160 task->setCalendarType(CalendarEvent::TASK_TYPE);
161 return createJSCalendarTask(context, task);
163 Catch(UnsupportedException)
165 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
166 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
170 Catch(InvalidArgumentException)
172 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
173 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
177 Catch(ConversionException)
179 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
180 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
186 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
187 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
193 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
195 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
196 return JSObjectMake(context, getClassRef(), priv);
199 const JSClassRef DLL_EXPORT JSCalendarTask::getClassRef()
202 m_jsClassRef = JSClassCreate(&m_classInfo);
207 const JSClassDefinition* JSCalendarTask::getClassInfo()
212 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
214 CalendarTaskPrivObject *priv =
215 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
217 ThrowMsg(NullPointerException, "Private object is null");
219 CalendarEventPtr result = priv->getObject();
221 ThrowMsg(NullPointerException, "Private object is null");
226 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
228 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 LoggerE("Error during replacing task object");
246 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
248 JSStringRef propertyName,
249 JSValueRef* exception)
253 CalendarTaskPrivObject *privateObject =
254 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
255 CalendarEventPtr task = privateObject->getObject();
257 if (task->getUId()==UNDEFINED_ID) {
258 return JSValueMakeNull(context);
260 return Converter(context).toJSValueRef(task->getUId());
265 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
267 return JSValueMakeUndefined(context);