Update change log and spec for wrt-plugins-tizen_0.4.49
[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 #define CONTACT_ATTR_LABEL "label"
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         { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
73         { 0, 0, 0, 0 }
74 };
75
76 JSStaticFunction JSContactPhoneNumber::m_functions[] =
77 {
78         { 0, 0, 0 }
79 };
80
81 JSClassRef JSContactPhoneNumber::m_classRef = JSClassCreate(&m_classInfo);
82
83 JSClassRef DLL_EXPORT JSContactPhoneNumber::getClassRef() {
84         if (!m_classRef) {
85                 m_classRef = JSClassCreate(&m_classInfo);
86         }
87         return m_classRef;
88 }
89
90 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
91 {
92         return JSValueIsObjectOfClass(context, value, getClassRef());
93 }
94
95 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
96 {
97         if (!isObjectOfClass(context, value)) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSObjectRef object = JSValueToObject(context, value, NULL);
101         if (!object) {
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103         }
104         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
105         if (!priv) {
106                 Throw(WrtDeviceApis::Commons::NullPointerException);
107         }
108         return priv->getObject();
109 }
110
111 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
112 {
113         if (!JSObjectGetPrivate(object))
114         {
115                 ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
116                 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, ContactPhoneNumberPtr(phoneNumber));
117                 if (!JSObjectSetPrivate(object, priv)) {
118                         delete priv;
119                 }
120         }
121 }
122
123 void JSContactPhoneNumber::Finalize(JSObjectRef object)
124 {
125         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
126
127         if (priv != NULL)
128                 delete (priv);
129 }
130
131 JSObjectRef JSContactPhoneNumber::createJSObject(JSContextRef context, ContactPhoneNumberPtr contactPhoneNumber)
132 {
133         JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, contactPhoneNumber);
134         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
135         if (NULL == jsObjectRef) {
136                 LoggerE("object creation error");
137                 return NULL;
138         }
139         return jsObjectRef;
140 }
141
142 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
143 {
144         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
145         if (!priv) {
146                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147         }
148         ContactPhoneNumberPtr result = priv->getObject();
149         if (!result) {
150                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151         }
152         return result;
153 }
154
155 JSObjectRef JSContactPhoneNumber::constructor(JSContextRef context,
156                 JSObjectRef constructor,
157                 size_t argumentCount,
158                 const JSValueRef arguments[],
159                 JSValueRef* exception)
160 {
161         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
162         if (!priv) {
163                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
164         }
165         JSContextRef gContext = priv->getContext();
166         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
167
168         std::string number;
169         ContactPhoneNumberTypeArrayPtr types(NULL);
170         bool isDefault = false;
171         std::string label;
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                 if (argumentCount >= 4)
190                         label = Argvalidator.toString(3, false);
191
192         } catch (const TypeMismatchException& err ) {
193                 JSWebAPIErrorFactory::postException(context, exception, err);
194                 return NULL;
195         } catch(const BasePlatformException& err) {
196                 JSWebAPIErrorFactory::postException(context, exception, err);
197                 return NULL;
198         } catch(const ConversionException& err) {
199                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
200                 return NULL;
201         } catch(const NullPointerException& err) {
202                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
203                 return NULL;
204         }
205
206         ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
207         contactPhoneNumber->setNumber(number);
208         contactPhoneNumber->setTypes(types);
209         contactPhoneNumber->setIsDefault(isDefault);
210         contactPhoneNumber->setLabel(label);
211
212         JSObjectRef jsobject;
213
214         Try {
215                 jsobject = createJSObject(gContext, contactPhoneNumber);
216         } Catch(Exception) {
217                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
218                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
219                 return NULL;
220         }
221
222         return jsobject;
223 }
224
225 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
226                 JSObjectRef constructor,
227                 JSValueRef possibleInstance,
228                 JSValueRef* exception)
229 {
230         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
231 }
232
233 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
234                 JSObjectRef object,
235                 JSStringRef propertyName,
236                 JSValueRef* exception)
237 {
238         Try
239         {
240                 ContactConverterFactory::ConverterType converter =
241                                 ContactConverterFactory::getConverter(context);
242                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
243                 return converter->toJSValueRef(contactPhoneNumber->getNumber());
244         }
245         Catch(WrtDeviceApis::Commons::Exception)
246         {
247                 LoggerW("trying to get incorrect value");
248         }
249         return JSValueMakeUndefined(context);
250 }
251
252 bool JSContactPhoneNumber::setNumber(JSContextRef context,
253                 JSObjectRef object,
254                 JSStringRef propertyName,
255                 JSValueRef value,
256                 JSValueRef* exception)
257 {
258         Try
259         {
260                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
261                 ContactConverterFactory::ConverterType converter =
262                                 ContactConverterFactory::getConverter(context);
263                 contactPhoneNumber->setNumber(converter->toString(value));
264         }
265         Catch(WrtDeviceApis::Commons::Exception)
266         {
267                 LoggerW("trying to set incorrect value");
268                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
269         }
270         return true;
271 }
272
273
274 JSValueRef JSContactPhoneNumber::getIsDefault(JSContextRef context,
275                 JSObjectRef object,
276                 JSStringRef propertyName,
277                 JSValueRef* exception)
278 {
279         Try
280         {
281                 ContactConverterFactory::ConverterType converter =
282                                 ContactConverterFactory::getConverter(context);
283                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
284                 LoggerD("contactPhoneNumber->getIsDefault() : " << contactPhoneNumber->getIsDefault());
285
286                 return converter->toJSValueRef(contactPhoneNumber->getIsDefault());
287         }
288         Catch(WrtDeviceApis::Commons::Exception)
289         {
290                 LoggerW("trying to get incorrect value");
291         }
292         return JSValueMakeUndefined(context);
293 }
294
295 bool JSContactPhoneNumber::setIsDefault(JSContextRef context,
296                 JSObjectRef object,
297                 JSStringRef propertyName,
298                 JSValueRef value,
299                 JSValueRef* exception)
300 {
301         Try
302         {
303                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
304                 ContactConverterFactory::ConverterType converter =
305                                 ContactConverterFactory::getConverter(context);
306                 BasicValidator validator =
307                                 BasicValidatorFactory::getValidator(context, exception);
308                 contactPhoneNumber->setIsDefault(converter->toBool(value));
309         }
310         Catch(WrtDeviceApis::Commons::Exception)
311         {
312                 LoggerW("trying to set incorrect value");
313                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
314         }
315         return true;
316 }
317
318
319 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
320                 JSObjectRef object,
321                 JSStringRef propertyName,
322                 JSValueRef* exception)
323 {
324         Try
325         {
326                 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
327                 if (!priv) {
328                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
329                 }
330                 JSContextRef gContext = priv->getContext();
331                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
332                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
333
334                 if(contactPhoneNumber->IsTypesSetJSArray()){
335                         return contactPhoneNumber->getTypesJSObj();
336                 }else{
337                         JSValueRef tempJSValue = contactPhoneNumber->getTypesJSArray();
338                         tempJSValue = converter->toJSValueRef(contactPhoneNumber->getTypes());
339
340                         JSObjectRef convertedJSObject = contactPhoneNumber->getTypesJSObj();
341                         convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
342                         contactPhoneNumber->setTypesJSArray(true, convertedJSObject);
343
344                         JSValueProtect(gContext, convertedJSObject);
345                         contactPhoneNumber->setContext(gContext);
346                         return tempJSValue;
347                 }
348         }
349         Catch(WrtDeviceApis::Commons::Exception)
350         {
351                 LoggerW("trying to get incorrect value");
352         }
353         return JSValueMakeUndefined(context);
354 }
355
356 bool JSContactPhoneNumber::setTypes(JSContextRef context,
357                 JSObjectRef object,
358                 JSStringRef propertyName,
359                 JSValueRef value,
360                 JSValueRef* exception)
361 {
362         Try
363         {
364                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
365                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
366
367                 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
368                 contactPhoneNumber->resetTypesJSObj();
369         }
370         Catch(WrtDeviceApis::Commons::Exception)
371         {
372                 LoggerW("trying to set incorrect value");
373                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
374         }
375         return true;
376 }
377
378 JSValueRef JSContactPhoneNumber::getLabel(JSContextRef context,
379                 JSObjectRef object,
380                 JSStringRef propertyName,
381                 JSValueRef* exception)
382 {
383         Try
384         {
385                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
386                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
387
388                 return converter->toJSValueRef(contactPhoneNumber->getLabel());
389         }
390         Catch(WrtDeviceApis::Commons::Exception)
391         {
392                 LoggerW("trying to get incorrect value");
393         }
394         return JSValueMakeUndefined(context);
395 }
396
397 bool JSContactPhoneNumber::setLabel(JSContextRef context,
398                 JSObjectRef object,
399                 JSStringRef propertyName,
400                 JSValueRef value,
401                 JSValueRef* exception)
402 {
403         Try
404         {
405                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
406                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
407                 contactPhoneNumber->setLabel(converter->toString(value));
408         }
409         Catch(WrtDeviceApis::Commons::Exception)
410         {
411                 LoggerW("trying to set incorrect value");
412                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
413         }
414         return true;
415 }
416
417 } // Contact
418 } // DeviceAPI