0b5cda86a6b3e3801b063e56c41b65b939527ed3
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSAttributeRangeFilter.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        JSAttributeRangeFilter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSAttributeRangeFilter class
23  */
24
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <JSTizenExceptionFactory.h>
30 #include <JSTizenException.h>
31 #include "AttributeRangeFilter.h"
32 #include "FilterConverter.h"
33 #include "JSAttributeRangeFilter.h"
34
35 #define ATTRIBUTE_RANGE_FILTER_CLASS_NAME "AttributeRangeFilter"
36
37 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
38 #define ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE "initialValue"
39 #define ATTRIBUTE_FILTER_ATTR_END_VALUE "endValue"
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 JSAttributeRangeFilter::m_classRef = NULL;
49
50 JSClassDefinition JSAttributeRangeFilter::m_classInfo =
51 {
52         0,
53         kJSClassAttributeNone,
54         ATTRIBUTE_RANGE_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 JSAttributeRangeFilter::m_property[] = {
72         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
73         { ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE, getInitialValue, setInitialValue, kJSPropertyAttributeNone },
74         { ATTRIBUTE_FILTER_ATTR_END_VALUE, getEndValue, setEndValue, kJSPropertyAttributeNone },
75         { 0, 0, 0, 0 }
76 };
77
78 JSStaticFunction JSAttributeRangeFilter::m_functions[] =
79 {
80         { 0, 0, 0 }
81 };
82
83 JSClassRef JSAttributeRangeFilter::getClassRef() {
84         if (!m_classRef) {
85                 m_classRef = JSClassCreate(&m_classInfo);
86         }
87         return m_classRef;
88 }
89
90 bool JSAttributeRangeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
91 {
92         return JSValueIsObjectOfClass(context, value, getClassRef());
93 }
94
95 AttributeRangeFilterPtr JSAttributeRangeFilter::getAttributeRangeFilter(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         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
105         if (!priv) {
106                 Throw(WrtDeviceApis::Commons::NullPointerException);
107         }
108         return priv->getObject();
109 }
110
111 JSObjectRef JSAttributeRangeFilter::createJSObject(JSContextRef context, AttributeRangeFilterPtr privateData)
112 {
113         JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(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 JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
123 {
124         if (!JSObjectGetPrivate(object))
125         {
126                 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
127                 JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
128                 if (!JSObjectSetPrivate(object, priv)) {
129                         delete priv;
130                 }
131         }
132 }
133
134 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
135 {
136         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
137
138         if (priv != NULL)
139         {
140                 delete (priv);
141         }
142
143         priv = NULL;
144 }
145
146 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
147 {
148         LogDebug("entered");
149         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
150         if (!priv) {
151                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152         }
153         AttributeRangeFilterPtr result = priv->getObject();
154         if (!result) {
155                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
156         }
157         return result;
158 }
159
160 JSObjectRef JSAttributeRangeFilter::constructor(JSContextRef context,
161                 JSObjectRef constructor,
162                 size_t argumentCount,
163                 const JSValueRef arguments[],
164                 JSValueRef* exception)
165 {
166         LogDebug("entered");
167
168         bool js2ndParamIsValue = false;
169         bool js3rdParamIsValue = false;
170
171 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
172 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
173
174         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(constructor));
175         if (!priv) {
176                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
177         }
178         JSContextRef gContext = priv->getContext();
179
180 //      JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
181
182         BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
183         Try {
184                 if (argumentCount < 1)
185                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
186
187                 if (!JSValueIsString(gContext, arguments[0]))
188                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
189
190                 // 2nd and 3rd argument can be any type.
191                 if (argumentCount >= 2)
192                 {
193                         if(!JSValueIsUndefined(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
194                                 js2ndParamIsValue = true;
195                 }
196
197                 if (argumentCount >= 3)
198                 {
199                         if(!JSValueIsUndefined(gContext, arguments[2]) && !JSValueIsNull(gContext, arguments[2]))
200                                 js3rdParamIsValue = true;
201                 }
202
203         } Catch(Exception ) {
204                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
205                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
206                 return NULL;
207         }
208
209         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
210
211         std::string attributeName;
212         AnyPtr initialValue(NULL);
213         AnyPtr endValue(NULL);
214
215         Try {
216                 attributeName = converter->toString(arguments[0]);
217         } Catch(Exception) {
218                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
219                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
220                 return NULL;
221         }
222
223         Try {
224                 if(js2ndParamIsValue)
225                         initialValue = converter->toAny(arguments[1]);
226                 else
227                         initialValue = AnyPtr(new Any());
228
229         } Catch(Exception) {
230                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
231                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
232                 return NULL;
233         }
234
235         Try {
236                 if(js3rdParamIsValue)
237                         endValue = converter->toAny(arguments[2]);
238                 else
239                         endValue = AnyPtr(new Any());
240
241         } Catch(Exception) {
242                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
243                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
244                 return NULL;
245         }
246
247         AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
248
249         JSObjectRef jsobject;
250
251         Try {
252                 jsobject = createJSObject(gContext, attributeRangeFilter);
253         } Catch(Exception) {
254                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
255                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
256                 return NULL;
257         }
258
259         return jsobject;
260 }
261
262 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
263                 JSObjectRef object,
264                 JSStringRef propertyName,
265                 JSValueRef* exception)
266 {
267         LogDebug("entered");
268         Try
269         {
270                 FilterConverterFactory::ConverterType converter =
271                                 FilterConverterFactory::getConverter(context);
272                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
273                 return converter->toJSValueRef(attributeRangeFilter->getAttributeName());
274         }
275         Catch(WrtDeviceApis::Commons::Exception)
276         {
277                 LogWarning("trying to get incorrect value");
278         }
279         return JSValueMakeUndefined(context);
280 }
281
282 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
283                 JSObjectRef object,
284                 JSStringRef propertyName,
285                 JSValueRef value,
286                 JSValueRef* exception)
287 {
288         Try
289         {
290                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
291                 FilterConverterFactory::ConverterType converter =
292                                 FilterConverterFactory::getConverter(context);
293                 attributeRangeFilter->setAttributeName(converter->toString(value));
294                 return true;
295         }
296         Catch(WrtDeviceApis::Commons::Exception)
297         {
298                 LogWarning("trying to set incorrect value");
299         }
300
301         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
302         return false;
303 }
304
305 JSValueRef JSAttributeRangeFilter::getInitialValue(JSContextRef context,
306                 JSObjectRef object,
307                 JSStringRef propertyName,
308                 JSValueRef* exception)
309 {
310         LogDebug("entered");
311         Try
312         {
313                 FilterConverterFactory::ConverterType converter =
314                                 FilterConverterFactory::getConverter(context);
315                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
316                 return converter->toJSValueRef(attributeRangeFilter->getInitialValue());
317         }
318         Catch(WrtDeviceApis::Commons::Exception)
319         {
320                 LogWarning("trying to get incorrect value");
321         }
322         return JSValueMakeUndefined(context);
323 }
324
325 bool JSAttributeRangeFilter::setInitialValue(JSContextRef context,
326                 JSObjectRef object,
327                 JSStringRef propertyName,
328                 JSValueRef value,
329                 JSValueRef* exception)
330 {
331         Try
332         {
333                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
334                 FilterConverterFactory::ConverterType converter =
335                                 FilterConverterFactory::getConverter(context);
336                 attributeRangeFilter->setInitialValue(converter->toAny(value));
337                 return true;
338         }
339         Catch(WrtDeviceApis::Commons::Exception)
340         {
341                 LogWarning("trying to set incorrect value");
342         }
343
344         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
345         return false;
346 }
347
348 JSValueRef JSAttributeRangeFilter::getEndValue(JSContextRef context,
349                 JSObjectRef object,
350                 JSStringRef propertyName,
351                 JSValueRef* exception)
352 {
353         LogDebug("entered");
354         Try
355         {
356                 FilterConverterFactory::ConverterType converter =
357                                 FilterConverterFactory::getConverter(context);
358                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
359                 return converter->toJSValueRef(attributeRangeFilter->getEndValue());
360         }
361         Catch(WrtDeviceApis::Commons::Exception)
362         {
363                 LogWarning("trying to get incorrect value");
364         }
365         return JSValueMakeUndefined(context);
366 }
367
368 bool JSAttributeRangeFilter::setEndValue(JSContextRef context,
369                 JSObjectRef object,
370                 JSStringRef propertyName,
371                 JSValueRef value,
372                 JSValueRef* exception)
373 {
374         Try
375         {
376                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
377                 FilterConverterFactory::ConverterType converter =
378                                 FilterConverterFactory::getConverter(context);
379                 attributeRangeFilter->setEndValue(converter->toAny(value));
380                 return true;
381         }
382         Catch(WrtDeviceApis::Commons::Exception)
383         {
384                 LogWarning("trying to set incorrect value");
385         }
386
387         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
388         return false;
389 }
390
391 } // Tizen
392 } // DeviceAPI
393