306dc8bd93f7a7add4e639699344b17cccca6cbc
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSCompositeFilter.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        JSCompositeFilter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSCompositeFilter class
23  */
24
25 #include "JSCompositeFilter.h"
26 #include <string>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <JSUtil.h>
30 #include <ArgumentValidator.h>
31 #include <JSWebAPIErrorFactory.h>
32 #include <Logger.h>
33
34 #define ATTRIBUTE_FILTER_CLASS_NAME "CompositeFilter"
35 #define ATTRIBUTE_FILTER_ATTR_TYPE "type"
36 #define ATTRIBUTE_FILTER_ATTR_FILTERS "filters"
37
38 namespace DeviceAPI {
39 namespace Tizen {
40
41 using namespace DeviceAPI::Common;
42 using namespace DeviceAPI::Tizen;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45
46 JSClassRef JSCompositeFilter::m_classRef = NULL;
47
48 JSClassDefinition JSCompositeFilter::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         ATTRIBUTE_FILTER_CLASS_NAME,
53         NULL,
54         m_property,
55         m_functions,
56         Initialize,
57         Finalize,
58         NULL,//hasProperty,
59         NULL,//getProperty,
60         setProperty,
61         NULL,//deleteProperty,
62         NULL,//getPropertyNames,
63         NULL, //CallAsFunction,
64         NULL, //CallAsConstructor,
65         NULL, //HasInstance,
66         NULL, //ConvertToType,
67 };
68
69 JSStaticValue JSCompositeFilter::m_property[] = {
70         { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
71         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSCompositeFilter::m_functions[] =
75 {
76         { 0, 0, 0 }
77 };
78
79 JSClassRef JSCompositeFilter::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
92 {
93         if (!isObjectOfClass(context, value))
94                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95
96         JSObjectRef object = JSValueToObject(context, value, NULL);
97         if (!object)
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99
100         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
101         if (!priv)
102                 Throw(WrtDeviceApis::Commons::NullPointerException);
103
104         return priv->getObject();
105 }
106
107 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context,
108                 CompositeFilterPtr privateData,
109                 JSValueRef jsValueFilters)
110 {
111         JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, privateData);
112         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
113         if (NULL == jsObjectRef)
114         {
115                 LoggerE("object creation error");
116                 return NULL;
117         }
118
119         const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
120
121         JSObjectSetProperty(context, jsObjectRef, jsStrFilters.get(), jsValueFilters, kJSPropertyAttributeNone, NULL);
122
123         return jsObjectRef;
124 }
125
126 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
127                 JSObjectRef constructor,
128                 size_t argumentCount,
129                 const JSValueRef arguments[],
130                 JSValueRef* exception)
131 {
132         LoggerD("entered");
133
134         FilterType type;
135         JSValueRef jsValueFilters = NULL;
136
137         ArgumentValidator validator(context, argumentCount, arguments);
138
139         try
140         {
141                 std::string typeStr = validator.toString(0, false);
142
143                 if(typeStr == "UNION")
144                         type = UNION_FILTER;
145                 else if(typeStr == "INTERSECTION")
146                         type = INTERSECTION_FILTER;
147                 else
148                         throw TypeMismatchException("Property is not valid CompositeFilterType value");
149         }
150         catch(BasePlatformException &e)
151         {
152                 return JSWebAPIErrorFactory::postException(context, exception, e);
153         }
154
155         try
156         {
157                 if(argumentCount < 2 ||
158                                 JSValueIsUndefined(context, arguments[1]) ||
159                                 JSValueIsNull(context, arguments[1]))
160                         jsValueFilters = JSCreateArrayObject(context, 0, NULL);
161                 else if(JSIsArrayValue(context, arguments[1]))
162                         jsValueFilters = arguments[1];
163                 else
164                         throw TypeMismatchException("Property is not valid Array value");
165         }
166         catch(BasePlatformException &e)
167         {
168                 return JSWebAPIErrorFactory::postException(context, exception, e);
169         }
170
171         CompositeFilterPtr compositeFilter(new CompositeFilter(type, FilterArrayPtr(new FilterArray())));
172
173         JSObjectRef jsobject;
174
175         Try
176         {
177                 jsobject = createJSObject(context, compositeFilter, jsValueFilters);
178         }
179         Catch(Exception)
180         {
181                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
182                 return JSWebAPIErrorFactory::postException(context, exception,
183                                 TypeMismatchException("Error occurred while creating object"));
184         }
185
186         return jsobject;
187 }
188
189 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
190 {
191         if (!JSObjectGetPrivate(object))
192         {
193                 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
194                 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
195                 if (!JSObjectSetPrivate(object, priv))
196                 {
197                         delete priv;
198                 }
199         }
200 }
201
202 void JSCompositeFilter::Finalize(JSObjectRef object)
203 {
204         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
205
206         if (priv != NULL)
207         {
208                 delete (priv);
209         }
210
211         priv = NULL;
212 }
213
214 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
215 {
216         LoggerD("entered");
217         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
218         if (!priv)
219                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
220
221         CompositeFilterPtr result = priv->getObject();
222         if (!result)
223                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
224
225         return result;
226 }
227
228 bool JSCompositeFilter::setProperty(JSContextRef context,
229                 JSObjectRef object,
230                 JSStringRef propertyName,
231                 JSValueRef value,
232                 JSValueRef* exception)
233 {
234         LoggerD("entered");
235
236         try
237         {
238                 CompositeFilterPtr compositeFilter = getPrivData(object);
239                 std::string name = JSUtil::JSStringToString(context, propertyName);
240                 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
241                 {
242                         if(!JSIsArrayValue(context, value))
243                                 return true;
244
245                         return false;
246                 }
247         }
248         catch(BasePlatformException &e)
249         {
250                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
251         }
252
253         return false;
254 }
255
256 JSValueRef JSCompositeFilter::getType(JSContextRef context,
257                 JSObjectRef object,
258                 JSStringRef propertyName,
259                 JSValueRef* exception)
260 {
261         LoggerD("entered");
262
263         try
264         {
265                 CompositeFilterPtr compositeFilter = getPrivData(object);
266                 FilterType type = compositeFilter->getFilterType();
267                 std::string typeStr;
268                 if(type == UNION_FILTER)
269                         typeStr = "UNION";
270                 else if(type == INTERSECTION_FILTER)
271                         typeStr = "INTERSECTION";
272                 else
273                         typeStr = "UNION";
274
275                 return JSUtil::toJSValueRef(context, typeStr);
276         }
277         catch(BasePlatformException &e)
278         {
279                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
280         }
281
282         return JSValueMakeUndefined(context);
283 }
284
285 bool JSCompositeFilter::setType(JSContextRef context,
286                 JSObjectRef object,
287                 JSStringRef propertyName,
288                 JSValueRef value,
289                 JSValueRef* exception)
290 {
291         LoggerD("entered");
292
293         try
294         {
295                 CompositeFilterPtr compositeFilter = getPrivData(object);
296
297                 FilterType type;
298                 std::string typeStr = JSUtil::JSValueToString(context, value);
299
300                 if(typeStr == "UNION")
301                         type = UNION_FILTER;
302                 else if(typeStr == "INTERSECTION")
303                         type = INTERSECTION_FILTER;
304                 else
305                         throw TypeMismatchException("Property is not valid CompositeFilterType value");
306
307                 compositeFilter->setFilterType(type);
308         }
309         catch(BasePlatformException &e)
310         {
311                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
312         }
313
314         return true;
315 }
316
317 } // Tizen
318 } // DeviceAPI