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 JSContactOrganization.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactOrganization class
24 #include "JSContactOrganization.h"
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
31 #define ORGANIZATION_CLASS_NAME "ContactOrganization"
33 #define CONTACT_ATTR_NAME "name"
34 #define CONTACT_ATTR_DEPARTMENT "department"
35 #define CONTACT_ATTR_OFFICE "office"
36 #define CONTACT_ATTR_TITLE "title"
37 #define CONTACT_ATTR_ROLE "role"
38 #define CONTACT_ATTR_LOGO_URI "logoURI"
44 using namespace TizenApis::Commons;
45 using namespace TizenApis::Api::Contact;
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 LogError("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 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
170 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
172 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(constructor));
174 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
176 JSContextRef gContext = priv->getContext();
178 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
180 if (argumentCount >= 1)
182 if (JSValueIsObject(gContext, arguments[0]))
183 js1stParamIsObject = true;
185 if (!js1stParamIsObject &&
186 !JSValueIsNull(gContext, arguments[0]) &&
187 !JSValueIsUndefined(gContext, arguments[0]))
188 ThrowMsg(InvalidArgumentException, "1st argument must be a 'object'");
191 } Catch(Exception ) {
192 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
193 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'object'");
197 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
199 ContactOrganizationPtr contactOrganization(NULL);
201 if(js1stParamIsObject)
202 contactOrganization = converter->toContactOrganizationFromInit(arguments[0]);
204 contactOrganization = ContactOrganizationPtr(new ContactOrganization());
207 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
208 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
212 JSObjectRef jsobject;
215 jsobject = createJSObject(gContext, contactOrganization);
217 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
218 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
225 bool JSContactOrganization::hasInstance(JSContextRef context,
226 JSObjectRef constructor,
227 JSValueRef possibleInstance,
228 JSValueRef* exception)
230 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
233 JSValueRef JSContactOrganization::getName(JSContextRef context,
235 JSStringRef propertyName,
236 JSValueRef* exception)
240 ContactConverterFactory::ConverterType converter =
241 ContactConverterFactory::getConverter(context);
242 ContactOrganizationPtr organization = getPrivData(object);
243 if(!organization->getNameIsSet())
244 return JSValueMakeNull(context);
246 return converter->toJSValueRef(organization->getName());
248 Catch(WrtDeviceApis::Commons::Exception)
250 LogWarning("trying to get incorrect value");
252 return JSValueMakeUndefined(context);
255 bool JSContactOrganization::setName(JSContextRef context,
257 JSStringRef propertyName,
259 JSValueRef* exception)
263 ContactOrganizationPtr organization = getPrivData(object);
264 ContactConverterFactory::ConverterType converter =
265 ContactConverterFactory::getConverter(context);
266 organization->setName(converter->toString(value));
269 Catch(WrtDeviceApis::Commons::Exception)
271 LogWarning("trying to set incorrect value");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
278 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
280 JSStringRef propertyName,
281 JSValueRef* exception)
285 ContactConverterFactory::ConverterType converter =
286 ContactConverterFactory::getConverter(context);
287 ContactOrganizationPtr organization = getPrivData(object);
288 if(!organization->getDepartmentIsSet())
289 return JSValueMakeNull(context);
291 return converter->toJSValueRef(organization->getDepartment());
293 Catch(WrtDeviceApis::Commons::Exception)
295 LogWarning("trying to get incorrect value");
297 return JSValueMakeUndefined(context);
300 bool JSContactOrganization::setDepartment(JSContextRef context,
302 JSStringRef propertyName,
304 JSValueRef* exception)
308 ContactOrganizationPtr organization = getPrivData(object);
309 ContactConverterFactory::ConverterType converter =
310 ContactConverterFactory::getConverter(context);
311 organization->setDepartment(converter->toString(value));
314 Catch(WrtDeviceApis::Commons::Exception)
316 LogWarning("trying to set incorrect value");
318 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
322 JSValueRef JSContactOrganization::getOffice(JSContextRef context,
324 JSStringRef propertyName,
325 JSValueRef* exception)
329 ContactConverterFactory::ConverterType converter =
330 ContactConverterFactory::getConverter(context);
331 ContactOrganizationPtr organization = getPrivData(object);
332 if(!organization->getOfficeIsSet())
333 return JSValueMakeNull(context);
335 return converter->toJSValueRef(organization->getOffice());
337 Catch(WrtDeviceApis::Commons::Exception)
339 LogWarning("trying to get incorrect value");
341 return JSValueMakeUndefined(context);
344 bool JSContactOrganization::setOffice(JSContextRef context,
346 JSStringRef propertyName,
348 JSValueRef* exception)
352 // NOTE: Currently not support this field
353 //ContactOrganizationPtr organization = getPrivData(object);
354 //ContactConverterFactory::ConverterType converter =
355 // ContactConverterFactory::getConverter(context);
356 //organization->setOffice(converter->toString(value));
359 Catch(WrtDeviceApis::Commons::Exception)
361 LogWarning("trying to set incorrect value");
363 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
367 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
369 JSStringRef propertyName,
370 JSValueRef* exception)
374 ContactConverterFactory::ConverterType converter =
375 ContactConverterFactory::getConverter(context);
376 ContactOrganizationPtr organization = getPrivData(object);
377 if(!organization->getTitleIsSet())
378 return JSValueMakeNull(context);
380 return converter->toJSValueRef(organization->getTitle());
382 Catch(WrtDeviceApis::Commons::Exception)
384 LogWarning("trying to get incorrect value");
386 return JSValueMakeUndefined(context);
389 bool JSContactOrganization::setTitle(JSContextRef context,
391 JSStringRef propertyName,
393 JSValueRef* exception)
397 ContactOrganizationPtr organization = getPrivData(object);
398 ContactConverterFactory::ConverterType converter =
399 ContactConverterFactory::getConverter(context);
400 organization->setTitle(converter->toString(value));
403 Catch(WrtDeviceApis::Commons::Exception)
405 LogWarning("trying to set incorrect value");
407 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
411 JSValueRef JSContactOrganization::getRole(JSContextRef context,
413 JSStringRef propertyName,
414 JSValueRef* exception)
418 ContactConverterFactory::ConverterType converter =
419 ContactConverterFactory::getConverter(context);
420 ContactOrganizationPtr organization = getPrivData(object);
421 if(!organization->getRoleIsSet())
422 return JSValueMakeNull(context);
424 return converter->toJSValueRef(organization->getRole());
426 Catch(WrtDeviceApis::Commons::Exception)
428 LogWarning("trying to get incorrect value");
430 return JSValueMakeUndefined(context);
433 bool JSContactOrganization::setRole(JSContextRef context,
435 JSStringRef propertyName,
437 JSValueRef* exception)
441 ContactOrganizationPtr organization = getPrivData(object);
442 ContactConverterFactory::ConverterType converter =
443 ContactConverterFactory::getConverter(context);
444 organization->setRole(converter->toString(value));
447 Catch(WrtDeviceApis::Commons::Exception)
449 LogWarning("trying to set incorrect value");
451 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
455 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
457 JSStringRef propertyName,
458 JSValueRef* exception)
462 ContactConverterFactory::ConverterType converter =
463 ContactConverterFactory::getConverter(context);
464 ContactOrganizationPtr organization = getPrivData(object);
465 if(!organization->getLogoURIIsSet())
466 return JSValueMakeNull(context);
468 return converter->toJSValueRef(organization->getLogoURI());
470 Catch(WrtDeviceApis::Commons::Exception)
472 LogWarning("trying to get incorrect value");
474 return JSValueMakeUndefined(context);
477 bool JSContactOrganization::setLogoURI(JSContextRef context,
479 JSStringRef propertyName,
481 JSValueRef* exception)
485 // NOTE: Currently not support this field
486 //ContactOrganizationPtr organization = getPrivData(object);
487 //ContactConverterFactory::ConverterType converter =
488 // ContactConverterFactory::getConverter(context);
489 //organization->setLogoURI(converter->toString(value));
492 Catch(WrtDeviceApis::Commons::Exception)
494 LogWarning("trying to set incorrect value");
496 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");