Update change log and spec for wrt-plugins-tizen_0.4.49
[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 "JSSortMode.h"
26 #include <string>
27 #include <dpl/shared_ptr.h>
28 #include <JSUtil.h>
29 #include <ArgumentValidator.h>
30 #include <JSWebAPIErrorFactory.h>
31 #include <Logger.h>
32
33 #define ATTRIBUTE_FILTER_CLASS_NAME "SortMode"
34
35 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
36 #define ATTRIBUTE_FILTER_ATTR_ORDER "order"
37
38 namespace DeviceAPI {\rnamespace Tizen {
39
40 using namespace DeviceAPI::Common;
41 using namespace DeviceAPI::Tizen;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
44
45 JSClassRef JSSortMode::m_classRef = NULL;
46
47 JSClassDefinition JSSortMode::m_classInfo =
48 {
49         0,
50         kJSClassAttributeNone,
51         ATTRIBUTE_FILTER_CLASS_NAME,
52         NULL,
53         m_property,
54         m_functions,
55         Initialize,
56         Finalize,
57         NULL, //hasProperty,
58         NULL, //GetProperty,
59         NULL, //SetProperty,
60         NULL, //DeleteProperty,
61         NULL, //getPropertyNames,
62         NULL, //CallAsFunction,
63         NULL, //CallAsConstructor,
64         NULL, //HasInstance,
65         NULL, //ConvertToType,
66 };
67
68 JSStaticValue JSSortMode::m_property[] = {
69         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
70         { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
71         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSSortMode::m_functions[] =
75 {
76         { 0, 0, 0 }
77 };
78
79 JSClassRef JSSortMode::getClassRef()
80 {
81         if (!m_classRef)
82                 m_classRef = JSClassCreate(&m_classInfo);
83
84         return m_classRef;
85 }
86
87 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
88 {
89         return JSValueIsObjectOfClass(context, value, getClassRef());
90 }
91
92 SortModePtr JSSortMode::getSortMode(JSContextRef context, JSValueRef value)
93 {
94         if (!isObjectOfClass(context, value))
95                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
96
97         JSObjectRef object = JSValueToObject(context, value, NULL);
98         if (!object)
99                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100
101         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
102         if (!priv)
103                 Throw(WrtDeviceApis::Commons::NullPointerException);
104
105         return priv->getObject();
106 }
107
108 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
109 {
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");
114                 return NULL;
115         }
116         return jsObjectRef;
117 }
118
119 JSObjectRef JSSortMode::constructor(JSContextRef context,
120                 JSObjectRef constructor,
121                 size_t argumentCount,
122                 const JSValueRef arguments[],
123                 JSValueRef* exception)
124 {
125         LoggerD("entered");
126
127         std::string attributeName;
128         SortOrder sortOrder;
129
130         ArgumentValidator validator(context, argumentCount, arguments);
131
132         try
133         {
134                 attributeName = validator.toString(0, false);
135         }
136         catch(BasePlatformException &e)
137         {
138                 LoggerW("Failed to convert 1st parameter to string. Using default value.");
139                 attributeName = "";
140         }
141
142         try
143         {
144                 std::string sortOrderStr;
145                 if(argumentCount < 2 ||
146                                 JSValueIsUndefined(context, arguments[1]) ||
147                                 JSValueIsNull(context, arguments[1]))
148                         sortOrderStr = "ASC";
149                 else
150                         sortOrderStr = validator.toString(1, true);
151
152                 if(sortOrderStr == "ASC")
153                         sortOrder = ASCENDING_SORT_ORDER;
154                 else if(sortOrderStr == "DESC")
155                         sortOrder = DESCENDING_SORT_ORDER;
156                 else
157                         throw TypeMismatchException("Property is not valid SortModeOrder value");
158         }
159         catch(BasePlatformException &e)
160         {
161                 LoggerW("Failed to convert 2nd parameter to sort order. Using default value.");
162                 sortOrder = ASCENDING_SORT_ORDER;
163         }
164
165         SortModePtr sortMode(new SortMode(attributeName, sortOrder));
166
167         JSObjectRef jsobject = NULL;
168
169         Try
170         {
171                 jsobject = createJSObject(context, sortMode);
172         }
173         Catch(Exception)
174         {
175                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
176         }
177
178         if(jsobject == NULL)
179                 return JSObjectMake(context, NULL, NULL);
180
181         return jsobject;
182 }
183
184 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
185 {
186         if (!JSObjectGetPrivate(object))
187         {
188                 SortModePtr sortMode(new SortMode(""));
189                 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
190                 if (!JSObjectSetPrivate(object, priv))
191                 {
192                         delete priv;
193                 }
194         }
195 }
196
197 void JSSortMode::Finalize(JSObjectRef object)
198 {
199         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
200
201         if (priv != NULL)
202         {
203                 delete (priv);
204         }
205
206         priv = NULL;
207 }
208
209 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
210 {
211         LoggerD("entered");
212         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
213         if (!priv)
214                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
215
216         SortModePtr result = priv->getObject();
217         if (!result)
218                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
219
220         return result;
221 }
222
223 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
224                 JSObjectRef object,
225                 JSStringRef propertyName,
226                 JSValueRef* exception)
227 {
228         LoggerD("entered");
229
230         try
231         {
232                 SortModePtr sortMode = getPrivData(object);
233                 return JSUtil::toJSValueRef(context, sortMode->getAttributeName());
234         }
235         catch(BasePlatformException &e)
236         {
237                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
238         }
239
240         return JSValueMakeUndefined(context);
241 }
242
243 bool JSSortMode::setAttributeName(JSContextRef context,
244                 JSObjectRef object,
245                 JSStringRef propertyName,
246                 JSValueRef value,
247                 JSValueRef* exception)
248 {
249         LoggerD("entered");
250
251         try
252         {
253                 SortModePtr sortMode = getPrivData(object);
254                 sortMode->setAttributeName(JSUtil::JSValueToString(context, value));
255         }
256         catch(BasePlatformException &e)
257         {
258                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
259         }
260
261         return true;
262 }
263
264 JSValueRef JSSortMode::getOrder(JSContextRef context,
265                 JSObjectRef object,
266                 JSStringRef propertyName,
267                 JSValueRef* exception)
268 {
269         LoggerD("entered");
270
271         try
272         {
273                 SortModePtr sortMode = getPrivData(object);
274                 SortOrder order = sortMode->getOrder();
275                 std::string orderStr;
276                 if(order == ASCENDING_SORT_ORDER)
277                         orderStr = "ASC";
278                 else if(order == DESCENDING_SORT_ORDER)
279                         orderStr = "DESC";
280                 else
281                         orderStr = "ASC";
282
283                 return JSUtil::toJSValueRef(context, orderStr);
284         }
285         catch(BasePlatformException &e)
286         {
287                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
288         }
289
290         return JSValueMakeUndefined(context);
291 }
292
293 bool JSSortMode::setOrder(JSContextRef context,
294                 JSObjectRef object,
295                 JSStringRef propertyName,
296                 JSValueRef value,
297                 JSValueRef* exception)
298 {
299         LoggerD("entered");
300
301         try
302         {
303                 SortModePtr sortMode = getPrivData(object);
304
305                 SortOrder order;
306                 std::string orderStr = JSUtil::JSValueToString(context, value);
307
308                 if(orderStr == "ASC")
309                         order = ASCENDING_SORT_ORDER;
310                 else if(orderStr == "DESC")
311                         order = DESCENDING_SORT_ORDER;
312                 else
313                         throw TypeMismatchException("Property is not valid SortModeOrder value");
314
315                 sortMode->setOrder(order);
316         }
317         catch(BasePlatformException &e)
318         {
319                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
320         }
321
322         return true;
323 }
324
325 } // Tizen
326 } // DeviceAPI
327