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 "FilterConverter.h"
33 #include "JSSortMode.h"
35 #define ATTRIBUTE_FILTER_CLASS_NAME "SortMode"
37 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
38 #define ATTRIBUTE_FILTER_ATTR_ORDER "order"
44 using namespace TizenApis::Commons;
45 using namespace TizenApis::Api::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
49 JSClassRef JSSortMode::m_classRef = NULL;
51 JSClassDefinition JSSortMode::m_classInfo =
54 kJSClassAttributeNone,
55 ATTRIBUTE_FILTER_CLASS_NAME,
64 NULL, //DeleteProperty,
65 NULL, //getPropertyNames,
66 NULL, //CallAsFunction,
67 constructor, //CallAsConstructor,
69 NULL, //ConvertToType,
72 JSStaticValue JSSortMode::m_property[] = {
73 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
74 { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
78 JSStaticFunction JSSortMode::m_functions[] =
83 JSClassRef JSSortMode::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 SortModePtr JSSortMode::getSortMode(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 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
113 JSSortModePriv *priv = new JSSortModePriv(context, privateData);
114 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
115 if (NULL == jsObjectRef) {
116 LogError("object creation error");
122 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
124 if (!JSObjectGetPrivate(object))
126 SortModePtr sortMode(new SortMode(""));
127 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
128 if (!JSObjectSetPrivate(object, priv)) {
134 void JSSortMode::Finalize(JSObjectRef object)
136 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
146 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
149 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 SortModePtr result = priv->getObject();
155 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160 JSObjectRef JSSortMode::constructor(JSContextRef context,
161 JSObjectRef constructor,
162 size_t argumentCount,
163 const JSValueRef arguments[],
164 JSValueRef* exception)
168 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
169 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
171 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(constructor));
173 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175 JSContextRef gContext = priv->getContext();
177 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
179 BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
181 if (argumentCount < 1 || argumentCount > 2)
182 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
184 if (!JSValueIsString(gContext, arguments[0]))
185 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
187 if (argumentCount >= 2)
189 if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
190 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
193 // 3rd argument can be any type.
195 } Catch(Exception ) {
196 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
197 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
201 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
203 std::string attributeName;
207 attributeName = converter->toString(arguments[0]);
209 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
210 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215 sortOrder = ASCENDING_SORT_ORDER;
216 if(argumentCount >= 2)
218 if(JSValueIsString(gContext, arguments[1]))
219 sortOrder = converter->toSortOrder(arguments[1]);
222 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
223 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
227 SortModePtr sortMode(new SortMode(attributeName, sortOrder));
229 JSObjectRef jsobject;
232 jsobject = createJSObject(gContext, sortMode);
234 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
235 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
242 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
244 JSStringRef propertyName,
245 JSValueRef* exception)
250 FilterConverterFactory::ConverterType converter =
251 FilterConverterFactory::getConverter(context);
252 SortModePtr sortMode = getPrivData(object);
253 return converter->toJSValueRef(sortMode->getAttributeName());
255 Catch(WrtDeviceApis::Commons::Exception)
257 LogWarning("trying to get incorrect value");
259 return JSValueMakeUndefined(context);
262 bool JSSortMode::setAttributeName(JSContextRef context,
264 JSStringRef propertyName,
266 JSValueRef* exception)
270 SortModePtr sortMode = getPrivData(object);
271 FilterConverterFactory::ConverterType converter =
272 FilterConverterFactory::getConverter(context);
273 sortMode->setAttributeName(converter->toString(value));
276 Catch(WrtDeviceApis::Commons::Exception)
278 LogWarning("trying to set incorrect value");
281 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
285 JSValueRef JSSortMode::getOrder(JSContextRef context,
287 JSStringRef propertyName,
288 JSValueRef* exception)
293 FilterConverterFactory::ConverterType converter =
294 FilterConverterFactory::getConverter(context);
295 SortModePtr sortMode = getPrivData(object);
296 return converter->toJSValueRef(sortMode->getOrder());
298 Catch(WrtDeviceApis::Commons::Exception)
300 LogWarning("trying to get incorrect value");
302 return JSValueMakeUndefined(context);
305 bool JSSortMode::setOrder(JSContextRef context,
307 JSStringRef propertyName,
309 JSValueRef* exception)
313 SortModePtr sortMode = getPrivData(object);
314 FilterConverterFactory::ConverterType converter =
315 FilterConverterFactory::getConverter(context);
316 sortMode->setOrder(converter->toSortOrder(value));
319 Catch(WrtDeviceApis::Commons::Exception)
321 LogWarning("trying to set incorrect value");
324 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");