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 JSSortMode.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSSortMode class
26 #include <dpl/log/log.h>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <CommonsJavaScript/Validator.h>
30 #include <JSTizenExceptionFactory.h>
31 #include <JSTizenException.h>
33 #include "FilterConverter.h"
34 #include "JSSortMode.h"
36 #define ATTRIBUTE_FILTER_CLASS_NAME "SortMode"
38 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
39 #define ATTRIBUTE_FILTER_ATTR_ORDER "order"
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 JSSortMode::m_classRef = NULL;
50 JSClassDefinition JSSortMode::m_classInfo =
53 kJSClassAttributeNone,
54 ATTRIBUTE_FILTER_CLASS_NAME,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
65 NULL, //CallAsFunction,
66 NULL, //CallAsConstructor,
68 NULL, //ConvertToType,
71 JSStaticValue JSSortMode::m_property[] = {
72 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
73 { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
77 JSStaticFunction JSSortMode::m_functions[] =
82 JSClassRef JSSortMode::getClassRef() {
84 m_classRef = JSClassCreate(&m_classInfo);
89 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
91 return JSValueIsObjectOfClass(context, value, getClassRef());
94 SortModePtr JSSortMode::getSortMode(JSContextRef context, JSValueRef value)
96 if (!isObjectOfClass(context, value)) {
97 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99 JSObjectRef object = JSValueToObject(context, value, NULL);
101 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
105 Throw(WrtDeviceApis::Commons::NullPointerException);
107 return priv->getObject();
110 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
112 JSSortModePriv *priv = new JSSortModePriv(context, privateData);
113 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
114 if (NULL == jsObjectRef) {
115 LogError("object creation error");
121 JSObjectRef JSSortMode::constructor(JSContextRef context,
122 JSObjectRef constructor,
123 size_t argumentCount,
124 const JSValueRef arguments[],
125 JSValueRef* exception)
129 bool js2ndParamIsString = false;
131 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
133 if (argumentCount < 1)
134 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
136 if (!JSValueIsString(context, arguments[0]))
137 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
139 if (argumentCount >= 2)
141 if (JSValueIsString(context, arguments[1]))
142 js2ndParamIsString = true;
144 if (!js2ndParamIsString &&
145 !JSValueIsNull(context, arguments[1]) &&
146 !JSValueIsUndefined(context, arguments[1]))
147 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
150 } Catch(Exception ) {
151 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
152 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
156 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
158 std::string attributeName;
162 attributeName = converter->toString(arguments[0]);
164 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
165 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
170 if(js2ndParamIsString)
171 sortOrder = converter->toSortOrder(arguments[1]);
173 sortOrder = ASCENDING_SORT_ORDER;
176 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
177 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
181 SortModePtr sortMode(new SortMode(attributeName, sortOrder));
183 JSObjectRef jsobject;
186 jsobject = createJSObject(context, sortMode);
188 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
189 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
196 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
198 if (!JSObjectGetPrivate(object))
200 SortModePtr sortMode(new SortMode(""));
201 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
202 if (!JSObjectSetPrivate(object, priv)) {
208 void JSSortMode::Finalize(JSObjectRef object)
210 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
220 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
223 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
225 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
227 SortModePtr result = priv->getObject();
229 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
234 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
236 JSStringRef propertyName,
237 JSValueRef* exception)
242 FilterConverterFactory::ConverterType converter =
243 FilterConverterFactory::getConverter(context);
244 SortModePtr sortMode = getPrivData(object);
245 return converter->toJSValueRef(sortMode->getAttributeName());
247 Catch(WrtDeviceApis::Commons::Exception)
249 LogWarning("trying to get incorrect value");
251 return JSValueMakeUndefined(context);
254 bool JSSortMode::setAttributeName(JSContextRef context,
256 JSStringRef propertyName,
258 JSValueRef* exception)
262 SortModePtr sortMode = getPrivData(object);
263 FilterConverterFactory::ConverterType converter =
264 FilterConverterFactory::getConverter(context);
265 sortMode->setAttributeName(converter->toString(value));
268 Catch(WrtDeviceApis::Commons::Exception)
270 LogWarning("trying to set incorrect value");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
277 JSValueRef JSSortMode::getOrder(JSContextRef context,
279 JSStringRef propertyName,
280 JSValueRef* exception)
285 FilterConverterFactory::ConverterType converter =
286 FilterConverterFactory::getConverter(context);
287 SortModePtr sortMode = getPrivData(object);
288 return converter->toJSValueRef(sortMode->getOrder());
290 Catch(WrtDeviceApis::Commons::Exception)
292 LogWarning("trying to get incorrect value");
294 return JSValueMakeUndefined(context);
297 bool JSSortMode::setOrder(JSContextRef context,
299 JSStringRef propertyName,
301 JSValueRef* exception)
305 SortModePtr sortMode = getPrivData(object);
306 FilterConverterFactory::ConverterType converter =
307 FilterConverterFactory::getConverter(context);
308 sortMode->setOrder(converter->toSortOrder(value));
311 Catch(WrtDeviceApis::Commons::Exception)
313 LogWarning("trying to set incorrect value");
316 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");