d2c1862da510e3e0bd212eaad4567941cf1daedf
[framework/web/wrt-plugins-tizen.git] / src / Calendar / JSCalendarAttendee.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "JSCalendarAttendee.h"
19 #include <JSTizenException.h>
20 #include <JSTizenExceptionFactory.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include "CalendarConverter.h"
23
24 #include <JSContactRef.h>
25 #include <ContactConverter.h>
26 #include <DurationProperties.h>
27 #include <ContactRef.h>
28 #include <Logger.h>
29 #include <Export.h>
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace DeviceAPI::Common;
34
35 namespace DeviceAPI {
36 namespace Calendar {
37
38 JSClassDefinition JSCalendarAttendee::m_classInfo = {
39     0,
40     kJSClassAttributeNone,
41     TIZEN_INTERFACE_CALENDAR_ATTENDEE,
42     0,
43     m_property,
44     NULL, //m_function,
45     initialize,
46     finalize,
47     NULL, //hasProperty,
48     NULL, //getProperty,
49     NULL, //setProperty,
50     NULL, //deleteProperty,
51     NULL, //getPropertyNames,
52     NULL, //callAsFunction,
53     NULL, //constructor,
54     NULL, //hasInstance,
55     NULL, //convertToType,
56 };
57
58 JSStaticValue JSCalendarAttendee::m_property[] = {
59     { TIZEN_ATTENDEE_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
60     { TIZEN_ATTENDEE_URI, getProperty, setProperty, kJSPropertyAttributeNone },
61     { TIZEN_ATTENDEE_ROLE, getProperty, setProperty, kJSPropertyAttributeNone },
62     { TIZEN_ATTENDEE_STATUS, getProperty, setProperty, kJSPropertyAttributeNone },
63     { TIZEN_ATTENDEE_RSVP, getProperty, setProperty, kJSPropertyAttributeNone },
64     { TIZEN_ATTENDEE_TYPE, getProperty, setProperty, kJSPropertyAttributeNone },
65     { TIZEN_ATTENDEE_GROUP, getProperty, setProperty, kJSPropertyAttributeNone },
66     { TIZEN_ATTENDEE_DELEGATORURI, getProperty, setProperty, kJSPropertyAttributeNone },
67     { TIZEN_ATTENDEE_DELEGATEURI, getProperty, setProperty, kJSPropertyAttributeNone },
68     { TIZEN_ATTENDEE_CONTACT_REF, getProperty, setProperty, kJSPropertyAttributeNone },
69
70     { 0, 0, 0, 0 }
71 };
72
73 JSClassRef JSCalendarAttendee::m_jsClassRef = JSClassCreate(
74         JSCalendarAttendee::getClassInfo());
75
76 const JSClassDefinition* JSCalendarAttendee::getClassInfo()
77 {
78     return &(m_classInfo);
79 }
80
81 JSClassRef DLL_EXPORT JSCalendarAttendee::getClassRef()
82 {
83     if (!m_jsClassRef) {
84         m_jsClassRef = JSClassCreate(&m_classInfo);
85     }
86     return m_jsClassRef;
87 }
88
89 JSObjectRef JSCalendarAttendee::createJSCalendarAttendee(JSContextRef context, EventAttendeePtr attendee)
90 {
91     AttendeePrivateObject *priv = new AttendeePrivateObject(context, attendee);
92     return JSObjectMake(context, getClassRef(), priv);
93 }
94
95 void JSCalendarAttendee::initialize(JSContextRef context,
96         JSObjectRef object)
97 {
98     if (!JSObjectGetPrivate(object)) {
99         LoggerD("Create calendar attendee private object.");
100         EventAttendeePtr attendee( new EventAttendee() );
101         AttendeePrivateObject *priv = new AttendeePrivateObject(context, attendee);
102         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
103             delete priv;
104         }
105     } else {
106         LoggerD("Private object already set.");
107     }
108 }
109
110 void JSCalendarAttendee::finalize(JSObjectRef object)
111 {
112     AttendeePrivateObject* priv = static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
113     if (priv) {
114         delete priv;
115         JSObjectSetPrivate(object, NULL);
116     }
117 }
118
119 EventAttendeePtr JSCalendarAttendee::getPrivateObject(JSObjectRef object)
120 {
121     AttendeePrivateObject *priv =
122         static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
123     if (!priv) {
124         ThrowMsg(NullPointerException, "Private object is null.");
125     }
126     EventAttendeePtr result = priv->getObject();
127     if (!result) {
128         ThrowMsg(NullPointerException, "Private object is null.");
129     }
130     return result;
131 }
132
133 void JSCalendarAttendee::setPrivateObject(const EventAttendeePtr &attendee,
134         JSContextRef ctx,
135         const JSObjectRef object)
136 {
137     Try
138     {
139         AttendeePrivateObject *priv =
140             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
141         delete priv;
142         priv = new AttendeePrivateObject(ctx, attendee);
143         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
144             delete priv;
145         }
146     }
147     Catch(Exception)
148     {
149         LoggerE("Error during replacing attendee object");
150     }
151 }
152
153 JSObjectRef DLL_EXPORT JSCalendarAttendee::constructor(JSContextRef context,
154     JSObjectRef constructor,
155     size_t argumentCount,
156     const JSValueRef arguments[],
157     JSValueRef* exception)
158 {
159     Try
160     {
161         CalendarConverter converter(context);
162         EventAttendeePtr attendee;
163
164         if (argumentCount==0) {
165             EventAttendeePtr result(new EventAttendee());
166             attendee = result;
167         } else if (argumentCount==1) {
168             EventAttendeePtr result(new EventAttendee());
169             attendee = result;
170             attendee->setURI(converter.toString(arguments[0]));
171         } else if (argumentCount>=2) {
172             if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
173                 EventAttendeePtr result(new EventAttendee());
174                 attendee = result;
175             } else {
176                 if (!JSValueIsObject(context, arguments[1])) {
177                     ThrowMsg(ConversionException, "Wrong second parameter type.");
178                 }
179
180                 attendee = converter.toAttendee(arguments[1]);
181                 if (!attendee) {
182                     ThrowMsg(ConversionException, "Parameter conversion failed.");
183                 }
184             }
185             attendee->setURI(converter.toString(arguments[0]));
186         }
187
188         return createJSCalendarAttendee(context, attendee);
189     }
190     Catch(UnsupportedException)
191     {
192         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
193                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
194         *exception = error;
195                 return error;
196     }
197     Catch(InvalidArgumentException)
198     {
199         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
200                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
201         *exception = error;
202                 return error;
203     }
204     Catch(ConversionException)
205     {
206         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
207                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
208         *exception = error;
209                 return error;
210     }
211     Catch(Exception)
212     {
213         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
214                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
215         *exception = error;
216                 return error;
217     }
218 }
219
220 JSValueRef JSCalendarAttendee::getProperty(JSContextRef context,
221         JSObjectRef object,
222         JSStringRef propertyName,
223         JSValueRef* exception)
224 {
225     CalendarConverter converter(context);
226     Try
227     {
228         AttendeePrivateObject* priv =
229             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
230         if (!priv) {
231             ThrowMsg(NullPointerException, "Private object is NULL.");
232         }
233         EventAttendeePtr attendee = priv->getObject();
234
235         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
236             return converter.toJSValueRef(attendee->getName());
237         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
238             return converter.toJSValueRef(attendee->getURI());
239         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
240             return converter.toJSValueRef(converter.toTizenValue(attendee->getRole()));
241         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
242             return converter.toJSValueRef(converter.toTizenValue(attendee->getStatus()));
243         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
244             return converter.toJSValueRef(attendee->getRSVP());
245         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
246            return converter.toJSValueRef(converter.toTizenValue(attendee->getType()));
247         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
248             return converter.toJSValueRef(attendee->getGroup());
249         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
250             return converter.toJSValueRef(attendee->getDelegatorURI());
251         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
252             return converter.toJSValueRef(attendee->getDelegateURI());
253         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
254             DeviceAPI::Contact::ContactConverter contactConverter(context);
255             DeviceAPI::Contact::ContactRefPtr contactRefPtr( new DeviceAPI::Contact::ContactRef() );
256             contactRefPtr->setContactId(attendee->getContactId());
257             contactRefPtr->setAddressBookId(attendee->getAddressBookId());
258             return contactConverter.toJSValueRef(contactRefPtr);
259         }
260     }
261     Catch(Exception)
262     {
263                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
264     }
265     return JSValueMakeUndefined(context);
266 }
267
268 bool JSCalendarAttendee::setProperty(JSContextRef context,
269         JSObjectRef object,
270         JSStringRef propertyName,
271         JSValueRef value,
272         JSValueRef* exception)
273 {
274     CalendarConverter converter(context);
275     Try
276     {
277         AttendeePrivateObject* priv =
278             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
279         if (!priv) {
280             ThrowMsg(NullPointerException, "Attendee object is NULL.");
281         }
282         EventAttendeePtr attendee = priv->getObject();
283
284         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
285             std::string name = converter.toString(value);
286             attendee->setName(name);
287             return true;
288         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
289             std::string uri = converter.toString(value);
290             attendee->setURI(uri);
291             return true;
292         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
293             std::string role = converter.toString(value);
294             attendee->setRole(converter.toEventAttendeeRole(role));
295             return true;
296         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
297             std::string status = converter.toString(value);
298             attendee->setStatus(converter.toEventAttendeeStatus(status));
299             return true;
300         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
301             bool RSVP = converter.toBool(value);
302             attendee->setRSVP(RSVP);
303             return true;
304         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
305             std::string type = converter.toString(value);
306             attendee->setType(converter.toEventAttendeeType(type));
307             return true;
308         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
309             std::string group = converter.toString(value);
310             attendee->setGroup(group);
311             return true;
312         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
313             std::string delegatorURI = converter.toString(value);
314             attendee->setDelegatorURI(delegatorURI);
315             return true;
316         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
317             std::string delegateURI = converter.toString(value);
318             attendee->setDelegateURI(delegateURI);
319             return true;
320         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
321             if (!JSValueIsObjectOfClass(context, value, DeviceAPI::Contact::JSContactRef::getClassRef())) {
322                 ThrowMsg(ConversionException, "Wrong contactRef type.");
323             }
324
325             DeviceAPI::Contact::ContactConverter contactConverter(context);
326             attendee->setContactId(contactConverter.toContactRef(value)->getContactId());
327             attendee->setAddressBookId(contactConverter.toContactRef(value)->getAddressBookId());
328             return true;
329         }
330     }
331     Catch(Exception)
332     {
333                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
334     }
335
336     return true;
337 }
338
339 }
340 }