Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactAddressTypeArray.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        JSContactAddressTypeArray.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 "JSContactAddressTypeArray.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 JSContactAddressTypeArray::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 JSContactAddressTypeArray::m_property[] = {
75         { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
76         { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSContactAddressTypeArray::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 JSContactAddressTypeArray::m_jsClassRef = JSClassCreate(
96                 JSContactAddressTypeArray::getClassInfo());
97
98 JSValueRef JSContactAddressTypeArray::getLength(JSContextRef context,
99                 JSObjectRef object,
100                 JSStringRef propertyName,
101                 JSValueRef* exception)
102 {
103         //LogDebug("enter");
104         Try
105         {
106                 JSContactAddressTypeArrayPriv* priv =
107                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
108                 if (!priv) {
109                         Throw(WrtDeviceApis::Commons::NullPointerException);
110                 }
111                 ContactAddressTypeArrayPtr 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 JSContactAddressTypeArray::createArray(JSContextRef context,
126                 const ContactAddressTypeArrayPtr &typeArray)
127 {
128         JSContactAddressTypeArrayPriv *priv = new JSContactAddressTypeArrayPriv(context, typeArray);
129         return JSObjectMake(context, getClassRef(), priv);
130 }
131
132 const JSClassDefinition* JSContactAddressTypeArray::getClassInfo()
133 {
134         return &(m_classInfo);
135 }
136
137 JSClassRef JSContactAddressTypeArray::getClassRef()
138 {
139         if (!m_jsClassRef) {
140                 m_jsClassRef = JSClassCreate(&m_classInfo);
141         }
142         return m_jsClassRef;
143 }
144
145 bool JSContactAddressTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
146 {
147         return JSValueIsObjectOfClass(context, value, getClassRef());
148 }
149
150 ContactAddressTypeArrayPtr JSContactAddressTypeArray::getContactAddressTypeArray(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         JSContactAddressTypeArrayPriv *priv = static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
160         if (!priv) {
161                 Throw(WrtDeviceApis::Commons::NullPointerException);
162         }
163         return priv->getObject();
164 }
165
166 void JSContactAddressTypeArray::initialize(JSContextRef context,
167                 JSObjectRef object)
168 {
169         //LogDebug("enter");
170 }
171
172 void JSContactAddressTypeArray::finalize(JSObjectRef object)
173 {
174         //LogDebug("enter");
175         JSContactAddressTypeArrayPriv* priv =
176                 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
177         delete priv;
178         JSObjectSetPrivate(object, NULL);
179 }
180
181 bool JSContactAddressTypeArray::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                 JSContactAddressTypeArrayPriv* priv =
192                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
193                 if (!priv) {
194                         Throw(WrtDeviceApis::Commons::NullPointerException);
195                 }
196                 ContactAddressTypeArrayPtr 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 JSContactAddressTypeArray::getProperty(JSContextRef context,
209                 JSObjectRef object,
210                 JSStringRef propertyName,
211                 JSValueRef* exception)
212 {
213         //LogDebug("enter");
214         ContactConverterFactory::ConverterType converter =
215                                         ContactConverterFactory::getConverter(context);
216         Try
217         {
218                 size_t index = converter->toSizeT(propertyName);
219                 JSContactAddressTypeArrayPriv* priv =
220                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
221                 if (!priv) {
222                         Throw(WrtDeviceApis::Commons::NullPointerException);
223                 }
224                 ContactAddressTypeArrayPtr typeArray = priv->getObject();
225                 if (index < typeArray->size()) {
226                         std::string result = converter->toContactAddressTypeStr(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 JSContactAddressTypeArray::setProperty(JSContextRef context,
240                 JSObjectRef object,
241                 JSStringRef propertyName,
242                 JSValueRef value,
243                 JSValueRef* exception)
244 {
245         //LogDebug("enter");
246         ContactConverterFactory::ConverterType converter =
247                                         ContactConverterFactory::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                 JSContactAddressTypeArrayPriv* priv =
256                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
257                 if (!priv) {
258                         Throw(WrtDeviceApis::Commons::NullPointerException);
259                 }
260                 ContactAddressTypeArrayPtr 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] = converter->toContactAddressType(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 JSContactAddressTypeArray::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                 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
289                 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
290                                 context,
291                                 typeArray);
292                 JSValueRef result = JSObjectMake(context,
293                                                                                  getClassRef(), newPrivateObject);
294
295                 //copy current typeArray
296                 JSContactAddressTypeArrayPriv* priv =
297                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
298                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
299                 for (size_t i = 0; i < currentTypes->size(); ++i) {
300                         typeArray->push_back(currentTypes->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 types
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->toContactAddressType(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 JSContactAddressTypeArray::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                 JSContactAddressTypeArrayPriv* priv =
342                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
343                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
344                 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
345                         separator = converter->toString(arguments[0]);
346                 }
347                 for (size_t i = 0; i < currentTypes->size(); ++i) {
348                         if (i != 0) {
349                                 result += separator;
350                         }
351                         result += currentTypes->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 JSContactAddressTypeArray::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                 ContactConverterFactory::ConverterType converter =
373                                                 ContactConverterFactory::getConverter(context);
374                 JSContactAddressTypeArrayPriv* priv =
375                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
376                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
377                 if (currentTypes->size() > 0) {
378                         std::string result = converter->toContactAddressTypeStr(currentTypes->at(currentTypes->size()-1));
379                         currentTypes->pop_back();
380                         return converter->toJSValueRef(result);
381                 }
382         }
383         Catch(WrtDeviceApis::Commons::Exception)
384         {
385                 LogError("error occured");
386         }
387         return JSValueMakeUndefined(context);
388 }
389
390 JSValueRef JSContactAddressTypeArray::push(JSContextRef context,
391                 JSObjectRef function,
392                 JSObjectRef thisObject,
393                 size_t argumentCount,
394                 const JSValueRef arguments[],
395                 JSValueRef* exception)
396 {
397         //LogDebug("entered");
398         Try
399         {
400                 ContactConverterFactory::ConverterType converter =
401                                                 ContactConverterFactory::getConverter(context);
402                 JSContactAddressTypeArrayPriv* priv =
403                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
404                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
405                 for (size_t i = 0; i < argumentCount; ++i) {
406                         currentTypes->push_back(converter->toContactAddressType(arguments[i]));
407                 }
408                 return converter->toJSValueRef(currentTypes->size());
409         }
410         Catch(WrtDeviceApis::Commons::Exception)
411         {
412                 LogError("error occured");
413         }
414         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
415 }
416
417 JSValueRef JSContactAddressTypeArray::reverse(JSContextRef context,
418                 JSObjectRef function,
419                 JSObjectRef thisObject,
420                 size_t argumentCount,
421                 const JSValueRef arguments[],
422                 JSValueRef* exception)
423 {
424         //LogDebug("entered");
425         Try
426         {
427                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
428                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
429                 JSContactAddressTypeArrayPriv* priv =
430                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
431                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
432                 std::reverse(currentTypes->begin(), currentTypes->end());
433                 return thisObject;
434         }
435         Catch(WrtDeviceApis::Commons::Exception)
436         {
437                 LogError("error occured");
438         }
439         return JSValueMakeUndefined(context);
440 }
441
442 JSValueRef JSContactAddressTypeArray::shift(JSContextRef context,
443                 JSObjectRef function,
444                 JSObjectRef thisObject,
445                 size_t argumentCount,
446                 const JSValueRef arguments[],
447                 JSValueRef* exception)
448 {
449         //LogDebug("entered");
450         Try
451         {
452                 ContactConverterFactory::ConverterType converter =
453                                                 ContactConverterFactory::getConverter(context);
454                 JSContactAddressTypeArrayPriv* priv =
455                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
456                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
457                 if (currentTypes->size() > 0) {
458                         std::string result = converter->toContactAddressTypeStr(currentTypes->at(0));
459                         currentTypes->erase(currentTypes->begin());
460                         return converter->toJSValueRef(result);
461                 }
462         }
463         Catch(WrtDeviceApis::Commons::Exception)
464         {
465                 LogError("error occured");
466         }
467         return JSValueMakeUndefined(context);
468 }
469
470 JSValueRef JSContactAddressTypeArray::slice(JSContextRef context,
471                 JSObjectRef function,
472                 JSObjectRef thisObject,
473                 size_t argumentCount,
474                 const JSValueRef arguments[],
475                 JSValueRef* exception)
476 {
477         //LogDebug("enter");
478         Try
479         {
480                 if (argumentCount < 1) {
481                         return JSValueMakeUndefined(context);
482                 }
483                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
484                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
485                 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
486                 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
487                                 context,
488                                 typeArray);
489                 JSValueRef result = JSObjectMake(context,
490                                                                                  getClassRef(), newPrivateObject);
491
492                 //copy current typeArray
493                 JSContactAddressTypeArrayPriv* priv =
494                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
495                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
496                 std::size_t first = converter->toSizeT(arguments[0]);
497                 std::size_t last = currentTypes->size() - 1;
498                 if (argumentCount > 1) {
499                         last = converter->toSizeT(arguments[1]);
500                         if (last >= currentTypes->size()) {
501                                 last = currentTypes->size() - 1;
502                         }
503                 }
504                 if (first < 0) {
505                         first = 0;
506                 }
507                 for (size_t i = first; i <= last; ++i) {
508                         typeArray->push_back(currentTypes->at(i));
509                 }
510
511                 return result;
512         }
513         Catch(WrtDeviceApis::Commons::Exception)
514         {
515                 LogError("error occured");
516         }
517         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
518 }
519
520 JSValueRef JSContactAddressTypeArray::sort(JSContextRef context,
521                 JSObjectRef function,
522                 JSObjectRef thisObject,
523                 size_t argumentCount,
524                 const JSValueRef arguments[],
525                 JSValueRef* exception)
526 {
527         //LogDebug("entered");
528         Try
529         {
530                 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
531                                 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
532                 JSContactAddressTypeArrayPriv* priv =
533                         static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
534                 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
535                 std::sort(currentTypes->begin(), currentTypes->end());
536                 return thisObject;
537         }
538         Catch(WrtDeviceApis::Commons::Exception)
539         {
540                 LogError("error occured");
541         }
542         return JSValueMakeUndefined(context);
543 }
544
545 JSValueRef JSContactAddressTypeArray::splice(JSContextRef context,
546                 JSObjectRef function,
547                 JSObjectRef thisObject,
548                 size_t argumentCount,
549                 const JSValueRef arguments[],
550                 JSValueRef* exception)
551 {
552         //LogDebug("entered");
553         return JSValueMakeUndefined(context);
554 }
555
556 JSValueRef JSContactAddressTypeArray::toString(JSContextRef context,
557                 JSObjectRef function,
558                 JSObjectRef thisObject,
559                 size_t argumentCount,
560                 const JSValueRef arguments[],
561                 JSValueRef* exception)
562 {
563         //LogDebug("entered");
564         return join(context, function, thisObject, 0, arguments, exception);
565 }
566
567 JSValueRef JSContactAddressTypeArray::unshift(JSContextRef context,
568                 JSObjectRef function,
569                 JSObjectRef thisObject,
570                 size_t argumentCount,
571                 const JSValueRef arguments[],
572                 JSValueRef* exception)
573 {
574         //LogDebug("entered");
575         return JSValueMakeUndefined(context);
576 }
577
578 JSValueRef JSContactAddressTypeArray::valueOf(JSContextRef context,
579                 JSObjectRef function,
580                 JSObjectRef thisObject,
581                 size_t argumentCount,
582                 const JSValueRef arguments[],
583                 JSValueRef* exception)
584 {
585         //LogDebug("entered");
586         return JSValueMakeUndefined(context);
587 }
588
589 } // Contact
590 } // Tizen1_0
591 } // TizenApis