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