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 JSContactAddressArray.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 "JSContactAddressArray.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 JSContactAddressArray::m_classInfo = {
56 kJSClassAttributeNone,
68 NULL, //callAsFunction,
69 NULL, //callAsConstructor,
71 NULL, //convertToType,
74 JSStaticValue JSContactAddressArray::m_property[] = {
75 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
79 JSStaticFunction JSContactAddressArray::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 JSContactAddressArray::m_jsClassRef = JSClassCreate(
96 JSContactAddressArray::getClassInfo());
98 JSValueRef JSContactAddressArray::getLength(JSContextRef context,
100 JSStringRef propertyName,
101 JSValueRef* exception)
105 JSContactAddressArrayPriv* priv =
106 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 ContactAddressArrayPtr contactAddresses = priv->getObject();
111 if (contactAddresses) {
112 ContactConverterFactory::ConverterType converter =
113 ContactConverterFactory::getConverter(context);
114 return converter->toJSValueRef(contactAddresses->size());
117 Catch(WrtDeviceApis::Commons::Exception)
119 LoggerE("invalid conversion");
121 return JSValueMakeUndefined(context);
124 JSObjectRef JSContactAddressArray::createArray(JSContextRef context,
125 const ContactAddressArrayPtr &contactAddresses)
127 JSContactAddressArrayPriv *priv = new JSContactAddressArrayPriv(context, contactAddresses);
128 return JSObjectMake(context, getClassRef(), priv);
131 const JSClassDefinition* JSContactAddressArray::getClassInfo()
133 return &(m_classInfo);
136 JSClassRef JSContactAddressArray::getClassRef()
139 m_jsClassRef = JSClassCreate(&m_classInfo);
144 bool JSContactAddressArray::isObjectOfClass(JSContextRef context, JSValueRef value)
146 return JSValueIsObjectOfClass(context, value, getClassRef());
149 ContactAddressArrayPtr JSContactAddressArray::getContactAddressArray(JSContextRef context, JSValueRef value)
151 if (!isObjectOfClass(context, value)) {
152 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
154 JSObjectRef object = JSValueToObject(context, value, NULL);
156 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
158 JSContactAddressArrayPriv *priv = static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
160 Throw(WrtDeviceApis::Commons::NullPointerException);
162 return priv->getObject();
165 void JSContactAddressArray::initialize(JSContextRef context,
170 void JSContactAddressArray::finalize(JSObjectRef object)
172 JSContactAddressArrayPriv* priv =
173 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
175 JSObjectSetPrivate(object, NULL);
178 bool JSContactAddressArray::hasProperty(JSContextRef context,
180 JSStringRef propertyName)
182 ContactConverterFactory::ConverterType converter =
183 ContactConverterFactory::getConverter(context);
186 size_t index = converter->toSizeT(propertyName);
187 JSContactAddressArrayPriv* priv =
188 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
190 Throw(WrtDeviceApis::Commons::NullPointerException);
192 ContactAddressArrayPtr contactAddresses = priv->getObject();
193 if (index < contactAddresses->size()) {
197 Catch(WrtDeviceApis::Commons::Exception)
199 //not reporting error is intended
204 JSValueRef JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
215 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
217 Throw(WrtDeviceApis::Commons::NullPointerException);
219 ContactAddressArrayPtr contactAddresses = priv->getObject();
220 if (index < contactAddresses->size()) {
221 ContactAddressPtr result = contactAddresses->at(index);
223 return converter->toJSValueRef(result);
227 Catch(WrtDeviceApis::Commons::Exception)
229 LoggerE("invalid property");
231 return JSValueMakeUndefined(context);
234 bool JSContactAddressArray::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 ContactAddressPtr contactAddress(NULL);
246 if (!JSValueIsUndefined(context, value)) {
247 contactAddress = converter->toContactAddress(value);
249 JSContactAddressArrayPriv* priv =
250 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
252 Throw(WrtDeviceApis::Commons::NullPointerException);
254 ContactAddressArrayPtr contactAddresses = priv->getObject();
255 if (!contactAddresses) {
256 Throw(WrtDeviceApis::Commons::NullPointerException);
258 if (contactAddresses->size() <= index) {
259 contactAddresses->resize(index + 1);
261 (*contactAddresses)[index] = contactAddress;
264 Catch(WrtDeviceApis::Commons::Exception)
266 LoggerE("error occured");
267 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
272 bool JSContactAddressArray::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 ContactAddressPtr contactAddress(NULL);
283 JSContactAddressArrayPriv* priv =
284 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
286 Throw(WrtDeviceApis::Commons::NullPointerException);
288 ContactAddressArrayPtr contactAddresses = priv->getObject();
289 if (!contactAddresses) {
290 Throw(WrtDeviceApis::Commons::NullPointerException);
292 if (contactAddresses->size() > index) {
293 (*contactAddresses)[index] = contactAddress;
297 Catch(WrtDeviceApis::Commons::Exception)
299 LoggerE("error occured");
300 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
305 void JSContactAddressArray::getPropertyNames(JSContextRef context,
307 JSPropertyNameAccumulatorRef propertyNames)
309 ContactConverterFactory::ConverterType converter =
310 ContactConverterFactory::getConverter(context);
313 JSContactAddressArrayPriv* priv =
314 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(object));
316 Throw(WrtDeviceApis::Commons::NullPointerException);
318 ContactAddressArrayPtr addresses = priv->getObject();
320 int count = addresses->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 JSContactAddressArray::concat(JSContextRef context,
335 JSObjectRef function,
336 JSObjectRef thisObject,
337 size_t argumentCount,
338 const JSValueRef arguments[],
339 JSValueRef* exception)
343 ContactAddressArrayPtr contactAddresses = ContactAddressArrayPtr(new ContactAddressArray());
344 JSContactAddressArrayPriv *newPrivateObject = new JSContactAddressArrayPriv(context, contactAddresses);
345 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
347 //copy current contactAddresses
348 JSContactAddressArrayPriv* priv =
349 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
350 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
351 for (size_t i = 0; i < currentContactAddresses->size(); ++i) {
352 contactAddresses->push_back(currentContactAddresses->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 contactAddresses->push_back(converter->toContactAddress(att));
372 Catch(WrtDeviceApis::Commons::Exception)
374 LoggerE("error occured");
376 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
379 JSValueRef JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
393 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
394 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
395 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
396 separator = converter->toString(arguments[0]);
398 for (size_t i = 0; i < currentContactAddresses->size(); ++i) {
402 //FIXME : to be changed to support join
403 //result += currentContactAddresses->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 JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
426 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
427 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
428 if (currentContactAddresses->size() > 0) {
429 ContactAddressPtr result = currentContactAddresses->at(
430 currentContactAddresses->size() - 1);
431 currentContactAddresses->pop_back();
432 return converter->toJSValueRef(result);
435 Catch(WrtDeviceApis::Commons::Exception)
437 LoggerE("error occured");
439 return JSValueMakeUndefined(context);
442 JSValueRef JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
454 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
455 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
456 for (size_t i = 0; i < argumentCount; ++i) {
457 currentContactAddresses->push_back(converter->toContactAddress(arguments[i]));
459 return converter->toJSValueRef(currentContactAddresses->size());
461 Catch(WrtDeviceApis::Commons::Exception)
463 LoggerE("error occured");
465 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
468 JSValueRef JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
480 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
481 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
482 std::reverse(currentContactAddresses->begin(), currentContactAddresses->end());
485 Catch(WrtDeviceApis::Commons::Exception)
487 LoggerE("error occured");
489 return JSValueMakeUndefined(context);
492 JSValueRef JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
504 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
505 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
506 if (currentContactAddresses->size() > 0) {
507 ContactAddressPtr result = currentContactAddresses->at(0);
508 currentContactAddresses->erase(currentContactAddresses->begin());
509 return converter->toJSValueRef(result);
512 Catch(WrtDeviceApis::Commons::Exception)
514 LoggerE("error occured");
516 return JSValueMakeUndefined(context);
519 JSValueRef JSContactAddressArray::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 ContactAddressArrayPtr contactAddresses = ContactAddressArrayPtr(new ContactAddressArray());
534 JSContactAddressArrayPriv *newPrivateObject = new JSContactAddressArrayPriv(
537 JSValueRef result = JSObjectMake(context,
538 getClassRef(), newPrivateObject);
540 //copy current contactAddresses
541 JSContactAddressArrayPriv* priv =
542 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
543 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
544 std::size_t first = converter->toSizeT(arguments[0]);
545 std::size_t last = currentContactAddresses->size() - 1;
546 if (argumentCount > 1) {
547 last = converter->toSizeT(arguments[1]);
548 if (last >= currentContactAddresses->size()) {
549 last = currentContactAddresses->size() - 1;
552 for (size_t i = first; i <= last; ++i) {
553 contactAddresses->push_back(currentContactAddresses->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 JSContactAddressArray::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 JSContactAddressArrayPriv* priv =
577 static_cast<JSContactAddressArrayPriv*>(JSObjectGetPrivate(thisObject));
578 ContactAddressArrayPtr currentContactAddresses = priv->getObject();
579 std::sort(currentContactAddresses->begin(), currentContactAddresses->end());
582 Catch(WrtDeviceApis::Commons::Exception)
584 LoggerE("error occured");
586 return JSValueMakeUndefined(context);
589 JSValueRef JSContactAddressArray::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 JSContactAddressArray::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 JSContactAddressArray::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 JSContactAddressArray::valueOf(JSContextRef context,
620 JSObjectRef function,
621 JSObjectRef thisObject,
622 size_t argumentCount,
623 const JSValueRef arguments[],
624 JSValueRef* exception)
626 return JSValueMakeUndefined(context);