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