Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactPhoneNumberTypeArray.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        JSContactPhoneNumberTypeArray.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 "JSContactPhoneNumberTypeArray.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 JSContactPhoneNumberTypeArray::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 JSContactPhoneNumberTypeArray::m_property[] = {
75         { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
76         { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSContactPhoneNumberTypeArray::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 JSContactPhoneNumberTypeArray::m_jsClassRef = JSClassCreate(
96                 JSContactPhoneNumberTypeArray::getClassInfo());
97
98 JSValueRef JSContactPhoneNumberTypeArray::getLength(JSContextRef context,
99                 JSObjectRef object,
100                 JSStringRef propertyName,
101                 JSValueRef* exception)
102 {
103         //LogDebug("enter");
104         Try
105         {
106                 JSContactPhoneNumberTypeArrayPriv* priv =
107                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
108                 if (!priv) {
109                         Throw(WrtDeviceApis::Commons::NullPointerException);
110                 }
111                 ContactPhoneNumberTypeArrayPtr 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 JSContactPhoneNumberTypeArray::createArray(JSContextRef context,
126                 const ContactPhoneNumberTypeArrayPtr &typeArray)
127 {
128         JSContactPhoneNumberTypeArrayPriv *priv = new JSContactPhoneNumberTypeArrayPriv(context, typeArray);
129         return JSObjectMake(context, getClassRef(), priv);
130 }
131
132 const JSClassDefinition* JSContactPhoneNumberTypeArray::getClassInfo()
133 {
134         return &(m_classInfo);
135 }
136
137 JSClassRef JSContactPhoneNumberTypeArray::getClassRef()
138 {
139         if (!m_jsClassRef) {
140                 m_jsClassRef = JSClassCreate(&m_classInfo);
141         }
142         return m_jsClassRef;
143 }
144
145 bool JSContactPhoneNumberTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
146 {
147         return JSValueIsObjectOfClass(context, value, getClassRef());
148 }
149
150 ContactPhoneNumberTypeArrayPtr JSContactPhoneNumberTypeArray::getContactPhoneNumberTypeArray(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         JSContactPhoneNumberTypeArrayPriv *priv = static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
160         if (!priv) {
161                 Throw(WrtDeviceApis::Commons::NullPointerException);
162         }
163         return priv->getObject();
164 }
165
166 void JSContactPhoneNumberTypeArray::initialize(JSContextRef context,
167                 JSObjectRef object)
168 {
169         //LogDebug("enter");
170 }
171
172 void JSContactPhoneNumberTypeArray::finalize(JSObjectRef object)
173 {
174         //LogDebug("enter");
175         JSContactPhoneNumberTypeArrayPriv* priv =
176                 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
177         delete priv;
178         JSObjectSetPrivate(object, NULL);
179 }
180
181 bool JSContactPhoneNumberTypeArray::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                 JSContactPhoneNumberTypeArrayPriv* priv =
192                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
193                 if (!priv) {
194                         Throw(WrtDeviceApis::Commons::NullPointerException);
195                 }
196                 ContactPhoneNumberTypeArrayPtr 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 JSContactPhoneNumberTypeArray::getProperty(JSContextRef context,
209                 JSObjectRef object,
210                 JSStringRef propertyName,
211                 JSValueRef* exception)
212 {
213         //LogDebug("enter");
214         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
215         Try
216         {
217                 size_t index = converter->toSizeT(propertyName);
218                 JSContactPhoneNumberTypeArrayPriv* priv =
219                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
220                 if (!priv) {
221                         Throw(WrtDeviceApis::Commons::NullPointerException);
222                 }
223                 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
224                 if (index < typeArray->size()) {
225                         std::string result = converter->toContactPhoneNumberTypeStr(typeArray->at(index));
226                         if (!result.empty()) {
227                                 return converter->toJSValueRef(result);
228                         }
229                 }
230         }
231         Catch(WrtDeviceApis::Commons::Exception)
232         {
233                 LogError("invalid property");
234         }
235         return JSValueMakeUndefined(context);
236 }
237
238 bool JSContactPhoneNumberTypeArray::setProperty(JSContextRef context,
239                 JSObjectRef object,
240                 JSStringRef propertyName,
241                 JSValueRef value,
242                 JSValueRef* exception)
243 {
244         //LogDebug("enter");
245         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
246         Try
247         {
248                 size_t index = converter->toSizeT(propertyName);
249                 std::string str;
250                 if (!JSValueIsUndefined(context, value)) {
251                         str = converter->toString(value);
252                 }
253                 JSContactPhoneNumberTypeArrayPriv* priv =
254                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
255                 if (!priv) {
256                         Throw(WrtDeviceApis::Commons::NullPointerException);
257                 }
258                 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
259                 if (!typeArray) {
260                         Throw(WrtDeviceApis::Commons::NullPointerException);
261                 }
262                 if (typeArray->size() <= index) {
263                         typeArray->resize(index + 1);
264                 }
265                 (*typeArray)[index] = converter->toContactPhoneNumberType(str);
266                 return true;
267         }
268         Catch(WrtDeviceApis::Commons::Exception)
269         {
270                 LogError("error occured");
271                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
272         }
273         return false;
274 }
275
276 JSValueRef JSContactPhoneNumberTypeArray::concat(JSContextRef context,
277                 JSObjectRef function,
278                 JSObjectRef thisObject,
279                 size_t argumentCount,
280                 const JSValueRef arguments[],
281                 JSValueRef* exception)
282 {
283         //LogDebug("enter");
284         Try
285         {
286                 ContactPhoneNumberTypeArrayPtr typeArray = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
287                 JSContactPhoneNumberTypeArrayPriv *newPrivateObject = new JSContactPhoneNumberTypeArrayPriv(
288                                 context,
289                                 typeArray);
290                 JSValueRef result = JSObjectMake(context,
291                                                                                  getClassRef(), newPrivateObject);
292
293                 //copy current typeArray
294                 JSContactPhoneNumberTypeArrayPriv* priv =
295                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
296                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
297                 for (size_t i = 0; i < currentStrings->size(); ++i) {
298                         typeArray->push_back(currentStrings->at(i));
299                 }
300
301                 //copy submitted arrays
302                 ContactConverterFactory::ConverterType converter =
303                                                 ContactConverterFactory::getConverter(context);
304                 for (size_t i = 0; i < argumentCount; ++i) {
305                         if (!JSIsArrayValue(context, arguments[i])) {
306                                 Throw(WrtDeviceApis::Commons::ConversionException);
307                         }
308                         // process array of strings
309                         JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
310                         unsigned int len = JSGetArrayLength(context, arrayObj);
311                         for (unsigned int e = 0; e < len; ++e) {
312                                 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
313                                 typeArray->push_back(converter->toContactPhoneNumberType(att));
314                         }
315                 }
316                 return result;
317         }
318         Catch(WrtDeviceApis::Commons::Exception)
319         {
320                 LogError("error occured");
321         }
322         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
323 }
324
325 JSValueRef JSContactPhoneNumberTypeArray::join(JSContextRef context,
326                 JSObjectRef function,
327                 JSObjectRef thisObject,
328                 size_t argumentCount,
329                 const JSValueRef arguments[],
330                 JSValueRef* exception)
331 {
332         //LogDebug("entered");
333         Try
334         {
335                 std::string result;
336                 std::string separator(",");
337                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
338                 JSContactPhoneNumberTypeArrayPriv* priv =
339                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
340                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
341                 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
342                         separator = converter->toString(arguments[0]);
343                 }
344                 for (size_t i = 0; i < currentStrings->size(); ++i) {
345                         if (i != 0) {
346                                 result += separator;
347                         }
348                         result += currentStrings->at(i);
349                 }
350                 return converter->toJSValueRef(result);
351         }
352         Catch(WrtDeviceApis::Commons::Exception)
353         {
354                 LogError("error occured");
355         }
356         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
357 }
358
359 JSValueRef JSContactPhoneNumberTypeArray::pop(JSContextRef context,
360                 JSObjectRef function,
361                 JSObjectRef thisObject,
362                 size_t argumentCount,
363                 const JSValueRef arguments[],
364                 JSValueRef* exception)
365 {
366         //LogDebug("entered");
367         Try
368         {
369                 ContactConverterFactory::ConverterType converter =
370                                                 ContactConverterFactory::getConverter(context);
371                 JSContactPhoneNumberTypeArrayPriv* priv =
372                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
373                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
374                 if (currentStrings->size() > 0) {
375                         std::string result =
376                                         converter->toContactPhoneNumberTypeStr(currentStrings->at(currentStrings->size() - 1));
377                         currentStrings->pop_back();
378                         return converter->toJSValueRef(result);
379                 }
380         }
381         Catch(WrtDeviceApis::Commons::Exception)
382         {
383                 LogError("error occured");
384         }
385         return JSValueMakeUndefined(context);
386 }
387
388 JSValueRef JSContactPhoneNumberTypeArray::push(JSContextRef context,
389                 JSObjectRef function,
390                 JSObjectRef thisObject,
391                 size_t argumentCount,
392                 const JSValueRef arguments[],
393                 JSValueRef* exception)
394 {
395         //LogDebug("entered");
396         Try
397         {
398                 ContactConverterFactory::ConverterType converter =
399                                                 ContactConverterFactory::getConverter(context);
400                 JSContactPhoneNumberTypeArrayPriv* priv =
401                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
402                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
403                 for (size_t i = 0; i < argumentCount; ++i) {
404                         currentStrings->push_back(converter->toContactPhoneNumberType(arguments[i]));
405                 }
406                 return converter->toJSValueRef(currentStrings->size());
407         }
408         Catch(WrtDeviceApis::Commons::Exception)
409         {
410                 LogError("error occured");
411         }
412         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
413 }
414
415 JSValueRef JSContactPhoneNumberTypeArray::reverse(JSContextRef context,
416                 JSObjectRef function,
417                 JSObjectRef thisObject,
418                 size_t argumentCount,
419                 const JSValueRef arguments[],
420                 JSValueRef* exception)
421 {
422         //LogDebug("entered");
423         Try
424         {
425                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
426                 JSContactPhoneNumberTypeArrayPriv* priv =
427                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
428                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
429                 std::reverse(currentStrings->begin(), currentStrings->end());
430                 return thisObject;
431         }
432         Catch(WrtDeviceApis::Commons::Exception)
433         {
434                 LogError("error occured");
435         }
436         return JSValueMakeUndefined(context);
437 }
438
439 JSValueRef JSContactPhoneNumberTypeArray::shift(JSContextRef context,
440                 JSObjectRef function,
441                 JSObjectRef thisObject,
442                 size_t argumentCount,
443                 const JSValueRef arguments[],
444                 JSValueRef* exception)
445 {
446         //LogDebug("entered");
447         Try
448         {
449                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
450                 JSContactPhoneNumberTypeArrayPriv* priv =
451                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
452                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
453                 if (currentStrings->size() > 0) {
454                         std::string result = converter->toContactPhoneNumberTypeStr(currentStrings->at(0));
455                         currentStrings->erase(currentStrings->begin());
456                         return converter->toJSValueRef(result);
457                 }
458         }
459         Catch(WrtDeviceApis::Commons::Exception)
460         {
461                 LogError("error occured");
462         }
463         return JSValueMakeUndefined(context);
464 }
465
466 JSValueRef JSContactPhoneNumberTypeArray::slice(JSContextRef context,
467                 JSObjectRef function,
468                 JSObjectRef thisObject,
469                 size_t argumentCount,
470                 const JSValueRef arguments[],
471                 JSValueRef* exception)
472 {
473         //LogDebug("enter");
474         Try
475         {
476                 if (argumentCount < 1) {
477                         return JSValueMakeUndefined(context);
478                 }
479                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
480                 ContactPhoneNumberTypeArrayPtr typeArray = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
481                 JSContactPhoneNumberTypeArrayPriv *newPrivateObject = new JSContactPhoneNumberTypeArrayPriv(
482                                 context,
483                                 typeArray);
484                 JSValueRef result = JSObjectMake(context,
485                                                                                  getClassRef(), newPrivateObject);
486
487                 //copy current typeArray
488                 JSContactPhoneNumberTypeArrayPriv* priv =
489                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
490                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
491                 std::size_t first = converter->toSizeT(arguments[0]);
492                 std::size_t last = currentStrings->size() - 1;
493                 if (argumentCount > 1) {
494                         last = converter->toSizeT(arguments[1]);
495                         if (last >= currentStrings->size()) {
496                                 last = currentStrings->size() - 1;
497                         }
498                 }
499                 if (first < 0) {
500                         first = 0;
501                 }
502                 for (size_t i = first; i <= last; ++i) {
503                         typeArray->push_back(currentStrings->at(i));
504                 }
505
506                 return result;
507         }
508         Catch(WrtDeviceApis::Commons::Exception)
509         {
510                 LogError("error occured");
511         }
512         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
513 }
514
515 JSValueRef JSContactPhoneNumberTypeArray::sort(JSContextRef context,
516                 JSObjectRef function,
517                 JSObjectRef thisObject,
518                 size_t argumentCount,
519                 const JSValueRef arguments[],
520                 JSValueRef* exception)
521 {
522         //LogDebug("entered");
523         Try
524         {
525                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
526                 JSContactPhoneNumberTypeArrayPriv* priv =
527                         static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
528                 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
529                 std::sort(currentStrings->begin(), currentStrings->end());
530                 return thisObject;
531         }
532         Catch(WrtDeviceApis::Commons::Exception)
533         {
534                 LogError("error occured");
535         }
536         return JSValueMakeUndefined(context);
537 }
538
539 JSValueRef JSContactPhoneNumberTypeArray::splice(JSContextRef context,
540                 JSObjectRef function,
541                 JSObjectRef thisObject,
542                 size_t argumentCount,
543                 const JSValueRef arguments[],
544                 JSValueRef* exception)
545 {
546         //LogDebug("entered");
547         return JSValueMakeUndefined(context);
548 }
549
550 JSValueRef JSContactPhoneNumberTypeArray::toString(JSContextRef context,
551                 JSObjectRef function,
552                 JSObjectRef thisObject,
553                 size_t argumentCount,
554                 const JSValueRef arguments[],
555                 JSValueRef* exception)
556 {
557         //LogDebug("entered");
558         return join(context, function, thisObject, 0, arguments, exception);
559 }
560
561 JSValueRef JSContactPhoneNumberTypeArray::unshift(JSContextRef context,
562                 JSObjectRef function,
563                 JSObjectRef thisObject,
564                 size_t argumentCount,
565                 const JSValueRef arguments[],
566                 JSValueRef* exception)
567 {
568         //LogDebug("entered");
569         return JSValueMakeUndefined(context);
570 }
571
572 JSValueRef JSContactPhoneNumberTypeArray::valueOf(JSContextRef context,
573                 JSObjectRef function,
574                 JSObjectRef thisObject,
575                 size_t argumentCount,
576                 const JSValueRef arguments[],
577                 JSValueRef* exception)
578 {
579         //LogDebug("entered");
580         return JSValueMakeUndefined(context);
581 }
582
583 } // Contact
584 } // Tizen1_0
585 } // TizenApis