2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <API/Calendar/EventId.h>
25 #include "JSCalendarEvent.h"
26 #include "JSCalendarItemProperties.h"
27 #include "CalendarConverter.h"
28 #include "JSEventId.h"
29 #include <Tizen/TimeUtil/TimeUtilConverter.h>
30 #include <Tizen/TimeUtil/JSTZDate.h>
32 using namespace TizenApis::Api::Calendar;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
40 #define TIZEN_CALENDAR_EVENT_ATTRIBUTENAME "CalendarEvent"
42 JSClassDefinition JSCalendarEvent::m_classInfo = {
44 kJSClassAttributeNone,
45 TIZEN_CALENDAR_EVENT_ATTRIBUTENAME,
46 JSCalendarItemProperties::getClassRef(),
54 NULL, //DeleteProperty,
55 NULL, //GetPropertyNames,
56 NULL, //CallAsFunction,
57 NULL, //CallAsConstructor,
62 JSStaticValue JSCalendarEvent::m_property[] = {
63 { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
64 NULL, kJSPropertyAttributeReadOnly },
65 { TIZEN_CALENDAR_EVENT_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
66 NULL, kJSPropertyAttributeReadOnly },
67 { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
68 NULL, kJSPropertyAttributeReadOnly },
73 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
75 void JSCalendarEvent::initialize(JSContextRef context,
79 CalendarEventPrivObject *priv =
80 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
82 CalendarEventPtr privateData(new CalendarEvent());
83 priv = new CalendarEventPrivObject(context, privateData);
84 JSObjectSetPrivate(object, static_cast<void*>(priv));
85 LogDebug("new event is created");
87 LogDebug("private object already exists");
91 void JSCalendarEvent::finalize(JSObjectRef object)
96 const JSClassRef JSCalendarEvent::getClassRef()
99 m_jsClassRef = JSClassCreate(&m_classInfo);
104 const JSClassDefinition* JSCalendarEvent::getClassInfo()
109 CalendarEventPtr JSCalendarEvent::getPrivateObject(JSObjectRef object)
112 CalendarEventPrivObject *priv =
113 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
115 ThrowMsg(NullPointerException, "Private object is null");
117 CalendarEventPtr result = priv->getObject();
119 ThrowMsg(NullPointerException, "Private object is null");
124 void JSCalendarEvent::setPrivateObject(const CalendarEventPtr &event,
126 const JSObjectRef object)
131 CalendarEventPrivObject *priv =
132 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
134 priv = new CalendarEventPrivObject(ctx, event);
135 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
141 LogError("Error during replacing event object");
145 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
147 JSStringRef propertyName,
148 JSValueRef* exception)
153 CalendarConverterFactory::ConverterType converter =
154 CalendarConverterFactory::getConverter(context);
155 CalendarEventPtr event = getPrivateObject(object);
157 EventIdPtr eventId( new EventId() );
158 eventId->setUId(event->getUId());
159 eventId->setRecurrenceId(event->getRecurrenceId());
160 eventId->setTimeZone(event->getTimeZone());
161 return converter->toJSValueRef(eventId);
165 LogWarning("trying to get incorrect value");
167 return JSValueMakeUndefined(context);
170 JSValueRef JSCalendarEvent::getPropertyLastModificationDate(JSContextRef context,
172 JSStringRef propertyName,
173 JSValueRef* exception)
178 CalendarEventPrivObject *privateObject =
179 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
180 CalendarEventPtr event = privateObject->getObject();
183 Throw(NullPointerException);
185 if (event->getLastModifiedDate() != 0) {
186 // Use the global context saved in the event struct.
187 return JSTZDate::createJSObject(privateObject->getContext(), event->getLastModifiedDate(), event->getTimeZone());
189 return JSValueMakeUndefined(context);
194 LogWarning("trying to get incorrect value");
196 return JSValueMakeUndefined(context);
199 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
201 JSStringRef propertyName,
202 JSValueRef* exception)
207 CalendarEventPtr event = getPrivateObject(object);
208 if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
209 return JSValueMakeUndefined(context);
212 Converter converter(context);
213 return converter.toJSValueRef(event->getIsDetached());
217 LogWarning("trying to get incorrect value");
219 return JSValueMakeUndefined(context);
222 bool JSCalendarEvent::validate(JSContextRef ctx,
223 const JSObjectRef object,
224 JSValueRef* exception)
227 CalendarEventPrivObject *priv =
228 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
232 CalendarEventPtr event = priv->getObject();
236 return event->validate();