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