merge wrt-plugins-tizen_0.2.0-2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactPhoneNumber.cpp
index 50cd1e8..b17f12b 100755 (executable)
 
 #include <dpl/log/log.h>
 #include <dpl/shared_ptr.h>
-#include <CommonsJavaScript/Converter.h>
+#include <CommonsJavaScript/Validator.h>
 #include <Tizen/Common/JSTizenExceptionFactory.h>
 #include <Tizen/Common/JSTizenException.h>
+//#include <Tizen/Common/JSGlobalContextFactory.h>
 #include "ContactConverter.h"
 #include "JSContactPhoneNumberTypeArray.h"
 #include "JSContactPhoneNumber.h"
@@ -41,8 +42,8 @@ namespace Contact {
 
 using namespace TizenApis::Commons;
 using namespace TizenApis::Api::Contact;
-
-JSClassRef JSContactPhoneNumber::m_classRef = NULL;
+using namespace WrtDeviceApis::Commons;
+using namespace WrtDeviceApis::CommonsJavaScript;
 
 JSClassDefinition JSContactPhoneNumber::m_classInfo =
 {
@@ -59,9 +60,9 @@ JSClassDefinition JSContactPhoneNumber::m_classInfo =
        NULL, //SetProperty,
        NULL, //DeleteProperty,
        NULL, //getPropertyNames,
-       NULL,
-       NULL,
-       NULL,
+       NULL, //CallAsFunction,
+       constructor, //CallAsConstructor,
+       hasInstance, //HasInstance,
        NULL, //ConvertToType,
 };
 
@@ -76,6 +77,8 @@ JSStaticFunction JSContactPhoneNumber::m_functions[] =
        { 0, 0, 0 }
 };
 
+JSClassRef JSContactPhoneNumber::m_classRef = JSClassCreate(&m_classInfo);
+
 JSClassRef JSContactPhoneNumber::getClassRef() {
        if (!m_classRef) {
                m_classRef = JSClassCreate(&m_classInfo);
@@ -83,20 +86,6 @@ JSClassRef JSContactPhoneNumber::getClassRef() {
        return m_classRef;
 }
 
-JSValueRef JSContactPhoneNumber::createJSObject(JSContextRef context
-               // FIXME
-               )
-{
-       ContactPhoneNumberPtr privateData = ContactPhoneNumberPtr(new ContactPhoneNumber());
-       JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, privateData);
-       JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
-       if (NULL == jsValueRef) {
-               LogError("object creation error");
-               return JSValueMakeUndefined(context);
-       }
-       return jsValueRef;
-}
-
 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
 {
        return JSValueIsObjectOfClass(context, value, getClassRef());
@@ -120,17 +109,37 @@ ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef c
 
 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
 {
-       assert(NULL != JSObjectGetPrivate(object));
+       if (!JSObjectGetPrivate(object))
+       {
+               ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
+               JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, ContactPhoneNumberPtr(phoneNumber));
+               if (!JSObjectSetPrivate(object, priv)) {
+                       delete priv;
+               }
+       }
 }
 
 void JSContactPhoneNumber::Finalize(JSObjectRef object)
 {
-       //delete (JSObjectGetPrivate(object));
+       JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
+
+       if (priv != NULL)
+               delete (priv);
+}
+
+JSObjectRef JSContactPhoneNumber::createJSObject(JSContextRef context, ContactPhoneNumberPtr contactPhoneNumber)
+{
+       JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, contactPhoneNumber);
+       JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
+       if (NULL == jsObjectRef) {
+               LogError("object creation error");
+               return NULL;
+       }
+       return jsObjectRef;
 }
 
 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
 {
-       //LogDebug("entered");
        JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
        if (!priv) {
                ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
@@ -142,12 +151,95 @@ ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
        return result;
 }
 
+JSObjectRef JSContactPhoneNumber::constructor(JSContextRef context,
+               JSObjectRef constructor,
+               size_t argumentCount,
+               const JSValueRef arguments[],
+               JSValueRef* exception)
+{
+       LogDebug("entered");
+
+//     AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
+//     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+
+       JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
+       if (!priv) {
+               ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
+       }
+       JSContextRef gContext = priv->getContext();
+
+//     JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
+
+       BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
+       Try {
+               if (argumentCount != 2)
+                       ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
+
+               if (!JSValueIsString(gContext, arguments[0]))
+                       ThrowMsg(InvalidArgumentException, "1st argument is not string.");
+
+               if (!JSIsArrayValue(gContext, arguments[1]))
+                       ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
+
+       } Catch(Exception ) {
+               LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
+               return NULL;
+       }
+
+       ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+
+       std::string number;
+       ContactPhoneNumberTypeArrayPtr types;
+
+       Try {
+               number = converter->toString(arguments[0]);
+       } Catch(Exception) {
+               LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
+               return NULL;
+       }
+
+       Try {
+               types = converter->toContactPhoneNumberTypeArray(arguments[1]);
+               if(types->size() == 0)
+                       types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
+       } Catch(Exception) {
+               LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
+               return NULL;
+       }
+
+       ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
+       contactPhoneNumber->setNumber(number);
+       contactPhoneNumber->setTypes(types);
+
+       JSObjectRef jsobject;
+
+       Try {
+               jsobject = createJSObject(gContext, contactPhoneNumber);
+       } Catch(Exception) {
+               LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
+               return NULL;
+       }
+
+       return jsobject;
+}
+
+bool JSContactPhoneNumber::hasInstance(JSContextRef context,
+               JSObjectRef constructor,
+               JSValueRef possibleInstance,
+               JSValueRef* exception)
+{
+       return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
+}
+
 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
                JSObjectRef object,
                JSStringRef propertyName,
                JSValueRef* exception)
 {
-       //LogDebug("entered");
        Try
        {
                ContactConverterFactory::ConverterType converter =
@@ -190,13 +282,12 @@ JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
                JSStringRef propertyName,
                JSValueRef* exception)
 {
-       //LogDebug("entered");
        Try
        {
                ContactConverterFactory::ConverterType converter =
                                ContactConverterFactory::getConverter(context);
                ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
-               //return JSContactPhoneNumberTypeArray::createArray(context, contactPhoneNumber->getTypes());
+
                return converter->toJSValueRef(contactPhoneNumber->getTypes());
        }
        Catch(WrtDeviceApis::Commons::Exception)
@@ -218,13 +309,7 @@ bool JSContactPhoneNumber::setTypes(JSContextRef context,
                ContactConverterFactory::ConverterType converter =
                                ContactConverterFactory::getConverter(context);
 
-               if(JSContactPhoneNumberTypeArray::isObjectOfClass(context, value)) {
-                       contactPhoneNumber->setTypes(converter->toStringArray(value));
-                       //contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));        // TODO to implement this function on converter
-               } else {
-                       contactPhoneNumber->setTypes(converter->toStringArray(value));
-//                     contactPhoneNumber->setTypes(JSContactPhoneNumberTypeArray::getContactPhoneNumberTypeArray(context, value));
-               }
+               contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
 
                return true;
        }