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 constructor, //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 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
123 if (!JSObjectGetPrivate(object))
125 SortModePtr sortMode(new SortMode(""));
126 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
127 if (!JSObjectSetPrivate(object, priv)) {
133 void JSSortMode::Finalize(JSObjectRef object)
135 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
145 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
148 JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
150 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152 SortModePtr result = priv->getObject();
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159 JSObjectRef JSSortMode::constructor(JSContextRef context,
160 JSObjectRef constructor,
161 size_t argumentCount,
162 const JSValueRef arguments[],
163 JSValueRef* exception)
167 bool js2ndParamIsString = false;
169 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(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)
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]))
191 js2ndParamIsString = true;
193 if (!js2ndParamIsString &&
194 !JSValueIsNull(gContext, arguments[1]) &&
195 !JSValueIsUndefined(gContext, arguments[1]))
196 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
199 } Catch(Exception ) {
200 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
201 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
205 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
207 std::string attributeName;
211 attributeName = converter->toString(arguments[0]);
213 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
214 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
219 if(js2ndParamIsString)
220 sortOrder = converter->toSortOrder(arguments[1]);
222 sortOrder = ASCENDING_SORT_ORDER;
225 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
226 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
230 SortModePtr sortMode(new SortMode(attributeName, sortOrder));
232 JSObjectRef jsobject;
235 jsobject = createJSObject(gContext, sortMode);
237 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
238 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
245 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
247 JSStringRef propertyName,
248 JSValueRef* exception)
253 FilterConverterFactory::ConverterType converter =
254 FilterConverterFactory::getConverter(context);
255 SortModePtr sortMode = getPrivData(object);
256 return converter->toJSValueRef(sortMode->getAttributeName());
258 Catch(WrtDeviceApis::Commons::Exception)
260 LogWarning("trying to get incorrect value");
262 return JSValueMakeUndefined(context);
265 bool JSSortMode::setAttributeName(JSContextRef context,
267 JSStringRef propertyName,
269 JSValueRef* exception)
273 SortModePtr sortMode = getPrivData(object);
274 FilterConverterFactory::ConverterType converter =
275 FilterConverterFactory::getConverter(context);
276 sortMode->setAttributeName(converter->toString(value));
279 Catch(WrtDeviceApis::Commons::Exception)
281 LogWarning("trying to set incorrect value");
284 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
288 JSValueRef JSSortMode::getOrder(JSContextRef context,
290 JSStringRef propertyName,
291 JSValueRef* exception)
296 FilterConverterFactory::ConverterType converter =
297 FilterConverterFactory::getConverter(context);
298 SortModePtr sortMode = getPrivData(object);
299 return converter->toJSValueRef(sortMode->getOrder());
301 Catch(WrtDeviceApis::Commons::Exception)
303 LogWarning("trying to get incorrect value");
305 return JSValueMakeUndefined(context);
308 bool JSSortMode::setOrder(JSContextRef context,
310 JSStringRef propertyName,
312 JSValueRef* exception)
316 SortModePtr sortMode = getPrivData(object);
317 FilterConverterFactory::ConverterType converter =
318 FilterConverterFactory::getConverter(context);
319 sortMode->setOrder(converter->toSortOrder(value));
322 Catch(WrtDeviceApis::Commons::Exception)
324 LogWarning("trying to set incorrect value");
327 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");