wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactRef.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        JSContactRef.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactRef 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 "JSContactRef.h"
31 #include <Logger.h>
32
33 #define CONTACT_CLASS_NAME                                      "ContactRef"
34 #define CONTACT_PROP_ATTR_ADDRESS_BOOK_ID                       "addressBookId"
35 #define CONTACT_PROP_ATTR_CONTACT_ID            "contactId"
36
37 namespace DeviceAPI {
38 namespace Contact {
39
40 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43
44 JSClassDefinition JSContactRef::m_classInfo =
45 {
46         0,
47         kJSClassAttributeNone,
48         CONTACT_CLASS_NAME,
49         NULL,
50         m_property,
51         m_functions,
52         Initialize,
53         Finalize,
54         NULL, //hasProperty,
55         NULL, //GetProperty,
56         NULL, //SetProperty,
57         NULL, //DeleteProperty,
58         NULL, //getPropertyNames,
59         NULL, //CallAsFunction,
60         constructor, //CallAsConstructor,
61         hasInstance, //HasInstance,
62         NULL, //ConvertToType,
63 };
64
65 JSStaticValue JSContactRef::m_property[] = {
66         { CONTACT_PROP_ATTR_ADDRESS_BOOK_ID, getAddressBookId, setAddressBookId, kJSPropertyAttributeNone },
67         { CONTACT_PROP_ATTR_CONTACT_ID, getContactId, setContactId, kJSPropertyAttributeNone },
68         { 0, 0, 0, 0 }
69 };
70
71 JSStaticFunction JSContactRef::m_functions[] =
72 {
73         { 0, 0, 0 }
74 };
75
76 JSClassRef JSContactRef::m_classRef = JSClassCreate(&m_classInfo);
77
78 JSClassRef JSContactRef::getClassRef()
79 {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 bool JSContactRef::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 ContactRefPtr JSContactRef::getContactRef(JSContextRef context, JSValueRef value)
92 {
93         if (!isObjectOfClass(context, value)) {
94                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95         }
96
97         JSObjectRef object = JSValueToObject(context, value, NULL);
98         if (!object) {
99                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100         }
101
102         JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object));
103         if (!priv) {
104                 Throw(WrtDeviceApis::Commons::NullPointerException);
105         }
106
107         return priv->getObject();
108 }
109
110 void JSContactRef::Initialize(JSContextRef context, JSObjectRef object)
111 {
112         if (!JSObjectGetPrivate(object))
113         {
114                 ContactRefPtr name(new ContactRef());
115                 JSContactRefPriv *priv = new JSContactRefPriv(context, ContactRefPtr(name));
116                 if (!JSObjectSetPrivate(object, priv)) {
117                         delete priv;
118                 }
119         }
120 }
121
122 void JSContactRef::Finalize(JSObjectRef object)
123 {
124         JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object));
125
126         if (priv != NULL)
127                 delete (priv);
128 }
129
130 JSObjectRef JSContactRef::createJSObject(JSContextRef context, ContactRefPtr contactRef)
131 {
132         JSContactRefPriv *priv = new JSContactRefPriv(context, contactRef);
133         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
134         if (NULL == jsObjectRef)
135         {
136                 LoggerE("object creation error");
137                 return NULL;
138         }
139         return jsObjectRef;
140 }
141
142 ContactRefPtr JSContactRef::getPrivData(JSObjectRef object)
143 {
144         JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object));
145         if (!priv) {
146                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147         }
148
149         ContactRefPtr result = priv->getObject();
150         if (!result) {
151                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152         }
153
154         return result;
155 }
156
157 JSObjectRef JSContactRef::constructor(JSContextRef context,
158                 JSObjectRef constructor,
159                 size_t argumentCount,
160                 const JSValueRef arguments[],
161                 JSValueRef* exception)
162 {
163         LoggerD("entered");
164
165         JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(constructor));
166         if (!priv) {
167                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
168         }
169         JSContextRef gContext = priv->getContext();
170
171         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
172         Try {
173                 if (argumentCount < 2)
174                         ThrowMsg(InvalidArgumentException, "2nd argument must be contact id");
175
176                 if (!JSValueIsString(gContext, arguments[0]))
177                         ThrowMsg(InvalidArgumentException, "1st argument must be address book id");
178
179                 if (!JSValueIsString(gContext, arguments[1]))
180                         ThrowMsg(InvalidArgumentException, "2nd argument must be contact id");
181
182         } Catch(Exception ) {
183                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
184                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Wrong arguments");
185                 return NULL;
186         }
187
188         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
189
190         std::string addressBookId;
191         std::string contactId;
192
193         Try {
194                 addressBookId = converter->toString(arguments[0]);
195         } Catch(Exception) {
196                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
197                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
198                 return NULL;
199         }
200
201         Try {
202                 contactId = converter->toString(arguments[1]);
203         } Catch(Exception) {
204                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
205                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
206                 return NULL;
207         }
208
209         ContactRefPtr contactRef(new ContactRef());
210         contactRef->setAddressBookId(addressBookId);
211         contactRef->setContactId(contactId);
212
213         JSObjectRef jsobject;
214
215         Try {
216                 jsobject = createJSObject(gContext, contactRef);
217         } Catch(Exception) {
218                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
219                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
220                 return NULL;
221         }
222
223         return jsobject;
224 }
225
226 bool JSContactRef::hasInstance(JSContextRef context,
227                 JSObjectRef constructor,
228                 JSValueRef possibleInstance,
229                 JSValueRef* exception)
230 {
231         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
232 }
233
234 JSValueRef JSContactRef::getAddressBookId(JSContextRef context,
235                 JSObjectRef object,
236                 JSStringRef propertyName,
237                 JSValueRef* exception)
238 {
239         Try
240         {
241                 ContactConverterFactory::ConverterType converter =
242                                 ContactConverterFactory::getConverter(context);
243                 ContactRefPtr contactRef = getPrivData(object);
244                 return converter->toJSValueRef(contactRef->getAddressBookId());
245         }
246         Catch(WrtDeviceApis::Commons::Exception)
247         {
248                 LoggerW("trying to get incorrect value");
249         }
250
251         return JSValueMakeUndefined(context);
252 }
253
254 bool JSContactRef::setAddressBookId(JSContextRef context,
255                 JSObjectRef object,
256                 JSStringRef propertyName,
257                 JSValueRef value,
258                 JSValueRef* exception)
259 {
260         Try
261         {
262                 ContactRefPtr contactRef = getPrivData(object);
263                 ContactConverterFactory::ConverterType converter =
264                                 ContactConverterFactory::getConverter(context);
265                 contactRef->setAddressBookId(converter->toString(value));
266                 return true;
267         }
268         Catch(WrtDeviceApis::Commons::Exception)
269         {
270                 LoggerW("trying to set incorrect value");
271         }
272
273         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
274         return false;
275 }
276
277 JSValueRef JSContactRef::getContactId(JSContextRef context,
278                 JSObjectRef object,
279                 JSStringRef propertyName,
280                 JSValueRef* exception)
281 {
282         Try
283         {
284                 ContactConverterFactory::ConverterType converter =
285                                 ContactConverterFactory::getConverter(context);
286                 ContactRefPtr contactRef = getPrivData(object);
287                 return converter->toJSValueRef(contactRef->getContactId());
288         }
289         Catch(WrtDeviceApis::Commons::Exception)
290         {
291                 LoggerW("trying to get incorrect value");
292         }
293
294         return JSValueMakeUndefined(context);
295 }
296
297 bool JSContactRef::setContactId(JSContextRef context,
298                 JSObjectRef object,
299                 JSStringRef propertyName,
300                 JSValueRef value,
301                 JSValueRef* exception)
302 {
303         Try
304         {
305                 ContactRefPtr contactRef = getPrivData(object);
306                 ContactConverterFactory::ConverterType converter =
307                                 ContactConverterFactory::getConverter(context);
308                 contactRef->setContactId(converter->toString(value));
309                 return true;
310         }
311         Catch(WrtDeviceApis::Commons::Exception)
312         {
313                 LoggerW("trying to set incorrect value");
314         }
315
316         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
317         return false;
318 }
319
320 } // Contact
321 } // DeviceAPI