Beta merge 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 <Tizen/Common/JSGlobalContextFactory.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 TizenApis {
42 namespace Tizen1_0 {
43 namespace Tizen {
44
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Tizen;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49
50 JSClassRef JSSortMode::m_classRef = NULL;
51
52 JSClassDefinition JSSortMode::m_classInfo =
53 {
54         0,
55         kJSClassAttributeNone,
56         ATTRIBUTE_FILTER_CLASS_NAME,
57         NULL,
58         m_property,
59         m_functions,
60         Initialize,
61         Finalize,
62         NULL, //hasProperty,
63         NULL, //GetProperty,
64         NULL, //SetProperty,
65         NULL, //DeleteProperty,
66         NULL, //getPropertyNames,
67         NULL, //CallAsFunction,
68         constructor, //CallAsConstructor,
69         NULL, //HasInstance,
70         NULL, //ConvertToType,
71 };
72
73 JSStaticValue JSSortMode::m_property[] = {
74         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
75         { ATTRIBUTE_FILTER_ATTR_ORDER, getOrder, setOrder, kJSPropertyAttributeNone },
76         { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSSortMode::m_functions[] =
80 {
81         { 0, 0, 0 }
82 };
83
84 JSClassRef JSSortMode::getClassRef() {
85         if (!m_classRef) {
86                 m_classRef = JSClassCreate(&m_classInfo);
87         }
88         return m_classRef;
89 }
90
91 bool JSSortMode::isObjectOfClass(JSContextRef context, JSValueRef value)
92 {
93         return JSValueIsObjectOfClass(context, value, getClassRef());
94 }
95
96 SortModePtr JSSortMode::getSortMode(JSContextRef context, JSValueRef value)
97 {
98         if (!isObjectOfClass(context, value)) {
99                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100         }
101         JSObjectRef object = JSValueToObject(context, value, NULL);
102         if (!object) {
103                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104         }
105         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
106         if (!priv) {
107                 Throw(WrtDeviceApis::Commons::NullPointerException);
108         }
109         return priv->getObject();
110 }
111
112 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
113 {
114 //      if (!JSObjectGetPrivate(object))
115 //      {
116 //              SortModePtr coord(new SortMode(""));
117 //              JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(coord));
118 //              if (!JSObjectSetPrivate(object, priv)) {
119 //                      delete priv;
120 //              }
121 //      }
122 }
123
124 void JSSortMode::Finalize(JSObjectRef object)
125 {
126         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
127
128         if (priv != NULL)
129         {
130                 delete (priv);
131         }
132
133         priv = NULL;
134 }
135
136 JSObjectRef JSSortMode::createJSObject(JSContextRef context, SortModePtr privateData)
137 {
138         JSSortModePriv *priv = new JSSortModePriv(context, privateData);
139         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
140         if (NULL == jsObjectRef) {
141                 LogError("object creation error");
142                 return NULL;
143         }
144         return jsObjectRef;
145 }
146
147 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
148 {
149         LogDebug("entered");
150         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
151         if (!priv) {
152                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153         }
154         SortModePtr result = priv->getObject();
155         if (!result) {
156                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157         }
158         return result;
159 }
160
161 JSObjectRef JSSortMode::constructor(JSContextRef context,
162                 JSObjectRef constructor,
163                 size_t argumentCount,
164                 const JSValueRef arguments[],
165                 JSValueRef* exception)
166 {
167         LogDebug("entered");
168
169 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
170 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
171
172 //      JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(constructor));
173 //      if (!priv) {
174 //              ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175 //      }
176 //      JSContextRef gContext = priv->getContext();
177
178         JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
179
180         BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
181         Try {
182                 if (argumentCount < 1 || argumentCount > 2)
183                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
184
185                 if (!JSValueIsString(gContext, arguments[0]))
186                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
187
188                 if (argumentCount >= 2)
189                 {
190                         if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
191                                 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
192                 }
193
194                 // 3rd argument can be any type.
195
196         } Catch(Exception ) {
197                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
198                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
199                 return NULL;
200         }
201
202         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
203
204         std::string attributeName;
205         SortOrder sortOrder;
206
207         Try {
208                 attributeName = converter->toString(arguments[0]);
209         } Catch(Exception) {
210                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
211                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
212                 return NULL;
213         }
214
215         Try {
216                 sortOrder = ASCENDING_SORT_ORDER;
217                 if(argumentCount >= 2)
218                 {
219                         if(JSValueIsString(gContext, arguments[1]))
220                                 sortOrder = converter->toSortOrder(arguments[1]);
221                 }
222         } Catch(Exception) {
223                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
224                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
225                 return NULL;
226         }
227
228         SortModePtr sortMode(new SortMode(attributeName, sortOrder));
229
230         JSObjectRef jsobject;
231
232         Try {
233                 jsobject = createJSObject(gContext, sortMode);
234         } Catch(Exception) {
235                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
236                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
237                 return NULL;
238         }
239
240         return jsobject;
241 }
242
243 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
244                 JSObjectRef object,
245                 JSStringRef propertyName,
246                 JSValueRef* exception)
247 {
248         LogDebug("entered");
249         Try
250         {
251                 FilterConverterFactory::ConverterType converter =
252                                 FilterConverterFactory::getConverter(context);
253                 SortModePtr sortMode = getPrivData(object);
254                 return converter->toJSValueRef(sortMode->getAttributeName());
255         }
256         Catch(WrtDeviceApis::Commons::Exception)
257         {
258                 LogWarning("trying to get incorrect value");
259         }
260         return JSValueMakeUndefined(context);
261 }
262
263 bool JSSortMode::setAttributeName(JSContextRef context,
264                 JSObjectRef object,
265                 JSStringRef propertyName,
266                 JSValueRef value,
267                 JSValueRef* exception)
268 {
269         Try
270         {
271                 SortModePtr sortMode = getPrivData(object);
272                 FilterConverterFactory::ConverterType converter =
273                                 FilterConverterFactory::getConverter(context);
274                 sortMode->setAttributeName(converter->toString(value));
275                 return true;
276         }
277         Catch(WrtDeviceApis::Commons::Exception)
278         {
279                 LogWarning("trying to set incorrect value");
280         }
281
282         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
283         return false;
284 }
285
286 JSValueRef JSSortMode::getOrder(JSContextRef context,
287                 JSObjectRef object,
288                 JSStringRef propertyName,
289                 JSValueRef* exception)
290 {
291         LogDebug("entered");
292         Try
293         {
294                 FilterConverterFactory::ConverterType converter =
295                                 FilterConverterFactory::getConverter(context);
296                 SortModePtr sortMode = getPrivData(object);
297                 return converter->toJSValueRef(sortMode->getOrder());
298         }
299         Catch(WrtDeviceApis::Commons::Exception)
300         {
301                 LogWarning("trying to get incorrect value");
302         }
303         return JSValueMakeUndefined(context);
304 }
305
306 bool JSSortMode::setOrder(JSContextRef context,
307                 JSObjectRef object,
308                 JSStringRef propertyName,
309                 JSValueRef value,
310                 JSValueRef* exception)
311 {
312         Try
313         {
314                 SortModePtr sortMode = getPrivData(object);
315                 FilterConverterFactory::ConverterType converter =
316                                 FilterConverterFactory::getConverter(context);
317                 sortMode->setOrder(converter->toSortOrder(value));
318                 return true;
319         }
320         Catch(WrtDeviceApis::Commons::Exception)
321         {
322                 LogWarning("trying to set incorrect value");
323         }
324
325         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
326         return false;
327 }
328
329 } // Tizen
330 } // Tizen1_0
331 } // TizenApis