Update change log and spec for wrt-plugins-tizen_0.4.29
[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                 return JSWebAPIErrorFactory::postException(context, exception, e);
154         }
155
156         initialValue = AnyFactory::createAnyEmpty(context);
157
158         if(argumentCount >= 2)
159                 jsValueInitialValue = arguments[1];
160         else
161                 jsValueInitialValue = JSValueMakeUndefined(context);
162
163         endValue = AnyFactory::createAnyEmpty(context);
164
165         if(argumentCount >= 3)
166                 jsValueEndValue = arguments[2];
167         else
168                 jsValueEndValue = JSValueMakeUndefined(context);
169
170         AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
171
172         JSObjectRef jsobject;
173
174         Try
175         {
176                 jsobject = createJSObject(context, attributeRangeFilter, jsValueInitialValue, jsValueEndValue);
177         }
178         Catch(Exception)
179         {
180                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
181                 return JSWebAPIErrorFactory::postException(context, exception,
182                                 TypeMismatchException("Error occurred while creating object"));
183         }
184
185         return jsobject;
186 }
187
188 void JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
189 {
190         if (!JSObjectGetPrivate(object))
191         {
192                 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
193                 JSAttributeRangeFilterPriv *priv =
194                                 new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
195                 if (!JSObjectSetPrivate(object, priv))
196                 {
197                         delete priv;
198                 }
199         }
200 }
201
202 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
203 {
204         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
205
206         if (priv != NULL)
207         {
208                 delete (priv);
209         }
210
211         priv = NULL;
212 }
213
214 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
215 {
216         LoggerD("entered");
217         JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
218         if (!priv)
219                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
220
221         AttributeRangeFilterPtr result = priv->getObject();
222         if (!result)
223                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
224
225         return result;
226 }
227
228 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
229                 JSObjectRef object,
230                 JSStringRef propertyName,
231                 JSValueRef* exception)
232 {
233         LoggerD("entered");
234
235         try
236         {
237                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
238                 return JSUtil::toJSValueRef(context, attributeRangeFilter->getAttributeName());
239         }
240         catch(BasePlatformException &e)
241         {
242                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
243         }
244
245         return JSValueMakeUndefined(context);
246 }
247
248 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyName,
251                 JSValueRef value,
252                 JSValueRef* exception)
253 {
254         LoggerD("entered");
255
256         try
257         {
258                 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
259                 attributeRangeFilter->setAttributeName(JSUtil::JSValueToString(context, value));
260         }
261         catch(BasePlatformException &e)
262         {
263                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
264         }
265
266         return true;
267 }
268
269 } // Tizen
270 } // DeviceAPI