diff options
Diffstat (limited to 'mobile_src/Contact/JSContactRef.cpp')
-rw-r--r-- | mobile_src/Contact/JSContactRef.cpp | 320 |
1 files changed, 320 insertions, 0 deletions
diff --git a/mobile_src/Contact/JSContactRef.cpp b/mobile_src/Contact/JSContactRef.cpp new file mode 100644 index 0000000..6a0df3b --- /dev/null +++ b/mobile_src/Contact/JSContactRef.cpp @@ -0,0 +1,320 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/** + * @file JSContactRef.cpp + * @version 0.1 + * @brief Implementation of the JSContactRef class + */ + +#include <dpl/shared_ptr.h> +#include <CommonsJavaScript/Validator.h> +#include <JSWebAPIErrorFactory.h> +#include "ContactConverter.h" +#include "JSContactRef.h" +#include <Logger.h> +#include <Export.h> + +#define CONTACT_CLASS_NAME "ContactRef" +#define CONTACT_PROP_ATTR_ADDRESS_BOOK_ID "addressBookId" +#define CONTACT_PROP_ATTR_CONTACT_ID "contactId" + +namespace DeviceAPI { +namespace Contact { + +using namespace DeviceAPI::Common; +using namespace WrtDeviceApis::Commons; +using namespace WrtDeviceApis::CommonsJavaScript; + +JSClassDefinition JSContactRef::m_classInfo = +{ + 0, + kJSClassAttributeNone, + CONTACT_CLASS_NAME, + NULL, + m_property, + m_functions, + Initialize, + Finalize, + NULL, //hasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //getPropertyNames, + NULL, //CallAsFunction, + constructor, //CallAsConstructor, + hasInstance, //HasInstance, + NULL, //ConvertToType, +}; + +JSStaticValue JSContactRef::m_property[] = { + { CONTACT_PROP_ATTR_ADDRESS_BOOK_ID, getAddressBookId, setAddressBookId, kJSPropertyAttributeNone }, + { CONTACT_PROP_ATTR_CONTACT_ID, getContactId, setContactId, kJSPropertyAttributeNone }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSContactRef::m_functions[] = +{ + { 0, 0, 0 } +}; + +JSClassRef JSContactRef::m_classRef = JSClassCreate(&m_classInfo); + +JSClassRef DLL_EXPORT JSContactRef::getClassRef() +{ + if (!m_classRef) { + m_classRef = JSClassCreate(&m_classInfo); + } + return m_classRef; +} + +bool JSContactRef::isObjectOfClass(JSContextRef context, JSValueRef value) +{ + return JSValueIsObjectOfClass(context, value, getClassRef()); +} + +ContactRefPtr JSContactRef::getContactRef(JSContextRef context, JSValueRef value) +{ + if (!isObjectOfClass(context, value)) { + Throw(WrtDeviceApis::Commons::InvalidArgumentException); + } + + JSObjectRef object = JSValueToObject(context, value, NULL); + if (!object) { + Throw(WrtDeviceApis::Commons::InvalidArgumentException); + } + + JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object)); + if (!priv) { + Throw(WrtDeviceApis::Commons::NullPointerException); + } + + return priv->getObject(); +} + +void JSContactRef::Initialize(JSContextRef context, JSObjectRef object) +{ + if (!JSObjectGetPrivate(object)) + { + ContactRefPtr name(new ContactRef()); + JSContactRefPriv *priv = new JSContactRefPriv(context, ContactRefPtr(name)); + if (!JSObjectSetPrivate(object, priv)) { + delete priv; + } + } +} + +void JSContactRef::Finalize(JSObjectRef object) +{ + JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object)); + + if (priv != NULL) + delete (priv); +} + +JSObjectRef JSContactRef::createJSObject(JSContextRef context, ContactRefPtr contactRef) +{ + JSContactRefPriv *priv = new JSContactRefPriv(context, contactRef); + JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv)); + if (NULL == jsObjectRef) + { + LoggerE("object creation error"); + return NULL; + } + return jsObjectRef; +} + +ContactRefPtr JSContactRef::getPrivData(JSObjectRef object) +{ + JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(object)); + if (!priv) { + ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null"); + } + + ContactRefPtr result = priv->getObject(); + if (!result) { + ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null"); + } + + return result; +} + +JSObjectRef JSContactRef::constructor(JSContextRef context, + JSObjectRef constructor, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("entered"); + + JSContactRefPriv *priv = static_cast<JSContactRefPriv*>(JSObjectGetPrivate(constructor)); + if (!priv) { + ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null"); + } + JSContextRef gContext = priv->getContext(); + + BasicValidator validator = BasicValidatorFactory::getValidator(context, exception); + Try { + if (argumentCount < 2) + ThrowMsg(InvalidArgumentException, "2nd argument must be contact id"); + + if (!JSValueIsString(gContext, arguments[0])) + ThrowMsg(InvalidArgumentException, "1st argument must be address book id"); + + if (!JSValueIsString(gContext, arguments[1])) + ThrowMsg(InvalidArgumentException, "2nd argument must be contact id"); + + } Catch(Exception ) { + LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage()); +// *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Wrong arguments"); + return NULL; + } + + ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext); + + std::string addressBookId; + std::string contactId; + + Try { + addressBookId = converter->toString(arguments[0]); + } Catch(Exception) { + LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage()); +// *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments"); + return NULL; + } + + Try { + contactId = converter->toString(arguments[1]); + } Catch(Exception) { + LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage()); +// *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments"); + return NULL; + } + + ContactRefPtr contactRef(new ContactRef()); + contactRef->setAddressBookId(addressBookId); + contactRef->setContactId(contactId); + + JSObjectRef jsobject; + + Try { + jsobject = createJSObject(gContext, contactRef); + } Catch(Exception) { + LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage()); +// *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments"); + return NULL; + } + + return jsobject; +} + +bool JSContactRef::hasInstance(JSContextRef context, + JSObjectRef constructor, + JSValueRef possibleInstance, + JSValueRef* exception) +{ + return JSValueIsObjectOfClass(context, possibleInstance, getClassRef()); +} + +JSValueRef JSContactRef::getAddressBookId(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + ContactConverterFactory::ConverterType converter = + ContactConverterFactory::getConverter(context); + ContactRefPtr contactRef = getPrivData(object); + return converter->toJSValueRef(contactRef->getAddressBookId()); + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerW("trying to get incorrect value"); + } + + return JSValueMakeUndefined(context); +} + +bool JSContactRef::setAddressBookId(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + ContactRefPtr contactRef = getPrivData(object); + ContactConverterFactory::ConverterType converter = + ContactConverterFactory::getConverter(context); + contactRef->setAddressBookId(converter->toString(value)); + return true; + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerW("trying to set incorrect value"); + } + +// JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch"); + return false; +} + +JSValueRef JSContactRef::getContactId(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + ContactConverterFactory::ConverterType converter = + ContactConverterFactory::getConverter(context); + ContactRefPtr contactRef = getPrivData(object); + return converter->toJSValueRef(contactRef->getContactId()); + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerW("trying to get incorrect value"); + } + + return JSValueMakeUndefined(context); +} + +bool JSContactRef::setContactId(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + ContactRefPtr contactRef = getPrivData(object); + ContactConverterFactory::ConverterType converter = + ContactConverterFactory::getConverter(context); + contactRef->setContactId(converter->toString(value)); + return true; + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerW("trying to set incorrect value"); + } + +// JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch"); + return false; +} + +} // Contact +} // DeviceAPI |