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)
25 #include <dpl/log/log.h>
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)
104 JSContactPhoneNumberArrayPriv* priv =
105 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
107 Throw(WrtDeviceApis::Commons::NullPointerException);
109 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
110 if (contactPhoneNumbers) {
111 ContactConverterFactory::ConverterType converter =
112 ContactConverterFactory::getConverter(context);
113 return converter->toJSValueRef(contactPhoneNumbers->size());
116 Catch(WrtDeviceApis::Commons::Exception)
118 LogError("invalid conversion");
120 return JSValueMakeUndefined(context);
123 JSObjectRef JSContactPhoneNumberArray::createArray(JSContextRef context,
124 const ContactPhoneNumberArrayPtr &contactPhoneNumbers)
126 JSContactPhoneNumberArrayPriv *priv = new JSContactPhoneNumberArrayPriv(context, contactPhoneNumbers);
127 return JSObjectMake(context, getClassRef(), priv);
130 const JSClassDefinition* JSContactPhoneNumberArray::getClassInfo()
132 return &(m_classInfo);
135 JSClassRef JSContactPhoneNumberArray::getClassRef()
138 m_jsClassRef = JSClassCreate(&m_classInfo);
143 bool JSContactPhoneNumberArray::isObjectOfClass(JSContextRef context, JSValueRef value)
145 return JSValueIsObjectOfClass(context, value, getClassRef());
148 ContactPhoneNumberArrayPtr JSContactPhoneNumberArray::getContactPhoneNumberArray(JSContextRef context, JSValueRef value)
150 if (!isObjectOfClass(context, value)) {
151 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
153 JSObjectRef object = JSValueToObject(context, value, NULL);
155 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
157 JSContactPhoneNumberArrayPriv *priv = static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
159 Throw(WrtDeviceApis::Commons::NullPointerException);
161 return priv->getObject();
164 void JSContactPhoneNumberArray::initialize(JSContextRef context,
169 void JSContactPhoneNumberArray::finalize(JSObjectRef object)
171 JSContactPhoneNumberArrayPriv* priv =
172 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
174 JSObjectSetPrivate(object, NULL);
177 bool JSContactPhoneNumberArray::hasProperty(JSContextRef context,
179 JSStringRef propertyName)
181 ContactConverterFactory::ConverterType converter =
182 ContactConverterFactory::getConverter(context);
185 size_t index = converter->toSizeT(propertyName);
186 JSContactPhoneNumberArrayPriv* priv =
187 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
189 Throw(WrtDeviceApis::Commons::NullPointerException);
191 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
192 if (index < contactPhoneNumbers->size()) {
196 Catch(WrtDeviceApis::Commons::Exception)
198 //not reporting error is intended
203 JSValueRef JSContactPhoneNumberArray::getProperty(JSContextRef context,
205 JSStringRef propertyName,
206 JSValueRef* exception)
208 ContactConverterFactory::ConverterType converter =
209 ContactConverterFactory::getConverter(context);
212 size_t index = converter->toSizeT(propertyName);
213 JSContactPhoneNumberArrayPriv* priv =
214 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
216 Throw(WrtDeviceApis::Commons::NullPointerException);
218 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
219 if (index < contactPhoneNumbers->size()) {
220 ContactPhoneNumberPtr result = contactPhoneNumbers->at(index);
222 return converter->toJSValueRef(result);
226 Catch(WrtDeviceApis::Commons::Exception)
228 LogError("invalid property");
230 return JSValueMakeUndefined(context);
233 bool JSContactPhoneNumberArray::setProperty(JSContextRef context,
235 JSStringRef propertyName,
237 JSValueRef* exception)
239 ContactConverterFactory::ConverterType converter =
240 ContactConverterFactory::getConverter(context);
243 size_t index = converter->toSizeT(propertyName);
244 ContactPhoneNumberPtr contactPhoneNumber;
245 if (!JSValueIsUndefined(context, value)) {
246 contactPhoneNumber = converter->toContactPhoneNumber(value);
248 JSContactPhoneNumberArrayPriv* priv =
249 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(object));
251 Throw(WrtDeviceApis::Commons::NullPointerException);
253 ContactPhoneNumberArrayPtr contactPhoneNumbers = priv->getObject();
254 if (!contactPhoneNumbers) {
255 Throw(WrtDeviceApis::Commons::NullPointerException);
257 if (contactPhoneNumbers->size() <= index) {
258 contactPhoneNumbers->resize(index + 1);
260 (*contactPhoneNumbers)[index] = contactPhoneNumber;
263 Catch(WrtDeviceApis::Commons::Exception)
265 LogError("error occured");
266 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
271 JSValueRef JSContactPhoneNumberArray::concat(JSContextRef context,
272 JSObjectRef function,
273 JSObjectRef thisObject,
274 size_t argumentCount,
275 const JSValueRef arguments[],
276 JSValueRef* exception)
280 ContactPhoneNumberArrayPtr contactPhoneNumbers = ContactPhoneNumberArrayPtr(new ContactPhoneNumberArray());
281 JSContactPhoneNumberArrayPriv *newPrivateObject = new JSContactPhoneNumberArrayPriv(context, contactPhoneNumbers);
282 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
284 //copy current contactPhoneNumbers
285 JSContactPhoneNumberArrayPriv* priv =
286 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
287 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
288 for (size_t i = 0; i < currentContactPhoneNumbers->size(); ++i) {
289 contactPhoneNumbers->push_back(currentContactPhoneNumbers->at(i));
292 //copy submitted arrays
293 ContactConverterFactory::ConverterType converter =
294 ContactConverterFactory::getConverter(context);
295 for (size_t i = 0; i < argumentCount; ++i) {
296 if (!JSIsArrayValue(context, arguments[i])) {
297 Throw(WrtDeviceApis::Commons::ConversionException);
299 // process array of strings
300 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
301 unsigned int len = JSGetArrayLength(context, arrayObj);
302 for (unsigned int e = 0; e < len; ++e) {
303 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
304 contactPhoneNumbers->push_back(converter->toContactPhoneNumber(att));
309 Catch(WrtDeviceApis::Commons::Exception)
311 LogError("error occured");
313 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
316 JSValueRef JSContactPhoneNumberArray::join(JSContextRef context,
317 JSObjectRef function,
318 JSObjectRef thisObject,
319 size_t argumentCount,
320 const JSValueRef arguments[],
321 JSValueRef* exception)
326 std::string separator(",");
327 ContactConverterFactory::ConverterType converter =
328 ContactConverterFactory::getConverter(context);
329 JSContactPhoneNumberArrayPriv* priv =
330 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
331 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
332 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
333 separator = converter->toString(arguments[0]);
335 for (size_t i = 0; i < currentContactPhoneNumbers->size(); ++i) {
339 //FIXME : to be changed to support join
340 //result += currentContactPhoneNumbers->at(i);
342 return converter->toJSValueRef(result);
344 Catch(WrtDeviceApis::Commons::Exception)
346 LogError("error occured");
348 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
351 JSValueRef JSContactPhoneNumberArray::pop(JSContextRef context,
352 JSObjectRef function,
353 JSObjectRef thisObject,
354 size_t argumentCount,
355 const JSValueRef arguments[],
356 JSValueRef* exception)
360 ContactConverterFactory::ConverterType converter =
361 ContactConverterFactory::getConverter(context);
362 JSContactPhoneNumberArrayPriv* priv =
363 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
364 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
365 if (currentContactPhoneNumbers->size() > 0) {
366 ContactPhoneNumberPtr result = currentContactPhoneNumbers->at(
367 currentContactPhoneNumbers->size() - 1);
368 currentContactPhoneNumbers->pop_back();
369 return converter->toJSValueRef(result);
372 Catch(WrtDeviceApis::Commons::Exception)
374 LogError("error occured");
376 return JSValueMakeUndefined(context);
379 JSValueRef JSContactPhoneNumberArray::push(JSContextRef context,
380 JSObjectRef function,
381 JSObjectRef thisObject,
382 size_t argumentCount,
383 const JSValueRef arguments[],
384 JSValueRef* exception)
388 ContactConverterFactory::ConverterType converter =
389 ContactConverterFactory::getConverter(context);
390 JSContactPhoneNumberArrayPriv* priv =
391 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
392 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
393 for (size_t i = 0; i < argumentCount; ++i) {
394 currentContactPhoneNumbers->push_back(converter->toContactPhoneNumber(arguments[i]));
396 return converter->toJSValueRef(currentContactPhoneNumbers->size());
398 Catch(WrtDeviceApis::Commons::Exception)
400 LogError("error occured");
402 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
405 JSValueRef JSContactPhoneNumberArray::reverse(JSContextRef context,
406 JSObjectRef function,
407 JSObjectRef thisObject,
408 size_t argumentCount,
409 const JSValueRef arguments[],
410 JSValueRef* exception)
414 ContactConverterFactory::ConverterType converter =
415 ContactConverterFactory::getConverter(context);
416 JSContactPhoneNumberArrayPriv* priv =
417 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
418 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
419 std::reverse(currentContactPhoneNumbers->begin(), currentContactPhoneNumbers->end());
422 Catch(WrtDeviceApis::Commons::Exception)
424 LogError("error occured");
426 return JSValueMakeUndefined(context);
429 JSValueRef JSContactPhoneNumberArray::shift(JSContextRef context,
430 JSObjectRef function,
431 JSObjectRef thisObject,
432 size_t argumentCount,
433 const JSValueRef arguments[],
434 JSValueRef* exception)
438 ContactConverterFactory::ConverterType converter =
439 ContactConverterFactory::getConverter(context);
440 JSContactPhoneNumberArrayPriv* priv =
441 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
442 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
443 if (currentContactPhoneNumbers->size() > 0) {
444 ContactPhoneNumberPtr result = currentContactPhoneNumbers->at(0);
445 currentContactPhoneNumbers->erase(currentContactPhoneNumbers->begin());
446 return converter->toJSValueRef(result);
449 Catch(WrtDeviceApis::Commons::Exception)
451 LogError("error occured");
453 return JSValueMakeUndefined(context);
456 JSValueRef JSContactPhoneNumberArray::slice(JSContextRef context,
457 JSObjectRef function,
458 JSObjectRef thisObject,
459 size_t argumentCount,
460 const JSValueRef arguments[],
461 JSValueRef* exception)
465 if (argumentCount < 1) {
466 return JSValueMakeUndefined(context);
468 ContactConverterFactory::ConverterType converter =
469 ContactConverterFactory::getConverter(context);
470 ContactPhoneNumberArrayPtr contactPhoneNumbers = ContactPhoneNumberArrayPtr(new ContactPhoneNumberArray());
471 JSContactPhoneNumberArrayPriv *newPrivateObject = new JSContactPhoneNumberArrayPriv(
473 contactPhoneNumbers);
474 JSValueRef result = JSObjectMake(context,
475 getClassRef(), newPrivateObject);
477 //copy current contactPhoneNumbers
478 JSContactPhoneNumberArrayPriv* priv =
479 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
480 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
481 std::size_t first = converter->toSizeT(arguments[0]);
482 std::size_t last = currentContactPhoneNumbers->size() - 1;
483 if (argumentCount > 1) {
484 last = converter->toSizeT(arguments[1]);
485 if (last >= currentContactPhoneNumbers->size()) {
486 last = currentContactPhoneNumbers->size() - 1;
492 for (size_t i = first; i <= last; ++i) {
493 contactPhoneNumbers->push_back(currentContactPhoneNumbers->at(i));
498 Catch(WrtDeviceApis::Commons::Exception)
500 LogError("error occured");
502 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
505 JSValueRef JSContactPhoneNumberArray::sort(JSContextRef context,
506 JSObjectRef function,
507 JSObjectRef thisObject,
508 size_t argumentCount,
509 const JSValueRef arguments[],
510 JSValueRef* exception)
514 ContactConverterFactory::ConverterType converter =
515 ContactConverterFactory::getConverter(context);
516 JSContactPhoneNumberArrayPriv* priv =
517 static_cast<JSContactPhoneNumberArrayPriv*>(JSObjectGetPrivate(thisObject));
518 ContactPhoneNumberArrayPtr currentContactPhoneNumbers = priv->getObject();
519 std::sort(currentContactPhoneNumbers->begin(), currentContactPhoneNumbers->end());
522 Catch(WrtDeviceApis::Commons::Exception)
524 LogError("error occured");
526 return JSValueMakeUndefined(context);
529 JSValueRef JSContactPhoneNumberArray::splice(JSContextRef context,
530 JSObjectRef function,
531 JSObjectRef thisObject,
532 size_t argumentCount,
533 const JSValueRef arguments[],
534 JSValueRef* exception)
536 return JSValueMakeUndefined(context);
539 JSValueRef JSContactPhoneNumberArray::toString(JSContextRef context,
540 JSObjectRef function,
541 JSObjectRef thisObject,
542 size_t argumentCount,
543 const JSValueRef arguments[],
544 JSValueRef* exception)
546 return join(context, function, thisObject, 0, arguments, exception);
549 JSValueRef JSContactPhoneNumberArray::unshift(JSContextRef context,
550 JSObjectRef function,
551 JSObjectRef thisObject,
552 size_t argumentCount,
553 const JSValueRef arguments[],
554 JSValueRef* exception)
556 return JSValueMakeUndefined(context);
559 JSValueRef JSContactPhoneNumberArray::valueOf(JSContextRef context,
560 JSObjectRef function,
561 JSObjectRef thisObject,
562 size_t argumentCount,
563 const JSValueRef arguments[],
564 JSValueRef* exception)
566 return JSValueMakeUndefined(context);