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.
18 * @file JSContactEmailAddress.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactEmailAddress class
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Validator.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactEmailAddressTypeArray.h"
30 #include "JSContactEmailAddress.h"
32 #define CONTACT_CLASS_NAME "EmailAddress"
34 #define CONTACT_ATTR_EMAIL "email"
35 #define CONTACT_ATTR_TYPES "types"
41 using namespace TizenApis::Commons;
42 using namespace TizenApis::Api::Contact;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
46 JSClassDefinition JSContactEmailAddress::m_classInfo =
49 kJSClassAttributeNone,
59 NULL, //DeleteProperty,
60 NULL, //getPropertyNames,
61 NULL, //CallAsFunction,
62 constructor, //CallAsConstructor,
63 hasInstance, //HasInstance,
64 NULL, //ConvertToType,
67 JSStaticValue JSContactEmailAddress::m_property[] = {
68 { CONTACT_ATTR_EMAIL, getEmail, setEmail, kJSPropertyAttributeNone },
69 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
73 JSStaticFunction JSContactEmailAddress::m_functions[] =
78 JSClassRef JSContactEmailAddress::m_classRef = JSClassCreate(&m_classInfo);
80 JSClassRef JSContactEmailAddress::getClassRef() {
82 m_classRef = JSClassCreate(&m_classInfo);
87 bool JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
89 return JSValueIsObjectOfClass(context, value, getClassRef());
92 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(JSContextRef context, JSValueRef value)
94 if (!isObjectOfClass(context, value)) {
95 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97 JSObjectRef object = JSValueToObject(context, value, NULL);
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
103 Throw(WrtDeviceApis::Commons::NullPointerException);
105 return priv->getObject();
108 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
110 if (!JSObjectGetPrivate(object))
112 ContactEmailAddressPtr emailAddress(new ContactEmailAddress());
113 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, ContactEmailAddressPtr(emailAddress));
114 if (!JSObjectSetPrivate(object, priv)) {
120 void JSContactEmailAddress::Finalize(JSObjectRef object)
122 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
128 JSObjectRef JSContactEmailAddress::createJSObject(JSContextRef context,
129 const ContactEmailAddressPtr emailAddress)
131 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, emailAddress);
132 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
133 if (NULL == jsObjectRef) {
134 LogError("object creation error");
140 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
142 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
144 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146 ContactEmailAddressPtr result = priv->getObject();
148 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 JSObjectRef JSContactEmailAddress::constructor(JSContextRef context,
154 JSObjectRef constructor,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
161 bool js2ndParamIsArray = false;
163 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
164 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
166 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
168 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
170 JSContextRef gContext = priv->getContext();
172 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
174 if (argumentCount >= 2)
176 if (JSIsArrayValue(gContext, arguments[1]))
177 js2ndParamIsArray = true;
179 if (!js2ndParamIsArray &&
180 !JSValueIsNull(gContext, arguments[1]) &&
181 !JSValueIsUndefined(gContext, arguments[1]))
182 ThrowMsg(InvalidArgumentException, "2nd argument must be array of ContactEmailAddress types");
185 } Catch(Exception ) {
186 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
187 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "2nd argument must be array of ContactEmailAddress types");
191 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
194 ContactEmailAddressTypeArrayPtr types(NULL);
197 if(argumentCount >= 1)
198 email = converter->toString(arguments[0]);
200 email = converter->toString(JSValueMakeUndefined(context));
203 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
204 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
209 if(js2ndParamIsArray)
210 types = converter->toContactEmailAddressTypeArray(arguments[1]);
212 types = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
215 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
216 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
220 ContactEmailAddressPtr contactEmailAddress(new ContactEmailAddress());
221 contactEmailAddress->setEmail(email);
222 contactEmailAddress->setTypes(types);
224 JSObjectRef jsobject;
227 jsobject = createJSObject(gContext, contactEmailAddress);
229 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
230 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
237 bool JSContactEmailAddress::hasInstance(JSContextRef context,
238 JSObjectRef constructor,
239 JSValueRef possibleInstance,
240 JSValueRef* exception)
242 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
245 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
247 JSStringRef propertyName,
248 JSValueRef* exception)
252 ContactConverterFactory::ConverterType converter =
253 ContactConverterFactory::getConverter(context);
254 ContactEmailAddressPtr emailAddress = getPrivData(object);
255 return converter->toJSValueRef(emailAddress->getEmail());
257 Catch(WrtDeviceApis::Commons::Exception)
259 LogWarning("trying to get incorrect value");
261 return JSValueMakeUndefined(context);
264 bool JSContactEmailAddress::setEmail(JSContextRef context,
266 JSStringRef propertyName,
268 JSValueRef* exception)
272 ContactEmailAddressPtr emailAddress = getPrivData(object);
273 ContactConverterFactory::ConverterType converter =
274 ContactConverterFactory::getConverter(context);
275 emailAddress->setEmail(converter->toString(value));
278 Catch(WrtDeviceApis::Commons::Exception)
280 LogWarning("trying to set incorrect value");
282 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
287 JSValueRef JSContactEmailAddress::getTypes(JSContextRef context,
289 JSStringRef propertyName,
290 JSValueRef* exception)
294 ContactConverterFactory::ConverterType converter =
295 ContactConverterFactory::getConverter(context);
296 ContactEmailAddressPtr emailAddress = getPrivData(object);
297 return JSContactEmailAddressTypeArray::createArray(context, emailAddress->getTypes());
299 Catch(WrtDeviceApis::Commons::Exception)
301 LogWarning("trying to get incorrect value");
303 return JSValueMakeUndefined(context);
306 bool JSContactEmailAddress::setTypes(JSContextRef context,
308 JSStringRef propertyName,
310 JSValueRef* exception)
314 ContactEmailAddressPtr emailAddress = getPrivData(object);
315 ContactConverterFactory::ConverterType converter =
316 ContactConverterFactory::getConverter(context);
318 if(JSContactEmailAddressTypeArray::isObjectOfClass(context, value))
319 emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value));
321 emailAddress->setTypes(JSContactEmailAddressTypeArray::getContactEmailAddressTypeArray(context, value));
325 Catch(WrtDeviceApis::Commons::Exception)
327 LogWarning("trying to set incorrect value");
329 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");