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 NULL, //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 JSObjectRef JSAttributeRangeFilter::constructor(JSContextRef context,
123 JSObjectRef constructor,
124 size_t argumentCount,
125 const JSValueRef arguments[],
126 JSValueRef* exception)
130 bool js2ndParamIsValue = false;
131 bool js3rdParamIsValue = false;
133 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
135 if (argumentCount < 1)
136 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
138 if (!JSValueIsString(context, arguments[0]))
139 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
141 // 2nd and 3rd argument can be any type.
142 if (argumentCount >= 2)
144 if(!JSValueIsUndefined(context, arguments[1]) && !JSValueIsNull(context, arguments[1]))
145 js2ndParamIsValue = true;
148 if (argumentCount >= 3)
150 if(!JSValueIsUndefined(context, arguments[2]) && !JSValueIsNull(context, arguments[2]))
151 js3rdParamIsValue = true;
154 } Catch(Exception ) {
155 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
156 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
160 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
162 std::string attributeName;
163 AnyPtr initialValue(NULL);
164 AnyPtr endValue(NULL);
167 attributeName = converter->toString(arguments[0]);
169 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
170 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
175 if(js2ndParamIsValue)
176 initialValue = converter->toAny(arguments[1]);
178 initialValue = AnyPtr(new Any());
181 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
182 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
187 if(js3rdParamIsValue)
188 endValue = converter->toAny(arguments[2]);
190 endValue = AnyPtr(new Any());
193 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
194 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
198 AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
200 JSObjectRef jsobject;
203 jsobject = createJSObject(context, attributeRangeFilter);
205 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
206 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
213 void JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
215 if (!JSObjectGetPrivate(object))
217 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
218 JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
219 if (!JSObjectSetPrivate(object, priv)) {
225 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
227 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
237 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
240 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
242 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
244 AttributeRangeFilterPtr result = priv->getObject();
246 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
251 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
253 JSStringRef propertyName,
254 JSValueRef* exception)
259 FilterConverterFactory::ConverterType converter =
260 FilterConverterFactory::getConverter(context);
261 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
262 return converter->toJSValueRef(attributeRangeFilter->getAttributeName());
264 Catch(WrtDeviceApis::Commons::Exception)
266 LogWarning("trying to get incorrect value");
268 return JSValueMakeUndefined(context);
271 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
273 JSStringRef propertyName,
275 JSValueRef* exception)
279 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
280 FilterConverterFactory::ConverterType converter =
281 FilterConverterFactory::getConverter(context);
282 attributeRangeFilter->setAttributeName(converter->toString(value));
285 Catch(WrtDeviceApis::Commons::Exception)
287 LogWarning("trying to set incorrect value");
290 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
294 JSValueRef JSAttributeRangeFilter::getInitialValue(JSContextRef context,
296 JSStringRef propertyName,
297 JSValueRef* exception)
302 FilterConverterFactory::ConverterType converter =
303 FilterConverterFactory::getConverter(context);
304 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
305 return converter->toJSValueRef(attributeRangeFilter->getInitialValue());
307 Catch(WrtDeviceApis::Commons::Exception)
309 LogWarning("trying to get incorrect value");
311 return JSValueMakeUndefined(context);
314 bool JSAttributeRangeFilter::setInitialValue(JSContextRef context,
316 JSStringRef propertyName,
318 JSValueRef* exception)
322 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
323 FilterConverterFactory::ConverterType converter =
324 FilterConverterFactory::getConverter(context);
325 attributeRangeFilter->setInitialValue(converter->toAny(value));
328 Catch(WrtDeviceApis::Commons::Exception)
330 LogWarning("trying to set incorrect value");
333 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
337 JSValueRef JSAttributeRangeFilter::getEndValue(JSContextRef context,
339 JSStringRef propertyName,
340 JSValueRef* exception)
345 FilterConverterFactory::ConverterType converter =
346 FilterConverterFactory::getConverter(context);
347 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
348 return converter->toJSValueRef(attributeRangeFilter->getEndValue());
350 Catch(WrtDeviceApis::Commons::Exception)
352 LogWarning("trying to get incorrect value");
354 return JSValueMakeUndefined(context);
357 bool JSAttributeRangeFilter::setEndValue(JSContextRef context,
359 JSStringRef propertyName,
361 JSValueRef* exception)
365 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
366 FilterConverterFactory::ConverterType converter =
367 FilterConverterFactory::getConverter(context);
368 attributeRangeFilter->setEndValue(converter->toAny(value));
371 Catch(WrtDeviceApis::Commons::Exception)
373 LogWarning("trying to set incorrect value");
376 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");