merge wrt-plugins-tizen_0.2.0-12
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactPhoneNumber.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file        JSContactPhoneNumber.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactPhoneNumber class
22  */
23
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 //#include <Tizen/Common/JSGlobalContextFactory.h>
30 #include "ContactConverter.h"
31 #include "JSContactPhoneNumberTypeArray.h"
32 #include "JSContactPhoneNumber.h"
33
34 #define FILTER_CLASS_NAME "ContactPhoneNumber"
35
36 #define CONTACT_ATTR_NUMBER "number"
37 #define CONTACT_ATTR_TYPES "types"
38
39 namespace TizenApis {
40 namespace Tizen1_0 {
41 namespace Contact {
42
43 using namespace TizenApis::Commons;
44 using namespace TizenApis::Api::Contact;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47
48 JSClassDefinition JSContactPhoneNumber::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         FILTER_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 JSContactPhoneNumber::m_property[] = {
70         { CONTACT_ATTR_NUMBER, getNumber, setNumber, kJSPropertyAttributeNone },
71         { CONTACT_ATTR_TYPES, getTypes, setTypes, 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                 LogError("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         LogDebug("entered");
161
162 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
163 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
164
165         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
166         if (!priv) {
167                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
168         }
169         JSContextRef gContext = priv->getContext();
170
171 //      JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
172
173         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
174         Try {
175                 if (argumentCount < 1 || argumentCount > 2)
176                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
177
178                 if (!JSValueIsString(gContext, arguments[0]))
179                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
180
181                 if (argumentCount >= 2)
182                 {
183                         if (!JSIsArrayValue(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
184                                 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
185                 }
186
187         } Catch(Exception ) {
188                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
189                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
190                 return NULL;
191         }
192
193         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
194
195         std::string number;
196         ContactPhoneNumberTypeArrayPtr types(NULL);
197
198         Try {
199                 number = converter->toString(arguments[0]);
200         } Catch(Exception) {
201                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
202                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
203                 return NULL;
204         }
205
206         Try {
207                 if(argumentCount >= 2)
208                 {
209                         if(JSIsArrayValue(gContext, arguments[1]))
210                                 types = converter->toContactPhoneNumberTypeArray(arguments[1]);
211                 }
212                 if(types == NULL)
213                 {
214                         types = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
215                         types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
216                 }
217         } Catch(Exception) {
218                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
219                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
220                 return NULL;
221         }
222
223         ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
224         contactPhoneNumber->setNumber(number);
225         contactPhoneNumber->setTypes(types);
226
227         JSObjectRef jsobject;
228
229         Try {
230                 jsobject = createJSObject(gContext, contactPhoneNumber);
231         } Catch(Exception) {
232                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
233                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
234                 return NULL;
235         }
236
237         return jsobject;
238 }
239
240 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
241                 JSObjectRef constructor,
242                 JSValueRef possibleInstance,
243                 JSValueRef* exception)
244 {
245         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
246 }
247
248 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyName,
251                 JSValueRef* exception)
252 {
253         Try
254         {
255                 ContactConverterFactory::ConverterType converter =
256                                 ContactConverterFactory::getConverter(context);
257                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
258                 return converter->toJSValueRef(contactPhoneNumber->getNumber());
259         }
260         Catch(WrtDeviceApis::Commons::Exception)
261         {
262                 LogWarning("trying to get incorrect value");
263         }
264         return JSValueMakeUndefined(context);
265 }
266
267 bool JSContactPhoneNumber::setNumber(JSContextRef context,
268                 JSObjectRef object,
269                 JSStringRef propertyName,
270                 JSValueRef value,
271                 JSValueRef* exception)
272 {
273         Try
274         {
275                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
276                 ContactConverterFactory::ConverterType converter =
277                                 ContactConverterFactory::getConverter(context);
278                 contactPhoneNumber->setNumber(converter->toString(value));
279                 return true;
280         }
281         Catch(WrtDeviceApis::Commons::Exception)
282         {
283                 LogWarning("trying to set incorrect value");
284         }
285         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
286         return false;
287 }
288
289
290 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
291                 JSObjectRef object,
292                 JSStringRef propertyName,
293                 JSValueRef* exception)
294 {
295         Try
296         {
297                 ContactConverterFactory::ConverterType converter =
298                                 ContactConverterFactory::getConverter(context);
299                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
300
301                 return converter->toJSValueRef(contactPhoneNumber->getTypes());
302         }
303         Catch(WrtDeviceApis::Commons::Exception)
304         {
305                 LogWarning("trying to get incorrect value");
306         }
307         return JSValueMakeUndefined(context);
308 }
309
310 bool JSContactPhoneNumber::setTypes(JSContextRef context,
311                 JSObjectRef object,
312                 JSStringRef propertyName,
313                 JSValueRef value,
314                 JSValueRef* exception)
315 {
316         Try
317         {
318                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
319                 ContactConverterFactory::ConverterType converter =
320                                 ContactConverterFactory::getConverter(context);
321
322                 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
323
324                 return true;
325         }
326         Catch(WrtDeviceApis::Commons::Exception)
327         {
328                 LogWarning("trying to set incorrect value");
329         }
330         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
331         return false;
332 }
333
334 } // Contact
335 } // Tizen1_0
336 } // TizenApis