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 "JSCalendarAttendee.h"
19 #include <dpl/log/log.h>
20 #include <Tizen/Common/JSTizenException.h>
21 #include <Tizen/Common/JSTizenExceptionFactory.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include "CalendarConverter.h"
25 using namespace TizenApis::Api::Calendar;
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
28 using namespace TizenApis::Commons;
34 JSClassDefinition JSCalendarAttendee::m_classInfo = {
36 kJSClassAttributeNone,
37 TIZEN_INTERFACE_CALENDAR_ATTENDEE,
46 NULL, //deleteProperty,
47 NULL, //getPropertyNames,
48 NULL, //callAsFunction,
51 NULL, //convertToType,
54 JSStaticValue JSCalendarAttendee::m_property[] = {
55 { TIZEN_ATTENDEE_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
56 { TIZEN_ATTENDEE_URI, getProperty, setProperty, kJSPropertyAttributeNone },
57 { TIZEN_ATTENDEE_ROLE, getProperty, setProperty, kJSPropertyAttributeNone },
58 { TIZEN_ATTENDEE_STATUS, getProperty, setProperty, kJSPropertyAttributeNone },
59 { TIZEN_ATTENDEE_RSVP, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_ATTENDEE_TYPE, getProperty, setProperty, kJSPropertyAttributeNone },
61 { TIZEN_ATTENDEE_GROUP, getProperty, setProperty, kJSPropertyAttributeNone },
62 { TIZEN_ATTENDEE_DELEGATORURI, getProperty, setProperty, kJSPropertyAttributeNone },
63 { TIZEN_ATTENDEE_DELEGATEURI, getProperty, setProperty, kJSPropertyAttributeNone },
64 { TIZEN_ATTENDEE_CONTACT_REF, getProperty, setProperty, kJSPropertyAttributeNone },
69 JSClassRef JSCalendarAttendee::m_jsClassRef = JSClassCreate(
70 JSCalendarAttendee::getClassInfo());
72 const JSClassDefinition* JSCalendarAttendee::getClassInfo()
74 return &(m_classInfo);
77 JSClassRef JSCalendarAttendee::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_classInfo);
85 JSObjectRef JSCalendarAttendee::createJSCalendarAttendee(JSContextRef context, EventAttendeePtr attendee)
87 AttendeePrivateObject *priv = new AttendeePrivateObject(context, attendee);
88 return JSObjectMake(context, getClassRef(), priv);
91 void JSCalendarAttendee::initialize(JSContextRef context,
97 void JSCalendarAttendee::finalize(JSObjectRef object)
100 AttendeePrivateObject* priv =
101 static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
103 JSObjectSetPrivate(object, NULL);
106 EventAttendeePtr JSCalendarAttendee::getPrivateObject(JSObjectRef object)
109 AttendeePrivateObject *priv =
110 static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
112 ThrowMsg(NullPointerException, "Private object is null.");
114 EventAttendeePtr result = priv->getObject();
116 ThrowMsg(NullPointerException, "Private object is null.");
121 void JSCalendarAttendee::setPrivateObject(const EventAttendeePtr &attendee,
123 const JSObjectRef object)
128 AttendeePrivateObject *priv =
129 static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
131 priv = new AttendeePrivateObject(ctx, attendee);
132 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
138 LogError("Error during replacing attendee object");
142 JSObjectRef JSCalendarAttendee::constructor(JSContextRef context,
143 JSObjectRef constructor,
144 size_t argumentCount,
145 const JSValueRef arguments[],
146 JSValueRef* exception)
152 AttendeePrivateObject* privateObject = static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(constructor));
153 JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
155 CalendarConverter converter(globalContext);
156 EventAttendeePtr attendee;
158 if (argumentCount==1) {
159 if (!JSValueIsString(context, arguments[0])) {
160 ThrowMsg(ConversionException, "Wrong parameter type.");
163 EventAttendeePtr result(new EventAttendee());
165 attendee->setURI(converter.toString(arguments[0]));
167 ThrowMsg(ConversionException, "Parameter conversion failed.");
169 } else if (argumentCount==2) {
170 if (!JSValueIsString(context, arguments[0])) {
171 ThrowMsg(ConversionException, "Wrong parameter type.");
174 attendee = converter.toAttendee(arguments[1]);
176 ThrowMsg(ConversionException, "Parameter conversion failed.");
178 attendee->setURI(converter.toString(arguments[0]));
180 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
183 return createJSCalendarAttendee(globalContext, attendee);
185 Catch(UnsupportedException)
187 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
188 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
190 Catch(InvalidArgumentException)
192 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
193 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
195 Catch(ConversionException)
197 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
198 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
202 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
203 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
209 JSValueRef JSCalendarAttendee::getProperty(JSContextRef context,
211 JSStringRef propertyName,
212 JSValueRef* exception)
215 CalendarConverterFactory::ConverterType converter =
216 CalendarConverterFactory::getConverter(context);
219 AttendeePrivateObject* priv =
220 static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
222 Throw(NullPointerException);
224 EventAttendeePtr attendee = priv->getObject();
226 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
227 return converter->toJSValueRef(attendee->getName());
228 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
229 return converter->toJSValueRef(attendee->getURI());
230 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
231 return converter->toJSValueRef(converter->toTizenValue(attendee->getRole()));
232 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
233 return converter->toJSValueRef(converter->toTizenValue(attendee->getStatus()));
234 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
235 return converter->toJSValueRef(attendee->getRSVP());
236 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
237 return converter->toJSValueRef(converter->toTizenValue(attendee->getType()));
238 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
239 return converter->toJSValueRef(attendee->getGroup());
240 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
241 return converter->toJSValueRef(attendee->getDelegatorURI());
242 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
243 return converter->toJSValueRef(attendee->getDelegateURI());
244 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
245 return converter->toJSValueRef(attendee->getPersonId());
250 LogError("invalid property");
252 return JSValueMakeUndefined(context);
255 bool JSCalendarAttendee::setProperty(JSContextRef context,
257 JSStringRef propertyName,
259 JSValueRef* exception)
262 CalendarConverterFactory::ConverterType converter =
263 CalendarConverterFactory::getConverter(context);
266 AttendeePrivateObject* priv =
267 static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
269 Throw(NullPointerException);
271 EventAttendeePtr attendee = priv->getObject();
273 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
274 if (!JSValueIsString(context, value)) {
275 Throw(InvalidArgumentException);
277 std::string name = converter->toString(value);
278 attendee->setName(name);
280 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
281 if (!JSValueIsString(context, value)) {
282 Throw(InvalidArgumentException);
284 std::string uri = converter->toString(value);
285 attendee->setURI(uri);
287 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
288 if (!JSValueIsString(context, value)) {
289 Throw(InvalidArgumentException);
291 std::string role = converter->toString(value);
292 attendee->setRole(converter->toEventAttendeeRole(role));
294 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
295 if (!JSValueIsString(context, value)) {
296 Throw(InvalidArgumentException);
298 std::string status = converter->toString(value);
299 attendee->setStatus(converter->toEventAttendeeStatus(status));
301 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
302 if (!JSValueIsBoolean(context, value)) {
303 Throw(InvalidArgumentException);
305 bool RSVP = converter->toBool(value);
306 attendee->setRSVP(RSVP);
308 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
309 if (!JSValueIsString(context, value)) {
310 Throw(InvalidArgumentException);
312 std::string type = converter->toString(value);
313 attendee->setType(converter->toEventAttendeeType(type));
315 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
316 if (!JSValueIsString(context, value)) {
317 Throw(InvalidArgumentException);
319 std::string group = converter->toString(value);
320 attendee->setGroup(group);
322 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
323 if (!JSValueIsString(context, value)) {
324 Throw(InvalidArgumentException);
326 std::string delegatorURI = converter->toString(value);
327 attendee->setDelegatorURI(delegatorURI);
329 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
330 if (!JSValueIsString(context, value)) {
331 Throw(InvalidArgumentException);
333 std::string delegateURI = converter->toString(value);
334 attendee->setDelegateURI(delegateURI);
336 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
337 if (!JSValueIsString(context, value)) {
338 Throw(InvalidArgumentException);
340 std::string uid = converter->toString(value);
341 attendee->setPersonId(uid);
347 LogWarning("trying to set incorrect value");
349 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);