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