wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactPhoneNumber.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        JSContactPhoneNumber.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactPhoneNumber class
23  */
24
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactPhoneNumberTypeArray.h"
31 #include "JSContactPhoneNumber.h"
32 #include <Logger.h>
33
34 #define FILTER_CLASS_NAME "ContactPhoneNumber"
35
36 #define CONTACT_ATTR_NUMBER "number"
37 #define CONTACT_ATTR_TYPES "types"
38 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
39
40 namespace DeviceAPI {
41 namespace Contact {
42
43 using namespace DeviceAPI::Common;
44 using namespace WrtDeviceApis::Commons;
45 using namespace WrtDeviceApis::CommonsJavaScript;
46
47 JSClassDefinition JSContactPhoneNumber::m_classInfo =
48 {
49         0,
50         kJSClassAttributeNone,
51         FILTER_CLASS_NAME,
52         NULL,
53         m_property,
54         m_functions,
55         Initialize,
56         Finalize,
57         NULL, //hasProperty,
58         NULL, //GetProperty,
59         NULL, //SetProperty,
60         NULL, //DeleteProperty,
61         NULL, //getPropertyNames,
62         NULL, //CallAsFunction,
63         constructor, //CallAsConstructor,
64         hasInstance, //HasInstance,
65         NULL, //ConvertToType,
66 };
67
68 JSStaticValue JSContactPhoneNumber::m_property[] = {
69         { CONTACT_ATTR_NUMBER, getNumber, setNumber, kJSPropertyAttributeNone },
70         { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
71         { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
72         { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSContactPhoneNumber::m_functions[] =
76 {
77         { 0, 0, 0 }
78 };
79
80 JSClassRef JSContactPhoneNumber::m_classRef = JSClassCreate(&m_classInfo);
81
82 JSClassRef JSContactPhoneNumber::getClassRef() {
83         if (!m_classRef) {
84                 m_classRef = JSClassCreate(&m_classInfo);
85         }
86         return m_classRef;
87 }
88
89 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
90 {
91         return JSValueIsObjectOfClass(context, value, getClassRef());
92 }
93
94 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
95 {
96         if (!isObjectOfClass(context, value)) {
97                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98         }
99         JSObjectRef object = JSValueToObject(context, value, NULL);
100         if (!object) {
101                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102         }
103         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
104         if (!priv) {
105                 Throw(WrtDeviceApis::Commons::NullPointerException);
106         }
107         return priv->getObject();
108 }
109
110 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
111 {
112         if (!JSObjectGetPrivate(object))
113         {
114                 ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
115                 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, ContactPhoneNumberPtr(phoneNumber));
116                 if (!JSObjectSetPrivate(object, priv)) {
117                         delete priv;
118                 }
119         }
120 }
121
122 void JSContactPhoneNumber::Finalize(JSObjectRef object)
123 {
124         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
125
126         if (priv != NULL)
127                 delete (priv);
128 }
129
130 JSObjectRef JSContactPhoneNumber::createJSObject(JSContextRef context, ContactPhoneNumberPtr contactPhoneNumber)
131 {
132         JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, contactPhoneNumber);
133         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
134         if (NULL == jsObjectRef) {
135                 LoggerE("object creation error");
136                 return NULL;
137         }
138         return jsObjectRef;
139 }
140
141 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
142 {
143         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
144         if (!priv) {
145                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146         }
147         ContactPhoneNumberPtr result = priv->getObject();
148         if (!result) {
149                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
150         }
151         return result;
152 }
153
154 JSObjectRef JSContactPhoneNumber::constructor(JSContextRef context,
155                 JSObjectRef constructor,
156                 size_t argumentCount,
157                 const JSValueRef arguments[],
158                 JSValueRef* exception)
159 {
160         LoggerD("entered");
161
162         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
163         if (!priv) {
164                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165         }
166         JSContextRef gContext = priv->getContext();
167         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
168
169         std::string number;
170         ContactPhoneNumberTypeArrayPtr types(NULL);
171         bool isDefault = false;
172
173         try {
174                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
175                 number = Argvalidator.toString(0, false);
176
177                 if (argumentCount >= 2){
178                         Argvalidator.toArrayObject(1, true);
179                         types = converter->toContactPhoneNumberTypeArray(arguments[1]);
180                 }else
181                         types = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
182
183                 if(types->size() == 0)
184                         types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
185
186                 if (argumentCount >= 3)
187                         isDefault = Argvalidator.toBool(2, false);
188
189         } catch (const TypeMismatchException& err ) {
190                 JSWebAPIError::throwException(context, exception, err);
191                 return NULL;
192         } catch(const BasePlatformException& err) {
193                 JSWebAPIError::throwException(context, exception, err);
194                 return NULL;
195         } catch(const ConversionException& err) {
196                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
197                 return NULL;
198         } catch(const NullPointerException& err) {
199                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
200                 return NULL;
201         }
202
203         ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
204         contactPhoneNumber->setNumber(number);
205         contactPhoneNumber->setTypes(types);
206         contactPhoneNumber->setIsDefault(isDefault);
207
208         JSObjectRef jsobject;
209
210         Try {
211                 jsobject = createJSObject(gContext, contactPhoneNumber);
212         } Catch(Exception) {
213                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
214                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215                 return NULL;
216         }
217
218         return jsobject;
219 }
220
221 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
222                 JSObjectRef constructor,
223                 JSValueRef possibleInstance,
224                 JSValueRef* exception)
225 {
226         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
227 }
228
229 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
230                 JSObjectRef object,
231                 JSStringRef propertyName,
232                 JSValueRef* exception)
233 {
234         Try
235         {
236                 ContactConverterFactory::ConverterType converter =
237                                 ContactConverterFactory::getConverter(context);
238                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
239                 return converter->toJSValueRef(contactPhoneNumber->getNumber());
240         }
241         Catch(WrtDeviceApis::Commons::Exception)
242         {
243                 LoggerW("trying to get incorrect value");
244         }
245         return JSValueMakeUndefined(context);
246 }
247
248 bool JSContactPhoneNumber::setNumber(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyName,
251                 JSValueRef value,
252                 JSValueRef* exception)
253 {
254         Try
255         {
256                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
257                 ContactConverterFactory::ConverterType converter =
258                                 ContactConverterFactory::getConverter(context);
259                 contactPhoneNumber->setNumber(converter->toString(value));
260         }
261         Catch(WrtDeviceApis::Commons::Exception)
262         {
263                 LoggerW("trying to set incorrect value");
264                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
265         }
266         return true;
267 }
268
269
270 JSValueRef JSContactPhoneNumber::getIsDefault(JSContextRef context,
271                 JSObjectRef object,
272                 JSStringRef propertyName,
273                 JSValueRef* exception)
274 {
275         Try
276         {
277                 ContactConverterFactory::ConverterType converter =
278                                 ContactConverterFactory::getConverter(context);
279                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
280                 return converter->toJSValueRef(contactPhoneNumber->getIsDefault());
281         }
282         Catch(WrtDeviceApis::Commons::Exception)
283         {
284                 LoggerW("trying to get incorrect value");
285         }
286         return JSValueMakeUndefined(context);
287 }
288
289 bool JSContactPhoneNumber::setIsDefault(JSContextRef context,
290                 JSObjectRef object,
291                 JSStringRef propertyName,
292                 JSValueRef value,
293                 JSValueRef* exception)
294 {
295         Try
296         {
297                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
298                 ContactConverterFactory::ConverterType converter =
299                                 ContactConverterFactory::getConverter(context);
300                 BasicValidator validator =
301                                 BasicValidatorFactory::getValidator(context, exception);
302                 contactPhoneNumber->setIsDefault(converter->toBool(value));
303         }
304         Catch(WrtDeviceApis::Commons::Exception)
305         {
306                 LoggerW("trying to set incorrect value");
307                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
308         }
309         return true;
310 }
311
312
313 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
314                 JSObjectRef object,
315                 JSStringRef propertyName,
316                 JSValueRef* exception)
317 {
318         Try
319         {
320                 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
321                 if (!priv) {
322                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
323                 }
324                 JSContextRef gContext = priv->getContext();
325                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
326                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
327
328                 if(contactPhoneNumber->IsTypesSetJSArray()){
329                         return contactPhoneNumber->getTypesJSObj();
330                 }else{
331                         JSValueRef tempJSValue = contactPhoneNumber->getTypesJSArray();
332                         tempJSValue = converter->toJSValueRef(contactPhoneNumber->getTypes());
333
334                         JSObjectRef convertedJSObject = contactPhoneNumber->getTypesJSObj();
335                         convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
336                         contactPhoneNumber->setTypesJSArray(true, convertedJSObject);
337
338                         JSValueProtect(gContext, convertedJSObject);
339                         contactPhoneNumber->setContext(gContext);
340                         return tempJSValue;
341                 }
342         }
343         Catch(WrtDeviceApis::Commons::Exception)
344         {
345                 LoggerW("trying to get incorrect value");
346         }
347         return JSValueMakeUndefined(context);
348 }
349
350 bool JSContactPhoneNumber::setTypes(JSContextRef context,
351                 JSObjectRef object,
352                 JSStringRef propertyName,
353                 JSValueRef value,
354                 JSValueRef* exception)
355 {
356         Try
357         {
358                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
359                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
360
361                 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
362                 contactPhoneNumber->resetTypesJSObj();
363         }
364         Catch(WrtDeviceApis::Commons::Exception)
365         {
366                 LoggerW("trying to set incorrect value");
367                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
368         }
369         return true;
370 }
371
372 } // Contact
373 } // DeviceAPI