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