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.
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <JSTizenExceptionFactory.h>
26 #include "ICalendar.h"
27 #include "CalendarFactory.h"
28 #include "JSCalendarTask.h"
29 #include "JSCalendarItemProperties.h"
30 #include "CalendarConverter.h"
31 #include "JSCalendarEventId.h"
32 #include <TimeUtilConverter.h>
34 #include <JSTizenException.h>
35 #include <JSTizenExceptionFactory.h>
37 #include <GlobalContextManager.h>
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41 using namespace DeviceAPI::Common;
42 using namespace DeviceAPI::Time;
47 JSClassDefinition JSCalendarTask::m_classInfo = {
49 kJSClassAttributeNone,
50 TIZEN_INTERFACE_CALENDAR_TASK,
51 JSCalendarItemProperties::getClassRef(),
59 NULL, //DeleteProperty,
60 NULL, //GetPropertyNames,
61 NULL, //CallAsFunction,
67 JSStaticValue JSCalendarTask::m_property[] = {
69 { TIZEN_CALENDAR_TASK_ID, getPropertyId, NULL, kJSPropertyAttributeReadOnly },
74 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
76 ICalendarPtr JSCalendarTask::m_calendar = CalendarFactory::getInstance().createCalendarObject();
78 void JSCalendarTask::initialize(JSContextRef context,
81 if (!JSObjectGetPrivate(object)) {
82 LogDebug("Create calendar task private object.");
83 CalendarEventPtr task( new CalendarEvent() );
84 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
85 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
89 LogDebug("Private object already set.");
93 m_calendar->setType(CalendarEvent::TASK_TYPE);
94 LogDebug("Calendar object type is set to task.");
98 void JSCalendarTask::finalize(JSObjectRef object)
100 CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
103 JSObjectSetPrivate(object, NULL);
107 JSObjectRef JSCalendarTask::constructor(JSContextRef context,
108 JSObjectRef constructor,
109 size_t argumentCount,
110 const JSValueRef arguments[],
111 JSValueRef* exception)
115 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
116 CalendarConverter converter(globalContext);
117 CalendarEventPtr task;
119 if (argumentCount==0) {
120 CalendarEventPtr result(new CalendarEvent());
122 } else if (argumentCount==1) {
123 LogInfo("taskInitDict case");
124 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
125 CalendarEventPtr result(new CalendarEvent());
127 } else if (JSValueIsObject(context, arguments[0])) {
128 task = converter.toItem(arguments[0]);
130 ThrowMsg(ConversionException, "Parameter conversion failed.");
133 ThrowMsg(ConversionException, "Parameter conversion failed.");
135 } else if (argumentCount>=2) {
136 LogInfo("task stringRepresentation case");
137 std::string eventStr;
138 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
139 eventStr = converter.toString(arguments[0]);
140 format = converter.toVObjectFormat(converter.toString(arguments[1]));
142 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
143 dplEvent->setEventString(eventStr);
144 dplEvent->setFormat(format);
145 dplEvent->setForSynchronousCall();
146 m_calendar->createEventFromString(dplEvent);
148 // Process the returned object.
149 if (dplEvent->getResult()) {
150 LogInfo("Successfully created a task.");
151 task = dplEvent->getEvent();
153 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
154 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
156 ThrowMsg(UnknownException, "Converting string failed.");
161 task->setCalendarType(CalendarEvent::TASK_TYPE);
162 return createJSCalendarTask(context, task);
164 Catch(UnsupportedException)
166 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
167 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
169 Catch(InvalidArgumentException)
171 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
172 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
174 Catch(ConversionException)
176 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
177 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
181 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
182 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
188 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
190 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
191 return JSObjectMake(context, getClassRef(), priv);
194 const JSClassRef JSCalendarTask::getClassRef()
197 m_jsClassRef = JSClassCreate(&m_classInfo);
202 const JSClassDefinition* JSCalendarTask::getClassInfo()
207 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
209 CalendarTaskPrivObject *priv =
210 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
212 ThrowMsg(NullPointerException, "Private object is null");
214 CalendarEventPtr result = priv->getObject();
216 ThrowMsg(NullPointerException, "Private object is null");
221 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
223 const JSObjectRef object)
227 CalendarTaskPrivObject *priv =
228 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
230 priv = new CalendarTaskPrivObject(ctx, event);
231 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
237 LogError("Error during replacing task object");
241 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
243 JSStringRef propertyName,
244 JSValueRef* exception)
248 CalendarTaskPrivObject *privateObject =
249 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
250 CalendarEventPtr task = privateObject->getObject();
252 if (task->getUId()==UNDEFINED_ID) {
253 return JSValueMakeNull(context);
255 return Converter(context).toJSValueRef(task->getUId());
260 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
262 return JSValueMakeUndefined(context);