Update change log and spec for wrt-plugins-tizen_0.4.11
[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         NULL, //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 JSObjectRef JSAttributeRangeFilter::constructor(JSContextRef context,
123                 JSObjectRef constructor,
124                 size_t argumentCount,
125                 const JSValueRef arguments[],
126                 JSValueRef* exception)
127 {
128         LogDebug("entered");
129
130         bool js2ndParamIsValue = false;
131         bool js3rdParamIsValue = false;
132
133         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
134         Try {
135                 if (argumentCount < 1)
136                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
137
138                 if (!JSValueIsString(context, arguments[0]))
139                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
140
141                 // 2nd and 3rd argument can be any type.
142                 if (argumentCount >= 2)
143                 {
144                         if(!JSValueIsUndefined(context, arguments[1]) && !JSValueIsNull(context, arguments[1]))
145                                 js2ndParamIsValue = true;
146                 }
147
148                 if (argumentCount >= 3)
149                 {
150                         if(!JSValueIsUndefined(context, arguments[2]) && !JSValueIsNull(context, arguments[2]))
151                                 js3rdParamIsValue = true;
152                 }
153
154         } Catch(Exception ) {
155                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
156                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
157                 return NULL;
158         }
159
160         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
161
162         std::string attributeName;
163         AnyPtr initialValue(NULL);
164         AnyPtr endValue(NULL);
165
166         Try {
167                 attributeName = converter->toString(arguments[0]);
168         } Catch(Exception) {
169                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
170                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
171                 return NULL;
172         }
173
174         Try {
175                 if(js2ndParamIsValue)
176                         initialValue = converter->toAny(arguments[1]);
177                 else
178                         initialValue = AnyPtr(new Any());
179
180         } Catch(Exception) {
181                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
182                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
183                 return NULL;
184         }
185
186         Try {
187                 if(js3rdParamIsValue)
188                         endValue = converter->toAny(arguments[2]);
189                 else
190                         endValue = AnyPtr(new Any());
191
192         } Catch(Exception) {
193                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
194                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
195                 return NULL;
196         }
197
198         AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
199
200         JSObjectRef jsobject;
201
202         Try {
203                 jsobject = createJSObject(context, attributeRangeFilter);
204         } Catch(Exception) {
205                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
206                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
207                 return NULL;
208         }
209
210         return jsobject;
211 }
212
213 void JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
214 {
215         if (!JSObjectGetPrivate(object))
216         {
217                 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
218                 JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
219                 if (!JSObjectSetPrivate(object, priv)) {
220                         delete priv;
221                 }
222         }
223 }
224
225 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
226 {
227         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
228
229         if (priv != NULL)
230         {
231                 delete (priv);
232         }
233
234         priv = NULL;
235 }
236
237 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
238 {
239         LogDebug("entered");
240         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
241         if (!priv) {
242                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
243         }
244         AttributeRangeFilterPtr result = priv->getObject();
245         if (!result) {
246                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
247         }
248         return result;
249 }
250
251 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
252                 JSObjectRef object,
253                 JSStringRef propertyName,
254                 JSValueRef* exception)
255 {
256         LogDebug("entered");
257         Try
258         {
259                 FilterConverterFactory::ConverterType converter =
260                                 FilterConverterFactory::getConverter(context);
261                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
262                 return converter->toJSValueRef(attributeRangeFilter->getAttributeName());
263         }
264         Catch(WrtDeviceApis::Commons::Exception)
265         {
266                 LogWarning("trying to get incorrect value");
267         }
268         return JSValueMakeUndefined(context);
269 }
270
271 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
272                 JSObjectRef object,
273                 JSStringRef propertyName,
274                 JSValueRef value,
275                 JSValueRef* exception)
276 {
277         Try
278         {
279                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
280                 FilterConverterFactory::ConverterType converter =
281                                 FilterConverterFactory::getConverter(context);
282                 attributeRangeFilter->setAttributeName(converter->toString(value));
283                 return true;
284         }
285         Catch(WrtDeviceApis::Commons::Exception)
286         {
287                 LogWarning("trying to set incorrect value");
288         }
289
290         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
291         return false;
292 }
293
294 JSValueRef JSAttributeRangeFilter::getInitialValue(JSContextRef context,
295                 JSObjectRef object,
296                 JSStringRef propertyName,
297                 JSValueRef* exception)
298 {
299         LogDebug("entered");
300         Try
301         {
302                 FilterConverterFactory::ConverterType converter =
303                                 FilterConverterFactory::getConverter(context);
304                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
305                 return converter->toJSValueRef(attributeRangeFilter->getInitialValue());
306         }
307         Catch(WrtDeviceApis::Commons::Exception)
308         {
309                 LogWarning("trying to get incorrect value");
310         }
311         return JSValueMakeUndefined(context);
312 }
313
314 bool JSAttributeRangeFilter::setInitialValue(JSContextRef context,
315                 JSObjectRef object,
316                 JSStringRef propertyName,
317                 JSValueRef value,
318                 JSValueRef* exception)
319 {
320         Try
321         {
322                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
323                 FilterConverterFactory::ConverterType converter =
324                                 FilterConverterFactory::getConverter(context);
325                 attributeRangeFilter->setInitialValue(converter->toAny(value));
326                 return true;
327         }
328         Catch(WrtDeviceApis::Commons::Exception)
329         {
330                 LogWarning("trying to set incorrect value");
331         }
332
333         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
334         return false;
335 }
336
337 JSValueRef JSAttributeRangeFilter::getEndValue(JSContextRef context,
338                 JSObjectRef object,
339                 JSStringRef propertyName,
340                 JSValueRef* exception)
341 {
342         LogDebug("entered");
343         Try
344         {
345                 FilterConverterFactory::ConverterType converter =
346                                 FilterConverterFactory::getConverter(context);
347                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
348                 return converter->toJSValueRef(attributeRangeFilter->getEndValue());
349         }
350         Catch(WrtDeviceApis::Commons::Exception)
351         {
352                 LogWarning("trying to get incorrect value");
353         }
354         return JSValueMakeUndefined(context);
355 }
356
357 bool JSAttributeRangeFilter::setEndValue(JSContextRef context,
358                 JSObjectRef object,
359                 JSStringRef propertyName,
360                 JSValueRef value,
361                 JSValueRef* exception)
362 {
363         Try
364         {
365                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
366                 FilterConverterFactory::ConverterType converter =
367                                 FilterConverterFactory::getConverter(context);
368                 attributeRangeFilter->setEndValue(converter->toAny(value));
369                 return true;
370         }
371         Catch(WrtDeviceApis::Commons::Exception)
372         {
373                 LogWarning("trying to set incorrect value");
374         }
375
376         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
377         return false;
378 }
379
380 } // Tizen
381 } // DeviceAPI
382