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 JSContactGroup.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactGroup class
25 #include "JSContactGroup.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 CONTACT_CLASS_NAME "ContactGroup"
34 #define CONTACT_PROP_ATTR_ID "id"
35 #define CONTACT_PROP_ATTR_ADDRESSBOOK_ID "addressBookId"
36 #define CONTACT_PROP_ATTR_NAME "name"
37 #define CONTACT_PROP_ATTR_RINGTONE_URI "ringtoneURI"
38 #define CONTACT_PROP_ATTR_PHOTO_URI "photoURI"
39 #define CONTACT_PROP_ATTR_READ_ONLY "readOnly"
44 using namespace DeviceAPI::Common;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
48 JSClassDefinition JSContactGroup::m_classInfo =
51 kJSClassAttributeNone,
61 NULL, //DeleteProperty,
62 NULL, //getPropertyNames,
63 NULL, //CallAsFunction,
64 constructor, //CallAsConstructor,
65 hasInstance, //HasInstance,
66 NULL, //ConvertToType,
69 JSStaticValue JSContactGroup::m_property[] = {
70 { CONTACT_PROP_ATTR_ID, getId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
71 { CONTACT_PROP_ATTR_ADDRESSBOOK_ID, getAddressBookId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
72 { CONTACT_PROP_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
73 { CONTACT_PROP_ATTR_RINGTONE_URI, getRingtoneURI, setRingtoneURI, kJSPropertyAttributeNone },
74 { CONTACT_PROP_ATTR_PHOTO_URI, getPhotoURI, setPhotoURI, kJSPropertyAttributeNone },
75 { CONTACT_PROP_ATTR_READ_ONLY, getReadOnly, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
79 JSStaticFunction JSContactGroup::m_functions[] =
84 JSClassRef JSContactGroup::m_classRef = JSClassCreate(&m_classInfo);
86 JSClassRef JSContactGroup::getClassRef()
89 m_classRef = JSClassCreate(&m_classInfo);
94 bool JSContactGroup::isObjectOfClass(JSContextRef context, JSValueRef value)
96 return JSValueIsObjectOfClass(context, value, getClassRef());
99 ContactGroupPtr JSContactGroup::getContactGroup(JSContextRef context, JSValueRef value)
101 if (!isObjectOfClass(context, value)) {
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
105 JSObjectRef object = JSValueToObject(context, value, NULL);
107 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
110 JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(object));
112 Throw(WrtDeviceApis::Commons::NullPointerException);
115 return priv->getObject();
118 void JSContactGroup::Initialize(JSContextRef context, JSObjectRef object)
120 if (!JSObjectGetPrivate(object))
122 ContactGroupPtr name(new ContactGroup());
123 JSContactGroupPriv *priv = new JSContactGroupPriv(context, ContactGroupPtr(name));
124 if (!JSObjectSetPrivate(object, priv)) {
130 void JSContactGroup::Finalize(JSObjectRef object)
132 JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(object));
138 JSObjectRef JSContactGroup::createJSObject(JSContextRef context, ContactGroupPtr contactGroup)
140 JSContactGroupPriv *priv = new JSContactGroupPriv(context, contactGroup);
141 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
142 if (NULL == jsObjectRef)
144 LoggerE("object creation error");
150 ContactGroupPtr JSContactGroup::getPrivData(JSObjectRef object)
152 JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(object));
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157 ContactGroupPtr result = priv->getObject();
159 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165 JSObjectRef JSContactGroup::constructor(JSContextRef context,
166 JSObjectRef constructor,
167 size_t argumentCount,
168 const JSValueRef arguments[],
169 JSValueRef* exception)
173 JSContactGroupPriv *priv = static_cast<JSContactGroupPriv*>(JSObjectGetPrivate(constructor));
175 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
177 JSContextRef gContext = priv->getContext();
179 ContactGroupPtr contactGroup = ContactGroupPtr(new ContactGroup());
181 ArgumentValidator Argvalidator(context, argumentCount, arguments);
182 contactGroup->setName(Argvalidator.toString(0, false));
184 if (argumentCount >= 2)
185 contactGroup->setRingtoneURI(Argvalidator.toString(1, true));
187 if (argumentCount >= 3)
188 contactGroup->setPhotoURI(Argvalidator.toString(2, true));
190 } catch (const TypeMismatchException& err ) {
191 JSWebAPIError::throwException(context, exception, err);
193 } catch(const BasePlatformException& err) {
194 JSWebAPIError::throwException(context, exception, err);
196 } catch(const ConversionException& err) {
197 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
199 } catch(const NullPointerException& err) {
200 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
204 JSObjectRef jsobject;
207 jsobject = createJSObject(gContext, contactGroup);
209 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
210 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
217 bool JSContactGroup::hasInstance(JSContextRef context,
218 JSObjectRef constructor,
219 JSValueRef possibleInstance,
220 JSValueRef* exception)
222 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
225 JSValueRef JSContactGroup::getId(JSContextRef context,
227 JSStringRef propertyName,
228 JSValueRef* exception)
232 ContactConverterFactory::ConverterType converter =
233 ContactConverterFactory::getConverter(context);
234 ContactGroupPtr contactGroup = getPrivData(object);
235 if(!contactGroup->getIdIsSet())
236 return JSValueMakeNull(context);
238 return converter->toJSValueRef(contactGroup->getId());
240 Catch(WrtDeviceApis::Commons::Exception)
242 LoggerW("trying to get incorrect value");
245 return JSValueMakeUndefined(context);
248 bool JSContactGroup::setId(JSContextRef context,
250 JSStringRef propertyName,
252 JSValueRef* exception)
256 ContactGroupPtr contactGroup = getPrivData(object);
257 ContactConverterFactory::ConverterType converter =
258 ContactConverterFactory::getConverter(context);
259 BasicValidator validator =
260 BasicValidatorFactory::getValidator(context, exception);
261 if(validator->isNullOrUndefined(value))
262 contactGroup->unsetId();
264 contactGroup->setId(converter->toString(value));
267 Catch(WrtDeviceApis::Commons::Exception)
269 LoggerW("trying to set incorrect value");
272 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
276 JSValueRef JSContactGroup::getAddressBookId(JSContextRef context,
278 JSStringRef propertyName,
279 JSValueRef* exception)
283 ContactConverterFactory::ConverterType converter =
284 ContactConverterFactory::getConverter(context);
285 ContactGroupPtr contactGroup = getPrivData(object);
286 if(!contactGroup->getAddressBookIdIsSet())
287 return JSValueMakeNull(context);
289 return converter->toJSValueRef(contactGroup->getAddressBookId());
291 Catch(WrtDeviceApis::Commons::Exception)
293 LoggerW("trying to get incorrect value");
296 return JSValueMakeUndefined(context);
299 bool JSContactGroup::setAddressBookId(JSContextRef context,
301 JSStringRef propertyName,
303 JSValueRef* exception)
307 ContactGroupPtr contactGroup = getPrivData(object);
308 ContactConverterFactory::ConverterType converter =
309 ContactConverterFactory::getConverter(context);
310 BasicValidator validator =
311 BasicValidatorFactory::getValidator(context, exception);
312 if(validator->isNullOrUndefined(value))
313 contactGroup->unsetAddressBookId();
315 contactGroup->setAddressBookId(converter->toString(value));
317 Catch(WrtDeviceApis::Commons::Exception)
319 LoggerW("trying to set incorrect value");
320 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
326 JSValueRef JSContactGroup::getName(JSContextRef context,
328 JSStringRef propertyName,
329 JSValueRef* exception)
333 ContactConverterFactory::ConverterType converter =
334 ContactConverterFactory::getConverter(context);
335 ContactGroupPtr contactGroup = getPrivData(object);
336 if(!contactGroup->getNameIsSet())
337 return JSValueMakeNull(context);
339 return converter->toJSValueRef(contactGroup->getName());
341 Catch(WrtDeviceApis::Commons::Exception)
343 LoggerW("trying to get incorrect value");
346 return JSValueMakeUndefined(context);
349 bool JSContactGroup::setName(JSContextRef context,
351 JSStringRef propertyName,
353 JSValueRef* exception)
357 ContactGroupPtr contactGroup = getPrivData(object);
358 ContactConverterFactory::ConverterType converter =
359 ContactConverterFactory::getConverter(context);
360 BasicValidator validator =
361 BasicValidatorFactory::getValidator(context, exception);
362 if(validator->isNullOrUndefined(value))
363 contactGroup->unsetName();
365 contactGroup->setName(converter->toString(value));
367 Catch(WrtDeviceApis::Commons::Exception)
369 LoggerW("trying to set incorrect value");
370 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
376 JSValueRef JSContactGroup::getRingtoneURI(JSContextRef context,
378 JSStringRef propertyName,
379 JSValueRef* exception)
383 ContactConverterFactory::ConverterType converter =
384 ContactConverterFactory::getConverter(context);
385 ContactGroupPtr contactGroup = getPrivData(object);
386 if(!contactGroup->getRingtoneURIIsSet())
387 return JSValueMakeNull(context);
389 return converter->toJSValueRef(contactGroup->getRingtoneURI());
391 Catch(WrtDeviceApis::Commons::Exception)
393 LoggerW("trying to get incorrect value");
396 return JSValueMakeUndefined(context);
399 bool JSContactGroup::setRingtoneURI(JSContextRef context,
401 JSStringRef propertyName,
403 JSValueRef* exception)
407 ContactGroupPtr contactGroup = getPrivData(object);
408 ContactConverterFactory::ConverterType converter =
409 ContactConverterFactory::getConverter(context);
410 BasicValidator validator =
411 BasicValidatorFactory::getValidator(context, exception);
412 if(validator->isNullOrUndefined(value))
413 contactGroup->unsetRingtoneURI();
415 contactGroup->setRingtoneURI(converter->toString(value));
417 Catch(WrtDeviceApis::Commons::Exception)
419 LoggerW("trying to set incorrect value");
420 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
426 JSValueRef JSContactGroup::getPhotoURI(JSContextRef context,
428 JSStringRef propertyName,
429 JSValueRef* exception)
433 ContactConverterFactory::ConverterType converter =
434 ContactConverterFactory::getConverter(context);
435 ContactGroupPtr contactGroup = getPrivData(object);
436 if(!contactGroup->getPhotoURIIsSet())
437 return JSValueMakeNull(context);
439 return converter->toJSValueRef(contactGroup->getPhotoURI());
441 Catch(WrtDeviceApis::Commons::Exception)
443 LoggerW("trying to get incorrect value");
446 return JSValueMakeUndefined(context);
449 bool JSContactGroup::setPhotoURI(JSContextRef context,
451 JSStringRef propertyName,
453 JSValueRef* exception)
457 ContactGroupPtr contactGroup = getPrivData(object);
458 ContactConverterFactory::ConverterType converter =
459 ContactConverterFactory::getConverter(context);
460 BasicValidator validator =
461 BasicValidatorFactory::getValidator(context, exception);
462 if(validator->isNullOrUndefined(value))
463 contactGroup->unsetRingtoneURI();
465 contactGroup->setPhotoURI(converter->toString(value));
467 Catch(WrtDeviceApis::Commons::Exception)
469 LoggerW("trying to set incorrect value");
470 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
476 JSValueRef JSContactGroup::getReadOnly(JSContextRef context,
478 JSStringRef propertyName,
479 JSValueRef* exception)
483 ContactConverterFactory::ConverterType converter =
484 ContactConverterFactory::getConverter(context);
485 ContactGroupPtr contactGroup = getPrivData(object);
487 return converter->toJSValueRef(contactGroup->getReadOnly());
489 Catch(WrtDeviceApis::Commons::Exception)
491 LoggerW("trying to get incorrect value");
494 return JSValueMakeUndefined(context);