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 },
69 { TIZEN_CALENDAR_TASK_COMPLETED_DATE, getPropertyCompletedDate, setPropertyCompletedDate, kJSPropertyAttributeNone | kJSPropertyAttributeDontDelete },
70 { TIZEN_CALENDAR_TASK_PROGRESS, getPropertyProgress, setPropertyProgress, kJSPropertyAttributeNone | kJSPropertyAttributeDontDelete },
75 JSClassRef JSCalendarTask::m_jsClassRef = JSClassCreate(JSCalendarTask::getClassInfo());
77 ICalendarPtr JSCalendarTask::m_calendar;
79 void JSCalendarTask::initialize(JSContextRef context,
82 if (!JSObjectGetPrivate(object)) {
83 LoggerD("Create calendar task private object.");
84 CalendarEventPtr task( new CalendarEvent() );
85 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
86 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
90 LoggerD("Private object already set.");
93 if (NULL==m_calendar) {
94 m_calendar = CalendarFactory::getInstance().createCalendarObject();
95 m_calendar->setType(CalendarEvent::TASK_TYPE);
96 LoggerD("Static calendar for task created.");
100 void JSCalendarTask::finalize(JSObjectRef object)
102 CalendarTaskPrivObject* priv = static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
105 JSObjectSetPrivate(object, NULL);
109 JSObjectRef DLL_EXPORT JSCalendarTask::constructor(JSContextRef context,
110 JSObjectRef constructor,
111 size_t argumentCount,
112 const JSValueRef arguments[],
113 JSValueRef* exception)
117 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
118 CalendarConverter converter(globalContext);
119 CalendarEventPtr task;
121 if (argumentCount==0) {
122 CalendarEventPtr result(new CalendarEvent());
124 } else if (argumentCount==1) {
125 LoggerI("taskInitDict case");
126 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
127 CalendarEventPtr result(new CalendarEvent());
129 } else if (JSValueIsObject(context, arguments[0])) {
130 task = converter.toItem(arguments[0], false);
132 ThrowMsg(ConversionException, "Parameter conversion failed.");
135 ThrowMsg(ConversionException, "Parameter conversion failed.");
137 } else if (argumentCount>=2) {
138 LoggerI("task stringRepresentation case");
139 std::string eventStr;
140 CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
141 eventStr = converter.toString(arguments[0]);
142 format = converter.toVObjectFormat(converter.toString(arguments[1]));
144 if (NULL==m_calendar) {
145 m_calendar = CalendarFactory::getInstance().createCalendarObject();
146 m_calendar->setType(CalendarEvent::TASK_TYPE);
147 LoggerD("Static calendar for task created.");
150 IEventCreateEventFromStringPtr dplEvent(new IEventCreateEventFromString());
151 dplEvent->setEventString(eventStr);
152 dplEvent->setFormat(format);
153 dplEvent->setForSynchronousCall();
154 m_calendar->createEventFromString(dplEvent);
156 // Process the returned object.
157 if (dplEvent->getResult()) {
158 LoggerI("Successfully created a task.");
159 task = dplEvent->getEvent();
161 if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
162 ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
164 ThrowMsg(UnknownException, "Converting string failed.");
169 task->setCalendarType(CalendarEvent::TASK_TYPE);
170 return createJSCalendarTask(context, task);
172 Catch(UnsupportedException)
174 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
175 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
177 Catch(InvalidArgumentException)
179 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
180 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
182 Catch(ConversionException)
184 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
185 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
189 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
190 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
194 JSObjectRef JSCalendarTask::createJSCalendarTask(JSContextRef context, CalendarEventPtr task)
196 CalendarTaskPrivObject *priv = new CalendarTaskPrivObject(context, task);
197 return JSObjectMake(context, getClassRef(), priv);
200 const JSClassRef DLL_EXPORT JSCalendarTask::getClassRef()
203 m_jsClassRef = JSClassCreate(&m_classInfo);
208 const JSClassDefinition* JSCalendarTask::getClassInfo()
213 CalendarEventPtr JSCalendarTask::getPrivateObject(JSObjectRef object)
215 CalendarTaskPrivObject *priv =
216 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
218 ThrowMsg(NullPointerException, "Private object is null");
220 CalendarEventPtr result = priv->getObject();
222 ThrowMsg(NullPointerException, "Private object is null");
227 void JSCalendarTask::setPrivateObject(const CalendarEventPtr &event,
229 const JSObjectRef object)
233 CalendarTaskPrivObject *priv =
234 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
236 priv = new CalendarTaskPrivObject(ctx, event);
237 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
243 LoggerE("Error during replacing task object");
247 JSValueRef JSCalendarTask::getPropertyId(JSContextRef context,
249 JSStringRef propertyName,
250 JSValueRef* exception)
254 CalendarTaskPrivObject *privateObject =
255 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
256 CalendarEventPtr task = privateObject->getObject();
258 if (task->getUId()==UNDEFINED_ID) {
259 return JSValueMakeNull(context);
261 return Converter(context).toJSValueRef(task->getUId());
266 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
268 return JSValueMakeUndefined(context);
271 JSValueRef JSCalendarTask::getPropertyCompletedDate(JSContextRef context,
273 JSStringRef propertyName,
274 JSValueRef* exception)
278 CalendarTaskPrivObject *privateObject =
279 static_cast<CalendarTaskPrivObject*>(JSObjectGetPrivate(object));
280 CalendarEventPtr task = privateObject->getObject();
282 ThrowMsg(NullPointerException, "Task object is NULL.");
285 if (UNDEFINED_TIME==task->getCompletedDate()) {
286 return JSValueMakeUndefined(context);
288 TimeUtilConverter timeConverter(context);
289 return timeConverter.toJSValueRefTZDate((double)(task->getCompletedDate()*1000.0), task->getTimeZone());
294 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
296 return JSValueMakeUndefined(context);
299 bool JSCalendarTask::setPropertyCompletedDate(JSContextRef context,
301 JSStringRef propertyName,
303 JSValueRef* exception)
307 CalendarEventPtr task = getPrivateObject(object);
309 ThrowMsg(NullPointerException, "Task object is NULL.");
312 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
313 ThrowMsg(ConversionException, "Wrong parameter type.");
316 TimeUtilConverter converter(context);
317 long long int completedDate = (long long int) (converter.getTimeInMilliseconds(value)/1000);
319 task->setCompletedDate(completedDate);
321 if( task->getTimeZone().empty() ) {
322 std::string timeZone = converter.getPropertiesInTZDate(value).timezone;
323 task->setTimeZone(timeZone);
329 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
335 JSValueRef JSCalendarTask::getPropertyProgress(JSContextRef context,
337 JSStringRef propertyName,
338 JSValueRef* exception)
342 CalendarEventPtr task = getPrivateObject(object);
343 Converter converter(context);
344 return converter.toJSValueRef(task->getProgress());
348 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
350 return JSValueMakeUndefined(context);
353 bool JSCalendarTask::setPropertyProgress(JSContextRef context,
355 JSStringRef propertyName,
357 JSValueRef* exception)
361 CalendarEventPtr task = getPrivateObject(object);
362 Converter converter(context);
363 int progress = converter.toInt(value);
364 task->setProgress(progress);
369 LoggerW("Exception: "<<_rethrown_exception.GetMessage());