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.
18 #include "JSCalendarEventId.h"
19 #include "CalendarConverter.h"
20 #include <dpl/log/log.h>
21 #include <Tizen/Common/JSTizenExceptionFactory.h>
22 #include <Tizen/Common/JSTizenException.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include <Tizen/TimeUtil/TimeUtilConverter.h>
25 #include <Tizen/TimeUtil/JSTZDate.h>
27 using namespace TizenApis::Api::Calendar;
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30 using namespace TizenApis::Commons;
36 JSClassDefinition JSCalendarEventId::m_classInfo = {
38 kJSClassAttributeNone,
39 TIZEN_INTERFACE_CALENDAR_EVENT_ID,
48 NULL, //deleteProperty,
49 NULL, //getPropertyNames,
50 NULL, //callAsFunction,
53 NULL, //convertToType,
56 JSStaticValue JSCalendarEventId::m_property[] = {
57 { TIZEN_CALENDAR_EVENT_ID_UID, getProperty, setProperty, kJSPropertyAttributeNone },
58 { TIZEN_CALENDAR_EVENT_ID_RECURRENCEID, getProperty, setProperty, kJSPropertyAttributeNone },
62 JSClassRef JSCalendarEventId::m_jsClassRef = JSClassCreate(
63 JSCalendarEventId::getClassInfo());
65 const JSClassDefinition* JSCalendarEventId::getClassInfo()
67 return &(m_classInfo);
70 JSClassRef JSCalendarEventId::getClassRef()
73 m_jsClassRef = JSClassCreate(&m_classInfo);
78 EventIdPtr JSCalendarEventId::getPrivateObject(JSObjectRef object)
81 EventIdPrivateObject *priv =
82 static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
84 ThrowMsg(NullPointerException, "Private object is null");
86 EventIdPtr result = priv->getObject();
88 ThrowMsg(NullPointerException, "Private object is null");
93 void JSCalendarEventId::setPrivateObject(const EventIdPtr &eventId,
95 const JSObjectRef object)
100 EventIdPrivateObject *priv =
101 static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
103 priv = new EventIdPrivateObject(ctx, eventId);
104 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
110 LogError("Error during replacing event id object.");
114 JSObjectRef JSCalendarEventId::createJSCalendarEventId(JSContextRef context, EventIdPtr eventId)
116 EventIdPrivateObject *priv = new EventIdPrivateObject(context, eventId);
117 return JSObjectMake(context, getClassRef(), priv);
120 void JSCalendarEventId::initialize(JSContextRef context,
126 void JSCalendarEventId::finalize(JSObjectRef object)
129 EventIdPrivateObject* priv =
130 static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
132 JSObjectSetPrivate(object, NULL);
135 JSObjectRef JSCalendarEventId::constructor(JSContextRef context,
136 JSObjectRef constructor,
137 size_t argumentCount,
138 const JSValueRef arguments[],
139 JSValueRef* exception)
145 EventIdPrivateObject* privateObject = static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(constructor));
146 JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
148 CalendarConverter converter(globalContext);
151 if (argumentCount==1) {
152 if (!JSValueIsString(context, arguments[0])) {
153 ThrowMsg(ConversionException, "Wrong parameter type.");
156 EventIdPtr result( new EventId());
158 eventId->setUId(converter.toString(arguments[0]));
159 } else if (argumentCount==2) {
160 if (!JSValueIsString(context, arguments[0])) {
161 ThrowMsg(ConversionException, "Wrong parameter type.");
163 if (!JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())) {
164 ThrowMsg(ConversionException, "Wrong parameter type.");
167 EventIdPtr result( new EventId());
169 eventId->setUId(converter.toString(arguments[0]));
171 TimeUtilConverter timeUtilConverter(globalContext);
172 eventId->setRecurrenceId(timeUtilConverter.toTZDateTimeT(arguments[1]));
174 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
177 return createJSCalendarEventId(globalContext, eventId);
179 Catch(UnsupportedException)
181 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
182 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
184 Catch(InvalidArgumentException)
186 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
187 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
189 Catch(ConversionException)
191 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
192 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
196 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
197 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
203 JSValueRef JSCalendarEventId::getProperty(JSContextRef context,
205 JSStringRef propertyName,
206 JSValueRef* exception)
209 Converter converter(context);
212 EventIdPrivateObject* priv =
213 static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
215 Throw(NullPointerException);
217 EventIdPtr eventId = priv->getObject();
219 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_UID)) {
220 return converter.toJSValueRef(eventId->getUId());
221 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_RECURRENCEID)) {
223 Throw(NullPointerException);
225 if (eventId->getRecurrenceId() != 0) {
226 // Use the global context saved in the event struct.
227 return JSTZDate::createJSObject(priv->getContext(), eventId->getRecurrenceId(), eventId->getTimeZone());
229 return JSValueMakeUndefined(context);
235 LogError("invalid property");
237 return JSValueMakeUndefined(context);
240 bool JSCalendarEventId::setProperty(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
247 Converter converter(context);
250 EventIdPrivateObject* priv =
251 static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
253 Throw(NullPointerException);
255 EventIdPtr eventId = priv->getObject();
257 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_UID)) {
258 if (!JSValueIsString(context, value)) {
259 Throw(InvalidArgumentException);
261 std::string uid = converter.toString(value);
262 eventId->setUId(uid);
264 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_RECURRENCEID)) {
265 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
266 Throw(InvalidArgumentException);
268 TimeUtilConverter timeConverter(context);
269 std::time_t recurrenceId = timeConverter.toTZDateTimeT(value);
270 eventId->setRecurrenceId(recurrenceId);
272 if( eventId->getTimeZone().empty() ) {
273 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
274 eventId->setTimeZone(timeZone);
281 LogWarning("trying to set incorrect value");
283 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);