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 <Tizen/Common/JSGlobalContextFactory.h>
29 #include "ContactConverter.h"
30 #include "JSContactEmailAddressTypeArray.h"
31 #include "JSContactEmailAddress.h"
33 #define CONTACT_CLASS_NAME "EmailAddress"
35 #define CONTACT_ATTR_EMAIL "email"
36 #define CONTACT_ATTR_TYPES "types"
42 using namespace TizenApis::Commons;
43 using namespace TizenApis::Api::Contact;
44 using namespace WrtDeviceApis::Commons;
45 using namespace WrtDeviceApis::CommonsJavaScript;
47 JSClassDefinition JSContactEmailAddress::m_classInfo =
50 kJSClassAttributeNone,
60 NULL, //DeleteProperty,
61 NULL, //getPropertyNames,
62 NULL, //CallAsFunction,
63 constructor, //CallAsConstructor,
64 hasInstance, //HasInstance,
65 NULL, //ConvertToType,
68 JSStaticValue JSContactEmailAddress::m_property[] = {
69 { CONTACT_ATTR_EMAIL, getEmail, setEmail, kJSPropertyAttributeNone },
70 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
74 JSStaticFunction JSContactEmailAddress::m_functions[] =
79 JSClassRef JSContactEmailAddress::m_classRef = JSClassCreate(&m_classInfo);
81 JSClassRef JSContactEmailAddress::getClassRef() {
83 m_classRef = JSClassCreate(&m_classInfo);
88 bool JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
90 return JSValueIsObjectOfClass(context, value, getClassRef());
93 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(JSContextRef context, JSValueRef value)
95 if (!isObjectOfClass(context, value)) {
96 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98 JSObjectRef object = JSValueToObject(context, value, NULL);
100 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
104 Throw(WrtDeviceApis::Commons::NullPointerException);
106 return priv->getObject();
109 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
111 if (!JSObjectGetPrivate(object))
113 ContactEmailAddressPtr emailAddress(new ContactEmailAddress());
114 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, ContactEmailAddressPtr(emailAddress));
115 if (!JSObjectSetPrivate(object, priv)) {
121 void JSContactEmailAddress::Finalize(JSObjectRef object)
123 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
129 JSObjectRef JSContactEmailAddress::createJSObject(JSContextRef context,
130 const ContactEmailAddressPtr emailAddress)
132 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, emailAddress);
133 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
134 if (NULL == jsObjectRef) {
135 LogError("object creation error");
141 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
143 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
145 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147 ContactEmailAddressPtr result = priv->getObject();
149 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154 JSObjectRef JSContactEmailAddress::constructor(JSContextRef context,
155 JSObjectRef constructor,
156 size_t argumentCount,
157 const JSValueRef arguments[],
158 JSValueRef* exception)
162 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
163 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
167 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
169 JSContextRef gContext = priv->getContext();
171 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
173 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
175 if (argumentCount < 1 || argumentCount > 2)
176 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
178 if (!JSValueIsString(gContext, arguments[0]))
179 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
181 if (argumentCount >= 2)
183 if (!JSIsArrayValue(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
184 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
187 } Catch(Exception ) {
188 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
189 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
193 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
196 ContactEmailAddressTypeArrayPtr types(NULL);
199 email = converter->toString(arguments[0]);
201 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
202 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
207 if(argumentCount >= 2)
209 if(JSIsArrayValue(gContext, arguments[1]))
210 types = converter->toContactEmailAddressTypeArray(arguments[1]);
213 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");