Update change log and spec for wrt-plugins-tizen_0.4.49
[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 "JSAttributeRangeFilter.h"
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
28 #include <JSUtil.h>
29 #include <ArgumentValidator.h>
30 #include <JSWebAPIErrorFactory.h>
31 #include "AnyFactory.h"
32 #include <Logger.h>
33
34 #define ATTRIBUTE_RANGE_FILTER_CLASS_NAME "AttributeRangeFilter"
35
36 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
37 #define ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE "initialValue"
38 #define ATTRIBUTE_FILTER_ATTR_END_VALUE "endValue"
39
40 namespace DeviceAPI {
41 namespace 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         { 0, 0, 0, 0 }
74 };
75
76 JSStaticFunction JSAttributeRangeFilter::m_functions[] =
77 {
78         { 0, 0, 0 }
79 };
80
81 JSClassRef JSAttributeRangeFilter::getClassRef() {
82         if (!m_classRef)
83                 m_classRef = JSClassCreate(&m_classInfo);
84
85         return m_classRef;
86 }
87
88 bool JSAttributeRangeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
89 {
90         return JSValueIsObjectOfClass(context, value, getClassRef());
91 }
92
93 AttributeRangeFilterPtr JSAttributeRangeFilter::getAttributeRangeFilter(JSContextRef context, JSValueRef value)
94 {
95         if (!isObjectOfClass(context, value))
96                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97
98         JSObjectRef object = JSValueToObject(context, value, NULL);
99         if (!object)
100                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101
102         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
103         if (!priv)
104                 Throw(WrtDeviceApis::Commons::NullPointerException);
105
106         return priv->getObject();
107 }
108
109 JSObjectRef JSAttributeRangeFilter::createJSObject(JSContextRef context,
110                 AttributeRangeFilterPtr privateData,
111                 JSValueRef jsValueInitialValue,
112                 JSValueRef jsValueEndValue)
113 {
114         JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, privateData);
115         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
116         if (NULL == jsObjectRef)
117         {
118                 LoggerE("object creation error");
119                 return NULL;
120         }
121
122         const ScopedJSStringRef jsStrInitialValue(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE));
123         const ScopedJSStringRef jsStrEndValue(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_END_VALUE));
124
125         JSObjectSetProperty(context, jsObjectRef, jsStrInitialValue.get(), jsValueInitialValue, kJSPropertyAttributeNone, NULL);
126         JSObjectSetProperty(context, jsObjectRef, jsStrEndValue.get(), jsValueEndValue, kJSPropertyAttributeNone, NULL);
127
128         return jsObjectRef;
129 }
130
131 JSObjectRef JSAttributeRangeFilter::constructor(JSContextRef context,
132                 JSObjectRef constructor,
133                 size_t argumentCount,
134                 const JSValueRef arguments[],
135                 JSValueRef* exception)
136 {
137         LoggerD("entered");
138
139         std::string attributeName;
140         AnyPtr initialValue(NULL);
141         JSValueRef jsValueInitialValue = NULL;
142         AnyPtr endValue(NULL);
143         JSValueRef jsValueEndValue = NULL;
144
145         ArgumentValidator validator(context, argumentCount, arguments);
146
147         try
148         {
149                 attributeName = validator.toString(0, false);
150         }
151         catch(BasePlatformException &e)
152         {
153                 LoggerW("Failed to convert 1st parameter to string. Using default value.");
154                 attributeName = "";
155         }
156
157         initialValue = AnyFactory::createAnyEmpty(context);
158
159         if(argumentCount >= 2)
160                 jsValueInitialValue = arguments[1];
161         else
162                 jsValueInitialValue = JSValueMakeUndefined(context);
163
164         endValue = AnyFactory::createAnyEmpty(context);
165
166         if(argumentCount >= 3)
167                 jsValueEndValue = arguments[2];
168         else
169                 jsValueEndValue = JSValueMakeUndefined(context);
170
171         AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
172
173         JSObjectRef jsobject = NULL;
174
175         Try
176         {
177                 jsobject = createJSObject(context, attributeRangeFilter, jsValueInitialValue, jsValueEndValue);
178         }
179         Catch(Exception)
180         {
181                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
182         }
183
184         if(jsobject == NULL)
185                 return JSObjectMake(context, NULL, NULL);
186
187         return jsobject;
188 }
189
190 void JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
191 {
192         if (!JSObjectGetPrivate(object))
193         {
194                 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
195                 JSAttributeRangeFilterPriv *priv =
196                                 new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
197                 if (!JSObjectSetPrivate(object, priv))
198                 {
199                         delete priv;
200                 }
201         }
202 }
203
204 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
205 {
206         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
207
208         if (priv != NULL)
209         {
210                 delete (priv);
211         }
212
213         priv = NULL;
214 }
215
216 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
217 {
218         LoggerD("entered");
219         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
220         if (!priv)
221                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
222
223         AttributeRangeFilterPtr result = priv->getObject();
224         if (!result)
225                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
226
227         return result;
228 }
229
230 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
231                 JSObjectRef object,
232                 JSStringRef propertyName,
233                 JSValueRef* exception)
234 {
235         LoggerD("entered");
236
237         try
238         {
239                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
240                 return JSUtil::toJSValueRef(context, attributeRangeFilter->getAttributeName());
241         }
242         catch(BasePlatformException &e)
243         {
244                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
245         }
246
247         return JSValueMakeUndefined(context);
248 }
249
250 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
251                 JSObjectRef object,
252                 JSStringRef propertyName,
253                 JSValueRef value,
254                 JSValueRef* exception)
255 {
256         LoggerD("entered");
257
258         try
259         {
260                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
261                 attributeRangeFilter->setAttributeName(JSUtil::JSValueToString(context, value));
262         }
263         catch(BasePlatformException &e)
264         {
265                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
266         }
267
268         return true;
269 }
270
271 } // Tizen
272 } // DeviceAPI