Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactEmailAddressTypeArray.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        JSContactEmailAddressTypeArray.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 "JSContactEmailAddressTypeArray.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 using namespace WrtDeviceApis::Commons;
54 using namespace WrtDeviceApis::CommonsJavaScript;
55
56 JSClassDefinition JSContactEmailAddressTypeArray::m_classInfo = {
57         0,
58         kJSClassAttributeNone,
59         ARRAY,
60         0,
61         m_property,
62         m_function,
63         initialize,
64         finalize,
65         hasProperty,
66         getProperty,
67         setProperty,
68         NULL, //deleteProperty,
69         NULL, //getPropertyNames,
70         NULL, //callAsFunction,
71         NULL, //callAsConstructor,
72         NULL, //hasInstance,
73         NULL, //convertToType,
74 };
75
76 JSStaticValue JSContactEmailAddressTypeArray::m_property[] = {
77         { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
78         { 0, 0, 0, 0 }
79 };
80
81 JSStaticFunction JSContactEmailAddressTypeArray::m_function[] = {
82         { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
83         { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
84         { FUNCTION_POP, pop, kJSPropertyAttributeNone },
85         { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
86         { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
87         { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
88         { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
89         { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
90         { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
91         { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
92         { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
93         { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
94         { 0, 0, 0 }
95 };
96
97 JSClassRef JSContactEmailAddressTypeArray::m_jsClassRef = JSClassCreate(
98                 JSContactEmailAddressTypeArray::getClassInfo());
99
100 JSValueRef JSContactEmailAddressTypeArray::getLength(JSContextRef context,
101                 JSObjectRef object,
102                 JSStringRef propertyName,
103                 JSValueRef* exception)
104 {
105         //LogDebug("enter");
106         Try
107         {
108                 JSContactEmailAddressTypeArrayPriv* priv =
109                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
110                 if (!priv) {
111                         Throw(NullPointerException);
112                 }
113                 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
114                 if (typeArray) {
115                         BasicConverter converter = BasicConverterFactory::getConverter(context);
116                         return converter->toJSValueRef(typeArray->size());
117                 }
118         }
119         Catch(Exception)
120         {
121                 LogError("invalid conversion");
122         }
123         return JSValueMakeUndefined(context);
124 }
125
126 JSObjectRef JSContactEmailAddressTypeArray::createArray(JSContextRef context,
127                 const ContactEmailAddressTypeArrayPtr &typeArray)
128 {
129         JSContactEmailAddressTypeArrayPriv *priv = new JSContactEmailAddressTypeArrayPriv(context, typeArray);
130         return JSObjectMake(context, getClassRef(), priv);
131 }
132
133 bool JSContactEmailAddressTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
134 {
135         return JSValueIsObjectOfClass(context, value, getClassRef());
136 }
137
138 ContactEmailAddressTypeArrayPtr JSContactEmailAddressTypeArray::getContactEmailAddressTypeArray(JSContextRef context, JSValueRef value)
139 {
140         if (!isObjectOfClass(context, value)) {
141                 Throw(InvalidArgumentException);
142         }
143         JSObjectRef object = JSValueToObject(context, value, NULL);
144         if (!object) {
145                 Throw(InvalidArgumentException);
146         }
147         JSContactEmailAddressTypeArrayPriv *priv = static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
148         if (!priv) {
149                 Throw(NullPointerException);
150         }
151         return priv->getObject();
152 }
153
154 const JSClassDefinition* JSContactEmailAddressTypeArray::getClassInfo()
155 {
156         return &(m_classInfo);
157 }
158
159 JSClassRef JSContactEmailAddressTypeArray::getClassRef()
160 {
161         if (!m_jsClassRef) {
162                 m_jsClassRef = JSClassCreate(&m_classInfo);
163         }
164         return m_jsClassRef;
165 }
166
167 void JSContactEmailAddressTypeArray::initialize(JSContextRef context,
168                 JSObjectRef object)
169 {
170         //LogDebug("enter");
171 }
172
173 void JSContactEmailAddressTypeArray::finalize(JSObjectRef object)
174 {
175         //LogDebug("enter");
176         JSContactEmailAddressTypeArrayPriv* priv =
177                 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
178         delete priv;
179         JSObjectSetPrivate(object, NULL);
180 }
181
182 bool JSContactEmailAddressTypeArray::hasProperty(JSContextRef context,
183                 JSObjectRef object,
184                 JSStringRef propertyName)
185 {
186         //LogDebug("enter");
187         BasicConverter converter = BasicConverterFactory::getConverter(context);
188         Try
189         {
190                 size_t index = converter->toSizeT(propertyName);
191                 JSContactEmailAddressTypeArrayPriv* priv =
192                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
193                 if (!priv) {
194                         Throw(NullPointerException);
195                 }
196                 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
197                 if (index < typeArray->size()) {
198                         return true;
199                 }
200         }
201         Catch(Exception)
202         {
203                 //not reporting error is intended
204         }
205         return false;
206 }
207
208 JSValueRef JSContactEmailAddressTypeArray::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                 JSContactEmailAddressTypeArrayPriv* priv =
220                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
221                 if (!priv) {
222                         Throw(NullPointerException);
223                 }
224                 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
225                 if (index < typeArray->size()) {
226                         std::string result = converter->toContactEmailAddressTypeStr(typeArray->at(index));
227                         if (!result.empty()) {
228                                 return converter->toJSValueRef(result);
229                         }
230                 }
231         }
232         Catch(Exception)
233         {
234                 LogError("invalid property");
235         }
236         return JSValueMakeUndefined(context);
237 }
238
239 bool JSContactEmailAddressTypeArray::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                 JSContactEmailAddressTypeArrayPriv* priv =
256                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
257                 if (!priv) {
258                         Throw(NullPointerException);
259                 }
260                 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
261                 if (!typeArray) {
262                         Throw(NullPointerException);
263                 }
264                 if (typeArray->size() <= index) {
265                         typeArray->resize(index + 1);
266                 }
267                 (*typeArray)[index] = converter->toContactEmailAddressType(str);
268                 return true;
269         }
270         Catch(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 JSContactEmailAddressTypeArray::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                 ContactEmailAddressTypeArrayPtr typeArray = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
289                 JSContactEmailAddressTypeArrayPriv *newPrivateObject = new JSContactEmailAddressTypeArrayPriv(
290                                 context,
291                                 typeArray);
292                 JSValueRef result = JSObjectMake(context,
293                                                                                  getClassRef(), newPrivateObject);
294
295                 //copy current typeArray
296                 JSContactEmailAddressTypeArrayPriv* priv =
297                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
298                 ContactEmailAddressTypeArrayPtr 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(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->toContactEmailAddressType(att));
316                         }
317                 }
318                 return result;
319         }
320         Catch(Exception)
321         {
322                 LogError("error occured");
323         }
324         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
325 }
326
327 JSValueRef JSContactEmailAddressTypeArray::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                 BasicConverter converter = BasicConverterFactory::getConverter(context);
340                 JSContactEmailAddressTypeArrayPriv* priv =
341                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
342                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
343                 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
344                         separator = converter->toString(arguments[0]);
345                 }
346                 for (size_t i = 0; i < currentStrings->size(); ++i) {
347                         if (i != 0) {
348                                 result += separator;
349                         }
350                         result += currentStrings->at(i);
351                 }
352                 return converter->toJSValueRef(result);
353         }
354         Catch(Exception)
355         {
356                 LogError("error occured");
357         }
358         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
359 }
360
361 JSValueRef JSContactEmailAddressTypeArray::pop(JSContextRef context,
362                 JSObjectRef function,
363                 JSObjectRef thisObject,
364                 size_t argumentCount,
365                 const JSValueRef arguments[],
366                 JSValueRef* exception)
367 {
368         //LogDebug("entered");
369         Try
370         {
371                 ContactConverterFactory::ConverterType converter =
372                                                 ContactConverterFactory::getConverter(context);
373                 JSContactEmailAddressTypeArrayPriv* priv =
374                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
375                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
376                 if (currentStrings->size() > 0) {
377                         std::string result = converter->toContactEmailAddressTypeStr(currentStrings->at(currentStrings->size() - 1));
378                         currentStrings->pop_back();
379                         return converter->toJSValueRef(result);
380                 }
381         }
382         Catch(Exception)
383         {
384                 LogError("error occured");
385         }
386         return JSValueMakeUndefined(context);
387 }
388
389 JSValueRef JSContactEmailAddressTypeArray::push(JSContextRef context,
390                 JSObjectRef function,
391                 JSObjectRef thisObject,
392                 size_t argumentCount,
393                 const JSValueRef arguments[],
394                 JSValueRef* exception)
395 {
396         //LogDebug("entered");
397         Try
398         {
399                 ContactConverterFactory::ConverterType converter =
400                                                 ContactConverterFactory::getConverter(context);
401                 JSContactEmailAddressTypeArrayPriv* priv =
402                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
403                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
404                 for (size_t i = 0; i < argumentCount; ++i) {
405                         currentStrings->push_back(converter->toContactEmailAddressType(arguments[i]));
406                 }
407                 return converter->toJSValueRef(currentStrings->size());
408         }
409         Catch(Exception)
410         {
411                 LogError("error occured");
412         }
413         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
414 }
415
416 JSValueRef JSContactEmailAddressTypeArray::reverse(JSContextRef context,
417                 JSObjectRef function,
418                 JSObjectRef thisObject,
419                 size_t argumentCount,
420                 const JSValueRef arguments[],
421                 JSValueRef* exception)
422 {
423         //LogDebug("entered");
424         Try
425         {
426                 Converter converter(context);
427                 JSContactEmailAddressTypeArrayPriv* priv =
428                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
429                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
430                 std::reverse(currentStrings->begin(), currentStrings->end());
431                 return thisObject;
432         }
433         Catch(Exception)
434         {
435                 LogError("error occured");
436         }
437         return JSValueMakeUndefined(context);
438 }
439
440 JSValueRef JSContactEmailAddressTypeArray::shift(JSContextRef context,
441                 JSObjectRef function,
442                 JSObjectRef thisObject,
443                 size_t argumentCount,
444                 const JSValueRef arguments[],
445                 JSValueRef* exception)
446 {
447         //LogDebug("entered");
448         Try
449         {
450                 ContactConverterFactory::ConverterType converter =
451                                                 ContactConverterFactory::getConverter(context);
452                 JSContactEmailAddressTypeArrayPriv* priv =
453                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
454                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
455                 if (currentStrings->size() > 0) {
456                         std::string result = converter->toContactEmailAddressTypeStr(currentStrings->at(0));
457                         currentStrings->erase(currentStrings->begin());
458                         return converter->toJSValueRef(result);
459                 }
460         }
461         Catch(Exception)
462         {
463                 LogError("error occured");
464         }
465         return JSValueMakeUndefined(context);
466 }
467
468 JSValueRef JSContactEmailAddressTypeArray::slice(JSContextRef context,
469                 JSObjectRef function,
470                 JSObjectRef thisObject,
471                 size_t argumentCount,
472                 const JSValueRef arguments[],
473                 JSValueRef* exception)
474 {
475         //LogDebug("enter");
476         Try
477         {
478                 if (argumentCount < 1) {
479                         return JSValueMakeUndefined(context);
480                 }
481                 BasicConverter converter = BasicConverterFactory::getConverter(context);
482                 ContactEmailAddressTypeArrayPtr typeArray = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
483                 JSContactEmailAddressTypeArrayPriv *newPrivateObject = new JSContactEmailAddressTypeArrayPriv(
484                                 context,
485                                 typeArray);
486                 JSValueRef result = JSObjectMake(context,
487                                                                                  getClassRef(), newPrivateObject);
488
489                 //copy current typeArray
490                 JSContactEmailAddressTypeArrayPriv* priv =
491                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
492                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
493                 std::size_t first = converter->toSizeT(arguments[0]);
494                 std::size_t last = currentStrings->size() - 1;
495                 if (argumentCount > 1) {
496                         last = converter->toSizeT(arguments[1]);
497                         if (last >= currentStrings->size()) {
498                                 last = currentStrings->size() - 1;
499                         }
500                 }
501                 if (first < 0) {
502                         first = 0;
503                 }
504                 for (size_t i = first; i <= last; ++i) {
505                         typeArray->push_back(currentStrings->at(i));
506                 }
507
508                 return result;
509         }
510         Catch(Exception)
511         {
512                 LogError("error occured");
513         }
514         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
515 }
516
517 JSValueRef JSContactEmailAddressTypeArray::sort(JSContextRef context,
518                 JSObjectRef function,
519                 JSObjectRef thisObject,
520                 size_t argumentCount,
521                 const JSValueRef arguments[],
522                 JSValueRef* exception)
523 {
524         //LogDebug("entered");
525         Try
526         {
527                 BasicConverter converter = BasicConverterFactory::getConverter(context);
528                 JSContactEmailAddressTypeArrayPriv* priv =
529                         static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
530                 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
531                 std::sort(currentStrings->begin(), currentStrings->end());
532                 return thisObject;
533         }
534         Catch(Exception)
535         {
536                 LogError("error occured");
537         }
538         return JSValueMakeUndefined(context);
539 }
540
541 JSValueRef JSContactEmailAddressTypeArray::splice(JSContextRef context,
542                 JSObjectRef function,
543                 JSObjectRef thisObject,
544                 size_t argumentCount,
545                 const JSValueRef arguments[],
546                 JSValueRef* exception)
547 {
548         //LogDebug("entered");
549         return JSValueMakeUndefined(context);
550 }
551
552 JSValueRef JSContactEmailAddressTypeArray::toString(JSContextRef context,
553                 JSObjectRef function,
554                 JSObjectRef thisObject,
555                 size_t argumentCount,
556                 const JSValueRef arguments[],
557                 JSValueRef* exception)
558 {
559         //LogDebug("entered");
560         return join(context, function, thisObject, 0, arguments, exception);
561 }
562
563 JSValueRef JSContactEmailAddressTypeArray::unshift(JSContextRef context,
564                 JSObjectRef function,
565                 JSObjectRef thisObject,
566                 size_t argumentCount,
567                 const JSValueRef arguments[],
568                 JSValueRef* exception)
569 {
570         //LogDebug("entered");
571         return JSValueMakeUndefined(context);
572 }
573
574 JSValueRef JSContactEmailAddressTypeArray::valueOf(JSContextRef context,
575                 JSObjectRef function,
576                 JSObjectRef thisObject,
577                 size_t argumentCount,
578                 const JSValueRef arguments[],
579                 JSValueRef* exception)
580 {
581         //LogDebug("entered");
582         return JSValueMakeUndefined(context);
583 }
584
585 } // Contact
586 } // Tizen1_0
587 } // TizenApis