2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file JSContactEmailAddress.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactEmailAddress class
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactEmailAddress.h"
33 #define CONTACT_CLASS_NAME "EmailAddress"
35 #define CONTACT_ATTR_EMAIL "email"
36 #define CONTACT_ATTR_TYPES "types"
37 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
42 using namespace DeviceAPI::Common;
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 },
70 { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
74 JSStaticFunction JSContactEmailAddress::m_functions[] =
79 JSClassRef JSContactEmailAddress::m_classRef = JSClassCreate(&m_classInfo);
81 JSClassRef DLL_EXPORT 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 LoggerE("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)
160 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
162 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
164 JSContextRef gContext = priv->getContext();
166 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
169 ContactEmailAddressTypeArrayPtr types(NULL);
170 bool isDefault = false;
173 ArgumentValidator Argvalidator(context, argumentCount, arguments);
174 email = Argvalidator.toString(0, false);
176 if (argumentCount >= 2){
177 Argvalidator.toArrayObject(1, true);
178 types = converter->toContactEmailAddressTypeArray(arguments[1]);
180 types = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
182 if(types->size() == 0)
183 types->push_back(CONTACT_EMAIL_TYPE_WORK);
185 if (argumentCount >= 3)
186 isDefault = Argvalidator.toBool(2, false);
188 } catch (const TypeMismatchException& err ) {
189 JSWebAPIErrorFactory::postException(context, exception, err);
191 } catch(const BasePlatformException& err) {
192 JSWebAPIErrorFactory::postException(context, exception, err);
194 } catch(const ConversionException& err) {
195 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
197 } catch(const NullPointerException& err) {
198 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
202 ContactEmailAddressPtr contactEmailAddress(new ContactEmailAddress());
203 contactEmailAddress->setEmail(email);
204 contactEmailAddress->setTypes(types);
205 contactEmailAddress->setIsDefault(isDefault);
207 JSObjectRef jsobject;
210 jsobject = createJSObject(gContext, contactEmailAddress);
212 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
213 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
220 bool JSContactEmailAddress::hasInstance(JSContextRef context,
221 JSObjectRef constructor,
222 JSValueRef possibleInstance,
223 JSValueRef* exception)
225 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
228 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
230 JSStringRef propertyName,
231 JSValueRef* exception)
235 ContactConverterFactory::ConverterType converter =
236 ContactConverterFactory::getConverter(context);
237 ContactEmailAddressPtr emailAddress = getPrivData(object);
238 return converter->toJSValueRef(emailAddress->getEmail());
240 Catch(WrtDeviceApis::Commons::Exception)
242 LoggerW("trying to get incorrect value");
244 return JSValueMakeUndefined(context);
247 bool JSContactEmailAddress::setEmail(JSContextRef context,
249 JSStringRef propertyName,
251 JSValueRef* exception)
255 ContactEmailAddressPtr emailAddress = getPrivData(object);
256 ContactConverterFactory::ConverterType converter =
257 ContactConverterFactory::getConverter(context);
258 emailAddress->setEmail(converter->toString(value));
260 Catch(WrtDeviceApis::Commons::Exception)
262 LoggerW("trying to set incorrect value");
263 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
269 JSValueRef JSContactEmailAddress::getIsDefault(JSContextRef context,
271 JSStringRef propertyName,
272 JSValueRef* exception)
276 ContactConverterFactory::ConverterType converter =
277 ContactConverterFactory::getConverter(context);
278 ContactEmailAddressPtr emailAddress = getPrivData(object);
279 return converter->toJSValueRef(emailAddress->getIsDefault());
281 Catch(WrtDeviceApis::Commons::Exception)
283 LoggerW("trying to get incorrect value");
285 return JSValueMakeUndefined(context);
288 bool JSContactEmailAddress::setIsDefault(JSContextRef context,
290 JSStringRef propertyName,
292 JSValueRef* exception)
296 ContactEmailAddressPtr emailAddress = getPrivData(object);
297 ContactConverterFactory::ConverterType converter =
298 ContactConverterFactory::getConverter(context);
299 BasicValidator validator =
300 BasicValidatorFactory::getValidator(context, exception);
301 emailAddress->setIsDefault(converter->toBool(value));
303 Catch(WrtDeviceApis::Commons::Exception)
305 LoggerW("trying to set incorrect value");
306 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
312 JSValueRef JSContactEmailAddress::getTypes(JSContextRef context,
314 JSStringRef propertyName,
315 JSValueRef* exception)
319 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
321 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
323 JSContextRef gContext = priv->getContext();
325 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
326 ContactEmailAddressPtr emailAddress = getPrivData(object);
328 if(emailAddress->IsTypesSetJSArray()){
329 return emailAddress->getTypesJSObj();
331 JSValueRef tempJSValue = emailAddress->getTypesJSArray();
332 tempJSValue = converter->toJSValueRef(emailAddress->getTypes());
334 JSObjectRef convertedJSObject = emailAddress->getTypesJSObj();
335 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
336 emailAddress->setTypesJSArray(true, convertedJSObject);
338 JSValueProtect(gContext, convertedJSObject);
339 emailAddress->setContext(gContext);
343 Catch(WrtDeviceApis::Commons::Exception)
345 LoggerW("trying to get incorrect value");
347 return JSValueMakeUndefined(context);
350 bool JSContactEmailAddress::setTypes(JSContextRef context,
352 JSStringRef propertyName,
354 JSValueRef* exception)
358 ContactEmailAddressPtr emailAddress = getPrivData(object);
359 ContactConverterFactory::ConverterType converter =
360 ContactConverterFactory::getConverter(context);
362 emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value));
363 emailAddress->resetTypesJSObj();
365 Catch(WrtDeviceApis::Commons::Exception)
367 LoggerW("trying to set incorrect value");
368 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");