2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file JSContactAnniversaryArray.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
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"
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"
44 #define ATTRIBUTE_LENGTH "length"
50 using namespace TizenApis::Commons;
51 using namespace TizenApis::Api::Contact;
53 JSClassDefinition JSContactAnniversaryArray::m_classInfo = {
55 kJSClassAttributeNone,
65 NULL, //deleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //callAsFunction,
68 NULL, //callAsConstructor,
70 NULL, //convertToType,
73 JSStaticValue JSContactAnniversaryArray::m_property[] = {
74 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
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 },
94 JSClassRef JSContactAnniversaryArray::m_jsClassRef = JSClassCreate(
95 JSContactAnniversaryArray::getClassInfo());
97 JSValueRef JSContactAnniversaryArray::getLength(JSContextRef context,
99 JSStringRef propertyName,
100 JSValueRef* exception)
104 JSContactAnniversaryArrayPriv* priv =
105 static_cast<JSContactAnniversaryArrayPriv*>(JSObjectGetPrivate(object));
107 Throw(WrtDeviceApis::Commons::NullPointerException);
109 ContactAnniversaryArrayPtr anniversaries = priv->getObject();
111 ContactConverterFactory::ConverterType converter =
112 ContactConverterFactory::getConverter(context);
113 return converter->toJSValueRef(anniversaries->size());
116 Catch(WrtDeviceApis::Commons::Exception)
118 LogError("invalid conversion");
120 return JSValueMakeUndefined(context);
123 JSObjectRef JSContactAnniversaryArray::createArray(JSContextRef context,
124 const ContactAnniversaryArrayPtr &anniversaries)
126 JSContactAnniversaryArrayPriv *priv = new JSContactAnniversaryArrayPriv(context, anniversaries);
127 return JSObjectMake(context, getClassRef(), priv);
130 const JSClassDefinition* JSContactAnniversaryArray::getClassInfo()
132 return &(m_classInfo);
135 bool JSContactAnniversaryArray::isObjectOfClass(JSContextRef context, JSValueRef value)
137 return JSValueIsObjectOfClass(context, value, getClassRef());
140 ContactAnniversaryArrayPtr JSContactAnniversaryArray::getContactAnniversaryArray(JSContextRef context, JSValueRef value)
142 if (!isObjectOfClass(context, value)) {
143 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
145 JSObjectRef object = JSValueToObject(context, value, NULL);
147 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
149 JSContactAnniversaryArrayPriv *priv = static_cast<JSContactAnniversaryArrayPriv*>(JSObjectGetPrivate(object));
151 Throw(WrtDeviceApis::Commons::NullPointerException);
153 return priv->getObject();
156 JSClassRef JSContactAnniversaryArray::getClassRef()
159 m_jsClassRef = JSClassCreate(&m_classInfo);
164 void JSContactAnniversaryArray::initialize(JSContextRef context,
169 void JSContactAnniversaryArray::finalize(JSObjectRef object)
171 JSContactAnniversaryArrayPriv* priv =
172 static_cast<JSContactAnniversaryArrayPriv*>(JSObjectGetPrivate(object));
174 JSObjectSetPrivate(object, NULL);
177 bool JSContactAnniversaryArray::hasProperty(JSContextRef context,
179 JSStringRef propertyName)
181 ContactConverterFactory::ConverterType converter =
182 ContactConverterFactory::getConverter(context);
185 size_t index = converter->toSizeT(propertyName);
186 JSContactAnniversaryArrayPriv* priv =
187 static_cast<JSContactAnniversaryArrayPriv*>(JSObjectGetPrivate(object));
189 Throw(WrtDeviceApis::Commons::NullPointerException);
191 ContactAnniversaryArrayPtr anniversaries = priv->getObject();
192 if (index < anniversaries->size()) {
196 Catch(WrtDeviceApis::Commons::Exception)
198 //not reporting error is intended
203 JSValueRef JSContactAnniversaryArray::getProperty(JSContextRef context,
205 JSStringRef propertyName,
206 JSValueRef* exception)
208 ContactConverterFactory::ConverterType converter =
209 ContactConverterFactory::getConverter(context);
212 size_t index = converter->toSizeT(propertyName);
213 JSContactAnniversaryArrayPriv* priv =
214 static_cast<JSContactAnniversaryArrayPriv*>(JSObjectGetPrivate(object));
216 Throw(WrtDeviceApis::Commons::NullPointerException);
218 ContactAnniversaryArrayPtr anniversaries = priv->getObject();
219 if (index < anniversaries->size()) {
220 ContactAnniversaryPtr result = anniversaries->at(index);
222 return converter->toJSValueRef(result);
226 Catch(WrtDeviceApis::Commons::Exception)
228 LogError("invalid property");
230 return JSValueMakeUndefined(context);
233 bool JSContactAnniversaryArray::setProperty(JSContextRef context,
235 JSStringRef propertyName,
237 JSValueRef* exception)
239 ContactConverterFactory::ConverterType converter =
240 ContactConverterFactory::getConverter(context);
243 size_t index = converter->toSizeT(propertyName);
244 ContactAnniversaryPtr anniversary;
245 if (!JSValueIsUndefined(context, value)) {
246 anniversary = converter->toContactAnniversary(value);
248 JSContactAnniversaryArrayPriv* priv =
249 static_cast<JSContactAnniversaryArrayPriv*>(JSObjectGetPrivate(object));
251 Throw(WrtDeviceApis::Commons::NullPointerException);
253 ContactAnniversaryArrayPtr anniversaries = priv->getObject();
254 if (!anniversaries) {
255 Throw(WrtDeviceApis::Commons::NullPointerException);
257 if (anniversaries->size() <= index) {
258 anniversaries->resize(index + 1);
260 (*anniversaries)[index] = anniversary;
263 Catch(WrtDeviceApis::Commons::Exception)
265 LogError("error occured");
266 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
271 JSValueRef JSContactAnniversaryArray::concat(JSContextRef context,
272 JSObjectRef function,
273 JSObjectRef thisObject,
274 size_t argumentCount,
275 const JSValueRef arguments[],
276 JSValueRef* exception)
280 ContactAnniversaryArrayPtr anniversaries = ContactAnniversaryArrayPtr(new ContactAnniversaryArray());
281 JSContactAnniversaryArrayPriv *newPrivateObject = new JSContactAnniversaryArrayPriv(context, anniversaries);
282 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
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));
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);
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));
309 Catch(WrtDeviceApis::Commons::Exception)
311 LogError("error occured");
313 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
316 JSValueRef JSContactAnniversaryArray::join(JSContextRef context,
317 JSObjectRef function,
318 JSObjectRef thisObject,
319 size_t argumentCount,
320 const JSValueRef arguments[],
321 JSValueRef* exception)
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]);
335 for (size_t i = 0; i < currentAnniversaries->size(); ++i) {
339 //FIXME : to be changed to support join
340 //result += currentAnniversaries->at(i);
342 return converter->toJSValueRef(result);
344 Catch(WrtDeviceApis::Commons::Exception)
346 LogError("error occured");
348 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
351 JSValueRef JSContactAnniversaryArray::pop(JSContextRef context,
352 JSObjectRef function,
353 JSObjectRef thisObject,
354 size_t argumentCount,
355 const JSValueRef arguments[],
356 JSValueRef* exception)
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);
372 Catch(WrtDeviceApis::Commons::Exception)
374 LogError("error occured");
376 return JSValueMakeUndefined(context);
379 JSValueRef JSContactAnniversaryArray::push(JSContextRef context,
380 JSObjectRef function,
381 JSObjectRef thisObject,
382 size_t argumentCount,
383 const JSValueRef arguments[],
384 JSValueRef* exception)
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]));
396 return converter->toJSValueRef(currentAnniversaries->size());
398 Catch(WrtDeviceApis::Commons::Exception)
400 LogError("error occured");
402 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
405 JSValueRef JSContactAnniversaryArray::reverse(JSContextRef context,
406 JSObjectRef function,
407 JSObjectRef thisObject,
408 size_t argumentCount,
409 const JSValueRef arguments[],
410 JSValueRef* exception)
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());
422 Catch(WrtDeviceApis::Commons::Exception)
424 LogError("error occured");
426 return JSValueMakeUndefined(context);
429 JSValueRef JSContactAnniversaryArray::shift(JSContextRef context,
430 JSObjectRef function,
431 JSObjectRef thisObject,
432 size_t argumentCount,
433 const JSValueRef arguments[],
434 JSValueRef* exception)
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);
449 Catch(WrtDeviceApis::Commons::Exception)
451 LogError("error occured");
453 return JSValueMakeUndefined(context);
456 JSValueRef JSContactAnniversaryArray::slice(JSContextRef context,
457 JSObjectRef function,
458 JSObjectRef thisObject,
459 size_t argumentCount,
460 const JSValueRef arguments[],
461 JSValueRef* exception)
465 if (argumentCount < 1) {
466 return JSValueMakeUndefined(context);
468 ContactConverterFactory::ConverterType converter =
469 ContactConverterFactory::getConverter(context);
470 ContactAnniversaryArrayPtr anniversaries = ContactAnniversaryArrayPtr(new ContactAnniversaryArray());
471 JSContactAnniversaryArrayPriv *newPrivateObject = new JSContactAnniversaryArrayPriv(
474 JSValueRef result = JSObjectMake(context,
475 getClassRef(), newPrivateObject);
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;
492 for (size_t i = first; i <= last; ++i) {
493 anniversaries->push_back(currentAnniversaries->at(i));
498 Catch(WrtDeviceApis::Commons::Exception)
500 LogError("error occured");
502 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
505 JSValueRef JSContactAnniversaryArray::sort(JSContextRef context,
506 JSObjectRef function,
507 JSObjectRef thisObject,
508 size_t argumentCount,
509 const JSValueRef arguments[],
510 JSValueRef* exception)
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());
522 Catch(WrtDeviceApis::Commons::Exception)
524 LogError("error occured");
526 return JSValueMakeUndefined(context);
529 JSValueRef JSContactAnniversaryArray::splice(JSContextRef context,
530 JSObjectRef function,
531 JSObjectRef thisObject,
532 size_t argumentCount,
533 const JSValueRef arguments[],
534 JSValueRef* exception)
536 return JSValueMakeUndefined(context);
539 JSValueRef JSContactAnniversaryArray::toString(JSContextRef context,
540 JSObjectRef function,
541 JSObjectRef thisObject,
542 size_t argumentCount,
543 const JSValueRef arguments[],
544 JSValueRef* exception)
546 return join(context, function, thisObject, 0, arguments, exception);
549 JSValueRef JSContactAnniversaryArray::unshift(JSContextRef context,
550 JSObjectRef function,
551 JSObjectRef thisObject,
552 size_t argumentCount,
553 const JSValueRef arguments[],
554 JSValueRef* exception)
556 return JSValueMakeUndefined(context);
559 JSValueRef JSContactAnniversaryArray::valueOf(JSContextRef context,
560 JSObjectRef function,
561 JSObjectRef thisObject,
562 size_t argumentCount,
563 const JSValueRef arguments[],
564 JSValueRef* exception)
566 return JSValueMakeUndefined(context);