2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file JSAddressBookArray.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
26 #include <CommonsJavaScript/ScopedJSStringRef.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSAddressBookArray.h"
33 #define FUNCTION_CONCAT "concat"
34 #define FUNCTION_JOIN "join"
35 #define FUNCTION_POP "pop"
36 #define FUNCTION_PUSH "push"
37 #define FUNCTION_REVERSE "reverse"
38 #define FUNCTION_SHIFT "shift"
39 #define FUNCTION_SLICE "slice"
40 #define FUNCTION_SORT "sort"
41 #define FUNCTION_SPLICE "splice"
42 #define FUNCTION_TOSTRING "toString"
43 #define FUNCTION_UNSHIFT "unshift"
44 #define FUNCTION_VALUEOF "valueOf"
46 #define ATTRIBUTE_LENGTH "length"
51 using namespace DeviceAPI::Common;
52 using namespace WrtDeviceApis::CommonsJavaScript;
54 JSClassDefinition JSAddressBookArray::m_classInfo = {
56 kJSClassAttributeNone,
68 NULL, //callAsFunction,
69 NULL, //callAsConstructor,
71 NULL, //convertToType,
74 JSStaticValue JSAddressBookArray::m_property[] = {
75 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
79 JSStaticFunction JSAddressBookArray::m_function[] = {
80 { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
81 { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
82 { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
83 { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
84 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
85 { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
86 { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
87 { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
88 { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
89 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
90 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
91 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
95 JSClassRef JSAddressBookArray::m_jsClassRef = JSClassCreate(
96 JSAddressBookArray::getClassInfo());
98 JSValueRef JSAddressBookArray::getLength(JSContextRef context,
100 JSStringRef propertyName,
101 JSValueRef* exception)
105 JSAddressBookArrayPriv* priv =
106 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 AddressBookArrayPtr addressBooks = priv->getObject();
112 ContactConverterFactory::ConverterType converter =
113 ContactConverterFactory::getConverter(context);
114 return converter->toJSValueRef(addressBooks->size());
117 Catch(WrtDeviceApis::Commons::Exception)
119 LoggerE("invalid conversion");
121 return JSValueMakeUndefined(context);
124 JSObjectRef JSAddressBookArray::createArray(JSContextRef context,
125 const AddressBookArrayPtr &addressBooks)
127 JSAddressBookArrayPriv *priv = new JSAddressBookArrayPriv(context, addressBooks);
128 return JSObjectMake(context, getClassRef(), priv);
131 const JSClassDefinition* JSAddressBookArray::getClassInfo()
133 return &(m_classInfo);
136 bool JSAddressBookArray::isObjectOfClass(JSContextRef context, JSValueRef value)
138 return JSValueIsObjectOfClass(context, value, getClassRef());
141 AddressBookArrayPtr JSAddressBookArray::getAddressBookArray(JSContextRef context, JSValueRef value)
143 if (!isObjectOfClass(context, value)) {
144 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
146 JSObjectRef object = JSValueToObject(context, value, NULL);
148 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
150 JSAddressBookArrayPriv *priv = static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
152 Throw(WrtDeviceApis::Commons::NullPointerException);
154 return priv->getObject();
157 JSClassRef JSAddressBookArray::getClassRef()
160 m_jsClassRef = JSClassCreate(&m_classInfo);
165 void JSAddressBookArray::initialize(JSContextRef context,
170 void JSAddressBookArray::finalize(JSObjectRef object)
172 JSAddressBookArrayPriv* priv =
173 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
175 JSObjectSetPrivate(object, NULL);
178 bool JSAddressBookArray::hasProperty(JSContextRef context,
180 JSStringRef propertyName)
182 ContactConverterFactory::ConverterType converter =
183 ContactConverterFactory::getConverter(context);
186 size_t index = converter->toSizeT(propertyName);
187 JSAddressBookArrayPriv* priv =
188 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
190 Throw(WrtDeviceApis::Commons::NullPointerException);
192 AddressBookArrayPtr addressBooks = priv->getObject();
193 if (index < addressBooks->size()) {
197 Catch(WrtDeviceApis::Commons::Exception)
199 //not reporting error is intended
204 JSValueRef JSAddressBookArray::getProperty(JSContextRef context,
206 JSStringRef propertyName,
207 JSValueRef* exception)
209 ContactConverterFactory::ConverterType converter =
210 ContactConverterFactory::getConverter(context);
213 size_t index = converter->toSizeT(propertyName);
214 JSAddressBookArrayPriv* priv =
215 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
217 Throw(WrtDeviceApis::Commons::NullPointerException);
219 AddressBookArrayPtr addressBooks = priv->getObject();
220 if (index < addressBooks->size()) {
221 AddressBookPtr result = addressBooks->at(index);
223 return converter->toJSValueRef(result);
227 Catch(WrtDeviceApis::Commons::Exception)
229 LoggerE("invalid property");
231 return JSValueMakeUndefined(context);
234 bool JSAddressBookArray::setProperty(JSContextRef context,
236 JSStringRef propertyName,
238 JSValueRef* exception)
240 ContactConverterFactory::ConverterType converter =
241 ContactConverterFactory::getConverter(context);
244 size_t index = converter->toSizeT(propertyName);
245 AddressBookPtr addressBook(NULL);
246 if (!JSValueIsUndefined(context, value)) {
247 addressBook = converter->toAddressBook(value);
249 JSAddressBookArrayPriv* priv =
250 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
252 Throw(WrtDeviceApis::Commons::NullPointerException);
254 AddressBookArrayPtr addressBooks = priv->getObject();
256 Throw(WrtDeviceApis::Commons::NullPointerException);
258 if (addressBooks->size() <= index) {
259 addressBooks->resize(index + 1);
261 (*addressBooks)[index] = addressBook;
264 Catch(WrtDeviceApis::Commons::Exception)
266 LoggerE("error occured");
267 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
272 bool JSAddressBookArray::deleteProperty(JSContextRef context,
274 JSStringRef propertyName,
275 JSValueRef* exception)
277 ContactConverterFactory::ConverterType converter =
278 ContactConverterFactory::getConverter(context);
281 size_t index = converter->toSizeT(propertyName);
282 AddressBookPtr addressBook(NULL);
283 JSAddressBookArrayPriv* priv =
284 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
286 Throw(WrtDeviceApis::Commons::NullPointerException);
288 AddressBookArrayPtr addressBooks = priv->getObject();
290 Throw(WrtDeviceApis::Commons::NullPointerException);
292 if (addressBooks->size() > index) {
293 (*addressBooks)[index] = addressBook;
297 Catch(WrtDeviceApis::Commons::Exception)
299 LoggerE("error occured");
300 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
305 void JSAddressBookArray::getPropertyNames(JSContextRef context,
307 JSPropertyNameAccumulatorRef propertyNames)
309 ContactConverterFactory::ConverterType converter =
310 ContactConverterFactory::getConverter(context);
313 JSAddressBookArrayPriv* priv =
314 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(object));
316 Throw(WrtDeviceApis::Commons::NullPointerException);
318 AddressBookArrayPtr addressBooks = priv->getObject();
320 int count = addressBooks->size();
322 for(int i=0; i < count; i++)
324 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
325 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
328 Catch(WrtDeviceApis::Commons::Exception)
330 LoggerE("invalid property");
334 JSValueRef JSAddressBookArray::concat(JSContextRef context,
335 JSObjectRef function,
336 JSObjectRef thisObject,
337 size_t argumentCount,
338 const JSValueRef arguments[],
339 JSValueRef* exception)
343 AddressBookArrayPtr addressBooks = AddressBookArrayPtr(new AddressBookArray());
344 JSAddressBookArrayPriv *newPrivateObject = new JSAddressBookArrayPriv(context, addressBooks);
345 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
347 //copy current addressBooks
348 JSAddressBookArrayPriv* priv =
349 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
350 AddressBookArrayPtr currentAddressBooks = priv->getObject();
351 for (size_t i = 0; i < currentAddressBooks->size(); ++i) {
352 addressBooks->push_back(currentAddressBooks->at(i));
355 //copy submitted arrays
356 ContactConverterFactory::ConverterType converter =
357 ContactConverterFactory::getConverter(context);
358 for (size_t i = 0; i < argumentCount; ++i) {
359 if (!JSIsArrayValue(context, arguments[i])) {
360 Throw(WrtDeviceApis::Commons::ConversionException);
362 // process array of strings
363 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
364 unsigned int len = JSGetArrayLength(context, arrayObj);
365 for (unsigned int e = 0; e < len; ++e) {
366 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
367 addressBooks->push_back(converter->toAddressBook(att));
372 Catch(WrtDeviceApis::Commons::Exception)
374 LoggerE("error occured");
376 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
379 JSValueRef JSAddressBookArray::join(JSContextRef context,
380 JSObjectRef function,
381 JSObjectRef thisObject,
382 size_t argumentCount,
383 const JSValueRef arguments[],
384 JSValueRef* exception)
389 std::string separator(",");
390 ContactConverterFactory::ConverterType converter =
391 ContactConverterFactory::getConverter(context);
392 JSAddressBookArrayPriv* priv =
393 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
394 AddressBookArrayPtr currentAddressBooks = priv->getObject();
395 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
396 separator = converter->toString(arguments[0]);
398 for (size_t i = 0; i < currentAddressBooks->size(); ++i) {
402 //FIXME : to be changed to support join
403 //result += currentAddressBooks->at(i);
405 return converter->toJSValueRef(result);
407 Catch(WrtDeviceApis::Commons::Exception)
409 LoggerE("error occured");
411 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
414 JSValueRef JSAddressBookArray::pop(JSContextRef context,
415 JSObjectRef function,
416 JSObjectRef thisObject,
417 size_t argumentCount,
418 const JSValueRef arguments[],
419 JSValueRef* exception)
423 ContactConverterFactory::ConverterType converter =
424 ContactConverterFactory::getConverter(context);
425 JSAddressBookArrayPriv* priv =
426 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
427 AddressBookArrayPtr currentAddressBooks = priv->getObject();
428 if (currentAddressBooks->size() > 0) {
429 AddressBookPtr result = currentAddressBooks->at(
430 currentAddressBooks->size() - 1);
431 currentAddressBooks->pop_back();
432 return converter->toJSValueRef(result);
435 Catch(WrtDeviceApis::Commons::Exception)
437 LoggerE("error occured");
439 return JSValueMakeUndefined(context);
442 JSValueRef JSAddressBookArray::push(JSContextRef context,
443 JSObjectRef function,
444 JSObjectRef thisObject,
445 size_t argumentCount,
446 const JSValueRef arguments[],
447 JSValueRef* exception)
451 ContactConverterFactory::ConverterType converter =
452 ContactConverterFactory::getConverter(context);
453 JSAddressBookArrayPriv* priv =
454 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
455 AddressBookArrayPtr currentAddressBooks = priv->getObject();
456 for (size_t i = 0; i < argumentCount; ++i) {
457 currentAddressBooks->push_back(converter->toAddressBook(arguments[i]));
459 return converter->toJSValueRef(currentAddressBooks->size());
461 Catch(WrtDeviceApis::Commons::Exception)
463 LoggerE("error occured");
465 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
468 JSValueRef JSAddressBookArray::reverse(JSContextRef context,
469 JSObjectRef function,
470 JSObjectRef thisObject,
471 size_t argumentCount,
472 const JSValueRef arguments[],
473 JSValueRef* exception)
477 ContactConverterFactory::ConverterType converter =
478 ContactConverterFactory::getConverter(context);
479 JSAddressBookArrayPriv* priv =
480 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
481 AddressBookArrayPtr currentAddressBooks = priv->getObject();
482 std::reverse(currentAddressBooks->begin(), currentAddressBooks->end());
485 Catch(WrtDeviceApis::Commons::Exception)
487 LoggerE("error occured");
489 return JSValueMakeUndefined(context);
492 JSValueRef JSAddressBookArray::shift(JSContextRef context,
493 JSObjectRef function,
494 JSObjectRef thisObject,
495 size_t argumentCount,
496 const JSValueRef arguments[],
497 JSValueRef* exception)
501 ContactConverterFactory::ConverterType converter =
502 ContactConverterFactory::getConverter(context);
503 JSAddressBookArrayPriv* priv =
504 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
505 AddressBookArrayPtr currentAddressBooks = priv->getObject();
506 if (currentAddressBooks->size() > 0) {
507 AddressBookPtr result = currentAddressBooks->at(0);
508 currentAddressBooks->erase(currentAddressBooks->begin());
509 return converter->toJSValueRef(result);
512 Catch(WrtDeviceApis::Commons::Exception)
514 LoggerE("error occured");
516 return JSValueMakeUndefined(context);
519 JSValueRef JSAddressBookArray::slice(JSContextRef context,
520 JSObjectRef function,
521 JSObjectRef thisObject,
522 size_t argumentCount,
523 const JSValueRef arguments[],
524 JSValueRef* exception)
528 if (argumentCount < 1) {
529 return JSValueMakeUndefined(context);
531 ContactConverterFactory::ConverterType converter =
532 ContactConverterFactory::getConverter(context);
533 AddressBookArrayPtr addressBooks = AddressBookArrayPtr(new AddressBookArray());
534 JSAddressBookArrayPriv *newPrivateObject = new JSAddressBookArrayPriv(
537 JSValueRef result = JSObjectMake(context,
538 getClassRef(), newPrivateObject);
540 //copy current addressBooks
541 JSAddressBookArrayPriv* priv =
542 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
543 AddressBookArrayPtr currentAddressBooks = priv->getObject();
544 std::size_t first = converter->toSizeT(arguments[0]);
545 std::size_t last = currentAddressBooks->size() - 1;
546 if (argumentCount > 1) {
547 last = converter->toSizeT(arguments[1]);
548 if (last >= currentAddressBooks->size()) {
549 last = currentAddressBooks->size() - 1;
552 for (size_t i = first; i <= last; ++i) {
553 addressBooks->push_back(currentAddressBooks->at(i));
558 Catch(WrtDeviceApis::Commons::Exception)
560 LoggerE("error occured");
562 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
565 JSValueRef JSAddressBookArray::sort(JSContextRef context,
566 JSObjectRef function,
567 JSObjectRef thisObject,
568 size_t argumentCount,
569 const JSValueRef arguments[],
570 JSValueRef* exception)
574 ContactConverterFactory::ConverterType converter =
575 ContactConverterFactory::getConverter(context);
576 JSAddressBookArrayPriv* priv =
577 static_cast<JSAddressBookArrayPriv*>(JSObjectGetPrivate(thisObject));
578 AddressBookArrayPtr currentAddressBooks = priv->getObject();
579 std::sort(currentAddressBooks->begin(), currentAddressBooks->end());
582 Catch(WrtDeviceApis::Commons::Exception)
584 LoggerE("error occured");
586 return JSValueMakeUndefined(context);
589 JSValueRef JSAddressBookArray::splice(JSContextRef context,
590 JSObjectRef function,
591 JSObjectRef thisObject,
592 size_t argumentCount,
593 const JSValueRef arguments[],
594 JSValueRef* exception)
596 return JSValueMakeUndefined(context);
599 JSValueRef JSAddressBookArray::toString(JSContextRef context,
600 JSObjectRef function,
601 JSObjectRef thisObject,
602 size_t argumentCount,
603 const JSValueRef arguments[],
604 JSValueRef* exception)
606 return join(context, function, thisObject, 0, arguments, exception);
609 JSValueRef JSAddressBookArray::unshift(JSContextRef context,
610 JSObjectRef function,
611 JSObjectRef thisObject,
612 size_t argumentCount,
613 const JSValueRef arguments[],
614 JSValueRef* exception)
616 return JSValueMakeUndefined(context);
619 JSValueRef JSAddressBookArray::valueOf(JSContextRef context,
620 JSObjectRef function,
621 JSObjectRef thisObject,
622 size_t argumentCount,
623 const JSValueRef arguments[],
624 JSValueRef* exception)
626 return JSValueMakeUndefined(context);