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