wrt-plugins-tizen_0.4.23
[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
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace Calendar {
36
37 JSClassDefinition JSCalendarAttendee::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     TIZEN_INTERFACE_CALENDAR_ATTENDEE,
41     0,
42     m_property,
43     NULL, //m_function,
44     initialize,
45     finalize,
46     NULL, //hasProperty,
47     NULL, //getProperty,
48     NULL, //setProperty,
49     NULL, //deleteProperty,
50     NULL, //getPropertyNames,
51     NULL, //callAsFunction,
52     constructor,
53     NULL, //hasInstance,
54     NULL, //convertToType,
55 };
56
57 JSStaticValue JSCalendarAttendee::m_property[] = {
58     { TIZEN_ATTENDEE_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
59     { TIZEN_ATTENDEE_URI, getProperty, setProperty, kJSPropertyAttributeNone },
60     { TIZEN_ATTENDEE_ROLE, getProperty, setProperty, kJSPropertyAttributeNone },
61     { TIZEN_ATTENDEE_STATUS, getProperty, setProperty, kJSPropertyAttributeNone },
62     { TIZEN_ATTENDEE_RSVP, getProperty, setProperty, kJSPropertyAttributeNone },
63     { TIZEN_ATTENDEE_TYPE, getProperty, setProperty, kJSPropertyAttributeNone },
64     { TIZEN_ATTENDEE_GROUP, getProperty, setProperty, kJSPropertyAttributeNone },
65     { TIZEN_ATTENDEE_DELEGATORURI, getProperty, setProperty, kJSPropertyAttributeNone },
66     { TIZEN_ATTENDEE_DELEGATEURI, getProperty, setProperty, kJSPropertyAttributeNone },
67     { TIZEN_ATTENDEE_CONTACT_REF, getProperty, setProperty, kJSPropertyAttributeNone },
68
69     { 0, 0, 0, 0 }
70 };
71
72 JSClassRef JSCalendarAttendee::m_jsClassRef = JSClassCreate(
73         JSCalendarAttendee::getClassInfo());
74
75 const JSClassDefinition* JSCalendarAttendee::getClassInfo()
76 {
77     return &(m_classInfo);
78 }
79
80 JSClassRef JSCalendarAttendee::getClassRef()
81 {
82     if (!m_jsClassRef) {
83         m_jsClassRef = JSClassCreate(&m_classInfo);
84     }
85     return m_jsClassRef;
86 }
87
88 JSObjectRef JSCalendarAttendee::createJSCalendarAttendee(JSContextRef context, EventAttendeePtr attendee)
89 {
90     AttendeePrivateObject *priv = new AttendeePrivateObject(context, attendee);
91     return JSObjectMake(context, getClassRef(), priv);
92 }
93
94 void JSCalendarAttendee::initialize(JSContextRef context,
95         JSObjectRef object)
96 {
97     if (!JSObjectGetPrivate(object)) {
98         LoggerD("Create calendar attendee private object.");
99         EventAttendeePtr attendee( new EventAttendee() );
100         AttendeePrivateObject *priv = new AttendeePrivateObject(context, attendee);
101         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
102             delete priv;
103         }
104     } else {
105         LoggerD("Private object already set.");
106     }
107 }
108
109 void JSCalendarAttendee::finalize(JSObjectRef object)
110 {
111     AttendeePrivateObject* priv = static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
112     if (priv) {
113         delete priv;
114         JSObjectSetPrivate(object, NULL);
115     }
116 }
117
118 EventAttendeePtr JSCalendarAttendee::getPrivateObject(JSObjectRef object)
119 {
120     AttendeePrivateObject *priv =
121         static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
122     if (!priv) {
123         ThrowMsg(NullPointerException, "Private object is null.");
124     }
125     EventAttendeePtr result = priv->getObject();
126     if (!result) {
127         ThrowMsg(NullPointerException, "Private object is null.");
128     }
129     return result;
130 }
131
132 void JSCalendarAttendee::setPrivateObject(const EventAttendeePtr &attendee,
133         JSContextRef ctx,
134         const JSObjectRef object)
135 {
136     Try
137     {
138         AttendeePrivateObject *priv =
139             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
140         delete priv;
141         priv = new AttendeePrivateObject(ctx, attendee);
142         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
143             delete priv;
144         }
145     }
146     Catch(Exception)
147     {
148         LoggerE("Error during replacing attendee object");
149     }
150 }
151
152 JSObjectRef JSCalendarAttendee::constructor(JSContextRef context,
153     JSObjectRef constructor,
154     size_t argumentCount,
155     const JSValueRef arguments[],
156     JSValueRef* exception)
157 {
158     Try
159     {
160         CalendarConverter converter(context);
161         EventAttendeePtr attendee;
162
163         if (argumentCount==0) {
164             EventAttendeePtr result(new EventAttendee());
165             attendee = result;
166         } else if (argumentCount==1) {
167             EventAttendeePtr result(new EventAttendee());
168             attendee = result;
169             attendee->setURI(converter.toString(arguments[0]));
170         } else if (argumentCount>=2) {
171             if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
172                 EventAttendeePtr result(new EventAttendee());
173                 attendee = result;
174             } else {
175                 if (!JSValueIsObject(context, arguments[1])) {
176                     ThrowMsg(ConversionException, "Wrong second parameter type.");
177                 }
178
179                 attendee = converter.toAttendee(arguments[1]);
180                 if (!attendee) {
181                     ThrowMsg(ConversionException, "Parameter conversion failed.");
182                 }
183             }
184             attendee->setURI(converter.toString(arguments[0]));
185         }
186
187         return createJSCalendarAttendee(context, attendee);
188     }
189     Catch(UnsupportedException)
190     {
191         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
192         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
193     }
194     Catch(InvalidArgumentException)
195     {
196         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
197         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
198     }
199     Catch(ConversionException)
200     {
201         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
202         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
203     }
204     Catch(Exception)
205     {
206         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
207         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
208     }
209
210     return NULL;
211 }
212
213 JSValueRef JSCalendarAttendee::getProperty(JSContextRef context,
214         JSObjectRef object,
215         JSStringRef propertyName,
216         JSValueRef* exception)
217 {
218     CalendarConverter converter(context);
219     Try
220     {
221         AttendeePrivateObject* priv =
222             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
223         if (!priv) {
224             ThrowMsg(NullPointerException, "Private object is NULL.");
225         }
226         EventAttendeePtr attendee = priv->getObject();
227
228         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
229             return converter.toJSValueRef(attendee->getName());
230         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
231             return converter.toJSValueRef(attendee->getURI());
232         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
233             return converter.toJSValueRef(converter.toTizenValue(attendee->getRole()));
234         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
235             return converter.toJSValueRef(converter.toTizenValue(attendee->getStatus()));
236         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
237             return converter.toJSValueRef(attendee->getRSVP());
238         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
239            return converter.toJSValueRef(converter.toTizenValue(attendee->getType()));
240         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
241             return converter.toJSValueRef(attendee->getGroup());
242         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
243             return converter.toJSValueRef(attendee->getDelegatorURI());
244         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
245             return converter.toJSValueRef(attendee->getDelegateURI());
246         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
247             DeviceAPI::Contact::ContactConverter contactConverter(context);
248             DeviceAPI::Contact::ContactRefPtr contactRefPtr( new DeviceAPI::Contact::ContactRef() );
249             contactRefPtr->setContactId(attendee->getContactId());
250             contactRefPtr->setAddressBookId(attendee->getAddressBookId());
251             return contactConverter.toJSValueRef(contactRefPtr);
252         }
253     }
254     Catch(Exception)
255     {
256                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
257     }
258     return JSValueMakeUndefined(context);
259 }
260
261 bool JSCalendarAttendee::setProperty(JSContextRef context,
262         JSObjectRef object,
263         JSStringRef propertyName,
264         JSValueRef value,
265         JSValueRef* exception)
266 {
267     CalendarConverter converter(context);
268     Try
269     {
270         AttendeePrivateObject* priv =
271             static_cast<AttendeePrivateObject*>(JSObjectGetPrivate(object));
272         if (!priv) {
273             ThrowMsg(NullPointerException, "Attendee object is NULL.");
274         }
275         EventAttendeePtr attendee = priv->getObject();
276
277         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_NAME)) {
278             std::string name = converter.toString(value);
279             attendee->setName(name);
280             return true;
281         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_URI)) {
282             std::string uri = converter.toString(value);
283             attendee->setURI(uri);
284             return true;
285         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_ROLE)) {
286             std::string role = converter.toString(value);
287             attendee->setRole(converter.toEventAttendeeRole(role));
288             return true;
289         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_STATUS)) {
290             std::string status = converter.toString(value);
291             attendee->setStatus(converter.toEventAttendeeStatus(status));
292             return true;
293         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_RSVP)) {
294             bool RSVP = converter.toBool(value);
295             attendee->setRSVP(RSVP);
296             return true;
297         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_TYPE)) {
298             std::string type = converter.toString(value);
299             attendee->setType(converter.toEventAttendeeType(type));
300             return true;
301         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_GROUP)) {
302             std::string group = converter.toString(value);
303             attendee->setGroup(group);
304             return true;
305         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATORURI)) {
306             std::string delegatorURI = converter.toString(value);
307             attendee->setDelegatorURI(delegatorURI);
308             return true;
309         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_DELEGATEURI)) {
310             std::string delegateURI = converter.toString(value);
311             attendee->setDelegateURI(delegateURI);
312             return true;
313         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_ATTENDEE_CONTACT_REF)) {
314             if (!JSValueIsObjectOfClass(context, value, DeviceAPI::Contact::JSContactRef::getClassRef())) {
315                 ThrowMsg(ConversionException, "Wrong contactRef type.");
316             }
317
318             DeviceAPI::Contact::ContactConverter contactConverter(context);
319             attendee->setContactId(contactConverter.toContactRef(value)->getContactId());
320             attendee->setAddressBookId(contactConverter.toContactRef(value)->getAddressBookId());
321             return true;
322         }
323     }
324     Catch(Exception)
325     {
326                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
327     }
328
329     return true;
330 }
331
332 }
333 }