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 <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactAddressTypeArray.h"
32 #define FUNCTION_CONCAT "concat"
33 #define FUNCTION_JOIN "join"
34 #define FUNCTION_POP "pop"
35 #define FUNCTION_PUSH "push"
36 #define FUNCTION_REVERSE "reverse"
37 #define FUNCTION_SHIFT "shift"
38 #define FUNCTION_SLICE "slice"
39 #define FUNCTION_SORT "sort"
40 #define FUNCTION_SPLICE "splice"
41 #define FUNCTION_TOSTRING "toString"
42 #define FUNCTION_UNSHIFT "unshift"
43 #define FUNCTION_VALUEOF "valueOf"
45 #define ATTRIBUTE_LENGTH "length"
51 using namespace TizenApis::Commons;
52 using namespace TizenApis::Api::Contact;
54 JSClassDefinition JSContactAddressTypeArray::m_classInfo = {
56 kJSClassAttributeNone,
66 NULL, //deleteProperty,
67 NULL, //getPropertyNames,
68 NULL, //callAsFunction,
69 NULL, //callAsConstructor,
71 NULL, //convertToType,
74 JSStaticValue JSContactAddressTypeArray::m_property[] = {
75 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
79 JSStaticFunction JSContactAddressTypeArray::m_function[] = {
80 { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
81 { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
82 { FUNCTION_POP, pop, kJSPropertyAttributeNone },
83 { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
84 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
85 { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
86 { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
87 { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
88 { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
89 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
90 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
91 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
95 JSClassRef JSContactAddressTypeArray::m_jsClassRef = JSClassCreate(
96 JSContactAddressTypeArray::getClassInfo());
98 JSValueRef JSContactAddressTypeArray::getLength(JSContextRef context,
100 JSStringRef propertyName,
101 JSValueRef* exception)
106 JSContactAddressTypeArrayPriv* priv =
107 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
109 Throw(WrtDeviceApis::Commons::NullPointerException);
111 ContactAddressTypeArrayPtr typeArray = priv->getObject();
113 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
114 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
115 return converter->toJSValueRef(typeArray->size());
118 Catch(WrtDeviceApis::Commons::Exception)
120 LogError("invalid conversion");
122 return JSValueMakeUndefined(context);
125 JSObjectRef JSContactAddressTypeArray::createArray(JSContextRef context,
126 const ContactAddressTypeArrayPtr &typeArray)
128 JSContactAddressTypeArrayPriv *priv = new JSContactAddressTypeArrayPriv(context, typeArray);
129 return JSObjectMake(context, getClassRef(), priv);
132 const JSClassDefinition* JSContactAddressTypeArray::getClassInfo()
134 return &(m_classInfo);
137 JSClassRef JSContactAddressTypeArray::getClassRef()
140 m_jsClassRef = JSClassCreate(&m_classInfo);
145 bool JSContactAddressTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
147 return JSValueIsObjectOfClass(context, value, getClassRef());
150 ContactAddressTypeArrayPtr JSContactAddressTypeArray::getContactAddressTypeArray(JSContextRef context, JSValueRef value)
152 if (!isObjectOfClass(context, value)) {
153 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
155 JSObjectRef object = JSValueToObject(context, value, NULL);
157 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
159 JSContactAddressTypeArrayPriv *priv = static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
161 Throw(WrtDeviceApis::Commons::NullPointerException);
163 return priv->getObject();
166 void JSContactAddressTypeArray::initialize(JSContextRef context,
172 void JSContactAddressTypeArray::finalize(JSObjectRef object)
175 JSContactAddressTypeArrayPriv* priv =
176 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
178 JSObjectSetPrivate(object, NULL);
181 bool JSContactAddressTypeArray::hasProperty(JSContextRef context,
183 JSStringRef propertyName)
186 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
187 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
190 size_t index = converter->toSizeT(propertyName);
191 JSContactAddressTypeArrayPriv* priv =
192 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
194 Throw(WrtDeviceApis::Commons::NullPointerException);
196 ContactAddressTypeArrayPtr typeArray = priv->getObject();
197 if (index < typeArray->size()) {
201 Catch(WrtDeviceApis::Commons::Exception)
203 //not reporting error is intended
208 JSValueRef JSContactAddressTypeArray::getProperty(JSContextRef context,
210 JSStringRef propertyName,
211 JSValueRef* exception)
214 ContactConverterFactory::ConverterType converter =
215 ContactConverterFactory::getConverter(context);
218 size_t index = converter->toSizeT(propertyName);
219 JSContactAddressTypeArrayPriv* priv =
220 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
222 Throw(WrtDeviceApis::Commons::NullPointerException);
224 ContactAddressTypeArrayPtr typeArray = priv->getObject();
225 if (index < typeArray->size()) {
226 std::string result = converter->toContactAddressTypeStr(typeArray->at(index));
227 if (!result.empty()) {
228 return converter->toJSValueRef(result);
232 Catch(WrtDeviceApis::Commons::Exception)
234 LogError("invalid property");
236 return JSValueMakeUndefined(context);
239 bool JSContactAddressTypeArray::setProperty(JSContextRef context,
241 JSStringRef propertyName,
243 JSValueRef* exception)
246 ContactConverterFactory::ConverterType converter =
247 ContactConverterFactory::getConverter(context);
250 size_t index = converter->toSizeT(propertyName);
252 if (!JSValueIsUndefined(context, value)) {
253 str = converter->toString(value);
255 JSContactAddressTypeArrayPriv* priv =
256 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
258 Throw(WrtDeviceApis::Commons::NullPointerException);
260 ContactAddressTypeArrayPtr typeArray = priv->getObject();
262 Throw(WrtDeviceApis::Commons::NullPointerException);
264 if (typeArray->size() <= index) {
265 typeArray->resize(index + 1);
267 (*typeArray)[index] = converter->toContactAddressType(str);
270 Catch(WrtDeviceApis::Commons::Exception)
272 LogError("error occured");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
278 JSValueRef JSContactAddressTypeArray::concat(JSContextRef context,
279 JSObjectRef function,
280 JSObjectRef thisObject,
281 size_t argumentCount,
282 const JSValueRef arguments[],
283 JSValueRef* exception)
288 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
289 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
292 JSValueRef result = JSObjectMake(context,
293 getClassRef(), newPrivateObject);
295 //copy current typeArray
296 JSContactAddressTypeArrayPriv* priv =
297 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
298 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
299 for (size_t i = 0; i < currentTypes->size(); ++i) {
300 typeArray->push_back(currentTypes->at(i));
303 //copy submitted arrays
304 ContactConverterFactory::ConverterType converter =
305 ContactConverterFactory::getConverter(context);
306 for (size_t i = 0; i < argumentCount; ++i) {
307 if (!JSIsArrayValue(context, arguments[i])) {
308 Throw(WrtDeviceApis::Commons::ConversionException);
310 // process array of types
311 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
312 unsigned int len = JSGetArrayLength(context, arrayObj);
313 for (unsigned int e = 0; e < len; ++e) {
314 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
315 typeArray->push_back(converter->toContactAddressType(att));
320 Catch(WrtDeviceApis::Commons::Exception)
322 LogError("error occured");
324 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
327 JSValueRef JSContactAddressTypeArray::join(JSContextRef context,
328 JSObjectRef function,
329 JSObjectRef thisObject,
330 size_t argumentCount,
331 const JSValueRef arguments[],
332 JSValueRef* exception)
334 //LogDebug("entered");
338 std::string separator(",");
339 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
340 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
341 JSContactAddressTypeArrayPriv* priv =
342 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
343 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
344 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
345 separator = converter->toString(arguments[0]);
347 for (size_t i = 0; i < currentTypes->size(); ++i) {
351 result += currentTypes->at(i);
353 return converter->toJSValueRef(result);
355 Catch(WrtDeviceApis::Commons::Exception)
357 LogError("error occured");
359 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
362 JSValueRef JSContactAddressTypeArray::pop(JSContextRef context,
363 JSObjectRef function,
364 JSObjectRef thisObject,
365 size_t argumentCount,
366 const JSValueRef arguments[],
367 JSValueRef* exception)
369 //LogDebug("entered");
372 ContactConverterFactory::ConverterType converter =
373 ContactConverterFactory::getConverter(context);
374 JSContactAddressTypeArrayPriv* priv =
375 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
376 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
377 if (currentTypes->size() > 0) {
378 std::string result = converter->toContactAddressTypeStr(currentTypes->at(currentTypes->size()-1));
379 currentTypes->pop_back();
380 return converter->toJSValueRef(result);
383 Catch(WrtDeviceApis::Commons::Exception)
385 LogError("error occured");
387 return JSValueMakeUndefined(context);
390 JSValueRef JSContactAddressTypeArray::push(JSContextRef context,
391 JSObjectRef function,
392 JSObjectRef thisObject,
393 size_t argumentCount,
394 const JSValueRef arguments[],
395 JSValueRef* exception)
397 //LogDebug("entered");
400 ContactConverterFactory::ConverterType converter =
401 ContactConverterFactory::getConverter(context);
402 JSContactAddressTypeArrayPriv* priv =
403 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
404 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
405 for (size_t i = 0; i < argumentCount; ++i) {
406 currentTypes->push_back(converter->toContactAddressType(arguments[i]));
408 return converter->toJSValueRef(currentTypes->size());
410 Catch(WrtDeviceApis::Commons::Exception)
412 LogError("error occured");
414 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
417 JSValueRef JSContactAddressTypeArray::reverse(JSContextRef context,
418 JSObjectRef function,
419 JSObjectRef thisObject,
420 size_t argumentCount,
421 const JSValueRef arguments[],
422 JSValueRef* exception)
424 //LogDebug("entered");
427 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
428 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
429 JSContactAddressTypeArrayPriv* priv =
430 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
431 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
432 std::reverse(currentTypes->begin(), currentTypes->end());
435 Catch(WrtDeviceApis::Commons::Exception)
437 LogError("error occured");
439 return JSValueMakeUndefined(context);
442 JSValueRef JSContactAddressTypeArray::shift(JSContextRef context,
443 JSObjectRef function,
444 JSObjectRef thisObject,
445 size_t argumentCount,
446 const JSValueRef arguments[],
447 JSValueRef* exception)
449 //LogDebug("entered");
452 ContactConverterFactory::ConverterType converter =
453 ContactConverterFactory::getConverter(context);
454 JSContactAddressTypeArrayPriv* priv =
455 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
456 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
457 if (currentTypes->size() > 0) {
458 std::string result = converter->toContactAddressTypeStr(currentTypes->at(0));
459 currentTypes->erase(currentTypes->begin());
460 return converter->toJSValueRef(result);
463 Catch(WrtDeviceApis::Commons::Exception)
465 LogError("error occured");
467 return JSValueMakeUndefined(context);
470 JSValueRef JSContactAddressTypeArray::slice(JSContextRef context,
471 JSObjectRef function,
472 JSObjectRef thisObject,
473 size_t argumentCount,
474 const JSValueRef arguments[],
475 JSValueRef* exception)
480 if (argumentCount < 1) {
481 return JSValueMakeUndefined(context);
483 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
484 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
485 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
486 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
489 JSValueRef result = JSObjectMake(context,
490 getClassRef(), newPrivateObject);
492 //copy current typeArray
493 JSContactAddressTypeArrayPriv* priv =
494 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
495 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
496 std::size_t first = converter->toSizeT(arguments[0]);
497 std::size_t last = currentTypes->size() - 1;
498 if (argumentCount > 1) {
499 last = converter->toSizeT(arguments[1]);
500 if (last >= currentTypes->size()) {
501 last = currentTypes->size() - 1;
507 for (size_t i = first; i <= last; ++i) {
508 typeArray->push_back(currentTypes->at(i));
513 Catch(WrtDeviceApis::Commons::Exception)
515 LogError("error occured");
517 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
520 JSValueRef JSContactAddressTypeArray::sort(JSContextRef context,
521 JSObjectRef function,
522 JSObjectRef thisObject,
523 size_t argumentCount,
524 const JSValueRef arguments[],
525 JSValueRef* exception)
527 //LogDebug("entered");
530 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
531 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
532 JSContactAddressTypeArrayPriv* priv =
533 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
534 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
535 std::sort(currentTypes->begin(), currentTypes->end());
538 Catch(WrtDeviceApis::Commons::Exception)
540 LogError("error occured");
542 return JSValueMakeUndefined(context);
545 JSValueRef JSContactAddressTypeArray::splice(JSContextRef context,
546 JSObjectRef function,
547 JSObjectRef thisObject,
548 size_t argumentCount,
549 const JSValueRef arguments[],
550 JSValueRef* exception)
552 //LogDebug("entered");
553 return JSValueMakeUndefined(context);
556 JSValueRef JSContactAddressTypeArray::toString(JSContextRef context,
557 JSObjectRef function,
558 JSObjectRef thisObject,
559 size_t argumentCount,
560 const JSValueRef arguments[],
561 JSValueRef* exception)
563 //LogDebug("entered");
564 return join(context, function, thisObject, 0, arguments, exception);
567 JSValueRef JSContactAddressTypeArray::unshift(JSContextRef context,
568 JSObjectRef function,
569 JSObjectRef thisObject,
570 size_t argumentCount,
571 const JSValueRef arguments[],
572 JSValueRef* exception)
574 //LogDebug("entered");
575 return JSValueMakeUndefined(context);
578 JSValueRef JSContactAddressTypeArray::valueOf(JSContextRef context,
579 JSObjectRef function,
580 JSObjectRef thisObject,
581 size_t argumentCount,
582 const JSValueRef arguments[],
583 JSValueRef* exception)
585 //LogDebug("entered");
586 return JSValueMakeUndefined(context);