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
25 #include "JSSortMode.h"
27 #include <dpl/shared_ptr.h>
29 #include <ArgumentValidator.h>
30 #include <JSWebAPIErrorFactory.h>
33 #define ATTRIBUTE_FILTER_CLASS_NAME "SortMode"
35 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
36 #define ATTRIBUTE_FILTER_ATTR_ORDER "order"
38 namespace DeviceAPI {
\rnamespace Tizen {
40 using namespace DeviceAPI::Common;
41 using namespace DeviceAPI::Tizen;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
45 JSClassRef JSSortMode::m_classRef = NULL;
47 JSClassDefinition JSSortMode::m_classInfo =
50 kJSClassAttributeNone,
51 ATTRIBUTE_FILTER_CLASS_NAME,
60 NULL, //DeleteProperty,
61 NULL, //getPropertyNames,
62 NULL, //CallAsFunction,
63 NULL, //CallAsConstructor,
65 NULL, //ConvertToType,
68 JSStaticValue JSSortMode::m_property[] = {
69 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
70 { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
74 JSStaticFunction JSSortMode::m_functions[] =
79 JSClassRef JSSortMode::getClassRef()
82 m_classRef = JSClassCreate(&m_classInfo);
87 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
89 return JSValueIsObjectOfClass(context, value, getClassRef());
92 SortModePtr JSSortMode::getSortMode(JSContextRef context, JSValueRef value)
94 if (!isObjectOfClass(context, value))
95 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97 JSObjectRef object = JSValueToObject(context, value, NULL);
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
103 Throw(WrtDeviceApis::Commons::NullPointerException);
105 return priv->getObject();
108 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
110 JSSortModePriv *priv = new JSSortModePriv(context, privateData);
111 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
112 if (NULL == jsObjectRef) {
113 LoggerE("object creation error");
119 JSObjectRef JSSortMode::constructor(JSContextRef context,
120 JSObjectRef constructor,
121 size_t argumentCount,
122 const JSValueRef arguments[],
123 JSValueRef* exception)
127 std::string attributeName;
130 ArgumentValidator validator(context, argumentCount, arguments);
134 attributeName = validator.toString(0, false);
136 catch(BasePlatformException &e)
138 return JSWebAPIErrorFactory::postException(context, exception, e);
143 std::string sortOrderStr;
144 if(argumentCount < 2 ||
145 JSValueIsUndefined(context, arguments[1]) ||
146 JSValueIsNull(context, arguments[1]))
147 sortOrderStr = "ASC";
149 sortOrderStr = validator.toString(1, true);
151 if(sortOrderStr == "ASC")
152 sortOrder = ASCENDING_SORT_ORDER;
153 else if(sortOrderStr == "DESC")
154 sortOrder = DESCENDING_SORT_ORDER;
156 throw TypeMismatchException("Property is not valid SortModeOrder value");
158 catch(BasePlatformException &e)
160 return JSWebAPIErrorFactory::postException(context, exception, e);
163 SortModePtr sortMode(new SortMode(attributeName, sortOrder));
165 JSObjectRef jsobject;
169 jsobject = createJSObject(context, sortMode);
173 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
174 return JSWebAPIErrorFactory::postException(context, exception,
175 TypeMismatchException("Error occurred while creating object"));
181 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
183 if (!JSObjectGetPrivate(object))
185 SortModePtr sortMode(new SortMode(""));
186 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
187 if (!JSObjectSetPrivate(object, priv))
194 void JSSortMode::Finalize(JSObjectRef object)
196 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
206 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
209 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
211 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
213 SortModePtr result = priv->getObject();
215 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
220 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
222 JSStringRef propertyName,
223 JSValueRef* exception)
229 SortModePtr sortMode = getPrivData(object);
230 return JSUtil::toJSValueRef(context, sortMode->getAttributeName());
232 catch(BasePlatformException &e)
234 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
237 return JSValueMakeUndefined(context);
240 bool JSSortMode::setAttributeName(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
250 SortModePtr sortMode = getPrivData(object);
251 sortMode->setAttributeName(JSUtil::JSValueToString(context, value));
253 catch(BasePlatformException &e)
255 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
261 JSValueRef JSSortMode::getOrder(JSContextRef context,
263 JSStringRef propertyName,
264 JSValueRef* exception)
270 SortModePtr sortMode = getPrivData(object);
271 SortOrder order = sortMode->getOrder();
272 std::string orderStr;
273 if(order == ASCENDING_SORT_ORDER)
275 else if(order == DESCENDING_SORT_ORDER)
280 return JSUtil::toJSValueRef(context, orderStr);
282 catch(BasePlatformException &e)
284 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
287 return JSValueMakeUndefined(context);
290 bool JSSortMode::setOrder(JSContextRef context,
292 JSStringRef propertyName,
294 JSValueRef* exception)
300 SortModePtr sortMode = getPrivData(object);
303 std::string orderStr = JSUtil::JSValueToString(context, value);
305 if(orderStr == "ASC")
306 order = ASCENDING_SORT_ORDER;
307 else if(orderStr == "DESC")
308 order = DESCENDING_SORT_ORDER;
310 throw TypeMismatchException("Property is not valid SortModeOrder value");
312 sortMode->setOrder(order);
314 catch(BasePlatformException &e)
316 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());