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"
38 #define CONTACT_ATTR_LABEL "label"
43 using namespace DeviceAPI::Common;
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 },
71 { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
72 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
76 JSStaticFunction JSContactEmailAddress::m_functions[] =
81 JSClassRef JSContactEmailAddress::m_classRef = JSClassCreate(&m_classInfo);
83 JSClassRef DLL_EXPORT JSContactEmailAddress::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(JSContextRef context, JSValueRef value)
97 if (!isObjectOfClass(context, value)) {
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSObjectRef object = JSValueToObject(context, value, NULL);
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
113 if (!JSObjectGetPrivate(object))
115 ContactEmailAddressPtr emailAddress(new ContactEmailAddress());
116 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, ContactEmailAddressPtr(emailAddress));
117 if (!JSObjectSetPrivate(object, priv)) {
123 void JSContactEmailAddress::Finalize(JSObjectRef object)
125 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
131 JSObjectRef JSContactEmailAddress::createJSObject(JSContextRef context,
132 const ContactEmailAddressPtr emailAddress)
134 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, emailAddress);
135 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
136 if (NULL == jsObjectRef) {
137 LoggerE("object creation error");
143 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
145 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
147 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
149 ContactEmailAddressPtr result = priv->getObject();
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
156 JSObjectRef JSContactEmailAddress::constructor(JSContextRef context,
157 JSObjectRef constructor,
158 size_t argumentCount,
159 const JSValueRef arguments[],
160 JSValueRef* exception)
162 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
164 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
166 JSContextRef gContext = priv->getContext();
168 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
171 ContactEmailAddressTypeArrayPtr types(NULL);
172 bool isDefault = false;
176 ArgumentValidator Argvalidator(context, argumentCount, arguments);
177 email = Argvalidator.toString(0, false);
179 if (argumentCount >= 2){
180 Argvalidator.toArrayObject(1, true);
181 types = converter->toContactEmailAddressTypeArray(arguments[1]);
183 types = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
185 if(types->size() == 0)
186 types->push_back(CONTACT_EMAIL_TYPE_WORK);
188 if (argumentCount >= 3)
189 isDefault = Argvalidator.toBool(2, false);
191 if (argumentCount >= 4)
192 label = Argvalidator.toString(3, false);
194 } catch (const TypeMismatchException& err ) {
195 JSWebAPIErrorFactory::postException(context, exception, err);
197 } catch(const BasePlatformException& err) {
198 JSWebAPIErrorFactory::postException(context, exception, err);
200 } catch(const ConversionException& err) {
201 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
203 } catch(const NullPointerException& err) {
204 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
208 ContactEmailAddressPtr contactEmailAddress(new ContactEmailAddress());
209 contactEmailAddress->setEmail(email);
210 contactEmailAddress->setTypes(types);
211 contactEmailAddress->setIsDefault(isDefault);
212 contactEmailAddress->setLabel(label);
214 JSObjectRef jsobject;
217 jsobject = createJSObject(gContext, contactEmailAddress);
219 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
220 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
227 bool JSContactEmailAddress::hasInstance(JSContextRef context,
228 JSObjectRef constructor,
229 JSValueRef possibleInstance,
230 JSValueRef* exception)
232 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
235 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
237 JSStringRef propertyName,
238 JSValueRef* exception)
242 ContactConverterFactory::ConverterType converter =
243 ContactConverterFactory::getConverter(context);
244 ContactEmailAddressPtr emailAddress = getPrivData(object);
245 return converter->toJSValueRef(emailAddress->getEmail());
247 Catch(WrtDeviceApis::Commons::Exception)
249 LoggerW("trying to get incorrect value");
251 return JSValueMakeUndefined(context);
254 bool JSContactEmailAddress::setEmail(JSContextRef context,
256 JSStringRef propertyName,
258 JSValueRef* exception)
262 ContactEmailAddressPtr emailAddress = getPrivData(object);
263 ContactConverterFactory::ConverterType converter =
264 ContactConverterFactory::getConverter(context);
265 emailAddress->setEmail(converter->toString(value));
267 Catch(WrtDeviceApis::Commons::Exception)
269 LoggerW("trying to set incorrect value");
270 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
276 JSValueRef JSContactEmailAddress::getIsDefault(JSContextRef context,
278 JSStringRef propertyName,
279 JSValueRef* exception)
283 ContactConverterFactory::ConverterType converter =
284 ContactConverterFactory::getConverter(context);
285 ContactEmailAddressPtr emailAddress = getPrivData(object);
286 return converter->toJSValueRef(emailAddress->getIsDefault());
288 Catch(WrtDeviceApis::Commons::Exception)
290 LoggerW("trying to get incorrect value");
292 return JSValueMakeUndefined(context);
295 bool JSContactEmailAddress::setIsDefault(JSContextRef context,
297 JSStringRef propertyName,
299 JSValueRef* exception)
303 ContactEmailAddressPtr emailAddress = getPrivData(object);
304 ContactConverterFactory::ConverterType converter =
305 ContactConverterFactory::getConverter(context);
306 BasicValidator validator =
307 BasicValidatorFactory::getValidator(context, exception);
308 emailAddress->setIsDefault(converter->toBool(value));
310 Catch(WrtDeviceApis::Commons::Exception)
312 LoggerW("trying to set incorrect value");
313 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
319 JSValueRef JSContactEmailAddress::getTypes(JSContextRef context,
321 JSStringRef propertyName,
322 JSValueRef* exception)
326 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
328 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
330 JSContextRef gContext = priv->getContext();
332 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
333 ContactEmailAddressPtr emailAddress = getPrivData(object);
335 if(emailAddress->IsTypesSetJSArray()){
336 return emailAddress->getTypesJSObj();
338 JSValueRef tempJSValue = emailAddress->getTypesJSArray();
339 tempJSValue = converter->toJSValueRef(emailAddress->getTypes());
341 JSObjectRef convertedJSObject = emailAddress->getTypesJSObj();
342 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
343 emailAddress->setTypesJSArray(true, convertedJSObject);
345 JSValueProtect(gContext, convertedJSObject);
346 emailAddress->setContext(gContext);
350 Catch(WrtDeviceApis::Commons::Exception)
352 LoggerW("trying to get incorrect value");
354 return JSValueMakeUndefined(context);
357 bool JSContactEmailAddress::setTypes(JSContextRef context,
359 JSStringRef propertyName,
361 JSValueRef* exception)
365 ContactEmailAddressPtr emailAddress = getPrivData(object);
366 ContactConverterFactory::ConverterType converter =
367 ContactConverterFactory::getConverter(context);
369 emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value));
370 emailAddress->resetTypesJSObj();
372 Catch(WrtDeviceApis::Commons::Exception)
374 LoggerW("trying to set incorrect value");
375 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
380 JSValueRef JSContactEmailAddress::getLabel(JSContextRef context,
382 JSStringRef propertyName,
383 JSValueRef* exception)
387 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
388 ContactEmailAddressPtr emailAddress = getPrivData(object);
389 return converter->toJSValueRef(emailAddress->getLabel());
391 Catch(WrtDeviceApis::Commons::Exception)
393 LoggerW("trying to get incorrect value");
395 return JSValueMakeUndefined(context);
398 bool JSContactEmailAddress::setLabel(JSContextRef context,
400 JSStringRef propertyName,
402 JSValueRef* exception)
406 ContactEmailAddressPtr emailAddress = getPrivData(object);
407 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
408 emailAddress->setLabel(converter->toString(value));
410 Catch(WrtDeviceApis::Commons::Exception)
412 LoggerW("trying to set incorrect value");
413 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");