Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Tizen / JSAttributeFilter.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        JSAttributeFilter.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSAttributeFilter 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/AttributeFilter.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 "JSAttributeFilter.h"
35
36 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
37
38 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
40 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUES "matchValues"
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 JSAttributeFilter::m_classRef = NULL;
52
53 JSClassDefinition JSAttributeFilter::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 JSAttributeFilter::m_property[] = {
75         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
76         { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
77         { ATTRIBUTE_FILTER_ATTR_MATCH_VALUES, getMatchValues, setMatchValues, kJSPropertyAttributeNone },
78         { 0, 0, 0, 0 }
79 };
80
81 JSStaticFunction JSAttributeFilter::m_functions[] =
82 {
83         { 0, 0, 0 }
84 };
85
86 JSClassRef JSAttributeFilter::getClassRef() {
87         if (!m_classRef) {
88                 m_classRef = JSClassCreate(&m_classInfo);
89         }
90         return m_classRef;
91 }
92
93 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
94 {
95         return JSValueIsObjectOfClass(context, value, getClassRef());
96 }
97
98 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
99 {
100         if (!isObjectOfClass(context, value)) {
101                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102         }
103         JSObjectRef object = JSValueToObject(context, value, NULL);
104         if (!object) {
105                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
106         }
107         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
108         if (!priv) {
109                 Throw(WrtDeviceApis::Commons::NullPointerException);
110         }
111         return priv->getObject();
112 }
113
114 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
115 {
116 //      if (!JSObjectGetPrivate(object))
117 //      {
118 //              AttributeFilterPtr coord(new AttributeFilter(""));
119 //              JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(coord));
120 //              if (!JSObjectSetPrivate(object, priv)) {
121 //                      delete priv;
122 //              }
123 //      }
124 }
125
126 void JSAttributeFilter::Finalize(JSObjectRef object)
127 {
128         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
129
130         if (priv != NULL)
131         {
132                 delete (priv);
133         }
134
135         priv = NULL;
136 }
137
138 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
139 {
140         JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
141         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
142         if (NULL == jsObjectRef) {
143                 LogError("object creation error");
144                 return NULL;
145         }
146         return jsObjectRef;
147 }
148
149 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
150 {
151         LogDebug("entered");
152         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
153         if (!priv) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155         }
156         AttributeFilterPtr result = priv->getObject();
157         if (!result) {
158                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159         }
160         return result;
161 }
162
163 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
164                 JSObjectRef constructor,
165                 size_t argumentCount,
166                 const JSValueRef arguments[],
167                 JSValueRef* exception)
168 {
169         LogDebug("entered");
170
171 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
172 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
173
174 //      JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(constructor));
175 //      if (!priv) {
176 //              ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
177 //      }
178 //      JSContextRef gContext = priv->getContext();
179
180         JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
181
182         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
183         Try {
184                 if (argumentCount < 1 || argumentCount > 3)
185                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
186
187                 if (!JSValueIsString(gContext, arguments[0]))
188                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
189
190                 if (argumentCount >= 2)
191                 {
192                         if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
193                                 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
194                 }
195
196                 // 3rd argument can be any type.
197
198         } Catch(Exception ) {
199                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
201                 return NULL;
202         }
203
204         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
205
206         std::string attributeName;
207         MatchFlag matchFlag;
208         AnyArrayPtr matchValues;
209
210         Try {
211                 attributeName = converter->toString(arguments[0]);
212         } Catch(Exception) {
213                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
214                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215                 return NULL;
216         }
217
218         Try {
219                 matchFlag = MATCH_EXACTLY;
220                 if(argumentCount >= 2)
221                 {
222                         if(JSValueIsString(gContext, arguments[1]))
223                                 matchFlag = converter->toMatchFlag(arguments[1]);
224                 }
225         } Catch(Exception) {
226                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
227                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
228                 return NULL;
229         }
230
231         Try {
232                 if(argumentCount >= 3)
233                 {
234                         if(JSValueIsNull(gContext, arguments[2]))
235                                 matchValues = AnyArrayPtr(new AnyArray());
236                         else if(JSIsArrayValue(gContext, arguments[2]))
237                                 matchValues = converter->toAnyArray(arguments[2]);
238                         else
239                         {
240                                 matchValues = AnyArrayPtr(new AnyArray());
241                                 matchValues->push_back(converter->toAny(arguments[2]));
242                         }
243                 }
244                 else
245                 {
246                         matchValues = AnyArrayPtr(new AnyArray());
247                 }
248
249         } Catch(Exception) {
250                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
251                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
252                 return NULL;
253         }
254
255         AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValues));
256
257         JSObjectRef jsobject;
258
259         Try {
260                 jsobject = createJSObject(gContext, attributeFilter);
261         } Catch(Exception) {
262                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
263                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
264                 return NULL;
265         }
266
267         return jsobject;
268 }
269
270 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
271                 JSObjectRef object,
272                 JSStringRef propertyName,
273                 JSValueRef* exception)
274 {
275         LogDebug("entered");
276         Try
277         {
278                 FilterConverterFactory::ConverterType converter =
279                                 FilterConverterFactory::getConverter(context);
280                 AttributeFilterPtr attributeFilter = getPrivData(object);
281                 return converter->toJSValueRef(attributeFilter->getAttributeName());
282         }
283         Catch(WrtDeviceApis::Commons::Exception)
284         {
285                 LogWarning("trying to get incorrect value");
286         }
287         return JSValueMakeUndefined(context);
288 }
289
290 bool JSAttributeFilter::setAttributeName(JSContextRef context,
291                 JSObjectRef object,
292                 JSStringRef propertyName,
293                 JSValueRef value,
294                 JSValueRef* exception)
295 {
296         Try
297         {
298                 AttributeFilterPtr attributeFilter = getPrivData(object);
299                 FilterConverterFactory::ConverterType converter =
300                                 FilterConverterFactory::getConverter(context);
301                 attributeFilter->setAttributeName(converter->toString(value));
302                 return true;
303         }
304         Catch(WrtDeviceApis::Commons::Exception)
305         {
306                 LogWarning("trying to set incorrect value");
307         }
308
309         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
310         return false;
311 }
312
313 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
314                 JSObjectRef object,
315                 JSStringRef propertyName,
316                 JSValueRef* exception)
317 {
318         LogDebug("entered");
319         Try
320         {
321                 FilterConverterFactory::ConverterType converter =
322                                 FilterConverterFactory::getConverter(context);
323                 AttributeFilterPtr attributeFilter = getPrivData(object);
324                 return converter->toJSValueRef(attributeFilter->getMatchFlag());
325         }
326         Catch(WrtDeviceApis::Commons::Exception)
327         {
328                 LogWarning("trying to get incorrect value");
329         }
330         return JSValueMakeUndefined(context);
331 }
332
333 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
334                 JSObjectRef object,
335                 JSStringRef propertyName,
336                 JSValueRef value,
337                 JSValueRef* exception)
338 {
339         Try
340         {
341                 AttributeFilterPtr attributeFilter = getPrivData(object);
342                 FilterConverterFactory::ConverterType converter =
343                                 FilterConverterFactory::getConverter(context);
344                 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
345                 return true;
346         }
347         Catch(WrtDeviceApis::Commons::Exception)
348         {
349                 LogWarning("trying to set incorrect value");
350         }
351
352         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
353         return false;
354 }
355
356 JSValueRef JSAttributeFilter::getMatchValues(JSContextRef context,
357                 JSObjectRef object,
358                 JSStringRef propertyName,
359                 JSValueRef* exception)
360 {
361         LogDebug("entered");
362         Try
363         {
364                 FilterConverterFactory::ConverterType converter =
365                                 FilterConverterFactory::getConverter(context);
366                 AttributeFilterPtr attributeFilter = getPrivData(object);
367                 return converter->toJSValueRef(attributeFilter->getMatchValues());
368         }
369         Catch(WrtDeviceApis::Commons::Exception)
370         {
371                 LogWarning("trying to get incorrect value");
372         }
373
374         return JSValueMakeUndefined(context);
375 }
376
377 bool JSAttributeFilter::setMatchValues(JSContextRef context,
378                 JSObjectRef object,
379                 JSStringRef propertyName,
380                 JSValueRef value,
381                 JSValueRef* exception)
382 {
383         Try
384         {
385                 AttributeFilterPtr attributeFilter = getPrivData(object);
386                 FilterConverterFactory::ConverterType converter =
387                                 FilterConverterFactory::getConverter(context);
388                 attributeFilter->setMatchValues(converter->toAnyArray(value));
389                 return true;
390         }
391         Catch(WrtDeviceApis::Commons::Exception)
392         {
393                 LogWarning("trying to set incorrect value");
394         }
395
396         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
397         return false;
398 }
399
400 } // Tizen
401 } // Tizen1_0
402 } // TizenApis