Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSAttributesOfInterestArray.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        JSAttributesOfInterestArray.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include <algorithm>
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSAttributesOfInterestArray.h"
31
32 #define FUNCTION_CONCAT "concat"
33 #define FUNCTION_JOIN "join"
34 #define FUNCTION_POP "pop"
35 #define FUNCTION_PUSH "push"
36 #define FUNCTION_REVERSE "reverse"
37 #define FUNCTION_SHIFT "shift"
38 #define FUNCTION_SLICE "slice"
39 #define FUNCTION_SORT "sort"
40 #define FUNCTION_SPLICE "splice"
41 #define FUNCTION_TOSTRING "toString"
42 #define FUNCTION_UNSHIFT "unshift"
43 #define FUNCTION_VALUEOF "valueOf"
44 #define ARRAY "Array"
45 #define ATTRIBUTE_LENGTH "length"
46
47 namespace TizenApis {
48 namespace Tizen1_0 {
49 namespace Contact {
50
51 using namespace TizenApis::Commons;
52 using namespace TizenApis::Api::Contact;
53
54 JSClassDefinition JSAttributesOfInterestArray::m_classInfo = {
55     0,
56     kJSClassAttributeNone,
57     ARRAY,
58     0,
59     m_property,
60     m_function,
61     initialize,
62     finalize,
63     hasProperty,
64     getProperty,
65     setProperty,
66     NULL, //deleteProperty,
67     NULL, //getPropertyNames,
68     NULL, //callAsFunction,
69     NULL, //callAsConstructor,
70     NULL, //hasInstance,
71     NULL, //convertToType,
72 };
73
74 JSStaticValue JSAttributesOfInterestArray::m_property[] = {
75     { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
76     { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSAttributesOfInterestArray::m_function[] = {
80     { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
81     { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
82     { FUNCTION_POP, pop, kJSPropertyAttributeNone },
83     { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
84     { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
85     { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
86     { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
87     { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
88     { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
89     { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
90     { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
91     { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
92     { 0, 0, 0 }
93 };
94
95 JSClassRef JSAttributesOfInterestArray::m_jsClassRef = JSClassCreate(
96         JSAttributesOfInterestArray::getClassInfo());
97
98 JSValueRef JSAttributesOfInterestArray::getLength(JSContextRef context,
99         JSObjectRef object,
100         JSStringRef propertyName,
101         JSValueRef* exception)
102 {
103     //LogDebug("enter");
104     Try
105     {
106         JSAttributesOfInterestArrayPriv* priv =
107             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
108         if (!priv) {
109             Throw(WrtDeviceApis::Commons::NullPointerException);
110         }
111         AttributesOfInterestArrayPtr typeArray = priv->getObject();
112         if (typeArray) {
113                         WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
114                                         WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
115             return converter->toJSValueRef(typeArray->size());
116         }
117     }
118     Catch(WrtDeviceApis::Commons::Exception)
119     {
120         LogError("invalid conversion");
121     }
122     return JSValueMakeUndefined(context);
123 }
124
125 JSObjectRef JSAttributesOfInterestArray::createArray(JSContextRef context,
126         const AttributesOfInterestArrayPtr &typeArray)
127 {
128     JSAttributesOfInterestArrayPriv *priv = new JSAttributesOfInterestArrayPriv(context, typeArray);
129     return JSObjectMake(context, getClassRef(), priv);
130 }
131
132 const JSClassDefinition* JSAttributesOfInterestArray::getClassInfo()
133 {
134     return &(m_classInfo);
135 }
136
137 JSClassRef JSAttributesOfInterestArray::getClassRef()
138 {
139     if (!m_jsClassRef) {
140         m_jsClassRef = JSClassCreate(&m_classInfo);
141     }
142     return m_jsClassRef;
143 }
144
145 bool JSAttributesOfInterestArray::isObjectOfClass(JSContextRef context, JSValueRef value)
146 {
147         return JSValueIsObjectOfClass(context, value, getClassRef());
148 }
149
150 AttributesOfInterestArrayPtr JSAttributesOfInterestArray::getAttributesOfInterestArray(JSContextRef context, JSValueRef value)
151 {
152         if (!isObjectOfClass(context, value)) {
153                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
154         }
155         JSObjectRef object = JSValueToObject(context, value, NULL);
156         if (!object) {
157                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
158         }
159         JSAttributesOfInterestArrayPriv *priv = static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
160         if (!priv) {
161                 Throw(WrtDeviceApis::Commons::NullPointerException);
162         }
163         return priv->getObject();
164 }
165
166 void JSAttributesOfInterestArray::initialize(JSContextRef context,
167         JSObjectRef object)
168 {
169     //LogDebug("enter");
170 }
171
172 void JSAttributesOfInterestArray::finalize(JSObjectRef object)
173 {
174     //LogDebug("enter");
175     JSAttributesOfInterestArrayPriv* priv =
176         static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
177     delete priv;
178     JSObjectSetPrivate(object, NULL);
179 }
180
181 bool JSAttributesOfInterestArray::hasProperty(JSContextRef context,
182         JSObjectRef object,
183         JSStringRef propertyName)
184 {
185     //LogDebug("enter");
186         WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
187                         WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
188     Try
189     {
190         size_t index = converter->toSizeT(propertyName);
191         JSAttributesOfInterestArrayPriv* priv =
192             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
193         if (!priv) {
194             Throw(WrtDeviceApis::Commons::NullPointerException);
195         }
196         AttributesOfInterestArrayPtr typeArray = priv->getObject();
197         if (index < typeArray->size()) {
198             return true;
199         }
200     }
201     Catch(WrtDeviceApis::Commons::Exception)
202     {
203         //not reporting error is intended
204     }
205     return false;
206 }
207
208 JSValueRef JSAttributesOfInterestArray::getProperty(JSContextRef context,
209         JSObjectRef object,
210         JSStringRef propertyName,
211         JSValueRef* exception)
212 {
213     //LogDebug("enter");
214         WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
215                         WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
216     Try
217     {
218         size_t index = converter->toSizeT(propertyName);
219         JSAttributesOfInterestArrayPriv* priv =
220             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
221         if (!priv) {
222             Throw(WrtDeviceApis::Commons::NullPointerException);
223         }
224         AttributesOfInterestArrayPtr typeArray = priv->getObject();
225         if (index < typeArray->size()) {
226             std::string result = typeArray->at(index);
227             if (!result.empty()) {
228                 return converter->toJSValueRef(result);
229             }
230         }
231     }
232     Catch(WrtDeviceApis::Commons::Exception)
233     {
234         LogError("invalid property");
235     }
236     return JSValueMakeUndefined(context);
237 }
238
239 bool JSAttributesOfInterestArray::setProperty(JSContextRef context,
240         JSObjectRef object,
241         JSStringRef propertyName,
242         JSValueRef value,
243         JSValueRef* exception)
244 {
245     //LogDebug("enter");
246         WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
247                         WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
248     Try
249     {
250         size_t index = converter->toSizeT(propertyName);
251         std::string str;
252         if (!JSValueIsUndefined(context, value)) {
253             str = converter->toString(value);
254         }
255         JSAttributesOfInterestArrayPriv* priv =
256             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
257         if (!priv) {
258             Throw(WrtDeviceApis::Commons::NullPointerException);
259         }
260         AttributesOfInterestArrayPtr typeArray = priv->getObject();
261         if (!typeArray) {
262             Throw(WrtDeviceApis::Commons::NullPointerException);
263         }
264         if (typeArray->size() <= index) {
265             typeArray->resize(index + 1);
266         }
267         (*typeArray)[index] = str;
268         return true;
269     }
270     Catch(WrtDeviceApis::Commons::Exception)
271     {
272         LogError("error occured");
273         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
274     }
275     return false;
276 }
277
278 JSValueRef JSAttributesOfInterestArray::concat(JSContextRef context,
279         JSObjectRef function,
280         JSObjectRef thisObject,
281         size_t argumentCount,
282         const JSValueRef arguments[],
283         JSValueRef* exception)
284 {
285     //LogDebug("enter");
286     Try
287     {
288         AttributesOfInterestArrayPtr typeArray =
289                         AttributesOfInterestArrayPtr(new AttributesOfInterestArray());
290         JSAttributesOfInterestArrayPriv *newPrivateObject =
291                         new JSAttributesOfInterestArrayPriv(context, typeArray);
292         JSValueRef result = JSObjectMake(context,
293                                          getClassRef(), newPrivateObject);
294
295         //copy current typeArray
296         JSAttributesOfInterestArrayPriv* priv =
297             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
298         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
299         for (size_t i = 0; i < currentStrings->size(); ++i) {
300             typeArray->push_back(currentStrings->at(i));
301         }
302
303         //copy submitted arrays
304         ContactConverterFactory::ConverterType converter =
305                                         ContactConverterFactory::getConverter(context);
306         for (size_t i = 0; i < argumentCount; ++i) {
307             if (!JSIsArrayValue(context, arguments[i])) {
308                 Throw(WrtDeviceApis::Commons::ConversionException);
309             }
310             // process array of strings
311             JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
312             unsigned int len = JSGetArrayLength(context, arrayObj);
313             for (unsigned int e = 0; e < len; ++e) {
314                 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
315                 typeArray->push_back(converter->toString(att));
316             }
317         }
318         return result;
319     }
320     Catch(WrtDeviceApis::Commons::Exception)
321     {
322         LogError("error occured");
323     }
324     return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
325 }
326
327 JSValueRef JSAttributesOfInterestArray::join(JSContextRef context,
328         JSObjectRef function,
329         JSObjectRef thisObject,
330         size_t argumentCount,
331         const JSValueRef arguments[],
332         JSValueRef* exception)
333 {
334     //LogDebug("entered");
335     Try
336     {
337         std::string result;
338         std::string separator(",");
339                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
340                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
341         JSAttributesOfInterestArrayPriv* priv =
342             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
343         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
344         if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
345             separator = converter->toString(arguments[0]);
346         }
347         for (size_t i = 0; i < currentStrings->size(); ++i) {
348             if (i != 0) {
349                 result += separator;
350             }
351             result += currentStrings->at(i);
352         }
353         return converter->toJSValueRef(result);
354     }
355     Catch(WrtDeviceApis::Commons::Exception)
356     {
357         LogError("error occured");
358     }
359     return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
360 }
361
362 JSValueRef JSAttributesOfInterestArray::pop(JSContextRef context,
363         JSObjectRef function,
364         JSObjectRef thisObject,
365         size_t argumentCount,
366         const JSValueRef arguments[],
367         JSValueRef* exception)
368 {
369     //LogDebug("entered");
370     Try
371     {
372                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
373                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
374         JSAttributesOfInterestArrayPriv* priv =
375             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
376         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
377         if (currentStrings->size() > 0) {
378             std::string result = currentStrings->at(
379                     currentStrings->size() - 1);
380             currentStrings->pop_back();
381             return converter->toJSValueRef(result);
382         }
383     }
384     Catch(WrtDeviceApis::Commons::Exception)
385     {
386         LogError("error occured");
387     }
388     return JSValueMakeUndefined(context);
389 }
390
391 JSValueRef JSAttributesOfInterestArray::push(JSContextRef context,
392         JSObjectRef function,
393         JSObjectRef thisObject,
394         size_t argumentCount,
395         const JSValueRef arguments[],
396         JSValueRef* exception)
397 {
398     //LogDebug("entered");
399     Try
400     {
401         ContactConverterFactory::ConverterType converter =
402                                         ContactConverterFactory::getConverter(context);
403         JSAttributesOfInterestArrayPriv* priv =
404             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
405         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
406         for (size_t i = 0; i < argumentCount; ++i) {
407             currentStrings->push_back(converter->toString(arguments[i]));
408         }
409         return converter->toJSValueRef(currentStrings->size());
410     }
411     Catch(WrtDeviceApis::Commons::Exception)
412     {
413         LogError("error occured");
414     }
415     return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
416 }
417
418 JSValueRef JSAttributesOfInterestArray::reverse(JSContextRef context,
419         JSObjectRef function,
420         JSObjectRef thisObject,
421         size_t argumentCount,
422         const JSValueRef arguments[],
423         JSValueRef* exception)
424 {
425     //LogDebug("entered");
426     Try
427     {
428                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
429                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
430         JSAttributesOfInterestArrayPriv* priv =
431             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
432         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
433         std::reverse(currentStrings->begin(), currentStrings->end());
434         return thisObject;
435     }
436     Catch(WrtDeviceApis::Commons::Exception)
437     {
438         LogError("error occured");
439     }
440     return JSValueMakeUndefined(context);
441 }
442
443 JSValueRef JSAttributesOfInterestArray::shift(JSContextRef context,
444         JSObjectRef function,
445         JSObjectRef thisObject,
446         size_t argumentCount,
447         const JSValueRef arguments[],
448         JSValueRef* exception)
449 {
450     //LogDebug("entered");
451     Try
452     {
453                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
454                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
455         JSAttributesOfInterestArrayPriv* priv =
456             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
457         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
458         if (currentStrings->size() > 0) {
459             std::string result = currentStrings->at(0);
460             currentStrings->erase(currentStrings->begin());
461             return converter->toJSValueRef(result);
462         }
463     }
464     Catch(WrtDeviceApis::Commons::Exception)
465     {
466         LogError("error occured");
467     }
468     return JSValueMakeUndefined(context);
469 }
470
471 JSValueRef JSAttributesOfInterestArray::slice(JSContextRef context,
472         JSObjectRef function,
473         JSObjectRef thisObject,
474         size_t argumentCount,
475         const JSValueRef arguments[],
476         JSValueRef* exception)
477 {
478     //LogDebug("enter");
479     Try
480     {
481         if (argumentCount < 1) {
482             return JSValueMakeUndefined(context);
483         }
484                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
485                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
486         AttributesOfInterestArrayPtr typeArray = AttributesOfInterestArrayPtr(new StringArray());
487         JSAttributesOfInterestArrayPriv *newPrivateObject = new JSAttributesOfInterestArrayPriv(
488                 context,
489                 typeArray);
490         JSValueRef result = JSObjectMake(context,
491                                          getClassRef(), newPrivateObject);
492
493         //copy current typeArray
494         JSAttributesOfInterestArrayPriv* priv =
495             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
496         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
497         std::size_t first = converter->toSizeT(arguments[0]);
498         std::size_t last = currentStrings->size() - 1;
499         if (argumentCount > 1) {
500             last = converter->toSizeT(arguments[1]);
501             if (last >= currentStrings->size()) {
502                 last = currentStrings->size() - 1;
503             }
504         }
505         if (first < 0) {
506             first = 0;
507         }
508         for (size_t i = first; i <= last; ++i) {
509             typeArray->push_back(currentStrings->at(i));
510         }
511
512         return result;
513     }
514     Catch(WrtDeviceApis::Commons::Exception)
515     {
516         LogError("error occured");
517     }
518     return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
519 }
520
521 JSValueRef JSAttributesOfInterestArray::sort(JSContextRef context,
522         JSObjectRef function,
523         JSObjectRef thisObject,
524         size_t argumentCount,
525         const JSValueRef arguments[],
526         JSValueRef* exception)
527 {
528     //LogDebug("entered");
529     Try
530     {
531                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
532                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
533         JSAttributesOfInterestArrayPriv* priv =
534             static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
535         AttributesOfInterestArrayPtr currentStrings = priv->getObject();
536         std::sort(currentStrings->begin(), currentStrings->end());
537         return thisObject;
538     }
539     Catch(WrtDeviceApis::Commons::Exception)
540     {
541         LogError("error occured");
542     }
543     return JSValueMakeUndefined(context);
544 }
545
546 JSValueRef JSAttributesOfInterestArray::splice(JSContextRef context,
547         JSObjectRef function,
548         JSObjectRef thisObject,
549         size_t argumentCount,
550         const JSValueRef arguments[],
551         JSValueRef* exception)
552 {
553     //LogDebug("entered");
554     return JSValueMakeUndefined(context);
555 }
556
557 JSValueRef JSAttributesOfInterestArray::toString(JSContextRef context,
558         JSObjectRef function,
559         JSObjectRef thisObject,
560         size_t argumentCount,
561         const JSValueRef arguments[],
562         JSValueRef* exception)
563 {
564     //LogDebug("entered");
565     return join(context, function, thisObject, 0, arguments, exception);
566 }
567
568 JSValueRef JSAttributesOfInterestArray::unshift(JSContextRef context,
569         JSObjectRef function,
570         JSObjectRef thisObject,
571         size_t argumentCount,
572         const JSValueRef arguments[],
573         JSValueRef* exception)
574 {
575     //LogDebug("entered");
576     return JSValueMakeUndefined(context);
577 }
578
579 JSValueRef JSAttributesOfInterestArray::valueOf(JSContextRef context,
580         JSObjectRef function,
581         JSObjectRef thisObject,
582         size_t argumentCount,
583         const JSValueRef arguments[],
584         JSValueRef* exception)
585 {
586     //LogDebug("entered");
587     return JSValueMakeUndefined(context);
588 }
589
590 } // Contact
591 } // Tizen1_0
592 } // TizenApis