Update change log and spec for wrt-plugins-tizen_0.4.11
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSSortMode.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        JSSortMode.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSSortMode class
23  */
24
25 #include <string>
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>
32 #include "SortMode.h"
33 #include "FilterConverter.h"
34 #include "JSSortMode.h"
35
36 #define ATTRIBUTE_FILTER_CLASS_NAME "SortMode"
37
38 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
39 #define ATTRIBUTE_FILTER_ATTR_ORDER "order"
40
41 namespace DeviceAPI {\rnamespace Tizen {
42
43 using namespace DeviceAPI::Common;
44 using namespace DeviceAPI::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47
48 JSClassRef JSSortMode::m_classRef = NULL;
49
50 JSClassDefinition JSSortMode::m_classInfo =
51 {
52         0,
53         kJSClassAttributeNone,
54         ATTRIBUTE_FILTER_CLASS_NAME,
55         NULL,
56         m_property,
57         m_functions,
58         Initialize,
59         Finalize,
60         NULL, //hasProperty,
61         NULL, //GetProperty,
62         NULL, //SetProperty,
63         NULL, //DeleteProperty,
64         NULL, //getPropertyNames,
65         NULL, //CallAsFunction,
66         NULL, //CallAsConstructor,
67         NULL, //HasInstance,
68         NULL, //ConvertToType,
69 };
70
71 JSStaticValue JSSortMode::m_property[] = {
72         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
73         { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
74         { 0, 0, 0, 0 }
75 };
76
77 JSStaticFunction JSSortMode::m_functions[] =
78 {
79         { 0, 0, 0 }
80 };
81
82 JSClassRef JSSortMode::getClassRef() {
83         if (!m_classRef) {
84                 m_classRef = JSClassCreate(&m_classInfo);
85         }
86         return m_classRef;
87 }
88
89 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
90 {
91         return JSValueIsObjectOfClass(context, value, getClassRef());
92 }
93
94 SortModePtr JSSortMode::getSortMode(JSContextRef context, JSValueRef value)
95 {
96         if (!isObjectOfClass(context, value)) {
97                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98         }
99         JSObjectRef object = JSValueToObject(context, value, NULL);
100         if (!object) {
101                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102         }
103         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
104         if (!priv) {
105                 Throw(WrtDeviceApis::Commons::NullPointerException);
106         }
107         return priv->getObject();
108 }
109
110 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
111 {
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");
116                 return NULL;
117         }
118         return jsObjectRef;
119 }
120
121 JSObjectRef JSSortMode::constructor(JSContextRef context,
122                 JSObjectRef constructor,
123                 size_t argumentCount,
124                 const JSValueRef arguments[],
125                 JSValueRef* exception)
126 {
127         LogDebug("entered");
128
129         bool js2ndParamIsString = false;
130
131         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
132         Try {
133                 if (argumentCount < 1)
134                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
135
136                 if (!JSValueIsString(context, arguments[0]))
137                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
138
139                 if (argumentCount >= 2)
140                 {
141                         if (JSValueIsString(context, arguments[1]))
142                                 js2ndParamIsString = true;
143
144                         if (!js2ndParamIsString &&
145                                         !JSValueIsNull(context, arguments[1]) &&
146                                         !JSValueIsUndefined(context, arguments[1]))
147                                 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
148                 }
149
150         } Catch(Exception ) {
151                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
152                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
153                 return NULL;
154         }
155
156         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
157
158         std::string attributeName;
159         SortOrder sortOrder;
160
161         Try {
162                 attributeName = converter->toString(arguments[0]);
163         } Catch(Exception) {
164                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
165                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
166                 return NULL;
167         }
168
169         Try {
170                 if(js2ndParamIsString)
171                         sortOrder = converter->toSortOrder(arguments[1]);
172                 else
173                         sortOrder = ASCENDING_SORT_ORDER;
174
175         } Catch(Exception) {
176                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
177                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
178                 return NULL;
179         }
180
181         SortModePtr sortMode(new SortMode(attributeName, sortOrder));
182
183         JSObjectRef jsobject;
184
185         Try {
186                 jsobject = createJSObject(context, sortMode);
187         } Catch(Exception) {
188                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
189                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
190                 return NULL;
191         }
192
193         return jsobject;
194 }
195
196 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
197 {
198         if (!JSObjectGetPrivate(object))
199         {
200                 SortModePtr sortMode(new SortMode(""));
201                 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
202                 if (!JSObjectSetPrivate(object, priv)) {
203                         delete priv;
204                 }
205         }
206 }
207
208 void JSSortMode::Finalize(JSObjectRef object)
209 {
210         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
211
212         if (priv != NULL)
213         {
214                 delete (priv);
215         }
216
217         priv = NULL;
218 }
219
220 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
221 {
222         LogDebug("entered");
223         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
224         if (!priv) {
225                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
226         }
227         SortModePtr result = priv->getObject();
228         if (!result) {
229                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
230         }
231         return result;
232 }
233
234 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
235                 JSObjectRef object,
236                 JSStringRef propertyName,
237                 JSValueRef* exception)
238 {
239         LogDebug("entered");
240         Try
241         {
242                 FilterConverterFactory::ConverterType converter =
243                                 FilterConverterFactory::getConverter(context);
244                 SortModePtr sortMode = getPrivData(object);
245                 return converter->toJSValueRef(sortMode->getAttributeName());
246         }
247         Catch(WrtDeviceApis::Commons::Exception)
248         {
249                 LogWarning("trying to get incorrect value");
250         }
251         return JSValueMakeUndefined(context);
252 }
253
254 bool JSSortMode::setAttributeName(JSContextRef context,
255                 JSObjectRef object,
256                 JSStringRef propertyName,
257                 JSValueRef value,
258                 JSValueRef* exception)
259 {
260         Try
261         {
262                 SortModePtr sortMode = getPrivData(object);
263                 FilterConverterFactory::ConverterType converter =
264                                 FilterConverterFactory::getConverter(context);
265                 sortMode->setAttributeName(converter->toString(value));
266                 return true;
267         }
268         Catch(WrtDeviceApis::Commons::Exception)
269         {
270                 LogWarning("trying to set incorrect value");
271         }
272
273         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
274         return false;
275 }
276
277 JSValueRef JSSortMode::getOrder(JSContextRef context,
278                 JSObjectRef object,
279                 JSStringRef propertyName,
280                 JSValueRef* exception)
281 {
282         LogDebug("entered");
283         Try
284         {
285                 FilterConverterFactory::ConverterType converter =
286                                 FilterConverterFactory::getConverter(context);
287                 SortModePtr sortMode = getPrivData(object);
288                 return converter->toJSValueRef(sortMode->getOrder());
289         }
290         Catch(WrtDeviceApis::Commons::Exception)
291         {
292                 LogWarning("trying to get incorrect value");
293         }
294         return JSValueMakeUndefined(context);
295 }
296
297 bool JSSortMode::setOrder(JSContextRef context,
298                 JSObjectRef object,
299                 JSStringRef propertyName,
300                 JSValueRef value,
301                 JSValueRef* exception)
302 {
303         Try
304         {
305                 SortModePtr sortMode = getPrivData(object);
306                 FilterConverterFactory::ConverterType converter =
307                                 FilterConverterFactory::getConverter(context);
308                 sortMode->setOrder(converter->toSortOrder(value));
309                 return true;
310         }
311         Catch(WrtDeviceApis::Commons::Exception)
312         {
313                 LogWarning("trying to set incorrect value");
314         }
315
316         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
317         return false;
318 }
319
320 } // Tizen
321 } // DeviceAPI
322