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 JSContactPhoneNumberArray.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactPhoneNumberArray.h"
31 #define FUNCTION_CONCAT "concat"
32 #define FUNCTION_JOIN "join"
33 #define FUNCTION_POP "pop"
34 #define FUNCTION_PUSH "push"
35 #define FUNCTION_REVERSE "reverse"
36 #define FUNCTION_SHIFT "shift"
37 #define FUNCTION_SLICE "slice"
38 #define FUNCTION_SORT "sort"
39 #define FUNCTION_SPLICE "splice"
40 #define FUNCTION_TOSTRING "toString"
41 #define FUNCTION_UNSHIFT "unshift"
42 #define FUNCTION_VALUEOF "valueOf"
44 #define ATTRIBUTE_LENGTH "length"
50 using namespace TizenApis::Commons;
51 using namespace TizenApis::Api::Contact;
53 JSClassDefinition JSContactPhoneNumberArray::m_classInfo = {
55 kJSClassAttributeNone,
65 NULL, //deleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //callAsFunction,
68 NULL, //callAsConstructor,
70 NULL, //convertToType,
73 JSStaticValue JSContactPhoneNumberArray::m_property[] = {
74 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
78 JSStaticFunction JSContactPhoneNumberArray::m_function[] = {
79 { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
80 { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
81 { FUNCTION_POP, pop, kJSPropertyAttributeNone },
82 { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
83 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
84 { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
85 { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
86 { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
87 { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
88 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
89 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
90 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
94 JSClassRef JSContactPhoneNumberArray::m_jsClassRef = JSClassCreate(
95 JSContactPhoneNumberArray::getClassInfo());
97 JSValueRef JSContactPhoneNumberArray::getLength(JSContextRef context,
99 JSStringRef propertyName,
100 JSValueRef* exception)
105 JSContactPhoneNumberArrayPriv* priv =
106 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
111 if (contactPhoneNumbers) {
112 ContactConverterFactory::ConverterType converter =
113 ContactConverterFactory::getConverter(context);
114 return converter->toJSValueRef(contactPhoneNumbers->size());
117 Catch(WrtDeviceApis::Commons::Exception)
119 LogError("invalid conversion");
121 return JSValueMakeUndefined(context);
124 JSObjectRef JSContactPhoneNumberArray::createArray(JSContextRef context,
125 const ContactPhoneNumberArrayPtr &contactPhoneNumbers)
127 JSContactPhoneNumberArrayPriv *priv = new JSContactPhoneNumberArrayPriv(context, contactPhoneNumbers);
128 return JSObjectMake(context, getClassRef(), priv);
131 const JSClassDefinition* JSContactPhoneNumberArray::getClassInfo()
133 return &(m_classInfo);
136 JSClassRef JSContactPhoneNumberArray::getClassRef()
139 m_jsClassRef = JSClassCreate(&m_classInfo);
144 bool JSContactPhoneNumberArray::isObjectOfClass(JSContextRef context, JSValueRef value)
146 return JSValueIsObjectOfClass(context, value, getClassRef());
149 ContactPhoneNumberArrayPtr JSContactPhoneNumberArray::getContactPhoneNumberArray(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 JSContactPhoneNumberArrayPriv *priv = static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
160 Throw(WrtDeviceApis::Commons::NullPointerException);
162 return priv->getObject();
165 void JSContactPhoneNumberArray::initialize(JSContextRef context,
171 void JSContactPhoneNumberArray::finalize(JSObjectRef object)
174 JSContactPhoneNumberArrayPriv* priv =
175 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
177 JSObjectSetPrivate(object, NULL);
180 bool JSContactPhoneNumberArray::hasProperty(JSContextRef context,
182 JSStringRef propertyName)
185 ContactConverterFactory::ConverterType converter =
186 ContactConverterFactory::getConverter(context);
189 size_t index = converter->toSizeT(propertyName);
190 JSContactPhoneNumberArrayPriv* priv =
191 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
193 Throw(WrtDeviceApis::Commons::NullPointerException);
195 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
196 if (index < contactPhoneNumbers->size()) {
200 Catch(WrtDeviceApis::Commons::Exception)
202 //not reporting error is intended
207 JSValueRef JSContactPhoneNumberArray::getProperty(JSContextRef context,
209 JSStringRef propertyName,
210 JSValueRef* exception)
213 ContactConverterFactory::ConverterType converter =
214 ContactConverterFactory::getConverter(context);
217 size_t index = converter->toSizeT(propertyName);
218 JSContactPhoneNumberArrayPriv* priv =
219 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
221 Throw(WrtDeviceApis::Commons::NullPointerException);
223 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
224 if (index < contactPhoneNumbers->size()) {
225 ContactPhoneNumberPtr result = contactPhoneNumbers->at(index);
227 return converter->toJSValueRef(result);
231 Catch(WrtDeviceApis::Commons::Exception)
233 LogError("invalid property");
235 return JSValueMakeUndefined(context);
238 bool JSContactPhoneNumberArray::setProperty(JSContextRef context,
240 JSStringRef propertyName,
242 JSValueRef* exception)
245 ContactConverterFactory::ConverterType converter =
246 ContactConverterFactory::getConverter(context);
249 size_t index = converter->toSizeT(propertyName);
250 ContactPhoneNumberPtr contactPhoneNumber;
251 if (!JSValueIsUndefined(context, value)) {
252 contactPhoneNumber = converter->toContactPhoneNumber(value);
254 JSContactPhoneNumberArrayPriv* priv =
255 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
257 Throw(WrtDeviceApis::Commons::NullPointerException);
259 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
260 if (!contactPhoneNumbers) {
261 Throw(WrtDeviceApis::Commons::NullPointerException);
263 if (contactPhoneNumbers->size() <= index) {
264 contactPhoneNumbers->resize(index + 1);
266 (*contactPhoneNumbers)[index] = contactPhoneNumber;
269 Catch(WrtDeviceApis::Commons::Exception)
271 LogError("error occured");
272 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
277 JSValueRef JSContactPhoneNumberArray::concat(JSContextRef context,
278 JSObjectRef function,
279 JSObjectRef thisObject,
280 size_t argumentCount,
281 const JSValueRef arguments[],
282 JSValueRef* exception)
287 ContactPhoneNumberArrayPtr contactPhoneNumbers = ContactPhoneNumberArrayPtr(new ContactPhoneNumberArray());
288 JSContactPhoneNumberArrayPriv *newPrivateObject = new JSContactPhoneNumberArrayPriv(context, contactPhoneNumbers);
289 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
291 //copy current contactPhoneNumbers
292 JSContactPhoneNumberArrayPriv* priv =
293 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
294 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
295 for (size_t i = 0; i < currentContactPhoneNumbers->size(); ++i) {
296 contactPhoneNumbers->push_back(currentContactPhoneNumbers->at(i));
299 //copy submitted arrays
300 ContactConverterFactory::ConverterType converter =
301 ContactConverterFactory::getConverter(context);
302 for (size_t i = 0; i < argumentCount; ++i) {
303 if (!JSIsArrayValue(context, arguments[i])) {
304 Throw(WrtDeviceApis::Commons::ConversionException);
306 // process array of strings
307 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
308 unsigned int len = JSGetArrayLength(context, arrayObj);
309 for (unsigned int e = 0; e < len; ++e) {
310 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
311 contactPhoneNumbers->push_back(converter->toContactPhoneNumber(att));
316 Catch(WrtDeviceApis::Commons::Exception)
318 LogError("error occured");
320 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
323 JSValueRef JSContactPhoneNumberArray::join(JSContextRef context,
324 JSObjectRef function,
325 JSObjectRef thisObject,
326 size_t argumentCount,
327 const JSValueRef arguments[],
328 JSValueRef* exception)
330 //LogDebug("entered");
334 std::string separator(",");
335 ContactConverterFactory::ConverterType converter =
336 ContactConverterFactory::getConverter(context);
337 JSContactPhoneNumberArrayPriv* priv =
338 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
339 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
340 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
341 separator = converter->toString(arguments[0]);
343 for (size_t i = 0; i < currentContactPhoneNumbers->size(); ++i) {
347 //FIXME : to be changed to support join
348 //result += currentContactPhoneNumbers->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 JSContactPhoneNumberArray::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 JSContactPhoneNumberArrayPriv* priv =
372 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
373 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
374 if (currentContactPhoneNumbers->size() > 0) {
375 ContactPhoneNumberPtr result = currentContactPhoneNumbers->at(
376 currentContactPhoneNumbers->size() - 1);
377 currentContactPhoneNumbers->pop_back();
378 return converter->toJSValueRef(result);
381 Catch(WrtDeviceApis::Commons::Exception)
383 LogError("error occured");
385 return JSValueMakeUndefined(context);
388 JSValueRef JSContactPhoneNumberArray::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 JSContactPhoneNumberArrayPriv* priv =
401 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
402 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
403 for (size_t i = 0; i < argumentCount; ++i) {
404 currentContactPhoneNumbers->push_back(converter->toContactPhoneNumber(arguments[i]));
406 return converter->toJSValueRef(currentContactPhoneNumbers->size());
408 Catch(WrtDeviceApis::Commons::Exception)
410 LogError("error occured");
412 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
415 JSValueRef JSContactPhoneNumberArray::reverse(JSContextRef context,
416 JSObjectRef function,
417 JSObjectRef thisObject,
418 size_t argumentCount,
419 const JSValueRef arguments[],
420 JSValueRef* exception)
422 //LogDebug("entered");
425 ContactConverterFactory::ConverterType converter =
426 ContactConverterFactory::getConverter(context);
427 JSContactPhoneNumberArrayPriv* priv =
428 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
429 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
430 std::reverse(currentContactPhoneNumbers->begin(), currentContactPhoneNumbers->end());
433 Catch(WrtDeviceApis::Commons::Exception)
435 LogError("error occured");
437 return JSValueMakeUndefined(context);
440 JSValueRef JSContactPhoneNumberArray::shift(JSContextRef context,
441 JSObjectRef function,
442 JSObjectRef thisObject,
443 size_t argumentCount,
444 const JSValueRef arguments[],
445 JSValueRef* exception)
447 //LogDebug("entered");
450 ContactConverterFactory::ConverterType converter =
451 ContactConverterFactory::getConverter(context);
452 JSContactPhoneNumberArrayPriv* priv =
453 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
454 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
455 if (currentContactPhoneNumbers->size() > 0) {
456 ContactPhoneNumberPtr result = currentContactPhoneNumbers->at(0);
457 currentContactPhoneNumbers->erase(currentContactPhoneNumbers->begin());
458 return converter->toJSValueRef(result);
461 Catch(WrtDeviceApis::Commons::Exception)
463 LogError("error occured");
465 return JSValueMakeUndefined(context);
468 JSValueRef JSContactPhoneNumberArray::slice(JSContextRef context,
469 JSObjectRef function,
470 JSObjectRef thisObject,
471 size_t argumentCount,
472 const JSValueRef arguments[],
473 JSValueRef* exception)
478 if (argumentCount < 1) {
479 return JSValueMakeUndefined(context);
481 ContactConverterFactory::ConverterType converter =
482 ContactConverterFactory::getConverter(context);
483 ContactPhoneNumberArrayPtr contactPhoneNumbers = ContactPhoneNumberArrayPtr(new ContactPhoneNumberArray());
484 JSContactPhoneNumberArrayPriv *newPrivateObject = new JSContactPhoneNumberArrayPriv(
486 contactPhoneNumbers);
487 JSValueRef result = JSObjectMake(context,
488 getClassRef(), newPrivateObject);
490 //copy current contactPhoneNumbers
491 JSContactPhoneNumberArrayPriv* priv =
492 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
493 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
494 std::size_t first = converter->toSizeT(arguments[0]);
495 std::size_t last = currentContactPhoneNumbers->size() - 1;
496 if (argumentCount > 1) {
497 last = converter->toSizeT(arguments[1]);
498 if (last >= currentContactPhoneNumbers->size()) {
499 last = currentContactPhoneNumbers->size() - 1;
505 for (size_t i = first; i <= last; ++i) {
506 contactPhoneNumbers->push_back(currentContactPhoneNumbers->at(i));
511 Catch(WrtDeviceApis::Commons::Exception)
513 LogError("error occured");
515 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
518 JSValueRef JSContactPhoneNumberArray::sort(JSContextRef context,
519 JSObjectRef function,
520 JSObjectRef thisObject,
521 size_t argumentCount,
522 const JSValueRef arguments[],
523 JSValueRef* exception)
525 //LogDebug("entered");
528 ContactConverterFactory::ConverterType converter =
529 ContactConverterFactory::getConverter(context);
530 JSContactPhoneNumberArrayPriv* priv =
531 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
532 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
533 std::sort(currentContactPhoneNumbers->begin(), currentContactPhoneNumbers->end());
536 Catch(WrtDeviceApis::Commons::Exception)
538 LogError("error occured");
540 return JSValueMakeUndefined(context);
543 JSValueRef JSContactPhoneNumberArray::splice(JSContextRef context,
544 JSObjectRef function,
545 JSObjectRef thisObject,
546 size_t argumentCount,
547 const JSValueRef arguments[],
548 JSValueRef* exception)
550 //LogDebug("entered");
551 return JSValueMakeUndefined(context);
554 JSValueRef JSContactPhoneNumberArray::toString(JSContextRef context,
555 JSObjectRef function,
556 JSObjectRef thisObject,
557 size_t argumentCount,
558 const JSValueRef arguments[],
559 JSValueRef* exception)
561 //LogDebug("entered");
562 return join(context, function, thisObject, 0, arguments, exception);
565 JSValueRef JSContactPhoneNumberArray::unshift(JSContextRef context,
566 JSObjectRef function,
567 JSObjectRef thisObject,
568 size_t argumentCount,
569 const JSValueRef arguments[],
570 JSValueRef* exception)
572 //LogDebug("entered");
573 return JSValueMakeUndefined(context);
576 JSValueRef JSContactPhoneNumberArray::valueOf(JSContextRef context,
577 JSObjectRef function,
578 JSObjectRef thisObject,
579 size_t argumentCount,
580 const JSValueRef arguments[],
581 JSValueRef* exception)
583 //LogDebug("entered");
584 return JSValueMakeUndefined(context);