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 JSContactOrganization.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactOrganization class
25 #include "JSContactOrganization.h"
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
30 #include "ContactConverter.h"
33 #define ORGANIZATION_CLASS_NAME "ContactOrganization"
35 #define CONTACT_ATTR_NAME "name"
36 #define CONTACT_ATTR_DEPARTMENT "department"
37 #define CONTACT_ATTR_OFFICE "office"
38 #define CONTACT_ATTR_TITLE "title"
39 #define CONTACT_ATTR_ROLE "role"
40 #define CONTACT_ATTR_LOGO_URI "logoURI"
45 using namespace DeviceAPI::Common;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
49 JSClassDefinition JSContactOrganization::m_classInfo =
52 kJSClassAttributeNone,
53 ORGANIZATION_CLASS_NAME,
62 NULL, //DeleteProperty,
63 NULL, //getPropertyNames,
64 NULL, //CallAsFunction,
65 constructor, //CallAsConstructor,
66 hasInstance, //HasInstance,
67 NULL, //ConvertToType,
70 JSStaticValue JSContactOrganization::m_property[] = {
71 { CONTACT_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
72 { CONTACT_ATTR_DEPARTMENT, getDepartment, setDepartment, kJSPropertyAttributeNone },
73 { CONTACT_ATTR_OFFICE, getOffice, setOffice, kJSPropertyAttributeNone },
74 { CONTACT_ATTR_TITLE, getTitle, setTitle, kJSPropertyAttributeNone },
75 { CONTACT_ATTR_ROLE, getRole, setRole, kJSPropertyAttributeNone },
76 { CONTACT_ATTR_LOGO_URI, getLogoURI, setLogoURI, kJSPropertyAttributeNone },
80 JSStaticFunction JSContactOrganization::m_functions[] =
85 JSClassRef JSContactOrganization::m_classRef = JSClassCreate(&m_classInfo);
87 JSClassRef JSContactOrganization::getClassRef() {
89 m_classRef = JSClassCreate(&m_classInfo);
94 bool JSContactOrganization::isObjectOfClass(JSContextRef context, JSValueRef value)
96 return JSValueIsObjectOfClass(context, value, getClassRef());
99 ContactOrganizationPtr JSContactOrganization::getContactOrganization(JSContextRef context, JSValueRef value)
101 if (!isObjectOfClass(context, value)) {
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSObjectRef object = JSValueToObject(context, value, NULL);
106 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
108 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
110 Throw(WrtDeviceApis::Commons::NullPointerException);
112 return priv->getObject();
115 void JSContactOrganization::Initialize(JSContextRef context, JSObjectRef object)
117 if (!JSObjectGetPrivate(object))
119 ContactOrganizationPtr organization(new ContactOrganization());
120 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, ContactOrganizationPtr(organization));
121 if (!JSObjectSetPrivate(object, priv)) {
127 void JSContactOrganization::Finalize(JSObjectRef object)
129 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
135 JSObjectRef JSContactOrganization::createJSObject(JSContextRef context, ContactOrganizationPtr contactOrganization)
137 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, contactOrganization);
138 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
139 if (NULL == jsObjectRef) {
140 LoggerE("object creation error");
146 ContactOrganizationPtr JSContactOrganization::getPrivData(JSObjectRef object)
148 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
150 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152 ContactOrganizationPtr result = priv->getObject();
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159 JSObjectRef JSContactOrganization::constructor(JSContextRef context,
160 JSObjectRef constructor,
161 size_t argumentCount,
162 const JSValueRef arguments[],
163 JSValueRef* exception)
167 bool js1stParamIsObject = false;
169 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(constructor));
171 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
173 JSContextRef gContext = priv->getContext();
175 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
177 if (argumentCount >= 1)
179 if (JSValueIsObject(gContext, arguments[0]))
180 js1stParamIsObject = true;
182 if (!js1stParamIsObject &&
183 !JSValueIsNull(gContext, arguments[0]) &&
184 !JSValueIsUndefined(gContext, arguments[0]))
185 ThrowMsg(InvalidArgumentException, "1st argument must be a 'object'");
188 } Catch(Exception ) {
189 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
190 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'object'");
194 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
196 ContactOrganizationPtr contactOrganization(NULL);
198 if(js1stParamIsObject)
199 contactOrganization = converter->toContactOrganizationFromInit(arguments[0]);
201 contactOrganization = ContactOrganizationPtr(new ContactOrganization());
204 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
205 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
209 JSObjectRef jsobject;
212 jsobject = createJSObject(gContext, contactOrganization);
214 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
215 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
222 bool JSContactOrganization::hasInstance(JSContextRef context,
223 JSObjectRef constructor,
224 JSValueRef possibleInstance,
225 JSValueRef* exception)
227 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
230 JSValueRef JSContactOrganization::getName(JSContextRef context,
232 JSStringRef propertyName,
233 JSValueRef* exception)
237 ContactConverterFactory::ConverterType converter =
238 ContactConverterFactory::getConverter(context);
239 ContactOrganizationPtr organization = getPrivData(object);
240 if(!organization->getNameIsSet())
241 return JSValueMakeNull(context);
243 return converter->toJSValueRef(organization->getName());
245 Catch(WrtDeviceApis::Commons::Exception)
247 LoggerW("trying to get incorrect value");
249 return JSValueMakeUndefined(context);
252 bool JSContactOrganization::setName(JSContextRef context,
254 JSStringRef propertyName,
256 JSValueRef* exception)
260 ContactOrganizationPtr organization = getPrivData(object);
261 ContactConverterFactory::ConverterType converter =
262 ContactConverterFactory::getConverter(context);
263 BasicValidator validator =
264 BasicValidatorFactory::getValidator(context, exception);
265 if(validator->isNullOrUndefined(value))
266 organization->unsetName();
268 organization->setName(converter->toString(value));
270 Catch(WrtDeviceApis::Commons::Exception)
272 LoggerW("trying to set incorrect value");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
279 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
281 JSStringRef propertyName,
282 JSValueRef* exception)
286 ContactConverterFactory::ConverterType converter =
287 ContactConverterFactory::getConverter(context);
288 ContactOrganizationPtr organization = getPrivData(object);
289 if(!organization->getDepartmentIsSet())
290 return JSValueMakeNull(context);
292 return converter->toJSValueRef(organization->getDepartment());
294 Catch(WrtDeviceApis::Commons::Exception)
296 LoggerW("trying to get incorrect value");
298 return JSValueMakeUndefined(context);
301 bool JSContactOrganization::setDepartment(JSContextRef context,
303 JSStringRef propertyName,
305 JSValueRef* exception)
309 ContactOrganizationPtr organization = getPrivData(object);
310 ContactConverterFactory::ConverterType converter =
311 ContactConverterFactory::getConverter(context);
312 BasicValidator validator =
313 BasicValidatorFactory::getValidator(context, exception);
314 if(validator->isNullOrUndefined(value))
315 organization->unsetDepartment();
317 organization->setDepartment(converter->toString(value));
319 Catch(WrtDeviceApis::Commons::Exception)
321 LoggerW("trying to set incorrect value");
322 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
327 JSValueRef JSContactOrganization::getOffice(JSContextRef context,
329 JSStringRef propertyName,
330 JSValueRef* exception)
332 // NOTE: Deprecated field.
333 return JSValueMakeNull(context);
336 bool JSContactOrganization::setOffice(JSContextRef context,
338 JSStringRef propertyName,
340 JSValueRef* exception)
342 // NOTE: Deprecated field.
346 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
348 JSStringRef propertyName,
349 JSValueRef* exception)
353 ContactConverterFactory::ConverterType converter =
354 ContactConverterFactory::getConverter(context);
355 ContactOrganizationPtr organization = getPrivData(object);
356 if(!organization->getTitleIsSet())
357 return JSValueMakeNull(context);
359 return converter->toJSValueRef(organization->getTitle());
361 Catch(WrtDeviceApis::Commons::Exception)
363 LoggerW("trying to get incorrect value");
365 return JSValueMakeUndefined(context);
368 bool JSContactOrganization::setTitle(JSContextRef context,
370 JSStringRef propertyName,
372 JSValueRef* exception)
376 ContactOrganizationPtr organization = getPrivData(object);
377 ContactConverterFactory::ConverterType converter =
378 ContactConverterFactory::getConverter(context);
379 BasicValidator validator =
380 BasicValidatorFactory::getValidator(context, exception);
381 if(validator->isNullOrUndefined(value))
382 organization->unsetTitle();
384 organization->setTitle(converter->toString(value));
386 Catch(WrtDeviceApis::Commons::Exception)
388 LoggerW("trying to set incorrect value");
389 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
394 JSValueRef JSContactOrganization::getRole(JSContextRef context,
396 JSStringRef propertyName,
397 JSValueRef* exception)
401 ContactConverterFactory::ConverterType converter =
402 ContactConverterFactory::getConverter(context);
403 ContactOrganizationPtr organization = getPrivData(object);
404 if(!organization->getRoleIsSet())
405 return JSValueMakeNull(context);
407 return converter->toJSValueRef(organization->getRole());
409 Catch(WrtDeviceApis::Commons::Exception)
411 LoggerW("trying to get incorrect value");
413 return JSValueMakeUndefined(context);
416 bool JSContactOrganization::setRole(JSContextRef context,
418 JSStringRef propertyName,
420 JSValueRef* exception)
424 ContactOrganizationPtr organization = getPrivData(object);
425 ContactConverterFactory::ConverterType converter =
426 ContactConverterFactory::getConverter(context);
427 BasicValidator validator =
428 BasicValidatorFactory::getValidator(context, exception);
429 if(validator->isNullOrUndefined(value))
430 organization->unsetRole();
432 organization->setRole(converter->toString(value));
434 Catch(WrtDeviceApis::Commons::Exception)
436 LoggerW("trying to set incorrect value");
437 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
442 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
444 JSStringRef propertyName,
445 JSValueRef* exception)
449 ContactConverterFactory::ConverterType converter =
450 ContactConverterFactory::getConverter(context);
451 ContactOrganizationPtr organization = getPrivData(object);
452 if(!organization->getLogoURIIsSet())
453 return JSValueMakeNull(context);
455 return converter->toJSValueRef(organization->getLogoURI());
457 Catch(WrtDeviceApis::Commons::Exception)
459 LoggerW("trying to get incorrect value");
461 return JSValueMakeUndefined(context);
464 bool JSContactOrganization::setLogoURI(JSContextRef context,
466 JSStringRef propertyName,
468 JSValueRef* exception)
472 ContactOrganizationPtr organization = getPrivData(object);
473 ContactConverterFactory::ConverterType converter =
474 ContactConverterFactory::getConverter(context);
475 BasicValidator validator =
476 BasicValidatorFactory::getValidator(context, exception);
477 if(validator->isNullOrUndefined(value))
478 organization->unsetLogoURI();
480 organization->setLogoURI(converter->toString(value));
482 Catch(WrtDeviceApis::Commons::Exception)
484 LoggerW("trying to set incorrect value");
485 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");