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 JSContactAddress.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactAddress class
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactAddress.h"
30 #include "JSContactAddressTypeArray.h"
32 #define CONTACT_CLASS_NAME "ContactAddress"
34 #define CONTACT_ATTR_COUNTRY "country"
35 #define CONTACT_ATTR_REGION "region"
36 #define CONTACT_ATTR_CITY "city"
37 #define CONTACT_ATTR_STREET_ADDRESS "streetAddress"
38 #define CONTACT_ATTR_ADDITIONAL_INFORMATION "additionalInformation"
39 #define CONTACT_ATTR_POSTAL_CODE "postalCode"
40 #define CONTACT_ATTR_TYPES "types"
46 using namespace TizenApis::Commons;
47 using namespace TizenApis::Api::Contact;
49 JSClassRef JSContactAddress::m_classRef = NULL;
51 JSClassDefinition JSContactAddress::m_classInfo =
54 kJSClassAttributeNone,
64 NULL, //DeleteProperty,
65 NULL, //getPropertyNames,
69 NULL, //ConvertToType,
72 JSStaticValue JSContactAddress::m_property[] = {
73 { CONTACT_ATTR_COUNTRY, getCountry, setCountry, kJSPropertyAttributeNone },
74 { CONTACT_ATTR_REGION, getRegion, setRegion, kJSPropertyAttributeNone },
75 { CONTACT_ATTR_CITY, getCity, setCity, kJSPropertyAttributeNone },
76 { CONTACT_ATTR_STREET_ADDRESS, getStreetAddress, setStreetAddress, kJSPropertyAttributeNone },
77 { CONTACT_ATTR_ADDITIONAL_INFORMATION, getAdditionalInformation, setAdditionalInformation, kJSPropertyAttributeNone },
78 { CONTACT_ATTR_POSTAL_CODE, getPostalCode, setPostalCode, kJSPropertyAttributeNone },
79 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
83 JSStaticFunction JSContactAddress::m_functions[] =
88 JSClassRef JSContactAddress::getClassRef() {
90 m_classRef = JSClassCreate(&m_classInfo);
95 JSValueRef JSContactAddress::createJSObject(JSContextRef context,
96 const std::string country,
97 const std::string region,
98 const std::string county,
99 const std::string city,
100 const std::string streetAddress,
101 const std::string additionalInformation,
102 const std::string postalCode,
103 const ContactAddressTypeArrayPtr types)
105 ContactAddressPtr privateData = ContactAddressPtr(new ContactAddress());
107 privateData->setCountry(country);
108 privateData->setRegion(region);
109 privateData->setCity(city);
110 privateData->setStreetAddress(streetAddress);
111 privateData->setAdditionalInformation(additionalInformation);
112 privateData->setPostalCode(postalCode);
113 privateData->setTypes(types);
115 JSContactAddressPriv *priv = new JSContactAddressPriv(context, privateData);
116 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117 if (NULL == jsValueRef) {
118 LogError("object creation error");
119 return JSValueMakeUndefined(context);
124 bool JSContactAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
126 return JSValueIsObjectOfClass(context, value, getClassRef());
129 ContactAddressPtr JSContactAddress::getContactAddress(JSContextRef context, JSValueRef value)
131 if (!isObjectOfClass(context, value)) {
132 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
134 JSObjectRef object = JSValueToObject(context, value, NULL);
136 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
138 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
140 Throw(WrtDeviceApis::Commons::NullPointerException);
142 return priv->getObject();
145 void JSContactAddress::Initialize(JSContextRef context, JSObjectRef object)
147 assert(NULL != JSObjectGetPrivate(object));
150 void JSContactAddress::Finalize(JSObjectRef object)
152 //delete (JSObjectGetPrivate(object));
155 ContactAddressPtr JSContactAddress::getPrivData(JSObjectRef object)
157 //LogDebug("entered");
158 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
160 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
162 ContactAddressPtr result = priv->getObject();
164 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
169 JSValueRef JSContactAddress::getCountry(JSContextRef context,
171 JSStringRef propertyName,
172 JSValueRef* exception)
174 //LogDebug("entered");
177 ContactConverterFactory::ConverterType converter =
178 ContactConverterFactory::getConverter(context);
179 ContactAddressPtr contactAddress = getPrivData(object);
180 return converter->toJSValueRef(contactAddress->getCountry());
182 Catch(WrtDeviceApis::Commons::Exception)
184 LogWarning("trying to get incorrect value");
186 return JSValueMakeUndefined(context);
189 bool JSContactAddress::setCountry(JSContextRef context,
191 JSStringRef propertyName,
193 JSValueRef* exception)
197 ContactAddressPtr contactAddress = getPrivData(object);
198 ContactConverterFactory::ConverterType converter =
199 ContactConverterFactory::getConverter(context);
200 contactAddress->setCountry(converter->toString(value));
203 Catch(WrtDeviceApis::Commons::Exception)
205 LogWarning("trying to set incorrect value");
207 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
212 JSValueRef JSContactAddress::getRegion(JSContextRef context,
214 JSStringRef propertyName,
215 JSValueRef* exception)
217 //LogDebug("entered");
220 ContactConverterFactory::ConverterType converter =
221 ContactConverterFactory::getConverter(context);
222 ContactAddressPtr contactAddress = getPrivData(object);
223 return converter->toJSValueRef(contactAddress->getRegion());
225 Catch(WrtDeviceApis::Commons::Exception)
227 LogWarning("trying to get incorrect value");
229 return JSValueMakeUndefined(context);
232 bool JSContactAddress::setRegion(JSContextRef context,
234 JSStringRef propertyName,
236 JSValueRef* exception)
240 ContactAddressPtr contactAddress = getPrivData(object);
241 ContactConverterFactory::ConverterType converter =
242 ContactConverterFactory::getConverter(context);
243 contactAddress->setRegion(converter->toString(value));
246 Catch(WrtDeviceApis::Commons::Exception)
248 LogWarning("trying to set incorrect value");
250 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
254 JSValueRef JSContactAddress::getCity(JSContextRef context,
256 JSStringRef propertyName,
257 JSValueRef* exception)
259 //LogDebug("entered");
262 ContactConverterFactory::ConverterType converter =
263 ContactConverterFactory::getConverter(context);
264 ContactAddressPtr contactAddress = getPrivData(object);
265 return converter->toJSValueRef(contactAddress->getCity());
267 Catch(WrtDeviceApis::Commons::Exception)
269 LogWarning("trying to get incorrect value");
271 return JSValueMakeUndefined(context);
274 bool JSContactAddress::setCity(JSContextRef context,
276 JSStringRef propertyName,
278 JSValueRef* exception)
282 ContactAddressPtr contactAddress = getPrivData(object);
283 ContactConverterFactory::ConverterType converter =
284 ContactConverterFactory::getConverter(context);
285 contactAddress->setCity(converter->toString(value));
288 Catch(WrtDeviceApis::Commons::Exception)
290 LogWarning("trying to set incorrect value");
292 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
297 JSValueRef JSContactAddress::getStreetAddress(JSContextRef context,
299 JSStringRef propertyName,
300 JSValueRef* exception)
302 //LogDebug("entered");
305 ContactConverterFactory::ConverterType converter =
306 ContactConverterFactory::getConverter(context);
307 ContactAddressPtr contactAddress = getPrivData(object);
308 return converter->toJSValueRef(contactAddress->getStreetAddress());
310 Catch(WrtDeviceApis::Commons::Exception)
312 LogWarning("trying to get incorrect value");
314 return JSValueMakeUndefined(context);
317 bool JSContactAddress::setStreetAddress(JSContextRef context,
319 JSStringRef propertyName,
321 JSValueRef* exception)
325 ContactAddressPtr contactAddress = getPrivData(object);
326 ContactConverterFactory::ConverterType converter =
327 ContactConverterFactory::getConverter(context);
328 contactAddress->setStreetAddress(converter->toString(value));
331 Catch(WrtDeviceApis::Commons::Exception)
333 LogWarning("trying to set incorrect value");
335 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
340 JSValueRef JSContactAddress::getAdditionalInformation(JSContextRef context,
342 JSStringRef propertyName,
343 JSValueRef* exception)
345 //LogDebug("entered");
348 ContactConverterFactory::ConverterType converter =
349 ContactConverterFactory::getConverter(context);
350 ContactAddressPtr contactAddress = getPrivData(object);
351 return converter->toJSValueRef(contactAddress->getAdditionalInformation());
353 Catch(WrtDeviceApis::Commons::Exception)
355 LogWarning("trying to get incorrect value");
357 return JSValueMakeUndefined(context);
360 bool JSContactAddress::setAdditionalInformation(JSContextRef context,
362 JSStringRef propertyName,
364 JSValueRef* exception)
368 ContactAddressPtr contactAddress = getPrivData(object);
369 ContactConverterFactory::ConverterType converter =
370 ContactConverterFactory::getConverter(context);
371 contactAddress->setAdditionalInformation(converter->toString(value));
374 Catch(WrtDeviceApis::Commons::Exception)
376 LogWarning("trying to set incorrect value");
378 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
383 JSValueRef JSContactAddress::getPostalCode(JSContextRef context,
385 JSStringRef propertyName,
386 JSValueRef* exception)
388 //LogDebug("entered");
391 ContactConverterFactory::ConverterType converter =
392 ContactConverterFactory::getConverter(context);
393 ContactAddressPtr contactAddress = getPrivData(object);
394 return converter->toJSValueRef(contactAddress->getPostalCode());
396 Catch(WrtDeviceApis::Commons::Exception)
398 LogWarning("trying to get incorrect value");
400 return JSValueMakeUndefined(context);
403 bool JSContactAddress::setPostalCode(JSContextRef context,
405 JSStringRef propertyName,
407 JSValueRef* exception)
411 ContactAddressPtr contactAddress = getPrivData(object);
412 ContactConverterFactory::ConverterType converter =
413 ContactConverterFactory::getConverter(context);
414 contactAddress->setPostalCode(converter->toString(value));
417 Catch(WrtDeviceApis::Commons::Exception)
419 LogWarning("trying to set incorrect value");
421 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
426 JSValueRef JSContactAddress::getTypes(JSContextRef context,
428 JSStringRef propertyName,
429 JSValueRef* exception)
431 //LogDebug("entered");
434 ContactConverterFactory::ConverterType converter =
435 ContactConverterFactory::getConverter(context);
436 ContactAddressPtr contactAddress = getPrivData(object);
437 return JSContactAddressTypeArray::createArray(context, contactAddress->getTypes());
439 Catch(WrtDeviceApis::Commons::Exception)
441 LogWarning("trying to get incorrect value");
443 return JSValueMakeUndefined(context);
446 bool JSContactAddress::setTypes(JSContextRef context,
448 JSStringRef propertyName,
450 JSValueRef* exception)
454 ContactAddressPtr contactAddress = getPrivData(object);
455 ContactConverterFactory::ConverterType converter =
456 ContactConverterFactory::getConverter(context);
459 if(JSContactAddressTypeArray::isObjectOfClass(context, value))
460 contactAddress->setTypes(converter->toContactAddressTypeArray(value)); // TODO to implement this function on converter
462 contactAddress->setTypes(JSContactAddressTypeArray::getContactAddressTypeArray(context, value));
466 Catch(WrtDeviceApis::Commons::Exception)
468 LogWarning("trying to set incorrect value");
470 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");