wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactGroup.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 /**
19  * @file        JSContactGroup.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactGroup class
23  */
24
25 #include "JSContactGroup.h"
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
30 #include "ContactConverter.h"
31 #include <Logger.h>
32
33 #define CONTACT_CLASS_NAME                  "ContactGroup"
34 #define CONTACT_PROP_ATTR_ID                "id"
35 #define CONTACT_PROP_ATTR_ADDRESSBOOK_ID    "addressBookId"
36 #define CONTACT_PROP_ATTR_NAME              "name"
37 #define CONTACT_PROP_ATTR_RINGTONE_URI      "ringtoneURI"
38 #define CONTACT_PROP_ATTR_PHOTO_URI         "photoURI"
39 #define CONTACT_PROP_ATTR_READ_ONLY         "readOnly"
40
41 namespace DeviceAPI {
42 namespace Contact {
43
44 using namespace DeviceAPI::Common;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47
48 JSClassDefinition JSContactGroup::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         CONTACT_CLASS_NAME,
53         NULL,
54         m_property,
55         m_functions,
56         Initialize,
57         Finalize,
58         NULL, //hasProperty,
59         NULL, //GetProperty,
60         NULL, //SetProperty,
61         NULL, //DeleteProperty,
62         NULL, //getPropertyNames,
63         NULL, //CallAsFunction,
64         constructor, //CallAsConstructor,
65         hasInstance, //HasInstance,
66         NULL, //ConvertToType,
67 };
68
69 JSStaticValue JSContactGroup::m_property[] = {
70         { CONTACT_PROP_ATTR_ID, getId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
71         { CONTACT_PROP_ATTR_ADDRESSBOOK_ID, getAddressBookId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
72         { CONTACT_PROP_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
73         { CONTACT_PROP_ATTR_RINGTONE_URI, getRingtoneURI, setRingtoneURI, kJSPropertyAttributeNone },
74         { CONTACT_PROP_ATTR_PHOTO_URI, getPhotoURI, setPhotoURI, kJSPropertyAttributeNone },
75         { CONTACT_PROP_ATTR_READ_ONLY, getReadOnly, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
76         { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSContactGroup::m_functions[] =
80 {
81         { 0, 0, 0 }
82 };
83
84 JSClassRef JSContactGroup::m_classRef = JSClassCreate(&m_classInfo);
85
86 JSClassRef JSContactGroup::getClassRef()
87 {
88         if (!m_classRef) {
89                 m_classRef = JSClassCreate(&m_classInfo);
90         }
91         return m_classRef;
92 }
93
94 bool JSContactGroup::isObjectOfClass(JSContextRef context, JSValueRef value)
95 {
96         return JSValueIsObjectOfClass(context, value, getClassRef());
97 }
98
99 ContactGroupPtr JSContactGroup::getContactGroup(JSContextRef context, JSValueRef value)
100 {
101         if (!isObjectOfClass(context, value)) {
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103         }
104
105         JSObjectRef object = JSValueToObject(context, value, NULL);
106         if (!object) {
107                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
108         }
109
110         JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(object));
111         if (!priv) {
112                 Throw(WrtDeviceApis::Commons::NullPointerException);
113         }
114
115         return priv->getObject();
116 }
117
118 void JSContactGroup::Initialize(JSContextRef context, JSObjectRef object)
119 {
120         if (!JSObjectGetPrivate(object))
121         {
122                 ContactGroupPtr name(new ContactGroup());
123                 JSContactGroupPriv *priv = new JSContactGroupPriv(context, ContactGroupPtr(name));
124                 if (!JSObjectSetPrivate(object, priv)) {
125                         delete priv;
126                 }
127         }
128 }
129
130 void JSContactGroup::Finalize(JSObjectRef object)
131 {
132         JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(object));
133
134         if (priv != NULL)
135                 delete (priv);
136 }
137
138 JSObjectRef JSContactGroup::createJSObject(JSContextRef context, ContactGroupPtr contactGroup)
139 {
140         JSContactGroupPriv *priv = new JSContactGroupPriv(context, contactGroup);
141         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
142         if (NULL == jsObjectRef)
143         {
144                 LoggerE("object creation error");
145                 return NULL;
146         }
147         return jsObjectRef;
148 }
149
150 ContactGroupPtr JSContactGroup::getPrivData(JSObjectRef object)
151 {
152         JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(object));
153         if (!priv) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155         }
156
157         ContactGroupPtr result = priv->getObject();
158         if (!result) {
159                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160         }
161
162         return result;
163 }
164
165 JSObjectRef JSContactGroup::constructor(JSContextRef context,
166                 JSObjectRef constructor,
167                 size_t argumentCount,
168                 const JSValueRef arguments[],
169                 JSValueRef* exception)
170 {
171         LoggerD("entered");
172
173         JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(constructor));
174         if (!priv) {
175                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
176         }
177         JSContextRef gContext = priv->getContext();
178
179         ContactGroupPtr contactGroup = ContactGroupPtr(new ContactGroup());
180         try {
181                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
182                 contactGroup->setName(Argvalidator.toString(0, false));
183
184                 if (argumentCount >= 2)
185                         contactGroup->setRingtoneURI(Argvalidator.toString(1, true));
186
187                 if (argumentCount >= 3)
188                         contactGroup->setPhotoURI(Argvalidator.toString(2, true));
189
190         } catch (const TypeMismatchException& err ) {
191                 JSWebAPIError::throwException(context, exception, err);
192                 return NULL;
193         } catch(const BasePlatformException& err) {
194                 JSWebAPIError::throwException(context, exception, err);
195                 return NULL;
196         } catch(const ConversionException& err) {
197                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
198                 return NULL;
199         } catch(const NullPointerException& err) {
200                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
201                 return NULL;
202         }
203
204         JSObjectRef jsobject;
205
206         Try {
207                 jsobject = createJSObject(gContext, contactGroup);
208         } Catch(Exception) {
209                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
210                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
211                 return NULL;
212         }
213
214         return jsobject;
215 }
216
217 bool JSContactGroup::hasInstance(JSContextRef context,
218                 JSObjectRef constructor,
219                 JSValueRef possibleInstance,
220                 JSValueRef* exception)
221 {
222         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
223 }
224
225 JSValueRef JSContactGroup::getId(JSContextRef context,
226                 JSObjectRef object,
227                 JSStringRef propertyName,
228                 JSValueRef* exception)
229 {
230         Try
231         {
232                 ContactConverterFactory::ConverterType converter =
233                                 ContactConverterFactory::getConverter(context);
234                 ContactGroupPtr contactGroup = getPrivData(object);
235                 if(!contactGroup->getIdIsSet())
236                         return JSValueMakeNull(context);
237                 else
238                         return converter->toJSValueRef(contactGroup->getId());
239         }
240         Catch(WrtDeviceApis::Commons::Exception)
241         {
242                 LoggerW("trying to get incorrect value");
243         }
244
245         return JSValueMakeUndefined(context);
246 }
247
248 bool JSContactGroup::setId(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyName,
251                 JSValueRef value,
252                 JSValueRef* exception)
253 {
254         Try
255         {
256                 ContactGroupPtr contactGroup = getPrivData(object);
257                 ContactConverterFactory::ConverterType converter =
258                                 ContactConverterFactory::getConverter(context);
259                 BasicValidator validator =
260                                 BasicValidatorFactory::getValidator(context, exception);
261                 if(validator->isNullOrUndefined(value))
262                         contactGroup->unsetId();
263                 else
264                         contactGroup->setId(converter->toString(value));
265                 return true;
266         }
267         Catch(WrtDeviceApis::Commons::Exception)
268         {
269                 LoggerW("trying to set incorrect value");
270         }
271
272         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
273         return false;
274 }
275
276 JSValueRef JSContactGroup::getAddressBookId(JSContextRef context,
277                 JSObjectRef object,
278                 JSStringRef propertyName,
279                 JSValueRef* exception)
280 {
281         Try
282         {
283                 ContactConverterFactory::ConverterType converter =
284                                 ContactConverterFactory::getConverter(context);
285                 ContactGroupPtr contactGroup = getPrivData(object);
286                 if(!contactGroup->getAddressBookIdIsSet())
287                         return JSValueMakeNull(context);
288                 else
289                         return converter->toJSValueRef(contactGroup->getAddressBookId());
290         }
291         Catch(WrtDeviceApis::Commons::Exception)
292         {
293                 LoggerW("trying to get incorrect value");
294         }
295
296         return JSValueMakeUndefined(context);
297 }
298
299 bool JSContactGroup::setAddressBookId(JSContextRef context,
300                 JSObjectRef object,
301                 JSStringRef propertyName,
302                 JSValueRef value,
303                 JSValueRef* exception)
304 {
305         Try
306         {
307                 ContactGroupPtr contactGroup = getPrivData(object);
308                 ContactConverterFactory::ConverterType converter =
309                                 ContactConverterFactory::getConverter(context);
310                 BasicValidator validator =
311                                 BasicValidatorFactory::getValidator(context, exception);
312                 if(validator->isNullOrUndefined(value))
313                         contactGroup->unsetAddressBookId();
314                 else
315                         contactGroup->setAddressBookId(converter->toString(value));
316         }
317         Catch(WrtDeviceApis::Commons::Exception)
318         {
319                 LoggerW("trying to set incorrect value");
320                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
321         }
322
323         return true;
324 }
325
326 JSValueRef JSContactGroup::getName(JSContextRef context,
327                 JSObjectRef object,
328                 JSStringRef propertyName,
329                 JSValueRef* exception)
330 {
331         Try
332         {
333                 ContactConverterFactory::ConverterType converter =
334                                 ContactConverterFactory::getConverter(context);
335                 ContactGroupPtr contactGroup = getPrivData(object);
336                 if(!contactGroup->getNameIsSet())
337                         return JSValueMakeNull(context);
338                 else
339                         return converter->toJSValueRef(contactGroup->getName());
340         }
341         Catch(WrtDeviceApis::Commons::Exception)
342         {
343                 LoggerW("trying to get incorrect value");
344         }
345
346         return JSValueMakeUndefined(context);
347 }
348
349 bool JSContactGroup::setName(JSContextRef context,
350                 JSObjectRef object,
351                 JSStringRef propertyName,
352                 JSValueRef value,
353                 JSValueRef* exception)
354 {
355         Try
356         {
357                 ContactGroupPtr contactGroup = getPrivData(object);
358                 ContactConverterFactory::ConverterType converter =
359                                 ContactConverterFactory::getConverter(context);
360                 BasicValidator validator =
361                                 BasicValidatorFactory::getValidator(context, exception);
362                 if(validator->isNullOrUndefined(value))
363                         contactGroup->unsetName();
364                 else
365                         contactGroup->setName(converter->toString(value));
366         }
367         Catch(WrtDeviceApis::Commons::Exception)
368         {
369                 LoggerW("trying to set incorrect value");
370                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
371         }
372
373         return true;
374 }
375
376 JSValueRef JSContactGroup::getRingtoneURI(JSContextRef context,
377                 JSObjectRef object,
378                 JSStringRef propertyName,
379                 JSValueRef* exception)
380 {
381         Try
382         {
383                 ContactConverterFactory::ConverterType converter =
384                                 ContactConverterFactory::getConverter(context);
385                 ContactGroupPtr contactGroup = getPrivData(object);
386                 if(!contactGroup->getRingtoneURIIsSet())
387                         return JSValueMakeNull(context);
388                 else
389                         return converter->toJSValueRef(contactGroup->getRingtoneURI());
390         }
391         Catch(WrtDeviceApis::Commons::Exception)
392         {
393                 LoggerW("trying to get incorrect value");
394         }
395
396         return JSValueMakeUndefined(context);
397 }
398
399 bool JSContactGroup::setRingtoneURI(JSContextRef context,
400                 JSObjectRef object,
401                 JSStringRef propertyName,
402                 JSValueRef value,
403                 JSValueRef* exception)
404 {
405         Try
406         {
407                 ContactGroupPtr contactGroup = getPrivData(object);
408                 ContactConverterFactory::ConverterType converter =
409                                 ContactConverterFactory::getConverter(context);
410                 BasicValidator validator =
411                                 BasicValidatorFactory::getValidator(context, exception);
412                 if(validator->isNullOrUndefined(value))
413                         contactGroup->unsetRingtoneURI();
414                 else
415                         contactGroup->setRingtoneURI(converter->toString(value));
416         }
417         Catch(WrtDeviceApis::Commons::Exception)
418         {
419                 LoggerW("trying to set incorrect value");
420                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
421         }
422
423         return true;
424 }
425
426 JSValueRef JSContactGroup::getPhotoURI(JSContextRef context,
427                 JSObjectRef object,
428                 JSStringRef propertyName,
429                 JSValueRef* exception)
430 {
431         Try
432         {
433                 ContactConverterFactory::ConverterType converter =
434                                 ContactConverterFactory::getConverter(context);
435                 ContactGroupPtr contactGroup = getPrivData(object);
436                 if(!contactGroup->getPhotoURIIsSet())
437                         return JSValueMakeNull(context);
438                 else
439                         return converter->toJSValueRef(contactGroup->getPhotoURI());
440         }
441         Catch(WrtDeviceApis::Commons::Exception)
442         {
443                 LoggerW("trying to get incorrect value");
444         }
445
446         return JSValueMakeUndefined(context);
447 }
448
449 bool JSContactGroup::setPhotoURI(JSContextRef context,
450                 JSObjectRef object,
451                 JSStringRef propertyName,
452                 JSValueRef value,
453                 JSValueRef* exception)
454 {
455         Try
456         {
457                 ContactGroupPtr contactGroup = getPrivData(object);
458                 ContactConverterFactory::ConverterType converter =
459                                 ContactConverterFactory::getConverter(context);
460                 BasicValidator validator =
461                                 BasicValidatorFactory::getValidator(context, exception);
462                 if(validator->isNullOrUndefined(value))
463                         contactGroup->unsetRingtoneURI();
464                 else
465                         contactGroup->setPhotoURI(converter->toString(value));
466         }
467         Catch(WrtDeviceApis::Commons::Exception)
468         {
469                 LoggerW("trying to set incorrect value");
470                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
471         }
472
473         return true;
474 }
475
476 JSValueRef JSContactGroup::getReadOnly(JSContextRef context,
477                 JSObjectRef object,
478                 JSStringRef propertyName,
479                 JSValueRef* exception)
480 {
481         Try
482         {
483                 ContactConverterFactory::ConverterType converter =
484                                 ContactConverterFactory::getConverter(context);
485                 ContactGroupPtr contactGroup = getPrivData(object);
486
487                 return converter->toJSValueRef(contactGroup->getReadOnly());
488         }
489         Catch(WrtDeviceApis::Commons::Exception)
490         {
491                 LoggerW("trying to get incorrect value");
492         }
493
494         return JSValueMakeUndefined(context);
495 }
496
497 } // Contact
498 } // DeviceAPI