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 "JSCalendarItem.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_ITEM_ATTRIBUTENAME "CalendarItem"
42 JSClassDefinition JSCalendarItem::m_classInfo = {
44 kJSClassAttributeNone,
45 TIZEN_CALENDAR_ITEM_ATTRIBUTENAME,
46 JSCalendarItemProperties::getClassRef(),
54 NULL, //DeleteProperty,
55 NULL, //GetPropertyNames,
56 NULL, //CallAsFunction,
57 NULL, //CallAsConstructor,
62 JSStaticValue JSCalendarItem::m_property[] = {
64 { TIZEN_CALENDAR_ITEM_ID, getPropertyId,
65 NULL, kJSPropertyAttributeReadOnly },
66 { TIZEN_CALENDAR_ITEM_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
67 NULL, kJSPropertyAttributeReadOnly },
72 JSClassRef JSCalendarItem::m_jsClassRef = JSClassCreate(JSCalendarItem::getClassInfo());
74 void JSCalendarItem::initialize(JSContextRef context,
78 CalendarItemPrivObject *priv =
79 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
81 CalendarEventPtr privateData(new CalendarEvent());
82 priv = new CalendarItemPrivObject(context, privateData);
83 JSObjectSetPrivate(object, static_cast<void*>(priv));
84 LogDebug("new event is created");
86 LogDebug("private object already exists");
90 void JSCalendarItem::finalize(JSObjectRef object)
95 const JSClassRef JSCalendarItem::getClassRef()
98 m_jsClassRef = JSClassCreate(&m_classInfo);
103 const JSClassDefinition* JSCalendarItem::getClassInfo()
108 CalendarEventPtr JSCalendarItem::getPrivateObject(JSObjectRef object)
111 CalendarItemPrivObject *priv =
112 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
114 ThrowMsg(NullPointerException, "Private object is null");
116 CalendarEventPtr result = priv->getObject();
118 ThrowMsg(NullPointerException, "Private object is null");
123 void JSCalendarItem::setPrivateObject(const CalendarEventPtr &event,
125 const JSObjectRef object)
130 CalendarItemPrivObject *priv =
131 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
133 priv = new CalendarItemPrivObject(ctx, event);
134 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
140 LogError("Error during replacing event object");
144 JSValueRef JSCalendarItem::getPropertyId(JSContextRef context,
146 JSStringRef propertyName,
147 JSValueRef* exception)
152 CalendarConverterFactory::ConverterType converter =
153 CalendarConverterFactory::getConverter(context);
154 CalendarEventPtr event = getPrivateObject(object);
156 EventIdPtr eventId( new EventId() );
157 eventId->setUId(event->getUId());
158 LogDebug("event->getRecurrenceId() : " << event->getRecurrenceId());
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 JSCalendarItem::getPropertyLastModificationDate(JSContextRef context,
172 JSStringRef propertyName,
173 JSValueRef* exception)
178 CalendarItemPrivObject *privateObject =
179 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
180 CalendarEventPtr item = privateObject->getObject();
183 Throw(NullPointerException);
185 if (item->getLastModifiedDate() != 0) {
186 // Use the global context saved in the event struct.
187 return JSTZDate::createJSObject(privateObject->getContext(), item->getLastModifiedDate(), item->getTimeZone());
189 return JSValueMakeUndefined(context);
194 LogWarning("trying to get incorrect value");
196 return JSValueMakeUndefined(context);
199 bool JSCalendarItem::validate(JSContextRef ctx,
200 const JSObjectRef object,
201 JSValueRef* exception)
204 CalendarItemPrivObject *priv =
205 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
209 CalendarEventPtr event = priv->getObject();
213 return event->validate();