Beta merge 2
[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/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactPhoneNumberTypeArray.h"
31 #include "JSContactPhoneNumber.h"
32
33 #define FILTER_CLASS_NAME "ContactPhoneNumber"
34
35 #define CONTACT_ATTR_NUMBER "number"
36 #define CONTACT_ATTR_TYPES "types"
37
38 namespace TizenApis {
39 namespace Tizen1_0 {
40 namespace Contact {
41
42 using namespace TizenApis::Commons;
43 using namespace TizenApis::Api::Contact;
44
45 JSClassRef JSContactPhoneNumber::m_classRef = NULL;
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,
63         NULL,
64         NULL,
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         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSContactPhoneNumber::m_functions[] =
75 {
76         { 0, 0, 0 }
77 };
78
79 JSClassRef JSContactPhoneNumber::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 JSValueRef JSContactPhoneNumber::createJSObject(JSContextRef context
87                 // FIXME
88                 )
89 {
90         ContactPhoneNumberPtr privateData = ContactPhoneNumberPtr(new ContactPhoneNumber());
91         JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, privateData);
92         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
93         if (NULL == jsValueRef) {
94                 LogError("object creation error");
95                 return JSValueMakeUndefined(context);
96         }
97         return jsValueRef;
98 }
99
100 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
101 {
102         return JSValueIsObjectOfClass(context, value, getClassRef());
103 }
104
105 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
106 {
107         if (!isObjectOfClass(context, value)) {
108                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
109         }
110         JSObjectRef object = JSValueToObject(context, value, NULL);
111         if (!object) {
112                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
113         }
114         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
115         if (!priv) {
116                 Throw(WrtDeviceApis::Commons::NullPointerException);
117         }
118         return priv->getObject();
119 }
120
121 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
122 {
123         assert(NULL != JSObjectGetPrivate(object));
124 }
125
126 void JSContactPhoneNumber::Finalize(JSObjectRef object)
127 {
128         //delete (JSObjectGetPrivate(object));
129 }
130
131 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
132 {
133         //LogDebug("entered");
134         JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
135         if (!priv) {
136                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
137         }
138         ContactPhoneNumberPtr result = priv->getObject();
139         if (!result) {
140                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
141         }
142         return result;
143 }
144
145 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
146                 JSObjectRef object,
147                 JSStringRef propertyName,
148                 JSValueRef* exception)
149 {
150         //LogDebug("entered");
151         Try
152         {
153                 ContactConverterFactory::ConverterType converter =
154                                 ContactConverterFactory::getConverter(context);
155                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
156                 return converter->toJSValueRef(contactPhoneNumber->getNumber());
157         }
158         Catch(WrtDeviceApis::Commons::Exception)
159         {
160                 LogWarning("trying to get incorrect value");
161         }
162         return JSValueMakeUndefined(context);
163 }
164
165 bool JSContactPhoneNumber::setNumber(JSContextRef context,
166                 JSObjectRef object,
167                 JSStringRef propertyName,
168                 JSValueRef value,
169                 JSValueRef* exception)
170 {
171         Try
172         {
173                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
174                 ContactConverterFactory::ConverterType converter =
175                                 ContactConverterFactory::getConverter(context);
176                 contactPhoneNumber->setNumber(converter->toString(value));
177                 return true;
178         }
179         Catch(WrtDeviceApis::Commons::Exception)
180         {
181                 LogWarning("trying to set incorrect value");
182         }
183         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
184         return false;
185 }
186
187
188 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
189                 JSObjectRef object,
190                 JSStringRef propertyName,
191                 JSValueRef* exception)
192 {
193         //LogDebug("entered");
194         Try
195         {
196                 ContactConverterFactory::ConverterType converter =
197                                 ContactConverterFactory::getConverter(context);
198                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
199                 //return JSContactPhoneNumberTypeArray::createArray(context, contactPhoneNumber->getTypes());
200                 return converter->toJSValueRef(contactPhoneNumber->getTypes());
201         }
202         Catch(WrtDeviceApis::Commons::Exception)
203         {
204                 LogWarning("trying to get incorrect value");
205         }
206         return JSValueMakeUndefined(context);
207 }
208
209 bool JSContactPhoneNumber::setTypes(JSContextRef context,
210                 JSObjectRef object,
211                 JSStringRef propertyName,
212                 JSValueRef value,
213                 JSValueRef* exception)
214 {
215         Try
216         {
217                 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
218                 ContactConverterFactory::ConverterType converter =
219                                 ContactConverterFactory::getConverter(context);
220
221                 if(JSContactPhoneNumberTypeArray::isObjectOfClass(context, value)) {
222                         contactPhoneNumber->setTypes(converter->toStringArray(value));
223                         //contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));        // TODO to implement this function on converter
224                 } else {
225                         contactPhoneNumber->setTypes(converter->toStringArray(value));
226 //                      contactPhoneNumber->setTypes(JSContactPhoneNumberTypeArray::getContactPhoneNumberTypeArray(context, value));
227                 }
228
229                 return true;
230         }
231         Catch(WrtDeviceApis::Commons::Exception)
232         {
233                 LogWarning("trying to set incorrect value");
234         }
235         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
236         return false;
237 }
238
239 } // Contact
240 } // Tizen1_0
241 } // TizenApis