cf8b90fe44e1162bb170fcc06804f873a94611de
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSAttributeFilter.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        JSAttributeFilter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSAttributeFilter class
23  */
24
25 #include "JSAttributeFilter.h"
26
27 #include <string>
28 #include <dpl/shared_ptr.h>
29 #include <CommonsJavaScript/ScopedJSStringRef.h>
30 #include <JSUtil.h>
31 #include <ArgumentValidator.h>
32 #include <JSWebAPIErrorFactory.h>
33 #include "AnyFactory.h"
34 #include <Logger.h>
35
36 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
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 DeviceAPI {
42 namespace Tizen {
43
44 using namespace DeviceAPI::Common;
45 using namespace DeviceAPI::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48
49 JSClassRef JSAttributeFilter::m_classRef = NULL;
50
51 JSClassDefinition JSAttributeFilter::m_classInfo =
52 {
53         0,
54         kJSClassAttributeNone,
55         ATTRIBUTE_FILTER_CLASS_NAME,
56         NULL,
57         m_property,
58         m_functions,
59         Initialize,
60         Finalize,
61         NULL, //hasProperty,
62         NULL, //GetProperty,
63         NULL, //SetProperty,
64         NULL, //DeleteProperty,
65         NULL, //getPropertyNames,
66         NULL, //CallAsFunction,
67         NULL, //CallAsConstructor,
68         NULL, //HasInstance,
69         NULL, //ConvertToType,
70 };
71
72 JSStaticValue JSAttributeFilter::m_property[] = {
73         { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
74         { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
75         { 0, 0, 0, 0 }
76 };
77
78 JSStaticFunction JSAttributeFilter::m_functions[] =
79 {
80         { 0, 0, 0 }
81 };
82
83 JSClassRef JSAttributeFilter::getClassRef() {
84         if (!m_classRef)
85                 m_classRef = JSClassCreate(&m_classInfo);
86
87         return m_classRef;
88 }
89
90 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
91 {
92         return JSValueIsObjectOfClass(context, value, getClassRef());
93 }
94
95 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
96 {
97         if (!isObjectOfClass(context, value))
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99
100         JSObjectRef object = JSValueToObject(context, value, NULL);
101         if (!object)
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103
104         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
105         if (!priv)
106                 Throw(WrtDeviceApis::Commons::NullPointerException);
107
108         return priv->getObject();
109 }
110
111 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context,
112                 AttributeFilterPtr privateData,
113                 JSValueRef jsValueMatchValue)
114 {
115         JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
116         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117         if (NULL == jsObjectRef)
118         {
119                 LoggerE("object creation error");
120                 return NULL;
121         }
122
123         const ScopedJSStringRef jsStrMatchValue(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_MATCH_VALUE));
124
125         JSObjectSetProperty(context, jsObjectRef, jsStrMatchValue.get(),
126                         jsValueMatchValue, kJSPropertyAttributeNone, NULL);
127
128         return jsObjectRef;
129 }
130
131 JSObjectRef JSAttributeFilter::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         MatchFlag matchFlag;
141         AnyPtr matchValue(NULL);
142         JSValueRef jsValueMatchValue = NULL;
143
144         ArgumentValidator validator(context, argumentCount, arguments);
145
146         try
147         {
148                 attributeName = validator.toString(0, false);
149         }
150         catch(BasePlatformException &e)
151         {
152                 return JSWebAPIErrorFactory::postException(context, exception, e);
153         }
154
155         try
156         {
157                 std::string matchFlagStr;
158                 if(argumentCount < 2 ||
159                                 JSValueIsUndefined(context, arguments[1]) ||
160                                 JSValueIsNull(context, arguments[1]))
161                         matchFlagStr = "EXACTLY";
162                 else
163                         matchFlagStr = validator.toString(1, true);
164
165                 if(matchFlagStr == "EXACTLY")
166                         matchFlag = MATCH_EXACTLY;
167                 else if(matchFlagStr == "FULLSTRING")
168                         matchFlag = MATCH_FULLSTRING;
169                 else if(matchFlagStr == "CONTAINS")
170                         matchFlag = MATCH_CONTAINS;
171                 else if(matchFlagStr == "STARTSWITH")
172                         matchFlag = MATCH_STARTSWITH;
173                 else if(matchFlagStr == "ENDSWITH")
174                         matchFlag = MATCH_ENDSWITH;
175                 else if(matchFlagStr == "EXISTS")
176                         matchFlag = MATCH_EXISTS;
177                 else
178                         throw TypeMismatchException("Property is not valid FilterMatchFlag value");
179         }
180         catch(BasePlatformException &e)
181         {
182                 return JSWebAPIErrorFactory::postException(context, exception, e);
183         }
184
185         matchValue = AnyFactory::createAnyEmpty(context);
186
187         if(argumentCount >= 3)
188                 jsValueMatchValue = arguments[2];
189         else
190                 jsValueMatchValue = JSValueMakeUndefined(context);
191
192         AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
193
194         JSObjectRef jsobject;
195
196         Try
197         {
198                 jsobject = createJSObject(context, attributeFilter, jsValueMatchValue);
199         }
200         Catch(Exception)
201         {
202                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
203                 return JSWebAPIErrorFactory::postException(context, exception,
204                                 TypeMismatchException("Error occurred while creating object"));
205         }
206
207         return jsobject;
208 }
209
210 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
211 {
212         if (!JSObjectGetPrivate(object))
213         {
214                 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
215                 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
216                 if (!JSObjectSetPrivate(object, priv))
217                 {
218                         delete priv;
219                 }
220         }
221 }
222
223 void JSAttributeFilter::Finalize(JSObjectRef object)
224 {
225         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
226
227         if (priv != NULL)
228         {
229                 delete (priv);
230         }
231
232         priv = NULL;
233 }
234
235 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
236 {
237         LoggerD("entered");
238         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
239         if (!priv)
240                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
241
242         AttributeFilterPtr result = priv->getObject();
243         if (!result)
244                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
245
246         return result;
247 }
248
249 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
250                 JSObjectRef object,
251                 JSStringRef propertyName,
252                 JSValueRef* exception)
253 {
254         LoggerD("entered");
255
256         try
257         {
258                 AttributeFilterPtr attributeFilter = getPrivData(object);
259                 return JSUtil::toJSValueRef(context, attributeFilter->getAttributeName());
260         }
261         catch(BasePlatformException &e)
262         {
263                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
264         }
265
266         return JSValueMakeUndefined(context);
267 }
268
269 bool JSAttributeFilter::setAttributeName(JSContextRef context,
270                 JSObjectRef object,
271                 JSStringRef propertyName,
272                 JSValueRef value,
273                 JSValueRef* exception)
274 {
275         LoggerD("entered");
276
277         try
278         {
279                 AttributeFilterPtr attributeFilter = getPrivData(object);
280                 attributeFilter->setAttributeName(JSUtil::JSValueToString(context, value));
281         }
282         catch(BasePlatformException &e)
283         {
284                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
285         }
286
287         return true;
288 }
289
290 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
291                 JSObjectRef object,
292                 JSStringRef propertyName,
293                 JSValueRef* exception)
294 {
295         LoggerD("entered");
296
297         try
298         {
299                 AttributeFilterPtr attributeFilter = getPrivData(object);
300                 MatchFlag matchFlag = attributeFilter->getMatchFlag();
301                 std::string matchFlagStr;
302                 if(matchFlag == MATCH_EXACTLY)
303                         matchFlagStr = "EXACTLY";
304                 else if(matchFlag == MATCH_FULLSTRING)
305                         matchFlagStr = "FULLSTRING";
306                 else if(matchFlag == MATCH_CONTAINS)
307                         matchFlagStr = "CONTAINS";
308                 else if(matchFlag == MATCH_STARTSWITH)
309                         matchFlagStr = "STARTSWITH";
310                 else if(matchFlag == MATCH_ENDSWITH)
311                         matchFlagStr = "ENDSWITH";
312                 else if(matchFlag == MATCH_EXISTS)
313                         matchFlagStr = "EXISTS";
314                 else
315                         matchFlagStr = "EXACTLY";
316
317                 return JSUtil::toJSValueRef(context, matchFlagStr);
318         }
319         catch(BasePlatformException &e)
320         {
321                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
322         }
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         LoggerD("entered");
334
335         try
336         {
337                 AttributeFilterPtr attributeFilter = getPrivData(object);
338
339                 MatchFlag matchFlag;
340                 std::string matchFlagStr = JSUtil::JSValueToString(context, value);
341
342                 if(matchFlagStr == "EXACTLY")
343                         matchFlag = MATCH_EXACTLY;
344                 else if(matchFlagStr == "FULLSTRING")
345                         matchFlag = MATCH_FULLSTRING;
346                 else if(matchFlagStr == "CONTAINS")
347                         matchFlag = MATCH_CONTAINS;
348                 else if(matchFlagStr == "STARTSWITH")
349                         matchFlag = MATCH_STARTSWITH;
350                 else if(matchFlagStr == "ENDSWITH")
351                         matchFlag = MATCH_ENDSWITH;
352                 else if(matchFlagStr == "EXISTS")
353                         matchFlag = MATCH_EXISTS;
354                 else
355                         throw TypeMismatchException("Property is not valid FilterMatchFlag value");
356
357                 attributeFilter->setMatchFlag(matchFlag);
358         }
359         catch(BasePlatformException &e)
360         {
361                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
362         }
363
364         return true;
365 }
366
367 } // Tizen
368 } // DeviceAPI