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 <Tizen/Common/JSGlobalContextFactory.h>
30 #include "ContactConverter.h"
32 #define ORGANIZATION_CLASS_NAME "ContactOrganization"
34 #define CONTACT_ATTR_NAME "name"
35 #define CONTACT_ATTR_DEPARTMENT "department"
36 #define CONTACT_ATTR_OFFICE "office"
37 #define CONTACT_ATTR_TITLE "title"
38 #define CONTACT_ATTR_ROLE "role"
39 #define CONTACT_ATTR_LOGO_URI "logoURI"
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Contact;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
50 JSClassDefinition JSContactOrganization::m_classInfo =
53 kJSClassAttributeNone,
54 ORGANIZATION_CLASS_NAME,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
65 NULL, //CallAsFunction,
66 constructor, //CallAsConstructor,
67 hasInstance, //HasInstance,
68 NULL, //ConvertToType,
71 JSStaticValue JSContactOrganization::m_property[] = {
72 { CONTACT_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
73 { CONTACT_ATTR_DEPARTMENT, getDepartment, setDepartment, kJSPropertyAttributeNone },
74 { CONTACT_ATTR_OFFICE, getOffice, setOffice, kJSPropertyAttributeNone },
75 { CONTACT_ATTR_TITLE, getTitle, setTitle, kJSPropertyAttributeNone },
76 { CONTACT_ATTR_ROLE, getRole, setRole, kJSPropertyAttributeNone },
77 { CONTACT_ATTR_LOGO_URI, getLogoURI, setLogoURI, kJSPropertyAttributeNone },
81 JSStaticFunction JSContactOrganization::m_functions[] =
86 JSClassRef JSContactOrganization::m_classRef = JSClassCreate(&m_classInfo);
88 JSClassRef JSContactOrganization::getClassRef() {
90 m_classRef = JSClassCreate(&m_classInfo);
95 bool JSContactOrganization::isObjectOfClass(JSContextRef context, JSValueRef value)
97 return JSValueIsObjectOfClass(context, value, getClassRef());
100 ContactOrganizationPtr JSContactOrganization::getContactOrganization(JSContextRef context, JSValueRef value)
102 if (!isObjectOfClass(context, value)) {
103 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
105 JSObjectRef object = JSValueToObject(context, value, NULL);
107 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
109 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
111 Throw(WrtDeviceApis::Commons::NullPointerException);
113 return priv->getObject();
116 void JSContactOrganization::Initialize(JSContextRef context, JSObjectRef object)
118 if (!JSObjectGetPrivate(object))
120 ContactOrganizationPtr organization(new ContactOrganization());
121 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, ContactOrganizationPtr(organization));
122 if (!JSObjectSetPrivate(object, priv)) {
128 void JSContactOrganization::Finalize(JSObjectRef object)
130 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
136 JSObjectRef JSContactOrganization::createJSObject(JSContextRef context, ContactOrganizationPtr contactOrganization)
138 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, contactOrganization);
139 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
140 if (NULL == jsObjectRef) {
141 LogError("object creation error");
147 ContactOrganizationPtr JSContactOrganization::getPrivData(JSObjectRef object)
149 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 ContactOrganizationPtr result = priv->getObject();
155 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160 JSObjectRef JSContactOrganization::constructor(JSContextRef context,
161 JSObjectRef constructor,
162 size_t argumentCount,
163 const JSValueRef arguments[],
164 JSValueRef* exception)
168 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
169 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
171 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(constructor));
173 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175 JSContextRef gContext = priv->getContext();
177 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
179 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
181 if (argumentCount > 1)
182 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
184 if (argumentCount == 1)
186 if (!JSValueIsObject(gContext, arguments[0]) && !JSValueIsNull(gContext, arguments[0]))
187 ThrowMsg(InvalidArgumentException, "2nd argument is not object.");
190 } Catch(Exception ) {
191 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
192 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
196 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
198 ContactOrganizationPtr contactOrganization(NULL);
200 if(argumentCount == 1)
202 if(JSValueIsObject(gContext, arguments[0]))
203 contactOrganization = converter->toContactOrganizationFromInit(arguments[0]);
206 if(contactOrganization == NULL)
207 contactOrganization = ContactOrganizationPtr(new ContactOrganization());
210 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
211 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215 JSObjectRef jsobject;
218 jsobject = createJSObject(gContext, contactOrganization);
220 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
221 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
228 bool JSContactOrganization::hasInstance(JSContextRef context,
229 JSObjectRef constructor,
230 JSValueRef possibleInstance,
231 JSValueRef* exception)
233 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
236 JSValueRef JSContactOrganization::getName(JSContextRef context,
238 JSStringRef propertyName,
239 JSValueRef* exception)
243 ContactConverterFactory::ConverterType converter =
244 ContactConverterFactory::getConverter(context);
245 ContactOrganizationPtr organization = getPrivData(object);
246 if(!organization->getNameIsSet())
247 return JSValueMakeNull(context);
249 return converter->toJSValueRef(organization->getName());
251 Catch(WrtDeviceApis::Commons::Exception)
253 LogWarning("trying to get incorrect value");
255 return JSValueMakeUndefined(context);
258 bool JSContactOrganization::setName(JSContextRef context,
260 JSStringRef propertyName,
262 JSValueRef* exception)
266 ContactOrganizationPtr organization = getPrivData(object);
267 ContactConverterFactory::ConverterType converter =
268 ContactConverterFactory::getConverter(context);
269 organization->setName(converter->toString(value));
272 Catch(WrtDeviceApis::Commons::Exception)
274 LogWarning("trying to set incorrect value");
276 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
281 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
283 JSStringRef propertyName,
284 JSValueRef* exception)
288 ContactConverterFactory::ConverterType converter =
289 ContactConverterFactory::getConverter(context);
290 ContactOrganizationPtr organization = getPrivData(object);
291 if(!organization->getDepartmentIsSet())
292 return JSValueMakeNull(context);
294 return converter->toJSValueRef(organization->getDepartment());
296 Catch(WrtDeviceApis::Commons::Exception)
298 LogWarning("trying to get incorrect value");
300 return JSValueMakeUndefined(context);
303 bool JSContactOrganization::setDepartment(JSContextRef context,
305 JSStringRef propertyName,
307 JSValueRef* exception)
311 ContactOrganizationPtr organization = getPrivData(object);
312 ContactConverterFactory::ConverterType converter =
313 ContactConverterFactory::getConverter(context);
314 organization->setDepartment(converter->toString(value));
317 Catch(WrtDeviceApis::Commons::Exception)
319 LogWarning("trying to set incorrect value");
321 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
325 JSValueRef JSContactOrganization::getOffice(JSContextRef context,
327 JSStringRef propertyName,
328 JSValueRef* exception)
332 ContactConverterFactory::ConverterType converter =
333 ContactConverterFactory::getConverter(context);
334 ContactOrganizationPtr organization = getPrivData(object);
335 if(!organization->getOfficeIsSet())
336 return JSValueMakeNull(context);
338 return converter->toJSValueRef(organization->getOffice());
340 Catch(WrtDeviceApis::Commons::Exception)
342 LogWarning("trying to get incorrect value");
344 return JSValueMakeUndefined(context);
347 bool JSContactOrganization::setOffice(JSContextRef context,
349 JSStringRef propertyName,
351 JSValueRef* exception)
355 // NOTE: Currently not support this field
356 //ContactOrganizationPtr organization = getPrivData(object);
357 //ContactConverterFactory::ConverterType converter =
358 // ContactConverterFactory::getConverter(context);
359 //organization->setOffice(converter->toString(value));
362 Catch(WrtDeviceApis::Commons::Exception)
364 LogWarning("trying to set incorrect value");
366 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
370 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
372 JSStringRef propertyName,
373 JSValueRef* exception)
377 ContactConverterFactory::ConverterType converter =
378 ContactConverterFactory::getConverter(context);
379 ContactOrganizationPtr organization = getPrivData(object);
380 if(!organization->getTitleIsSet())
381 return JSValueMakeNull(context);
383 return converter->toJSValueRef(organization->getTitle());
385 Catch(WrtDeviceApis::Commons::Exception)
387 LogWarning("trying to get incorrect value");
389 return JSValueMakeUndefined(context);
392 bool JSContactOrganization::setTitle(JSContextRef context,
394 JSStringRef propertyName,
396 JSValueRef* exception)
400 ContactOrganizationPtr organization = getPrivData(object);
401 ContactConverterFactory::ConverterType converter =
402 ContactConverterFactory::getConverter(context);
403 organization->setTitle(converter->toString(value));
406 Catch(WrtDeviceApis::Commons::Exception)
408 LogWarning("trying to set incorrect value");
410 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
414 JSValueRef JSContactOrganization::getRole(JSContextRef context,
416 JSStringRef propertyName,
417 JSValueRef* exception)
421 ContactConverterFactory::ConverterType converter =
422 ContactConverterFactory::getConverter(context);
423 ContactOrganizationPtr organization = getPrivData(object);
424 if(!organization->getRoleIsSet())
425 return JSValueMakeNull(context);
427 return converter->toJSValueRef(organization->getRole());
429 Catch(WrtDeviceApis::Commons::Exception)
431 LogWarning("trying to get incorrect value");
433 return JSValueMakeUndefined(context);
436 bool JSContactOrganization::setRole(JSContextRef context,
438 JSStringRef propertyName,
440 JSValueRef* exception)
444 ContactOrganizationPtr organization = getPrivData(object);
445 ContactConverterFactory::ConverterType converter =
446 ContactConverterFactory::getConverter(context);
447 organization->setRole(converter->toString(value));
450 Catch(WrtDeviceApis::Commons::Exception)
452 LogWarning("trying to set incorrect value");
454 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
458 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
460 JSStringRef propertyName,
461 JSValueRef* exception)
465 ContactConverterFactory::ConverterType converter =
466 ContactConverterFactory::getConverter(context);
467 ContactOrganizationPtr organization = getPrivData(object);
468 if(!organization->getLogoURIIsSet())
469 return JSValueMakeNull(context);
471 return converter->toJSValueRef(organization->getLogoURI());
473 Catch(WrtDeviceApis::Commons::Exception)
475 LogWarning("trying to get incorrect value");
477 return JSValueMakeUndefined(context);
480 bool JSContactOrganization::setLogoURI(JSContextRef context,
482 JSStringRef propertyName,
484 JSValueRef* exception)
488 // NOTE: Currently not support this field
489 //ContactOrganizationPtr organization = getPrivData(object);
490 //ContactConverterFactory::ConverterType converter =
491 // ContactConverterFactory::getConverter(context);
492 //organization->setLogoURI(converter->toString(value));
495 Catch(WrtDeviceApis::Commons::Exception)
497 LogWarning("trying to set incorrect value");
499 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");