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 JSContactPhoneNumberTypeArray.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 "JSContactPhoneNumberTypeArray.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 JSContactPhoneNumberTypeArray::m_classInfo = {
56 kJSClassAttributeNone,
66 NULL, //deleteProperty,
67 NULL, //getPropertyNames,
68 NULL, //callAsFunction,
69 NULL, //callAsConstructor,
71 NULL, //convertToType,
74 JSStaticValue JSContactPhoneNumberTypeArray::m_property[] = {
75 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
79 JSStaticFunction JSContactPhoneNumberTypeArray::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 JSContactPhoneNumberTypeArray::m_jsClassRef = JSClassCreate(
96 JSContactPhoneNumberTypeArray::getClassInfo());
98 JSValueRef JSContactPhoneNumberTypeArray::getLength(JSContextRef context,
100 JSStringRef propertyName,
101 JSValueRef* exception)
106 JSContactPhoneNumberTypeArrayPriv* priv =
107 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
109 Throw(WrtDeviceApis::Commons::NullPointerException);
111 ContactPhoneNumberTypeArrayPtr 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 JSContactPhoneNumberTypeArray::createArray(JSContextRef context,
126 const ContactPhoneNumberTypeArrayPtr &typeArray)
128 JSContactPhoneNumberTypeArrayPriv *priv = new JSContactPhoneNumberTypeArrayPriv(context, typeArray);
129 return JSObjectMake(context, getClassRef(), priv);
132 const JSClassDefinition* JSContactPhoneNumberTypeArray::getClassInfo()
134 return &(m_classInfo);
137 JSClassRef JSContactPhoneNumberTypeArray::getClassRef()
140 m_jsClassRef = JSClassCreate(&m_classInfo);
145 bool JSContactPhoneNumberTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
147 return JSValueIsObjectOfClass(context, value, getClassRef());
150 ContactPhoneNumberTypeArrayPtr JSContactPhoneNumberTypeArray::getContactPhoneNumberTypeArray(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 JSContactPhoneNumberTypeArrayPriv *priv = static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
161 Throw(WrtDeviceApis::Commons::NullPointerException);
163 return priv->getObject();
166 void JSContactPhoneNumberTypeArray::initialize(JSContextRef context,
172 void JSContactPhoneNumberTypeArray::finalize(JSObjectRef object)
175 JSContactPhoneNumberTypeArrayPriv* priv =
176 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
178 JSObjectSetPrivate(object, NULL);
181 bool JSContactPhoneNumberTypeArray::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 JSContactPhoneNumberTypeArrayPriv* priv =
192 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
194 Throw(WrtDeviceApis::Commons::NullPointerException);
196 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
197 if (index < typeArray->size()) {
201 Catch(WrtDeviceApis::Commons::Exception)
203 //not reporting error is intended
208 JSValueRef JSContactPhoneNumberTypeArray::getProperty(JSContextRef context,
210 JSStringRef propertyName,
211 JSValueRef* exception)
214 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
217 size_t index = converter->toSizeT(propertyName);
218 JSContactPhoneNumberTypeArrayPriv* priv =
219 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
221 Throw(WrtDeviceApis::Commons::NullPointerException);
223 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
224 if (index < typeArray->size()) {
225 std::string result = converter->toContactPhoneNumberTypeStr(typeArray->at(index));
226 if (!result.empty()) {
227 return converter->toJSValueRef(result);
231 Catch(WrtDeviceApis::Commons::Exception)
233 LogError("invalid property");
235 return JSValueMakeUndefined(context);
238 bool JSContactPhoneNumberTypeArray::setProperty(JSContextRef context,
240 JSStringRef propertyName,
242 JSValueRef* exception)
245 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
248 size_t index = converter->toSizeT(propertyName);
250 if (!JSValueIsUndefined(context, value)) {
251 str = converter->toString(value);
253 JSContactPhoneNumberTypeArrayPriv* priv =
254 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
256 Throw(WrtDeviceApis::Commons::NullPointerException);
258 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
260 Throw(WrtDeviceApis::Commons::NullPointerException);
262 if (typeArray->size() <= index) {
263 typeArray->resize(index + 1);
265 (*typeArray)[index] = converter->toContactPhoneNumberType(str);
268 Catch(WrtDeviceApis::Commons::Exception)
270 LogError("error occured");
271 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
276 JSValueRef JSContactPhoneNumberTypeArray::concat(JSContextRef context,
277 JSObjectRef function,
278 JSObjectRef thisObject,
279 size_t argumentCount,
280 const JSValueRef arguments[],
281 JSValueRef* exception)
286 ContactPhoneNumberTypeArrayPtr typeArray = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
287 JSContactPhoneNumberTypeArrayPriv *newPrivateObject = new JSContactPhoneNumberTypeArrayPriv(
290 JSValueRef result = JSObjectMake(context,
291 getClassRef(), newPrivateObject);
293 //copy current typeArray
294 JSContactPhoneNumberTypeArrayPriv* priv =
295 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
296 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
297 for (size_t i = 0; i < currentStrings->size(); ++i) {
298 typeArray->push_back(currentStrings->at(i));
301 //copy submitted arrays
302 ContactConverterFactory::ConverterType converter =
303 ContactConverterFactory::getConverter(context);
304 for (size_t i = 0; i < argumentCount; ++i) {
305 if (!JSIsArrayValue(context, arguments[i])) {
306 Throw(WrtDeviceApis::Commons::ConversionException);
308 // process array of strings
309 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
310 unsigned int len = JSGetArrayLength(context, arrayObj);
311 for (unsigned int e = 0; e < len; ++e) {
312 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
313 typeArray->push_back(converter->toContactPhoneNumberType(att));
318 Catch(WrtDeviceApis::Commons::Exception)
320 LogError("error occured");
322 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
325 JSValueRef JSContactPhoneNumberTypeArray::join(JSContextRef context,
326 JSObjectRef function,
327 JSObjectRef thisObject,
328 size_t argumentCount,
329 const JSValueRef arguments[],
330 JSValueRef* exception)
332 //LogDebug("entered");
336 std::string separator(",");
337 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
338 JSContactPhoneNumberTypeArrayPriv* priv =
339 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
340 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
341 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
342 separator = converter->toString(arguments[0]);
344 for (size_t i = 0; i < currentStrings->size(); ++i) {
348 result += currentStrings->at(i);
350 return converter->toJSValueRef(result);
352 Catch(WrtDeviceApis::Commons::Exception)
354 LogError("error occured");
356 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
359 JSValueRef JSContactPhoneNumberTypeArray::pop(JSContextRef context,
360 JSObjectRef function,
361 JSObjectRef thisObject,
362 size_t argumentCount,
363 const JSValueRef arguments[],
364 JSValueRef* exception)
366 //LogDebug("entered");
369 ContactConverterFactory::ConverterType converter =
370 ContactConverterFactory::getConverter(context);
371 JSContactPhoneNumberTypeArrayPriv* priv =
372 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
373 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
374 if (currentStrings->size() > 0) {
376 converter->toContactPhoneNumberTypeStr(currentStrings->at(currentStrings->size() - 1));
377 currentStrings->pop_back();
378 return converter->toJSValueRef(result);
381 Catch(WrtDeviceApis::Commons::Exception)
383 LogError("error occured");
385 return JSValueMakeUndefined(context);
388 JSValueRef JSContactPhoneNumberTypeArray::push(JSContextRef context,
389 JSObjectRef function,
390 JSObjectRef thisObject,
391 size_t argumentCount,
392 const JSValueRef arguments[],
393 JSValueRef* exception)
395 //LogDebug("entered");
398 ContactConverterFactory::ConverterType converter =
399 ContactConverterFactory::getConverter(context);
400 JSContactPhoneNumberTypeArrayPriv* priv =
401 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
402 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
403 for (size_t i = 0; i < argumentCount; ++i) {
404 currentStrings->push_back(converter->toContactPhoneNumberType(arguments[i]));
406 return converter->toJSValueRef(currentStrings->size());
408 Catch(WrtDeviceApis::Commons::Exception)
410 LogError("error occured");
412 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
415 JSValueRef JSContactPhoneNumberTypeArray::reverse(JSContextRef context,
416 JSObjectRef function,
417 JSObjectRef thisObject,
418 size_t argumentCount,
419 const JSValueRef arguments[],
420 JSValueRef* exception)
422 //LogDebug("entered");
425 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
426 JSContactPhoneNumberTypeArrayPriv* priv =
427 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
428 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
429 std::reverse(currentStrings->begin(), currentStrings->end());
432 Catch(WrtDeviceApis::Commons::Exception)
434 LogError("error occured");
436 return JSValueMakeUndefined(context);
439 JSValueRef JSContactPhoneNumberTypeArray::shift(JSContextRef context,
440 JSObjectRef function,
441 JSObjectRef thisObject,
442 size_t argumentCount,
443 const JSValueRef arguments[],
444 JSValueRef* exception)
446 //LogDebug("entered");
449 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
450 JSContactPhoneNumberTypeArrayPriv* priv =
451 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
452 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
453 if (currentStrings->size() > 0) {
454 std::string result = converter->toContactPhoneNumberTypeStr(currentStrings->at(0));
455 currentStrings->erase(currentStrings->begin());
456 return converter->toJSValueRef(result);
459 Catch(WrtDeviceApis::Commons::Exception)
461 LogError("error occured");
463 return JSValueMakeUndefined(context);
466 JSValueRef JSContactPhoneNumberTypeArray::slice(JSContextRef context,
467 JSObjectRef function,
468 JSObjectRef thisObject,
469 size_t argumentCount,
470 const JSValueRef arguments[],
471 JSValueRef* exception)
476 if (argumentCount < 1) {
477 return JSValueMakeUndefined(context);
479 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
480 ContactPhoneNumberTypeArrayPtr typeArray = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
481 JSContactPhoneNumberTypeArrayPriv *newPrivateObject = new JSContactPhoneNumberTypeArrayPriv(
484 JSValueRef result = JSObjectMake(context,
485 getClassRef(), newPrivateObject);
487 //copy current typeArray
488 JSContactPhoneNumberTypeArrayPriv* priv =
489 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
490 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
491 std::size_t first = converter->toSizeT(arguments[0]);
492 std::size_t last = currentStrings->size() - 1;
493 if (argumentCount > 1) {
494 last = converter->toSizeT(arguments[1]);
495 if (last >= currentStrings->size()) {
496 last = currentStrings->size() - 1;
502 for (size_t i = first; i <= last; ++i) {
503 typeArray->push_back(currentStrings->at(i));
508 Catch(WrtDeviceApis::Commons::Exception)
510 LogError("error occured");
512 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
515 JSValueRef JSContactPhoneNumberTypeArray::sort(JSContextRef context,
516 JSObjectRef function,
517 JSObjectRef thisObject,
518 size_t argumentCount,
519 const JSValueRef arguments[],
520 JSValueRef* exception)
522 //LogDebug("entered");
525 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
526 JSContactPhoneNumberTypeArrayPriv* priv =
527 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
528 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
529 std::sort(currentStrings->begin(), currentStrings->end());
532 Catch(WrtDeviceApis::Commons::Exception)
534 LogError("error occured");
536 return JSValueMakeUndefined(context);
539 JSValueRef JSContactPhoneNumberTypeArray::splice(JSContextRef context,
540 JSObjectRef function,
541 JSObjectRef thisObject,
542 size_t argumentCount,
543 const JSValueRef arguments[],
544 JSValueRef* exception)
546 //LogDebug("entered");
547 return JSValueMakeUndefined(context);
550 JSValueRef JSContactPhoneNumberTypeArray::toString(JSContextRef context,
551 JSObjectRef function,
552 JSObjectRef thisObject,
553 size_t argumentCount,
554 const JSValueRef arguments[],
555 JSValueRef* exception)
557 //LogDebug("entered");
558 return join(context, function, thisObject, 0, arguments, exception);
561 JSValueRef JSContactPhoneNumberTypeArray::unshift(JSContextRef context,
562 JSObjectRef function,
563 JSObjectRef thisObject,
564 size_t argumentCount,
565 const JSValueRef arguments[],
566 JSValueRef* exception)
568 //LogDebug("entered");
569 return JSValueMakeUndefined(context);
572 JSValueRef JSContactPhoneNumberTypeArray::valueOf(JSContextRef context,
573 JSObjectRef function,
574 JSObjectRef thisObject,
575 size_t argumentCount,
576 const JSValueRef arguments[],
577 JSValueRef* exception)
579 //LogDebug("entered");
580 return JSValueMakeUndefined(context);