merge wrt-plugins-tizen_0.2.0-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 "FilterConverter.h"
33 #include "JSAttributeFilter.h"
34
35 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
36
37 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
38 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUE "matchValue"
40
41 namespace TizenApis {
42 namespace Tizen1_0 {
43 namespace Tizen {
44
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Tizen;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49
50 JSClassRef JSAttributeFilter::m_classRef = NULL;
51
52 JSClassDefinition JSAttributeFilter::m_classInfo =
53 {
54         0,
55         kJSClassAttributeNone,
56         ATTRIBUTE_FILTER_CLASS_NAME,
57         NULL,
58         m_property,
59         m_functions,
60         Initialize,
61         Finalize,
62         NULL, //hasProperty,
63         NULL, //GetProperty,
64         NULL, //SetProperty,
65         NULL, //DeleteProperty,
66         NULL, //getPropertyNames,
67         NULL, //CallAsFunction,
68         constructor, //CallAsConstructor,
69         NULL, //HasInstance,
70         NULL, //ConvertToType,
71 };
72
73 JSStaticValue JSAttributeFilter::m_property[] = {
74         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
75         { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
76         { ATTRIBUTE_FILTER_ATTR_MATCH_VALUE, getMatchValue, setMatchValue, kJSPropertyAttributeNone },
77         { 0, 0, 0, 0 }
78 };
79
80 JSStaticFunction JSAttributeFilter::m_functions[] =
81 {
82         { 0, 0, 0 }
83 };
84
85 JSClassRef JSAttributeFilter::getClassRef() {
86         if (!m_classRef) {
87                 m_classRef = JSClassCreate(&m_classInfo);
88         }
89         return m_classRef;
90 }
91
92 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
93 {
94         return JSValueIsObjectOfClass(context, value, getClassRef());
95 }
96
97 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(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         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
107         if (!priv) {
108                 Throw(WrtDeviceApis::Commons::NullPointerException);
109         }
110         return priv->getObject();
111 }
112
113 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
114 {
115         JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
116         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117         if (NULL == jsObjectRef) {
118                 LogError("object creation error");
119                 return NULL;
120         }
121         return jsObjectRef;
122 }
123
124 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
125 {
126         if (!JSObjectGetPrivate(object))
127         {
128                 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
129                 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
130                 if (!JSObjectSetPrivate(object, priv)) {
131                         delete priv;
132                 }
133         }
134 }
135
136 void JSAttributeFilter::Finalize(JSObjectRef object)
137 {
138         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
139
140         if (priv != NULL)
141         {
142                 delete (priv);
143         }
144
145         priv = NULL;
146 }
147
148 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
149 {
150         LogDebug("entered");
151         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
152         if (!priv) {
153                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154         }
155         AttributeFilterPtr result = priv->getObject();
156         if (!result) {
157                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
158         }
159         return result;
160 }
161
162 JSObjectRef JSAttributeFilter::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         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(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(context, 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 (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
192                                 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
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         std::string attributeName;
206         MatchFlag matchFlag;
207         AnyPtr matchValue;
208
209         Try {
210                 attributeName = converter->toString(arguments[0]);
211         } Catch(Exception) {
212                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
213                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
214                 return NULL;
215         }
216
217         Try {
218                 matchFlag = MATCH_EXACTLY;
219                 if(argumentCount >= 2)
220                 {
221                         if(JSValueIsString(gContext, arguments[1]))
222                                 matchFlag = converter->toMatchFlag(arguments[1]);
223                 }
224         } Catch(Exception) {
225                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
226                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
227                 return NULL;
228         }
229
230         Try {
231                 if(argumentCount >= 3)
232                 {
233                         if(JSValueIsNull(gContext, arguments[2]))
234                                 matchValue = AnyPtr(new Any());
235                         else
236                                 matchValue = converter->toAny(arguments[2]);
237                 }
238                 else
239                 {
240                         matchValue = AnyPtr(new Any());
241                 }
242
243         } Catch(Exception) {
244                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
245                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
246                 return NULL;
247         }
248
249         AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
250
251         JSObjectRef jsobject;
252
253         Try {
254                 jsobject = createJSObject(gContext, attributeFilter);
255         } Catch(Exception) {
256                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
257                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
258                 return NULL;
259         }
260
261         return jsobject;
262 }
263
264 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
265                 JSObjectRef object,
266                 JSStringRef propertyName,
267                 JSValueRef* exception)
268 {
269         LogDebug("entered");
270         Try
271         {
272                 FilterConverterFactory::ConverterType converter =
273                                 FilterConverterFactory::getConverter(context);
274                 AttributeFilterPtr attributeFilter = getPrivData(object);
275                 return converter->toJSValueRef(attributeFilter->getAttributeName());
276         }
277         Catch(WrtDeviceApis::Commons::Exception)
278         {
279                 LogWarning("trying to get incorrect value");
280         }
281         return JSValueMakeUndefined(context);
282 }
283
284 bool JSAttributeFilter::setAttributeName(JSContextRef context,
285                 JSObjectRef object,
286                 JSStringRef propertyName,
287                 JSValueRef value,
288                 JSValueRef* exception)
289 {
290         Try
291         {
292                 AttributeFilterPtr attributeFilter = getPrivData(object);
293                 FilterConverterFactory::ConverterType converter =
294                                 FilterConverterFactory::getConverter(context);
295                 attributeFilter->setAttributeName(converter->toString(value));
296                 return true;
297         }
298         Catch(WrtDeviceApis::Commons::Exception)
299         {
300                 LogWarning("trying to set incorrect value");
301         }
302
303         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
304         return false;
305 }
306
307 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
308                 JSObjectRef object,
309                 JSStringRef propertyName,
310                 JSValueRef* exception)
311 {
312         LogDebug("entered");
313         Try
314         {
315                 FilterConverterFactory::ConverterType converter =
316                                 FilterConverterFactory::getConverter(context);
317                 AttributeFilterPtr attributeFilter = getPrivData(object);
318                 return converter->toJSValueRef(attributeFilter->getMatchFlag());
319         }
320         Catch(WrtDeviceApis::Commons::Exception)
321         {
322                 LogWarning("trying to get incorrect value");
323         }
324         return JSValueMakeUndefined(context);
325 }
326
327 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
328                 JSObjectRef object,
329                 JSStringRef propertyName,
330                 JSValueRef value,
331                 JSValueRef* exception)
332 {
333         Try
334         {
335                 AttributeFilterPtr attributeFilter = getPrivData(object);
336                 FilterConverterFactory::ConverterType converter =
337                                 FilterConverterFactory::getConverter(context);
338                 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
339                 return true;
340         }
341         Catch(WrtDeviceApis::Commons::Exception)
342         {
343                 LogWarning("trying to set incorrect value");
344         }
345
346         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
347         return false;
348 }
349
350 JSValueRef JSAttributeFilter::getMatchValue(JSContextRef context,
351                 JSObjectRef object,
352                 JSStringRef propertyName,
353                 JSValueRef* exception)
354 {
355         LogDebug("entered");
356         Try
357         {
358                 FilterConverterFactory::ConverterType converter =
359                                 FilterConverterFactory::getConverter(context);
360                 AttributeFilterPtr attributeFilter = getPrivData(object);
361                 return converter->toJSValueRef(attributeFilter->getMatchValue());
362         }
363         Catch(WrtDeviceApis::Commons::Exception)
364         {
365                 LogWarning("trying to get incorrect value");
366         }
367
368         return JSValueMakeUndefined(context);
369 }
370
371 bool JSAttributeFilter::setMatchValue(JSContextRef context,
372                 JSObjectRef object,
373                 JSStringRef propertyName,
374                 JSValueRef value,
375                 JSValueRef* exception)
376 {
377         Try
378         {
379                 AttributeFilterPtr attributeFilter = getPrivData(object);
380                 FilterConverterFactory::ConverterType converter =
381                                 FilterConverterFactory::getConverter(context);
382                 attributeFilter->setMatchValue(converter->toAny(value));
383                 return true;
384         }
385         Catch(WrtDeviceApis::Commons::Exception)
386         {
387                 LogWarning("trying to set incorrect value");
388         }
389
390         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
391         return false;
392 }
393
394 } // Tizen
395 } // Tizen1_0
396 } // TizenApis