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>
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace DeviceAPI::Common;
40 using namespace DeviceAPI::Time;
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, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
72 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
74 ICalendarPtr JSCalendarTask::m_calendar = CalendarFactory::getInstance().createCalendarObject();
76 void JSCalendarTask::initialize(JSContextRef context,
79 if (!JSObjectGetPrivate(object)) {
80 LoggerD("Create calendar task private object.");
81 CalendarEventPtr task( new CalendarEvent() );
82 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
83 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
87 LoggerD("Private object already set.");
91 m_calendar->setType(CalendarEvent::TASK_TYPE);
92 LoggerD("Calendar object type is set to task.");
96 void JSCalendarTask::finalize(JSObjectRef object)
98 CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
101 JSObjectSetPrivate(object, NULL);
105 JSObjectRef JSCalendarTask::constructor(JSContextRef context,
106 JSObjectRef constructor,
107 size_t argumentCount,
108 const JSValueRef arguments[],
109 JSValueRef* exception)
113 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
114 CalendarConverter converter(globalContext);
115 CalendarEventPtr task;
117 if (argumentCount==0) {
118 CalendarEventPtr result(new CalendarEvent());
120 } else if (argumentCount==1) {
121 LoggerI("taskInitDict case");
122 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
123 CalendarEventPtr result(new CalendarEvent());
125 } else if (JSValueIsObject(context, arguments[0])) {
126 task = converter.toItem(arguments[0]);
128 ThrowMsg(ConversionException, "Parameter conversion failed.");
131 ThrowMsg(ConversionException, "Parameter conversion failed.");
133 } else if (argumentCount>=2) {
134 LoggerI("task stringRepresentation case");
135 std::string eventStr;
136 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
137 eventStr = converter.toString(arguments[0]);
138 format = converter.toVObjectFormat(converter.toString(arguments[1]));
140 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
141 dplEvent->setEventString(eventStr);
142 dplEvent->setFormat(format);
143 dplEvent->setForSynchronousCall();
144 m_calendar->createEventFromString(dplEvent);
146 // Process the returned object.
147 if (dplEvent->getResult()) {
148 LoggerI("Successfully created a task.");
149 task = dplEvent->getEvent();
151 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
152 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
154 ThrowMsg(UnknownException, "Converting string failed.");
159 task->setCalendarType(CalendarEvent::TASK_TYPE);
160 return createJSCalendarTask(context, task);
162 Catch(UnsupportedException)
164 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
165 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
167 Catch(InvalidArgumentException)
169 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
170 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
172 Catch(ConversionException)
174 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
175 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
179 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
180 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
186 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
188 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
189 return JSObjectMake(context, getClassRef(), priv);
192 const JSClassRef JSCalendarTask::getClassRef()
195 m_jsClassRef = JSClassCreate(&m_classInfo);
200 const JSClassDefinition* JSCalendarTask::getClassInfo()
205 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
207 CalendarTaskPrivObject *priv =
208 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
210 ThrowMsg(NullPointerException, "Private object is null");
212 CalendarEventPtr result = priv->getObject();
214 ThrowMsg(NullPointerException, "Private object is null");
219 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
221 const JSObjectRef object)
225 CalendarTaskPrivObject *priv =
226 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
228 priv = new CalendarTaskPrivObject(ctx, event);
229 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
235 LoggerE("Error during replacing task object");
239 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
241 JSStringRef propertyName,
242 JSValueRef* exception)
246 CalendarTaskPrivObject *privateObject =
247 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
248 CalendarEventPtr task = privateObject->getObject();
250 if (task->getUId()==UNDEFINED_ID) {
251 return JSValueMakeNull(context);
253 return Converter(context).toJSValueRef(task->getUId());
258 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
260 return JSValueMakeUndefined(context);