2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContact class
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include "ContactConverter.h"
28 #include "JSContactProperties.h"
29 #include "JSContact.h"
31 #define FILTER_CLASS_NAME "Contact"
33 #define CONTACT_ATTR_ID "id"
34 #define CONTACT_ATTR_READ_ONLY "readOnly"
35 #define CONTACT_ATTR_LAST_UPDATED "lastUpdated"
41 using namespace TizenApis::Api::Contact;
43 using TizenApis::Api::Contact::Contact;
45 JSClassRef JSContact::m_classRef = NULL;
47 JSClassDefinition JSContact::m_classInfo =
50 kJSClassAttributeNone,
52 JSContactProperties::getClassRef(),
60 NULL, //DeleteProperty,
61 NULL, //getPropertyNames,
65 NULL, //ConvertToType,
68 JSStaticValue JSContact::m_property[] = {
69 { CONTACT_ATTR_ID, getId, NULL, kJSPropertyAttributeReadOnly },
70 { CONTACT_ATTR_READ_ONLY, getReadOnly, NULL, kJSPropertyAttributeReadOnly },
71 { CONTACT_ATTR_LAST_UPDATED, getLastUpdated, NULL, kJSPropertyAttributeReadOnly },
75 JSStaticFunction JSContact::m_functions[] =
80 JSClassRef JSContact::getClassRef() {
82 m_classRef = JSClassCreate(&m_classInfo);
87 JSValueRef JSContact::createJSObject(JSContextRef context
91 ContactPtr privateData = ContactPtr(new Contact());
92 JSContactPriv *priv = new JSContactPriv(context, privateData);
93 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
94 if (NULL == jsValueRef) {
95 LogError("object creation error");
96 return JSValueMakeUndefined(context);
101 bool JSContact::isObjectOfClass(JSContextRef context, JSValueRef value)
103 return JSValueIsObjectOfClass(context, value, getClassRef());
106 ContactPtr JSContact::getContact(JSContextRef context, JSValueRef value)
108 if (!isObjectOfClass(context, value)) {
109 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
111 JSObjectRef object = JSValueToObject(context, value, NULL);
113 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
115 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
117 Throw(WrtDeviceApis::Commons::NullPointerException);
119 return priv->getObject();
122 void JSContact::Initialize(JSContextRef context, JSObjectRef object)
124 assert(NULL != JSObjectGetPrivate(object));
127 void JSContact::Finalize(JSObjectRef object)
129 //delete (JSObjectGetPrivate(object));
132 ContactPtr JSContact::getPrivData(JSObjectRef object)
135 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
137 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
139 ContactPtr result = priv->getObject();
141 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146 JSValueRef JSContact::getId(JSContextRef context,
148 JSStringRef propertyName,
149 JSValueRef* exception)
154 ContactConverterFactory::ConverterType converter =
155 ContactConverterFactory::getConverter(context);
156 ContactPtr contact = getPrivData(object);
157 return converter->toJSValueRef(contact->getId());
159 Catch(WrtDeviceApis::Commons::Exception)
161 LogWarning("trying to get incorrect value");
163 return JSValueMakeUndefined(context);
166 JSValueRef JSContact::getReadOnly(JSContextRef context,
168 JSStringRef propertyName,
169 JSValueRef* exception)
174 ContactConverterFactory::ConverterType converter =
175 ContactConverterFactory::getConverter(context);
176 ContactPtr contact = getPrivData(object);
177 return converter->toJSValueRef(contact->getReadOnly());
179 Catch(WrtDeviceApis::Commons::Exception)
181 LogWarning("trying to get incorrect value");
183 return JSValueMakeUndefined(context);
187 JSValueRef JSContact::getLastUpdated(JSContextRef context,
189 JSStringRef propertyName,
190 JSValueRef* exception)
192 //LogDebug("entered");
195 ContactConverterFactory::ConverterType converter =
196 ContactConverterFactory::getConverter(context);
197 ContactPtr contact = getPrivData(object);
198 return converter->toJSValueRef(contact->getLastUpdated());
200 Catch(WrtDeviceApis::Commons::Exception)
202 LogWarning("trying to get incorrect value");
205 return JSValueMakeUndefined(context);