2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSContactRef.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactRef class
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"
33 #define CONTACT_CLASS_NAME "ContactRef"
34 #define CONTACT_PROP_ATTR_ADDRESS_BOOK_ID "addressBookId"
35 #define CONTACT_PROP_ATTR_CONTACT_ID "contactId"
40 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
44 JSClassDefinition JSContactRef::m_classInfo =
47 kJSClassAttributeNone,
57 NULL, //DeleteProperty,
58 NULL, //getPropertyNames,
59 NULL, //CallAsFunction,
60 constructor, //CallAsConstructor,
61 hasInstance, //HasInstance,
62 NULL, //ConvertToType,
65 JSStaticValue JSContactRef::m_property[] = {
66 { CONTACT_PROP_ATTR_ADDRESS_BOOK_ID, getAddressBookId, setAddressBookId, kJSPropertyAttributeNone },
67 { CONTACT_PROP_ATTR_CONTACT_ID, getContactId, setContactId, kJSPropertyAttributeNone },
71 JSStaticFunction JSContactRef::m_functions[] =
76 JSClassRef JSContactRef::m_classRef = JSClassCreate(&m_classInfo);
78 JSClassRef JSContactRef::getClassRef()
81 m_classRef = JSClassCreate(&m_classInfo);
86 bool JSContactRef::isObjectOfClass(JSContextRef context, JSValueRef value)
88 return JSValueIsObjectOfClass(context, value, getClassRef());
91 ContactRefPtr JSContactRef::getContactRef(JSContextRef context, JSValueRef value)
93 if (!isObjectOfClass(context, value)) {
94 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97 JSObjectRef object = JSValueToObject(context, value, NULL);
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102 JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object));
104 Throw(WrtDeviceApis::Commons::NullPointerException);
107 return priv->getObject();
110 void JSContactRef::Initialize(JSContextRef context, JSObjectRef object)
112 if (!JSObjectGetPrivate(object))
114 ContactRefPtr name(new ContactRef());
115 JSContactRefPriv *priv = new JSContactRefPriv(context, ContactRefPtr(name));
116 if (!JSObjectSetPrivate(object, priv)) {
122 void JSContactRef::Finalize(JSObjectRef object)
124 JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object));
130 JSObjectRef JSContactRef::createJSObject(JSContextRef context, ContactRefPtr contactRef)
132 JSContactRefPriv *priv = new JSContactRefPriv(context, contactRef);
133 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
134 if (NULL == jsObjectRef)
136 LoggerE("object creation error");
142 ContactRefPtr JSContactRef::getPrivData(JSObjectRef object)
144 JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object));
146 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
149 ContactRefPtr result = priv->getObject();
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157 JSObjectRef JSContactRef::constructor(JSContextRef context,
158 JSObjectRef constructor,
159 size_t argumentCount,
160 const JSValueRef arguments[],
161 JSValueRef* exception)
165 JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(constructor));
167 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
169 JSContextRef gContext = priv->getContext();
171 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
173 if (argumentCount < 2)
174 ThrowMsg(InvalidArgumentException, "2nd argument must be contact id");
176 if (!JSValueIsString(gContext, arguments[0]))
177 ThrowMsg(InvalidArgumentException, "1st argument must be address book id");
179 if (!JSValueIsString(gContext, arguments[1]))
180 ThrowMsg(InvalidArgumentException, "2nd argument must be contact id");
182 } Catch(Exception ) {
183 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
184 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Wrong arguments");
188 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
190 std::string addressBookId;
191 std::string contactId;
194 addressBookId = converter->toString(arguments[0]);
196 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
197 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
202 contactId = converter->toString(arguments[1]);
204 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
205 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
209 ContactRefPtr contactRef(new ContactRef());
210 contactRef->setAddressBookId(addressBookId);
211 contactRef->setContactId(contactId);
213 JSObjectRef jsobject;
216 jsobject = createJSObject(gContext, contactRef);
218 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
219 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
226 bool JSContactRef::hasInstance(JSContextRef context,
227 JSObjectRef constructor,
228 JSValueRef possibleInstance,
229 JSValueRef* exception)
231 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
234 JSValueRef JSContactRef::getAddressBookId(JSContextRef context,
236 JSStringRef propertyName,
237 JSValueRef* exception)
241 ContactConverterFactory::ConverterType converter =
242 ContactConverterFactory::getConverter(context);
243 ContactRefPtr contactRef = getPrivData(object);
244 return converter->toJSValueRef(contactRef->getAddressBookId());
246 Catch(WrtDeviceApis::Commons::Exception)
248 LoggerW("trying to get incorrect value");
251 return JSValueMakeUndefined(context);
254 bool JSContactRef::setAddressBookId(JSContextRef context,
256 JSStringRef propertyName,
258 JSValueRef* exception)
262 ContactRefPtr contactRef = getPrivData(object);
263 ContactConverterFactory::ConverterType converter =
264 ContactConverterFactory::getConverter(context);
265 contactRef->setAddressBookId(converter->toString(value));
268 Catch(WrtDeviceApis::Commons::Exception)
270 LoggerW("trying to set incorrect value");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
277 JSValueRef JSContactRef::getContactId(JSContextRef context,
279 JSStringRef propertyName,
280 JSValueRef* exception)
284 ContactConverterFactory::ConverterType converter =
285 ContactConverterFactory::getConverter(context);
286 ContactRefPtr contactRef = getPrivData(object);
287 return converter->toJSValueRef(contactRef->getContactId());
289 Catch(WrtDeviceApis::Commons::Exception)
291 LoggerW("trying to get incorrect value");
294 return JSValueMakeUndefined(context);
297 bool JSContactRef::setContactId(JSContextRef context,
299 JSStringRef propertyName,
301 JSValueRef* exception)
305 ContactRefPtr contactRef = getPrivData(object);
306 ContactConverterFactory::ConverterType converter =
307 ContactConverterFactory::getConverter(context);
308 contactRef->setContactId(converter->toString(value));
311 Catch(WrtDeviceApis::Commons::Exception)
313 LoggerW("trying to set incorrect value");
316 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");