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 JSSortMode.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSSortMode 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 <API/Filter/SortMode.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/JSGlobalContextFactory.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"
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Tizen;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
50 JSClassRef JSSortMode::m_classRef = NULL;
52 JSClassDefinition JSSortMode::m_classInfo =
55 kJSClassAttributeNone,
56 ATTRIBUTE_FILTER_CLASS_NAME,
65 NULL, //DeleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //CallAsFunction,
68 constructor, //CallAsConstructor,
70 NULL, //ConvertToType,
73 JSStaticValue JSSortMode::m_property[] = {
74 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
75 { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
79 JSStaticFunction JSSortMode::m_functions[] =
84 JSClassRef JSSortMode::getClassRef() {
86 m_classRef = JSClassCreate(&m_classInfo);
91 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
93 return JSValueIsObjectOfClass(context, value, getClassRef());
96 SortModePtr JSSortMode::getSortMode(JSContextRef context, JSValueRef value)
98 if (!isObjectOfClass(context, value)) {
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101 JSObjectRef object = JSValueToObject(context, value, NULL);
103 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
105 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
107 Throw(WrtDeviceApis::Commons::NullPointerException);
109 return priv->getObject();
112 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
114 // if (!JSObjectGetPrivate(object))
116 // SortModePtr coord(new SortMode(""));
117 // JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(coord));
118 // if (!JSObjectSetPrivate(object, priv)) {
124 void JSSortMode::Finalize(JSObjectRef object)
126 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
136 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
138 JSSortModePriv *priv = new JSSortModePriv(context, privateData);
139 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
140 if (NULL == jsObjectRef) {
141 LogError("object creation error");
147 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
150 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
152 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154 SortModePtr result = priv->getObject();
156 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
161 JSObjectRef JSSortMode::constructor(JSContextRef context,
162 JSObjectRef constructor,
163 size_t argumentCount,
164 const JSValueRef arguments[],
165 JSValueRef* exception)
169 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
170 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
172 // JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(constructor));
174 // ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
176 // JSContextRef gContext = priv->getContext();
178 JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
180 BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
182 if (argumentCount < 1 || argumentCount > 2)
183 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
185 if (!JSValueIsString(gContext, arguments[0]))
186 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
188 if (argumentCount >= 2)
190 if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
191 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
194 // 3rd argument can be any type.
196 } Catch(Exception ) {
197 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
198 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
202 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
204 std::string attributeName;
208 attributeName = converter->toString(arguments[0]);
210 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
211 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
216 sortOrder = ASCENDING_SORT_ORDER;
217 if(argumentCount >= 2)
219 if(JSValueIsString(gContext, arguments[1]))
220 sortOrder = converter->toSortOrder(arguments[1]);
223 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
224 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
228 SortModePtr sortMode(new SortMode(attributeName, sortOrder));
230 JSObjectRef jsobject;
233 jsobject = createJSObject(gContext, sortMode);
235 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
236 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
243 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
245 JSStringRef propertyName,
246 JSValueRef* exception)
251 FilterConverterFactory::ConverterType converter =
252 FilterConverterFactory::getConverter(context);
253 SortModePtr sortMode = getPrivData(object);
254 return converter->toJSValueRef(sortMode->getAttributeName());
256 Catch(WrtDeviceApis::Commons::Exception)
258 LogWarning("trying to get incorrect value");
260 return JSValueMakeUndefined(context);
263 bool JSSortMode::setAttributeName(JSContextRef context,
265 JSStringRef propertyName,
267 JSValueRef* exception)
271 SortModePtr sortMode = getPrivData(object);
272 FilterConverterFactory::ConverterType converter =
273 FilterConverterFactory::getConverter(context);
274 sortMode->setAttributeName(converter->toString(value));
277 Catch(WrtDeviceApis::Commons::Exception)
279 LogWarning("trying to set incorrect value");
282 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
286 JSValueRef JSSortMode::getOrder(JSContextRef context,
288 JSStringRef propertyName,
289 JSValueRef* exception)
294 FilterConverterFactory::ConverterType converter =
295 FilterConverterFactory::getConverter(context);
296 SortModePtr sortMode = getPrivData(object);
297 return converter->toJSValueRef(sortMode->getOrder());
299 Catch(WrtDeviceApis::Commons::Exception)
301 LogWarning("trying to get incorrect value");
303 return JSValueMakeUndefined(context);
306 bool JSSortMode::setOrder(JSContextRef context,
308 JSStringRef propertyName,
310 JSValueRef* exception)
314 SortModePtr sortMode = getPrivData(object);
315 FilterConverterFactory::ConverterType converter =
316 FilterConverterFactory::getConverter(context);
317 sortMode->setOrder(converter->toSortOrder(value));
320 Catch(WrtDeviceApis::Commons::Exception)
322 LogWarning("trying to set incorrect value");
325 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");