Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarAttendee.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
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"
24
25 using namespace TizenApis::Api::Calendar;
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
28 using namespace TizenApis::Commons;
29
30 namespace TizenApis {
31 namespace Tizen1_0 {
32 namespace Calendar {
33
34 JSClassDefinition JSCalendarAttendee::m_classInfo = {
35     0,
36     kJSClassAttributeNone,
37     TIZEN_INTERFACE_CALENDAR_ATTENDEE,
38     0,
39     m_property,
40     NULL, //m_function,
41     initialize,
42     finalize,
43     NULL, //hasProperty,
44     NULL, //getProperty,
45     NULL, //setProperty,
46     NULL, //deleteProperty,
47     NULL, //getPropertyNames,
48     NULL, //callAsFunction,
49     constructor,
50     NULL, //hasInstance,
51     NULL, //convertToType,
52 };
53
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 },
65
66     { 0, 0, 0, 0 }
67 };
68
69 JSClassRef JSCalendarAttendee::m_jsClassRef = JSClassCreate(
70         JSCalendarAttendee::getClassInfo());
71
72 const JSClassDefinition* JSCalendarAttendee::getClassInfo()
73 {
74     return &(m_classInfo);
75 }
76
77 JSClassRef JSCalendarAttendee::getClassRef()
78 {
79     if (!m_jsClassRef) {
80         m_jsClassRef = JSClassCreate(&m_classInfo);
81     }
82     return m_jsClassRef;
83 }
84
85 JSObjectRef JSCalendarAttendee::createJSCalendarAttendee(JSContextRef context, EventAttendeePtr attendee)
86 {
87     AttendeePrivateObject *priv = new AttendeePrivateObject(context, attendee);
88     return JSObjectMake(context, getClassRef(), priv);
89 }
90
91 void JSCalendarAttendee::initialize(JSContextRef context,
92         JSObjectRef object)
93 {
94     LogDebug("enter");
95 }
96
97 void JSCalendarAttendee::finalize(JSObjectRef object)
98 {
99     LogDebug("enter");
100     AttendeePrivateObject* priv =
101         static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
102     delete priv;
103     JSObjectSetPrivate(object, NULL);
104 }
105
106 EventAttendeePtr JSCalendarAttendee::getPrivateObject(JSObjectRef object)
107 {
108     LogDebug("entered");
109     AttendeePrivateObject *priv =
110         static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
111     if (!priv) {
112         ThrowMsg(NullPointerException, "Private object is null.");
113     }
114     EventAttendeePtr result = priv->getObject();
115     if (!result) {
116         ThrowMsg(NullPointerException, "Private object is null.");
117     }
118     return result;
119 }
120
121 void JSCalendarAttendee::setPrivateObject(const EventAttendeePtr &attendee,
122         JSContextRef ctx,
123         const JSObjectRef object)
124 {
125     LogDebug("entered");
126     Try
127     {
128         AttendeePrivateObject *priv =
129             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
130         delete priv;
131         priv = new AttendeePrivateObject(ctx, attendee);
132         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
133             delete priv;
134         }
135     }
136     Catch(Exception)
137     {
138         LogError("Error during replacing attendee object");
139     }
140 }
141
142 JSObjectRef JSCalendarAttendee::constructor(JSContextRef context,
143     JSObjectRef constructor,
144     size_t argumentCount,
145     const JSValueRef arguments[],
146     JSValueRef* exception)
147 {
148     LogDebug("entered");
149
150     Try
151     {
152         AttendeePrivateObject* privateObject = static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(constructor));
153         JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
154
155         CalendarConverter converter(globalContext);
156         EventAttendeePtr attendee;
157
158         if (argumentCount==1) {
159             if (!JSValueIsString(context, arguments[0])) {
160                 ThrowMsg(ConversionException, "Wrong parameter type.");
161             }
162
163             EventAttendeePtr result(new EventAttendee());
164             attendee = result;
165             attendee->setURI(converter.toString(arguments[0]));
166             if (!attendee) {
167                 ThrowMsg(ConversionException, "Parameter conversion failed.");
168             }
169         } else if (argumentCount==2) {
170             if (!JSValueIsString(context, arguments[0])) {
171                 ThrowMsg(ConversionException, "Wrong parameter type.");
172             }
173
174             attendee = converter.toAttendee(arguments[1]);
175             if (!attendee) {
176                 ThrowMsg(ConversionException, "Parameter conversion failed.");
177             }
178             attendee->setURI(converter.toString(arguments[0]));
179         } else {
180             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
181         }
182
183         return createJSCalendarAttendee(globalContext, attendee);
184     }
185     Catch(UnsupportedException)
186     {
187         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
188         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
189     }
190     Catch(InvalidArgumentException)
191     {
192         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
193         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
194     }
195     Catch(ConversionException)
196     {
197         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
198         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
199     }
200     Catch(Exception)
201     {
202         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
203         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
204     }
205
206     return NULL;
207 }
208
209 JSValueRef JSCalendarAttendee::getProperty(JSContextRef context,
210         JSObjectRef object,
211         JSStringRef propertyName,
212         JSValueRef* exception)
213 {
214     LogDebug("enter");
215     CalendarConverterFactory::ConverterType converter =
216         CalendarConverterFactory::getConverter(context);
217     Try
218     {
219         AttendeePrivateObject* priv =
220             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
221         if (!priv) {
222             Throw(NullPointerException);
223         }
224         EventAttendeePtr attendee = priv->getObject();
225
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());
246         }
247     }
248     Catch(Exception)
249     {
250         LogError("invalid property");
251     }
252     return JSValueMakeUndefined(context);
253 }
254
255 bool JSCalendarAttendee::setProperty(JSContextRef context,
256         JSObjectRef object,
257         JSStringRef propertyName,
258         JSValueRef value,
259         JSValueRef* exception)
260 {
261     LogDebug("entered");
262     CalendarConverterFactory::ConverterType converter =
263         CalendarConverterFactory::getConverter(context);
264     Try
265     {
266         AttendeePrivateObject* priv =
267             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
268         if (!priv) {
269             Throw(NullPointerException);
270         }
271         EventAttendeePtr attendee = priv->getObject();
272
273         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
274             if (!JSValueIsString(context, value)) {
275                 Throw(InvalidArgumentException);
276             }
277             std::string name = converter->toString(value);
278             attendee->setName(name);
279             return true;
280         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
281             if (!JSValueIsString(context, value)) {
282                 Throw(InvalidArgumentException);
283             }
284             std::string uri = converter->toString(value);
285             attendee->setURI(uri);
286             return true;
287         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
288             if (!JSValueIsString(context, value)) {
289                 Throw(InvalidArgumentException);
290             }
291             std::string role = converter->toString(value);
292             attendee->setRole(converter->toEventAttendeeRole(role));
293             return true;
294         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
295             if (!JSValueIsString(context, value)) {
296                 Throw(InvalidArgumentException);
297             }
298             std::string status = converter->toString(value);
299             attendee->setStatus(converter->toEventAttendeeStatus(status));
300             return true;
301         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
302             if (!JSValueIsBoolean(context, value)) {
303                 Throw(InvalidArgumentException);
304             }
305             bool RSVP = converter->toBool(value);
306             attendee->setRSVP(RSVP);
307             return true;
308         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
309             if (!JSValueIsString(context, value)) {
310                 Throw(InvalidArgumentException);
311             }
312             std::string type = converter->toString(value);
313             attendee->setType(converter->toEventAttendeeType(type));
314             return true;
315         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
316             if (!JSValueIsString(context, value)) {
317                 Throw(InvalidArgumentException);
318             }
319             std::string group = converter->toString(value);
320             attendee->setGroup(group);
321             return true;
322         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
323             if (!JSValueIsString(context, value)) {
324                 Throw(InvalidArgumentException);
325             }
326             std::string delegatorURI = converter->toString(value);
327             attendee->setDelegatorURI(delegatorURI);
328             return true;
329         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
330             if (!JSValueIsString(context, value)) {
331                 Throw(InvalidArgumentException);
332             }
333             std::string delegateURI = converter->toString(value);
334             attendee->setDelegateURI(delegateURI);
335             return true;
336         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
337             if (!JSValueIsString(context, value)) {
338                 Throw(InvalidArgumentException);
339             }
340             std::string uid = converter->toString(value);
341             attendee->setPersonId(uid);
342             return true;
343         }
344     }
345     Catch(Exception)
346     {
347         LogWarning("trying to set incorrect value");
348     }
349     JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);
350     return false;
351 }
352
353 }
354 }
355 }