Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Tizen / JSCompositeFilter.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file        JSCompositeFilter.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSCompositeFilter class
22  */
23
24 #include <string>
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <API/Filter/CompositeFilter.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/JSGlobalContextFactory.h>
33 #include "FilterConverter.h"
34 #include "JSAbstractFilterArray.h"
35 #include "JSCompositeFilter.h"
36
37 #define ATTRIBUTE_FILTER_CLASS_NAME "CompositeFilter"
38
39 #define ATTRIBUTE_FILTER_ATTR_TYPE "type"
40 #define ATTRIBUTE_FILTER_ATTR_FILTERS "filters"
41
42 namespace TizenApis {
43 namespace Tizen1_0 {
44 namespace Tizen {
45
46 using namespace TizenApis::Commons;
47 using namespace TizenApis::Api::Tizen;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
50
51 JSClassRef JSCompositeFilter::m_classRef = NULL;
52
53 JSClassDefinition JSCompositeFilter::m_classInfo =
54 {
55         0,
56         kJSClassAttributeNone,
57         ATTRIBUTE_FILTER_CLASS_NAME,
58         NULL,
59         m_property,
60         m_functions,
61         Initialize,
62         Finalize,
63         NULL, //hasProperty,
64         NULL, //GetProperty,
65         NULL, //SetProperty,
66         NULL, //DeleteProperty,
67         NULL, //getPropertyNames,
68         NULL, //CallAsFunction,
69         constructor, //CallAsConstructor,
70         NULL, //HasInstance,
71         NULL, //ConvertToType,
72 };
73
74 JSStaticValue JSCompositeFilter::m_property[] = {
75         { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
76         { ATTRIBUTE_FILTER_ATTR_FILTERS, getFilters, setFilters, kJSPropertyAttributeNone },
77         { 0, 0, 0, 0 }
78 };
79
80 JSStaticFunction JSCompositeFilter::m_functions[] =
81 {
82         { 0, 0, 0 }
83 };
84
85 JSClassRef JSCompositeFilter::getClassRef() {
86         if (!m_classRef) {
87                 m_classRef = JSClassCreate(&m_classInfo);
88         }
89         return m_classRef;
90 }
91
92 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
93 {
94         return JSValueIsObjectOfClass(context, value, getClassRef());
95 }
96
97 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
98 {
99         if (!isObjectOfClass(context, value)) {
100                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101         }
102         JSObjectRef object = JSValueToObject(context, value, NULL);
103         if (!object) {
104                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
105         }
106         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
107         if (!priv) {
108                 Throw(WrtDeviceApis::Commons::NullPointerException);
109         }
110         return priv->getObject();
111 }
112
113 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
114 {
115 //      if (!JSObjectGetPrivate(object))
116 //      {
117 //              CompositeFilterPtr coord(new CompositeFilter(""));
118 //              JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(coord));
119 //              if (!JSObjectSetPrivate(object, priv)) {
120 //                      delete priv;
121 //              }
122 //      }
123 }
124
125 void JSCompositeFilter::Finalize(JSObjectRef object)
126 {
127         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
128
129         if (priv != NULL)
130         {
131                 delete (priv);
132         }
133
134         priv = NULL;
135 }
136
137 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context, CompositeFilterPtr privateData)
138 {
139         JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, privateData);
140         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
141         if (NULL == jsObjectRef) {
142                 LogError("object creation error");
143                 return NULL;
144         }
145         return jsObjectRef;
146 }
147
148 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
149 {
150         LogDebug("entered");
151         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
152         if (!priv) {
153                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154         }
155         CompositeFilterPtr result = priv->getObject();
156         if (!result) {
157                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
158         }
159         return result;
160 }
161
162 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
163                 JSObjectRef constructor,
164                 size_t argumentCount,
165                 const JSValueRef arguments[],
166                 JSValueRef* exception)
167 {
168         LogDebug("entered");
169
170 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
171 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
172
173 //      JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(constructor));
174 //      if (!priv) {
175 //              ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
176 //      }
177 //      JSContextRef gContext = priv->getContext();
178
179         JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
180
181         BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
182         Try {
183                 if (argumentCount < 1 || argumentCount > 3)
184                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
185
186                 if (!JSValueIsString(gContext, arguments[0]))
187                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
188
189                 if (argumentCount >= 2)
190                 {
191                         if (!JSValueIsObject(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
192                                 ThrowMsg(InvalidArgumentException, "2nd argument is not object.");
193                 }
194
195                 // 3rd argument can be any type.
196
197         } Catch(Exception ) {
198                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
199                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
200                 return NULL;
201         }
202
203         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
204
205         FilterType type;
206         FilterArrayPtr filters;
207
208         Try {
209                 type = converter->toCompositeFilterType(arguments[0]);
210         } Catch(Exception) {
211                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
212                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
213                 return NULL;
214         }
215
216         Try {
217                 if(argumentCount >= 2)
218                 {
219                         if(!JSValueIsNull(gContext, arguments[1]))
220                                 filters = converter->toFilterArray(arguments[1]);
221                         else
222                                 filters = FilterArrayPtr(new FilterArray());
223                 }
224                 else
225                         filters = FilterArrayPtr(new FilterArray());
226
227         } Catch(Exception) {
228                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
229                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
230                 return NULL;
231         }
232
233         CompositeFilterPtr compositeFilter(new CompositeFilter(type, filters));
234
235         JSObjectRef jsobject;
236
237         Try {
238                 jsobject = createJSObject(gContext, compositeFilter);
239         } Catch(Exception) {
240                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
241                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
242                 return NULL;
243         }
244
245         return jsobject;
246 }
247
248 JSValueRef JSCompositeFilter::getType(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyName,
251                 JSValueRef* exception)
252 {
253         LogDebug("entered");
254         Try
255         {
256                 FilterConverterFactory::ConverterType converter =
257                                 FilterConverterFactory::getConverter(context);
258                 CompositeFilterPtr compositeFilter = getPrivData(object);
259                 return converter->toJSValueRef(compositeFilter->getFilterType());
260         }
261         Catch(WrtDeviceApis::Commons::Exception)
262         {
263                 LogWarning("trying to get incorrect value");
264         }
265         return JSValueMakeUndefined(context);
266 }
267
268 bool JSCompositeFilter::setType(JSContextRef context,
269                 JSObjectRef object,
270                 JSStringRef propertyName,
271                 JSValueRef value,
272                 JSValueRef* exception)
273 {
274         Try
275         {
276                 CompositeFilterPtr compositeFilter = getPrivData(object);
277                 FilterConverterFactory::ConverterType converter =
278                                 FilterConverterFactory::getConverter(context);
279                 compositeFilter->setFilterType(converter->toCompositeFilterType(value));
280                 return true;
281         }
282         Catch(WrtDeviceApis::Commons::Exception)
283         {
284                 LogWarning("trying to set incorrect value");
285         }
286
287         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
288         return false;
289 }
290
291 JSValueRef JSCompositeFilter::getFilters(JSContextRef context,
292                 JSObjectRef object,
293                 JSStringRef propertyName,
294                 JSValueRef* exception)
295 {
296         LogDebug("entered");
297         Try
298         {
299                 FilterConverterFactory::ConverterType converter =
300                                 FilterConverterFactory::getConverter(context);
301                 CompositeFilterPtr compositeFilter = getPrivData(object);
302                 return converter->toJSValueRef(compositeFilter->getFilters());
303         }
304         Catch(WrtDeviceApis::Commons::Exception)
305         {
306                 LogWarning("trying to get incorrect value");
307         }
308         return JSValueMakeUndefined(context);
309 }
310
311 bool JSCompositeFilter::setFilters(JSContextRef context,
312                 JSObjectRef object,
313                 JSStringRef propertyName,
314                 JSValueRef value,
315                 JSValueRef* exception)
316 {
317         Try
318         {
319                 CompositeFilterPtr compositeFilter = getPrivData(object);
320                 FilterConverterFactory::ConverterType converter =
321                                 FilterConverterFactory::getConverter(context);
322                 compositeFilter->setFilters(converter->toFilterArray(value));
323                 return true;
324         }
325         Catch(WrtDeviceApis::Commons::Exception)
326         {
327                 LogWarning("trying to set incorrect value");
328         }
329
330         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
331         return false;
332 }
333
334 } // Tizen
335 } // Tizen1_0
336 } // TizenApis