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.
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 #define TIZEN_CALENDAR_EVENT_ID "id"
43 #define TIZEN_CALENDAR_EVENT_LASTMODIFIEDDATE "lastModificationDate"
44 #define TIZEN_CALENDAR_EVENT_IS_DETACHED "isDetached"
46 JSClassDefinition JSCalendarEvent::m_classInfo = {
48 kJSClassAttributeNone,
49 TIZEN_CALENDAR_EVENT_ATTRIBUTENAME,
50 JSCalendarItemProperties::getClassRef(),
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
66 JSStaticValue JSCalendarEvent::m_property[] = {
67 { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
68 NULL, kJSPropertyAttributeReadOnly },
69 { TIZEN_CALENDAR_EVENT_LASTMODIFIEDDATE, getPropertyLastModifiedDate,
70 NULL, kJSPropertyAttributeReadOnly },
71 { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
72 NULL, kJSPropertyAttributeReadOnly },
77 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
79 void JSCalendarEvent::initialize(JSContextRef context,
83 CalendarEventPrivObject *priv =
84 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
86 CalendarEventPtr privateData(new CalendarEvent());
87 priv = new CalendarEventPrivObject(context, privateData);
88 JSObjectSetPrivate(object, static_cast<void*>(priv));
89 LogDebug("new event is created");
91 LogDebug("private object already exists");
95 void JSCalendarEvent::finalize(JSObjectRef object)
100 const JSClassRef JSCalendarEvent::getClassRef()
103 m_jsClassRef = JSClassCreate(&m_classInfo);
108 const JSClassDefinition* JSCalendarEvent::getClassInfo()
113 CalendarEventPtr JSCalendarEvent::getPrivateObject(JSObjectRef object)
116 CalendarEventPrivObject *priv =
117 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
119 ThrowMsg(NullPointerException, "Private object is null");
121 CalendarEventPtr result = priv->getObject();
123 ThrowMsg(NullPointerException, "Private object is null");
128 void JSCalendarEvent::setPrivateObject(const CalendarEventPtr &event,
130 const JSObjectRef object)
135 CalendarEventPrivObject *priv =
136 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
138 priv = new CalendarEventPrivObject(ctx, event);
139 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
145 LogError("Error during replacing event object");
149 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
151 JSStringRef propertyName,
152 JSValueRef* exception)
157 CalendarConverterFactory::ConverterType converter =
158 CalendarConverterFactory::getConverter(context);
159 CalendarEventPtr event = getPrivateObject(object);
161 EventIdPtr eventId( new EventId() );
162 eventId->setUId(event->getUId());
163 eventId->setRecurrenceId(event->getRecurrenceId());
164 eventId->setTimeZone(event->getTimeZone());
165 return converter->toJSValueRef(eventId);
169 LogWarning("trying to get incorrect value");
171 return JSValueMakeUndefined(context);
174 JSValueRef JSCalendarEvent::getPropertyLastModifiedDate(JSContextRef context,
176 JSStringRef propertyName,
177 JSValueRef* exception)
182 CalendarEventPrivObject *privateObject =
183 static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
184 CalendarEventPtr event = privateObject->getObject();
187 Throw(NullPointerException);
189 if (event->getLastModifiedDate() != 0) {
190 // Use the global context saved in the event struct.
191 return JSTZDate::createJSObject(privateObject->getContext(), event->getLastModifiedDate(), event->getTimeZone());
193 return JSValueMakeUndefined(context);
198 LogWarning("trying to get incorrect value");
200 return JSValueMakeUndefined(context);
203 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
205 JSStringRef propertyName,
206 JSValueRef* exception)
211 Converter converter(context);
212 CalendarEventPtr event = getPrivateObject(object);
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();