3f3cfc87f5fe78072a2ade7d7d493580aaded6d3
[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                 return JSWebAPIErrorFactory::postException(context, exception, e);
139         }
140
141         try
142         {
143                 std::string sortOrderStr;
144                 if(argumentCount < 2 ||
145                                 JSValueIsUndefined(context, arguments[1]) ||
146                                 JSValueIsNull(context, arguments[1]))
147                         sortOrderStr = "ASC";
148                 else
149                         sortOrderStr = validator.toString(1, true);
150
151                 if(sortOrderStr == "ASC")
152                         sortOrder = ASCENDING_SORT_ORDER;
153                 else if(sortOrderStr == "DESC")
154                         sortOrder = DESCENDING_SORT_ORDER;
155                 else
156                         throw TypeMismatchException("Property is not valid SortModeOrder value");
157         }
158         catch(BasePlatformException &e)
159         {
160                 return JSWebAPIErrorFactory::postException(context, exception, e);
161         }
162
163         SortModePtr sortMode(new SortMode(attributeName, sortOrder));
164
165         JSObjectRef jsobject;
166
167         Try
168         {
169                 jsobject = createJSObject(context, sortMode);
170         }
171         Catch(Exception)
172         {
173                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
174                 return JSWebAPIErrorFactory::postException(context, exception,
175                                 TypeMismatchException("Error occurred while creating object"));
176         }
177
178         return jsobject;
179 }
180
181 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
182 {
183         if (!JSObjectGetPrivate(object))
184         {
185                 SortModePtr sortMode(new SortMode(""));
186                 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
187                 if (!JSObjectSetPrivate(object, priv))
188                 {
189                         delete priv;
190                 }
191         }
192 }
193
194 void JSSortMode::Finalize(JSObjectRef object)
195 {
196         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
197
198         if (priv != NULL)
199         {
200                 delete (priv);
201         }
202
203         priv = NULL;
204 }
205
206 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
207 {
208         LoggerD("entered");
209         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
210         if (!priv)
211                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
212
213         SortModePtr result = priv->getObject();
214         if (!result)
215                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
216
217         return result;
218 }
219
220 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
221                 JSObjectRef object,
222                 JSStringRef propertyName,
223                 JSValueRef* exception)
224 {
225         LoggerD("entered");
226
227         try
228         {
229                 SortModePtr sortMode = getPrivData(object);
230                 return JSUtil::toJSValueRef(context, sortMode->getAttributeName());
231         }
232         catch(BasePlatformException &e)
233         {
234                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
235         }
236
237         return JSValueMakeUndefined(context);
238 }
239
240 bool JSSortMode::setAttributeName(JSContextRef context,
241                 JSObjectRef object,
242                 JSStringRef propertyName,
243                 JSValueRef value,
244                 JSValueRef* exception)
245 {
246         LoggerD("entered");
247
248         try
249         {
250                 SortModePtr sortMode = getPrivData(object);
251                 sortMode->setAttributeName(JSUtil::JSValueToString(context, value));
252         }
253         catch(BasePlatformException &e)
254         {
255                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
256         }
257
258         return true;
259 }
260
261 JSValueRef JSSortMode::getOrder(JSContextRef context,
262                 JSObjectRef object,
263                 JSStringRef propertyName,
264                 JSValueRef* exception)
265 {
266         LoggerD("entered");
267
268         try
269         {
270                 SortModePtr sortMode = getPrivData(object);
271                 SortOrder order = sortMode->getOrder();
272                 std::string orderStr;
273                 if(order == ASCENDING_SORT_ORDER)
274                         orderStr = "ASC";
275                 else if(order == DESCENDING_SORT_ORDER)
276                         orderStr = "DESC";
277                 else
278                         orderStr = "ASC";
279
280                 return JSUtil::toJSValueRef(context, orderStr);
281         }
282         catch(BasePlatformException &e)
283         {
284                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
285         }
286
287         return JSValueMakeUndefined(context);
288 }
289
290 bool JSSortMode::setOrder(JSContextRef context,
291                 JSObjectRef object,
292                 JSStringRef propertyName,
293                 JSValueRef value,
294                 JSValueRef* exception)
295 {
296         LoggerD("entered");
297
298         try
299         {
300                 SortModePtr sortMode = getPrivData(object);
301
302                 SortOrder order;
303                 std::string orderStr = JSUtil::JSValueToString(context, value);
304
305                 if(orderStr == "ASC")
306                         order = ASCENDING_SORT_ORDER;
307                 else if(orderStr == "DESC")
308                         order = DESCENDING_SORT_ORDER;
309                 else
310                         throw TypeMismatchException("Property is not valid SortModeOrder value");
311
312                 sortMode->setOrder(order);
313         }
314         catch(BasePlatformException &e)
315         {
316                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
317         }
318
319         return true;
320 }
321
322 } // Tizen
323 } // DeviceAPI
324