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/JSTizenException.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <API/Calendar/EventId.h>
26 #include "JSCalendarItem.h"
27 #include "JSCalendarItemProperties.h"
28 #include "CalendarConverter.h"
29 #include "JSCalendarEventId.h"
30 #include <Tizen/TimeUtil/TimeUtilConverter.h>
31 #include <Tizen/TimeUtil/JSTZDate.h>
33 using namespace TizenApis::Api::Calendar;
34 using namespace WrtDeviceApis::Commons;
35 using namespace WrtDeviceApis::CommonsJavaScript;
36 using namespace TizenApis::Commons;
42 #define TIZEN_CALENDAR_ITEM_ATTRIBUTENAME "CalendarItem"
44 JSClassDefinition JSCalendarItem::m_classInfo = {
46 kJSClassAttributeNone,
47 TIZEN_CALENDAR_ITEM_ATTRIBUTENAME,
48 JSCalendarItemProperties::getClassRef(),
56 NULL, //DeleteProperty,
57 NULL, //GetPropertyNames,
58 NULL, //CallAsFunction,
59 NULL, //CallAsConstructor,
64 JSStaticValue JSCalendarItem::m_property[] = {
66 { TIZEN_CALENDAR_ITEM_ID, getPropertyId,
67 NULL, kJSPropertyAttributeReadOnly },
68 { TIZEN_CALENDAR_ITEM_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
69 NULL, kJSPropertyAttributeReadOnly },
70 { TIZEN_CALENDAR_ITEM_IS_DETACHED, getPropertyIsDetached,
71 NULL, kJSPropertyAttributeReadOnly },
76 JSClassRef JSCalendarItem::m_jsClassRef = JSClassCreate(JSCalendarItem::getClassInfo());
78 void JSCalendarItem::initialize(JSContextRef context,
82 CalendarItemPrivObject *priv =
83 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
85 CalendarEventPtr privateData(new CalendarEvent());
86 priv = new CalendarItemPrivObject(context, privateData);
87 JSObjectSetPrivate(object, static_cast<void*>(priv));
88 LogDebug("new event is created");
90 LogDebug("private object already exists");
94 void JSCalendarItem::finalize(JSObjectRef object)
99 const JSClassRef JSCalendarItem::getClassRef()
102 m_jsClassRef = JSClassCreate(&m_classInfo);
107 const JSClassDefinition* JSCalendarItem::getClassInfo()
112 CalendarEventPtr JSCalendarItem::getPrivateObject(JSObjectRef object)
115 CalendarItemPrivObject *priv =
116 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
118 ThrowMsg(NullPointerException, "Private object is null");
120 CalendarEventPtr result = priv->getObject();
122 ThrowMsg(NullPointerException, "Private object is null");
127 void JSCalendarItem::setPrivateObject(const CalendarEventPtr &event,
129 const JSObjectRef object)
134 CalendarItemPrivObject *priv =
135 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
137 priv = new CalendarItemPrivObject(ctx, event);
138 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
144 LogError("Error during replacing event object");
148 JSObjectRef JSCalendarItem::createJSCalendarItem(JSContextRef context, CalendarEventPtr item)
150 CalendarItemPrivObject *priv = new CalendarItemPrivObject(context, item);
151 return JSObjectMake(context, getClassRef(), priv);
154 JSValueRef JSCalendarItem::getPropertyId(JSContextRef context,
156 JSStringRef propertyName,
157 JSValueRef* exception)
162 CalendarItemPrivObject *privateObject =
163 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
164 CalendarEventPtr item = privateObject->getObject();
166 if (CalendarEvent::TASK_TYPE == item->getCalendarType()) {
167 Converter converter(context);
168 converter.toJSValueRef(item->getUId());
170 EventIdPtr eventId( new EventId() );
171 eventId->setUId(item->getUId());
172 LogDebug("item->getRecurrenceId() : " << item->getRecurrenceId());
173 eventId->setRecurrenceId(item->getRecurrenceId());
174 eventId->setTimeZone(item->getTimeZone());
175 return JSCalendarEventId::createJSCalendarEventId(privateObject->getContext(), eventId);
180 LogWarning("trying to get incorrect value");
182 return JSValueMakeUndefined(context);
185 JSValueRef JSCalendarItem::getPropertyLastModificationDate(JSContextRef context,
187 JSStringRef propertyName,
188 JSValueRef* exception)
193 CalendarItemPrivObject *privateObject =
194 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
195 CalendarEventPtr item = privateObject->getObject();
198 Throw(NullPointerException);
200 if (item->getLastModifiedDate() != 0) {
201 // Use the global context saved in the event struct.
202 return JSTZDate::createJSObject(privateObject->getContext(), item->getLastModifiedDate(), item->getTimeZone());
204 return JSValueMakeUndefined(context);
209 LogWarning("trying to get incorrect value");
211 return JSValueMakeUndefined(context);
214 JSValueRef JSCalendarItem::getPropertyIsDetached(JSContextRef context,
216 JSStringRef propertyName,
217 JSValueRef* exception)
222 CalendarEventPtr item = getPrivateObject(object);
223 if(CalendarEvent::EVENT_TYPE != item->getCalendarType()) {
224 return JSValueMakeUndefined(context);
227 Converter converter(context);
228 return converter.toJSValueRef(item->getIsDetached());
232 LogWarning("trying to get incorrect value");
234 return JSValueMakeUndefined(context);
237 bool JSCalendarItem::validate(JSContextRef ctx,
238 const JSObjectRef object,
239 JSValueRef* exception)
242 CalendarItemPrivObject *priv =
243 static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
247 CalendarEventPtr event = priv->getObject();
251 return event->validate();