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 JSContactName.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactName class
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactName.h"
32 #define CONTACT_CLASS_NAME "ContactName"
33 #define CONTACT_PROP_ATTR_PREFIX "prefix"
34 #define CONTACT_PROP_ATTR_FIRST_NAME "firstName"
35 #define CONTACT_PROP_ATTR_MIDDLE_NAME "middleName"
36 #define CONTACT_PROP_ATTR_LAST_NAME "lastName"
37 #define CONTACT_PROP_ATTR_NICKNAMES "nicknames"
38 #define CONTACT_PROP_ATTR_PHONETIC_NAME "phoneticName"
39 #define CONTACT_PROP_ATTR_DISPLAY_NAME "displayName"
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Contact;
48 JSClassRef JSContactName::m_classRef = NULL;
50 JSClassDefinition JSContactName::m_classInfo =
53 kJSClassAttributeNone,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
68 NULL, //ConvertToType,
71 JSStaticValue JSContactName::m_property[] = {
72 { CONTACT_PROP_ATTR_PREFIX, getPrefix, setPrefix, kJSPropertyAttributeNone },
73 { CONTACT_PROP_ATTR_FIRST_NAME, getFirstName, setFirstName, kJSPropertyAttributeNone },
74 { CONTACT_PROP_ATTR_MIDDLE_NAME, getMiddleName, setMiddleName, kJSPropertyAttributeNone },
75 { CONTACT_PROP_ATTR_LAST_NAME, getLastName, setLastName, kJSPropertyAttributeNone },
76 { CONTACT_PROP_ATTR_NICKNAMES, getNicknames, setNicknames, kJSPropertyAttributeNone },
77 { CONTACT_PROP_ATTR_PHONETIC_NAME, getPhoneticName, setPhoneticName, kJSPropertyAttributeNone },
78 { CONTACT_PROP_ATTR_DISPLAY_NAME, getDisplayName, setDisplayName, kJSPropertyAttributeNone },
82 JSStaticFunction JSContactName::m_functions[] =
87 JSClassRef JSContactName::getClassRef()
90 m_classRef = JSClassCreate(&m_classInfo);
95 JSValueRef JSContactName::createJSObject(JSContextRef context,
96 const std::string& prefix,
97 const std::string& firstName,
98 const std::string& middleName,
99 const std::string& lastName,
100 const StringArrayPtr& nicknames,
101 const std::string& phoneticName,
102 const std::string& displayName)
104 ContactNamePtr privateData = ContactNamePtr(new ContactName());
105 privateData->setPrefix(prefix);
106 privateData->setFirstName(firstName);
107 privateData->setMiddleName(middleName);
108 privateData->setLastName(lastName);
109 privateData->setNicknames(nicknames);
110 privateData->setPhoneticName(phoneticName);
111 privateData->setDisplayName(displayName);
112 JSContactNamePriv *priv = new JSContactNamePriv(context, privateData);
113 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
114 if (NULL == jsValueRef)
116 LogError("object creation error");
117 return JSValueMakeUndefined(context);
122 bool JSContactName::isObjectOfClass(JSContextRef context, JSValueRef value)
124 return JSValueIsObjectOfClass(context, value, getClassRef());
127 ContactNamePtr JSContactName::getContactName(JSContextRef context, JSValueRef value)
129 if (!isObjectOfClass(context, value)) {
130 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
133 JSObjectRef object = JSValueToObject(context, value, NULL);
135 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
138 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
140 Throw(WrtDeviceApis::Commons::NullPointerException);
143 return priv->getObject();
146 void JSContactName::Initialize(JSContextRef context, JSObjectRef object)
148 assert(NULL != JSObjectGetPrivate(object));
151 void JSContactName::Finalize(JSObjectRef object)
153 //delete (JSObjectGetPrivate(object));
156 ContactNamePtr JSContactName::getPrivData(JSObjectRef object)
158 //LogDebug("entered");
159 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
161 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
164 ContactNamePtr result = priv->getObject();
166 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
172 JSValueRef JSContactName::getPrefix(JSContextRef context,
174 JSStringRef propertyName,
175 JSValueRef* exception)
177 //LogDebug("entered");
180 ContactConverterFactory::ConverterType converter =
181 ContactConverterFactory::getConverter(context);
182 ContactNamePtr contactProperties = getPrivData(object);
183 return converter->toJSValueRef(contactProperties->getPrefix());
185 Catch(WrtDeviceApis::Commons::Exception)
187 LogWarning("trying to get incorrect value");
190 return JSValueMakeUndefined(context);
193 bool JSContactName::setPrefix(JSContextRef context,
195 JSStringRef propertyName,
197 JSValueRef* exception)
201 ContactNamePtr contactProperties = getPrivData(object);
202 ContactConverterFactory::ConverterType converter =
203 ContactConverterFactory::getConverter(context);
204 contactProperties->setPrefix(converter->toString(value));
207 Catch(WrtDeviceApis::Commons::Exception)
209 LogWarning("trying to set incorrect value");
212 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
216 JSValueRef JSContactName::getFirstName(JSContextRef context,
218 JSStringRef propertyName,
219 JSValueRef* exception)
221 //LogDebug("entered");
224 ContactConverterFactory::ConverterType converter =
225 ContactConverterFactory::getConverter(context);
226 ContactNamePtr contactProperties = getPrivData(object);
227 return converter->toJSValueRef(contactProperties->getFirstName());
229 Catch(WrtDeviceApis::Commons::Exception)
231 LogWarning("trying to get incorrect value");
234 return JSValueMakeUndefined(context);
237 bool JSContactName::setFirstName(JSContextRef context,
239 JSStringRef propertyName,
241 JSValueRef* exception)
245 ContactNamePtr contactProperties = getPrivData(object);
246 ContactConverterFactory::ConverterType converter =
247 ContactConverterFactory::getConverter(context);
248 contactProperties->setFirstName(converter->toString(value));
251 Catch(WrtDeviceApis::Commons::Exception)
253 LogWarning("trying to set incorrect value");
256 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
260 JSValueRef JSContactName::getMiddleName(JSContextRef context,
262 JSStringRef propertyName,
263 JSValueRef* exception)
265 //LogDebug("entered");
268 ContactConverterFactory::ConverterType converter =
269 ContactConverterFactory::getConverter(context);
270 ContactNamePtr contactProperties = getPrivData(object);
271 return converter->toJSValueRef(contactProperties->getMiddleName());
273 Catch(WrtDeviceApis::Commons::Exception)
275 LogWarning("trying to get incorrect value");
278 return JSValueMakeUndefined(context);
281 bool JSContactName::setMiddleName(JSContextRef context,
283 JSStringRef propertyName,
285 JSValueRef* exception)
289 ContactNamePtr contactProperties = getPrivData(object);
290 ContactConverterFactory::ConverterType converter =
291 ContactConverterFactory::getConverter(context);
292 contactProperties->setMiddleName(converter->toString(value));
295 Catch(WrtDeviceApis::Commons::Exception)
297 LogWarning("trying to set incorrect value");
300 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
304 JSValueRef JSContactName::getLastName(JSContextRef context,
306 JSStringRef propertyName,
307 JSValueRef* exception)
309 //LogDebug("entered");
312 ContactConverterFactory::ConverterType converter =
313 ContactConverterFactory::getConverter(context);
314 ContactNamePtr contactProperties = getPrivData(object);
315 return converter->toJSValueRef(contactProperties->getLastName());
317 Catch(WrtDeviceApis::Commons::Exception)
319 LogWarning("trying to get incorrect value");
322 return JSValueMakeUndefined(context);
325 bool JSContactName::setLastName(JSContextRef context,
327 JSStringRef propertyName,
329 JSValueRef* exception)
333 ContactNamePtr contactProperties = getPrivData(object);
334 ContactConverterFactory::ConverterType converter =
335 ContactConverterFactory::getConverter(context);
336 contactProperties->setLastName(converter->toString(value));
339 Catch(WrtDeviceApis::Commons::Exception)
341 LogWarning("trying to set incorrect value");
344 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
348 JSValueRef JSContactName::getNicknames(JSContextRef context,
350 JSStringRef propertyName,
351 JSValueRef* exception)
353 //LogDebug("entered");
356 ContactConverterFactory::ConverterType converter =
357 ContactConverterFactory::getConverter(context);
358 ContactNamePtr contactProperties = getPrivData(object);
359 return converter->toJSValueRef(contactProperties->getNicknames());
361 Catch(WrtDeviceApis::Commons::Exception)
363 LogWarning("trying to get incorrect value");
366 return JSValueMakeUndefined(context);
369 bool JSContactName::setNicknames(JSContextRef context,
371 JSStringRef propertyName,
373 JSValueRef* exception)
377 ContactNamePtr contactProperties = getPrivData(object);
378 ContactConverterFactory::ConverterType converter =
379 ContactConverterFactory::getConverter(context);
380 contactProperties->setNicknames(converter->toStringArray(value));
383 Catch(WrtDeviceApis::Commons::Exception)
385 LogWarning("trying to set incorrect value");
388 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
392 JSValueRef JSContactName::getPhoneticName(JSContextRef context,
394 JSStringRef propertyName,
395 JSValueRef* exception)
397 //LogDebug("entered");
400 ContactConverterFactory::ConverterType converter =
401 ContactConverterFactory::getConverter(context);
402 ContactNamePtr contactProperties = getPrivData(object);
403 return converter->toJSValueRef(contactProperties->getPhoneticName());
405 Catch(WrtDeviceApis::Commons::Exception)
407 LogWarning("trying to get incorrect value");
410 return JSValueMakeUndefined(context);
413 bool JSContactName::setPhoneticName(JSContextRef context,
415 JSStringRef propertyName,
417 JSValueRef* exception)
421 ContactNamePtr contactProperties = getPrivData(object);
422 ContactConverterFactory::ConverterType converter =
423 ContactConverterFactory::getConverter(context);
424 contactProperties->setPhoneticName(converter->toString(value));
427 Catch(WrtDeviceApis::Commons::Exception)
429 LogWarning("trying to set incorrect value");
432 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
436 JSValueRef JSContactName::getDisplayName(JSContextRef context,
438 JSStringRef propertyName,
439 JSValueRef* exception)
441 //LogDebug("entered");
444 ContactConverterFactory::ConverterType converter =
445 ContactConverterFactory::getConverter(context);
446 ContactNamePtr contactProperties = getPrivData(object);
447 return converter->toJSValueRef(contactProperties->getDisplayName());
449 Catch(WrtDeviceApis::Commons::Exception){
450 LogWarning("trying to get incorrect value");
452 return JSValueMakeUndefined(context);
455 bool JSContactName::setDisplayName(JSContextRef context,
457 JSStringRef propertyName,
459 JSValueRef* exception)
463 ContactNamePtr contactProperties = getPrivData(object);
464 ContactConverterFactory::ConverterType converter =
465 ContactConverterFactory::getConverter(context);
466 contactProperties->setDisplayName(converter->toString(value));
469 Catch(WrtDeviceApis::Commons::Exception)
471 LogWarning("trying to set incorrect value");
474 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");