5b568dfc7179a2b339ee3c69f38a6ef68f995463
[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         constructor, //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 void JSSortMode::Initialize(JSContextRef context, JSObjectRef object)
122 {
123         if (!JSObjectGetPrivate(object))
124         {
125                 SortModePtr sortMode(new SortMode(""));
126                 JSSortModePriv *priv = new JSSortModePriv(context, SortModePtr(sortMode));
127                 if (!JSObjectSetPrivate(object, priv)) {
128                         delete priv;
129                 }
130         }
131 }
132
133 void JSSortMode::Finalize(JSObjectRef object)
134 {
135         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
136
137         if (priv != NULL)
138         {
139                 delete (priv);
140         }
141
142         priv = NULL;
143 }
144
145 SortModePtr JSSortMode::getPrivData(JSObjectRef object)
146 {
147         LogDebug("entered");
148         JSSortModePriv *priv = static_cast<JSSortModePriv*>(JSObjectGetPrivate(object));
149         if (!priv) {
150                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151         }
152         SortModePtr result = priv->getObject();
153         if (!result) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155         }
156         return result;
157 }
158
159 JSObjectRef JSSortMode::constructor(JSContextRef context,
160                 JSObjectRef constructor,
161                 size_t argumentCount,
162                 const JSValueRef arguments[],
163                 JSValueRef* exception)
164 {
165         LogDebug("entered");
166
167         bool js2ndParamIsString = false;
168
169 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(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)
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]))
191                                 js2ndParamIsString = true;
192
193                         if (!js2ndParamIsString &&
194                                         !JSValueIsNull(gContext, arguments[1]) &&
195                                         !JSValueIsUndefined(gContext, arguments[1]))
196                                 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
197                 }
198
199         } Catch(Exception ) {
200                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
201                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
202                 return NULL;
203         }
204
205         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
206
207         std::string attributeName;
208         SortOrder sortOrder;
209
210         Try {
211                 attributeName = converter->toString(arguments[0]);
212         } Catch(Exception) {
213                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
214                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215                 return NULL;
216         }
217
218         Try {
219                 if(js2ndParamIsString)
220                         sortOrder = converter->toSortOrder(arguments[1]);
221                 else
222                         sortOrder = ASCENDING_SORT_ORDER;
223
224         } Catch(Exception) {
225                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
226                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
227                 return NULL;
228         }
229
230         SortModePtr sortMode(new SortMode(attributeName, sortOrder));
231
232         JSObjectRef jsobject;
233
234         Try {
235                 jsobject = createJSObject(gContext, sortMode);
236         } Catch(Exception) {
237                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
238                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
239                 return NULL;
240         }
241
242         return jsobject;
243 }
244
245 JSValueRef JSSortMode::getAttributeName(JSContextRef context,
246                 JSObjectRef object,
247                 JSStringRef propertyName,
248                 JSValueRef* exception)
249 {
250         LogDebug("entered");
251         Try
252         {
253                 FilterConverterFactory::ConverterType converter =
254                                 FilterConverterFactory::getConverter(context);
255                 SortModePtr sortMode = getPrivData(object);
256                 return converter->toJSValueRef(sortMode->getAttributeName());
257         }
258         Catch(WrtDeviceApis::Commons::Exception)
259         {
260                 LogWarning("trying to get incorrect value");
261         }
262         return JSValueMakeUndefined(context);
263 }
264
265 bool JSSortMode::setAttributeName(JSContextRef context,
266                 JSObjectRef object,
267                 JSStringRef propertyName,
268                 JSValueRef value,
269                 JSValueRef* exception)
270 {
271         Try
272         {
273                 SortModePtr sortMode = getPrivData(object);
274                 FilterConverterFactory::ConverterType converter =
275                                 FilterConverterFactory::getConverter(context);
276                 sortMode->setAttributeName(converter->toString(value));
277                 return true;
278         }
279         Catch(WrtDeviceApis::Commons::Exception)
280         {
281                 LogWarning("trying to set incorrect value");
282         }
283
284         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
285         return false;
286 }
287
288 JSValueRef JSSortMode::getOrder(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef* exception)
292 {
293         LogDebug("entered");
294         Try
295         {
296                 FilterConverterFactory::ConverterType converter =
297                                 FilterConverterFactory::getConverter(context);
298                 SortModePtr sortMode = getPrivData(object);
299                 return converter->toJSValueRef(sortMode->getOrder());
300         }
301         Catch(WrtDeviceApis::Commons::Exception)
302         {
303                 LogWarning("trying to get incorrect value");
304         }
305         return JSValueMakeUndefined(context);
306 }
307
308 bool JSSortMode::setOrder(JSContextRef context,
309                 JSObjectRef object,
310                 JSStringRef propertyName,
311                 JSValueRef value,
312                 JSValueRef* exception)
313 {
314         Try
315         {
316                 SortModePtr sortMode = getPrivData(object);
317                 FilterConverterFactory::ConverterType converter =
318                                 FilterConverterFactory::getConverter(context);
319                 sortMode->setOrder(converter->toSortOrder(value));
320                 return true;
321         }
322         Catch(WrtDeviceApis::Commons::Exception)
323         {
324                 LogWarning("trying to set incorrect value");
325         }
326
327         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
328         return false;
329 }
330
331 } // Tizen
332 } // DeviceAPI
333