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 JSContactAddressTypeArray.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
28 #include <Tizen/Common/JSTizenExceptionFactory.h>
29 #include <Tizen/Common/JSTizenException.h>
30 #include "ContactConverter.h"
31 #include "JSContactAddressTypeArray.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"
52 using namespace TizenApis::Commons;
53 using namespace TizenApis::Api::Contact;
54 using namespace WrtDeviceApis::CommonsJavaScript;
56 JSClassDefinition JSContactAddressTypeArray::m_classInfo = {
58 kJSClassAttributeNone,
68 NULL, //deleteProperty,
70 NULL, //callAsFunction,
71 NULL, //callAsConstructor,
73 NULL, //convertToType,
76 JSStaticValue JSContactAddressTypeArray::m_property[] = {
77 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
81 JSStaticFunction JSContactAddressTypeArray::m_function[] = {
82 { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
83 { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
84 { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
85 { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
86 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
87 { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
88 { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
89 { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
90 { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
91 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
92 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
93 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
97 JSClassRef JSContactAddressTypeArray::m_jsClassRef = JSClassCreate(
98 JSContactAddressTypeArray::getClassInfo());
100 JSValueRef JSContactAddressTypeArray::getLength(JSContextRef context,
102 JSStringRef propertyName,
103 JSValueRef* exception)
107 JSContactAddressTypeArrayPriv* priv =
108 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
110 Throw(WrtDeviceApis::Commons::NullPointerException);
112 ContactAddressTypeArrayPtr typeArray = priv->getObject();
114 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
115 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
116 return converter->toJSValueRef(typeArray->size());
119 Catch(WrtDeviceApis::Commons::Exception)
121 LogError("invalid conversion");
123 return JSValueMakeUndefined(context);
126 JSObjectRef JSContactAddressTypeArray::createArray(JSContextRef context,
127 const ContactAddressTypeArrayPtr &typeArray)
129 JSContactAddressTypeArrayPriv *priv = new JSContactAddressTypeArrayPriv(context, typeArray);
130 return JSObjectMake(context, getClassRef(), priv);
133 const JSClassDefinition* JSContactAddressTypeArray::getClassInfo()
135 return &(m_classInfo);
138 JSClassRef JSContactAddressTypeArray::getClassRef()
141 m_jsClassRef = JSClassCreate(&m_classInfo);
146 bool JSContactAddressTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
148 return JSValueIsObjectOfClass(context, value, getClassRef());
151 ContactAddressTypeArrayPtr JSContactAddressTypeArray::getContactAddressTypeArray(JSContextRef context, JSValueRef value)
153 if (!isObjectOfClass(context, value)) {
154 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
156 JSObjectRef object = JSValueToObject(context, value, NULL);
158 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
160 JSContactAddressTypeArrayPriv *priv = static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
162 Throw(WrtDeviceApis::Commons::NullPointerException);
164 return priv->getObject();
167 void JSContactAddressTypeArray::initialize(JSContextRef context,
172 void JSContactAddressTypeArray::finalize(JSObjectRef object)
174 JSContactAddressTypeArrayPriv* priv =
175 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
177 JSObjectSetPrivate(object, NULL);
180 bool JSContactAddressTypeArray::hasProperty(JSContextRef context,
182 JSStringRef propertyName)
184 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
185 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
188 size_t index = converter->toSizeT(propertyName);
189 JSContactAddressTypeArrayPriv* priv =
190 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
192 Throw(WrtDeviceApis::Commons::NullPointerException);
194 ContactAddressTypeArrayPtr typeArray = priv->getObject();
195 if (index < typeArray->size()) {
199 Catch(WrtDeviceApis::Commons::Exception)
201 //not reporting error is intended
206 JSValueRef JSContactAddressTypeArray::getProperty(JSContextRef context,
208 JSStringRef propertyName,
209 JSValueRef* exception)
211 ContactConverterFactory::ConverterType converter =
212 ContactConverterFactory::getConverter(context);
215 size_t index = converter->toSizeT(propertyName);
216 JSContactAddressTypeArrayPriv* priv =
217 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
219 Throw(WrtDeviceApis::Commons::NullPointerException);
221 ContactAddressTypeArrayPtr typeArray = priv->getObject();
222 if (index < typeArray->size()) {
223 std::string result = converter->toContactAddressTypeStr(typeArray->at(index));
224 if (!result.empty()) {
225 return converter->toJSValueRef(result);
229 Catch(WrtDeviceApis::Commons::Exception)
231 LogError("invalid property");
233 return JSValueMakeUndefined(context);
236 bool JSContactAddressTypeArray::setProperty(JSContextRef context,
238 JSStringRef propertyName,
240 JSValueRef* exception)
242 ContactConverterFactory::ConverterType converter =
243 ContactConverterFactory::getConverter(context);
246 size_t index = converter->toSizeT(propertyName);
248 if (!JSValueIsUndefined(context, value)) {
249 str = converter->toString(value);
251 JSContactAddressTypeArrayPriv* priv =
252 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
254 Throw(WrtDeviceApis::Commons::NullPointerException);
256 ContactAddressTypeArrayPtr typeArray = priv->getObject();
258 Throw(WrtDeviceApis::Commons::NullPointerException);
260 if (typeArray->size() <= index) {
261 typeArray->resize(index + 1);
263 (*typeArray)[index] = converter->toContactAddressType(str);
266 Catch(WrtDeviceApis::Commons::Exception)
268 LogError("error occured");
269 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
274 void JSContactAddressTypeArray::getPropertyNames(JSContextRef context,
276 JSPropertyNameAccumulatorRef propertyNames)
278 ContactConverterFactory::ConverterType converter =
279 ContactConverterFactory::getConverter(context);
282 JSContactAddressTypeArrayPriv* priv =
283 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
285 Throw(WrtDeviceApis::Commons::NullPointerException);
287 ContactAddressTypeArrayPtr addressTypes = priv->getObject();
289 int count = addressTypes->size();
291 for(int i=0; i < count; i++)
293 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
294 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
297 Catch(WrtDeviceApis::Commons::Exception)
299 LogError("invalid property");
303 JSValueRef JSContactAddressTypeArray::concat(JSContextRef context,
304 JSObjectRef function,
305 JSObjectRef thisObject,
306 size_t argumentCount,
307 const JSValueRef arguments[],
308 JSValueRef* exception)
312 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
313 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
316 JSValueRef result = JSObjectMake(context,
317 getClassRef(), newPrivateObject);
319 //copy current typeArray
320 JSContactAddressTypeArrayPriv* priv =
321 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
322 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
323 for (size_t i = 0; i < currentTypes->size(); ++i) {
324 typeArray->push_back(currentTypes->at(i));
327 //copy submitted arrays
328 ContactConverterFactory::ConverterType converter =
329 ContactConverterFactory::getConverter(context);
330 for (size_t i = 0; i < argumentCount; ++i) {
331 if (!JSIsArrayValue(context, arguments[i])) {
332 Throw(WrtDeviceApis::Commons::ConversionException);
334 // process array of types
335 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
336 unsigned int len = JSGetArrayLength(context, arrayObj);
337 for (unsigned int e = 0; e < len; ++e) {
338 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
339 typeArray->push_back(converter->toContactAddressType(att));
344 Catch(WrtDeviceApis::Commons::Exception)
346 LogError("error occured");
348 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
351 JSValueRef JSContactAddressTypeArray::join(JSContextRef context,
352 JSObjectRef function,
353 JSObjectRef thisObject,
354 size_t argumentCount,
355 const JSValueRef arguments[],
356 JSValueRef* exception)
361 std::string separator(",");
362 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
363 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
364 JSContactAddressTypeArrayPriv* priv =
365 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
366 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
367 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
368 separator = converter->toString(arguments[0]);
370 for (size_t i = 0; i < currentTypes->size(); ++i) {
374 result += currentTypes->at(i);
376 return converter->toJSValueRef(result);
378 Catch(WrtDeviceApis::Commons::Exception)
380 LogError("error occured");
382 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
385 JSValueRef JSContactAddressTypeArray::pop(JSContextRef context,
386 JSObjectRef function,
387 JSObjectRef thisObject,
388 size_t argumentCount,
389 const JSValueRef arguments[],
390 JSValueRef* exception)
394 ContactConverterFactory::ConverterType converter =
395 ContactConverterFactory::getConverter(context);
396 JSContactAddressTypeArrayPriv* priv =
397 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
398 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
399 if (currentTypes->size() > 0) {
400 std::string result = converter->toContactAddressTypeStr(currentTypes->at(currentTypes->size()-1));
401 currentTypes->pop_back();
402 return converter->toJSValueRef(result);
405 Catch(WrtDeviceApis::Commons::Exception)
407 LogError("error occured");
409 return JSValueMakeUndefined(context);
412 JSValueRef JSContactAddressTypeArray::push(JSContextRef context,
413 JSObjectRef function,
414 JSObjectRef thisObject,
415 size_t argumentCount,
416 const JSValueRef arguments[],
417 JSValueRef* exception)
421 ContactConverterFactory::ConverterType converter =
422 ContactConverterFactory::getConverter(context);
423 JSContactAddressTypeArrayPriv* priv =
424 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
425 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
426 for (size_t i = 0; i < argumentCount; ++i) {
427 currentTypes->push_back(converter->toContactAddressType(arguments[i]));
429 return converter->toJSValueRef(currentTypes->size());
431 Catch(WrtDeviceApis::Commons::Exception)
433 LogError("error occured");
435 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
438 JSValueRef JSContactAddressTypeArray::reverse(JSContextRef context,
439 JSObjectRef function,
440 JSObjectRef thisObject,
441 size_t argumentCount,
442 const JSValueRef arguments[],
443 JSValueRef* exception)
447 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
448 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
449 JSContactAddressTypeArrayPriv* priv =
450 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
451 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
452 std::reverse(currentTypes->begin(), currentTypes->end());
455 Catch(WrtDeviceApis::Commons::Exception)
457 LogError("error occured");
459 return JSValueMakeUndefined(context);
462 JSValueRef JSContactAddressTypeArray::shift(JSContextRef context,
463 JSObjectRef function,
464 JSObjectRef thisObject,
465 size_t argumentCount,
466 const JSValueRef arguments[],
467 JSValueRef* exception)
471 ContactConverterFactory::ConverterType converter =
472 ContactConverterFactory::getConverter(context);
473 JSContactAddressTypeArrayPriv* priv =
474 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
475 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
476 if (currentTypes->size() > 0) {
477 std::string result = converter->toContactAddressTypeStr(currentTypes->at(0));
478 currentTypes->erase(currentTypes->begin());
479 return converter->toJSValueRef(result);
482 Catch(WrtDeviceApis::Commons::Exception)
484 LogError("error occured");
486 return JSValueMakeUndefined(context);
489 JSValueRef JSContactAddressTypeArray::slice(JSContextRef context,
490 JSObjectRef function,
491 JSObjectRef thisObject,
492 size_t argumentCount,
493 const JSValueRef arguments[],
494 JSValueRef* exception)
498 if (argumentCount < 1) {
499 return JSValueMakeUndefined(context);
501 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
502 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
503 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
504 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
507 JSValueRef result = JSObjectMake(context,
508 getClassRef(), newPrivateObject);
510 //copy current typeArray
511 JSContactAddressTypeArrayPriv* priv =
512 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
513 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
514 std::size_t first = converter->toSizeT(arguments[0]);
515 std::size_t last = currentTypes->size() - 1;
516 if (argumentCount > 1) {
517 last = converter->toSizeT(arguments[1]);
518 if (last >= currentTypes->size()) {
519 last = currentTypes->size() - 1;
525 for (size_t i = first; i <= last; ++i) {
526 typeArray->push_back(currentTypes->at(i));
531 Catch(WrtDeviceApis::Commons::Exception)
533 LogError("error occured");
535 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
538 JSValueRef JSContactAddressTypeArray::sort(JSContextRef context,
539 JSObjectRef function,
540 JSObjectRef thisObject,
541 size_t argumentCount,
542 const JSValueRef arguments[],
543 JSValueRef* exception)
547 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
548 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
549 JSContactAddressTypeArrayPriv* priv =
550 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
551 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
552 std::sort(currentTypes->begin(), currentTypes->end());
555 Catch(WrtDeviceApis::Commons::Exception)
557 LogError("error occured");
559 return JSValueMakeUndefined(context);
562 JSValueRef JSContactAddressTypeArray::splice(JSContextRef context,
563 JSObjectRef function,
564 JSObjectRef thisObject,
565 size_t argumentCount,
566 const JSValueRef arguments[],
567 JSValueRef* exception)
569 return JSValueMakeUndefined(context);
572 JSValueRef JSContactAddressTypeArray::toString(JSContextRef context,
573 JSObjectRef function,
574 JSObjectRef thisObject,
575 size_t argumentCount,
576 const JSValueRef arguments[],
577 JSValueRef* exception)
579 return join(context, function, thisObject, 0, arguments, exception);
582 JSValueRef JSContactAddressTypeArray::unshift(JSContextRef context,
583 JSObjectRef function,
584 JSObjectRef thisObject,
585 size_t argumentCount,
586 const JSValueRef arguments[],
587 JSValueRef* exception)
589 return JSValueMakeUndefined(context);
592 JSValueRef JSContactAddressTypeArray::valueOf(JSContextRef context,
593 JSObjectRef function,
594 JSObjectRef thisObject,
595 size_t argumentCount,
596 const JSValueRef arguments[],
597 JSValueRef* exception)
599 return JSValueMakeUndefined(context);