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 JSAttributeRangeFilter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSAttributeRangeFilter class
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <JSTizenExceptionFactory.h>
30 #include <JSTizenException.h>
31 #include "AttributeRangeFilter.h"
32 #include "FilterConverter.h"
33 #include "JSAttributeRangeFilter.h"
35 #define ATTRIBUTE_RANGE_FILTER_CLASS_NAME "AttributeRangeFilter"
37 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
38 #define ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE "initialValue"
39 #define ATTRIBUTE_FILTER_ATTR_END_VALUE "endValue"
41 namespace DeviceAPI {
\rnamespace Tizen {
43 using namespace DeviceAPI::Common;
44 using namespace DeviceAPI::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
48 JSClassRef JSAttributeRangeFilter::m_classRef = NULL;
50 JSClassDefinition JSAttributeRangeFilter::m_classInfo =
53 kJSClassAttributeNone,
54 ATTRIBUTE_RANGE_FILTER_CLASS_NAME,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
65 NULL, //CallAsFunction,
66 constructor, //CallAsConstructor,
68 NULL, //ConvertToType,
71 JSStaticValue JSAttributeRangeFilter::m_property[] = {
72 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
73 { ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE, getInitialValue, setInitialValue, kJSPropertyAttributeNone },
74 { ATTRIBUTE_FILTER_ATTR_END_VALUE, getEndValue, setEndValue, kJSPropertyAttributeNone },
78 JSStaticFunction JSAttributeRangeFilter::m_functions[] =
83 JSClassRef JSAttributeRangeFilter::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSAttributeRangeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 AttributeRangeFilterPtr JSAttributeRangeFilter::getAttributeRangeFilter(JSContextRef context, JSValueRef value)
97 if (!isObjectOfClass(context, value)) {
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSObjectRef object = JSValueToObject(context, value, NULL);
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 JSObjectRef JSAttributeRangeFilter::createJSObject(JSContextRef context, AttributeRangeFilterPtr privateData)
113 JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, privateData);
114 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
115 if (NULL == jsObjectRef) {
116 LogError("object creation error");
122 void JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
124 if (!JSObjectGetPrivate(object))
126 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
127 JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
128 if (!JSObjectSetPrivate(object, priv)) {
134 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
136 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
146 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
149 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 AttributeRangeFilterPtr result = priv->getObject();
155 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160 JSObjectRef JSAttributeRangeFilter::constructor(JSContextRef context,
161 JSObjectRef constructor,
162 size_t argumentCount,
163 const JSValueRef arguments[],
164 JSValueRef* exception)
168 bool js2ndParamIsValue = false;
169 bool js3rdParamIsValue = false;
171 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
172 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
174 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(constructor));
176 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
178 JSContextRef gContext = priv->getContext();
180 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
182 BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
184 if (argumentCount < 1)
185 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
187 if (!JSValueIsString(gContext, arguments[0]))
188 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
190 // 2nd and 3rd argument can be any type.
191 if (argumentCount >= 2)
193 if(!JSValueIsUndefined(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
194 js2ndParamIsValue = true;
197 if (argumentCount >= 3)
199 if(!JSValueIsUndefined(gContext, arguments[2]) && !JSValueIsNull(gContext, arguments[2]))
200 js3rdParamIsValue = true;
203 } Catch(Exception ) {
204 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
205 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
209 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
211 std::string attributeName;
212 AnyPtr initialValue(NULL);
213 AnyPtr endValue(NULL);
216 attributeName = converter->toString(arguments[0]);
218 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
219 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
224 if(js2ndParamIsValue)
225 initialValue = converter->toAny(arguments[1]);
227 initialValue = AnyPtr(new Any());
230 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
231 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
236 if(js3rdParamIsValue)
237 endValue = converter->toAny(arguments[2]);
239 endValue = AnyPtr(new Any());
242 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
243 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
247 AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
249 JSObjectRef jsobject;
252 jsobject = createJSObject(gContext, attributeRangeFilter);
254 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
255 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
262 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
264 JSStringRef propertyName,
265 JSValueRef* exception)
270 FilterConverterFactory::ConverterType converter =
271 FilterConverterFactory::getConverter(context);
272 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
273 return converter->toJSValueRef(attributeRangeFilter->getAttributeName());
275 Catch(WrtDeviceApis::Commons::Exception)
277 LogWarning("trying to get incorrect value");
279 return JSValueMakeUndefined(context);
282 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
284 JSStringRef propertyName,
286 JSValueRef* exception)
290 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
291 FilterConverterFactory::ConverterType converter =
292 FilterConverterFactory::getConverter(context);
293 attributeRangeFilter->setAttributeName(converter->toString(value));
296 Catch(WrtDeviceApis::Commons::Exception)
298 LogWarning("trying to set incorrect value");
301 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
305 JSValueRef JSAttributeRangeFilter::getInitialValue(JSContextRef context,
307 JSStringRef propertyName,
308 JSValueRef* exception)
313 FilterConverterFactory::ConverterType converter =
314 FilterConverterFactory::getConverter(context);
315 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
316 return converter->toJSValueRef(attributeRangeFilter->getInitialValue());
318 Catch(WrtDeviceApis::Commons::Exception)
320 LogWarning("trying to get incorrect value");
322 return JSValueMakeUndefined(context);
325 bool JSAttributeRangeFilter::setInitialValue(JSContextRef context,
327 JSStringRef propertyName,
329 JSValueRef* exception)
333 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
334 FilterConverterFactory::ConverterType converter =
335 FilterConverterFactory::getConverter(context);
336 attributeRangeFilter->setInitialValue(converter->toAny(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 JSAttributeRangeFilter::getEndValue(JSContextRef context,
350 JSStringRef propertyName,
351 JSValueRef* exception)
356 FilterConverterFactory::ConverterType converter =
357 FilterConverterFactory::getConverter(context);
358 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
359 return converter->toJSValueRef(attributeRangeFilter->getEndValue());
361 Catch(WrtDeviceApis::Commons::Exception)
363 LogWarning("trying to get incorrect value");
365 return JSValueMakeUndefined(context);
368 bool JSAttributeRangeFilter::setEndValue(JSContextRef context,
370 JSStringRef propertyName,
372 JSValueRef* exception)
376 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
377 FilterConverterFactory::ConverterType converter =
378 FilterConverterFactory::getConverter(context);
379 attributeRangeFilter->setEndValue(converter->toAny(value));
382 Catch(WrtDeviceApis::Commons::Exception)
384 LogWarning("trying to set incorrect value");
387 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");