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 JSContactEmailAddressTypeArray.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 "JSContactEmailAddressTypeArray.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;
53 using namespace WrtDeviceApis::Commons;
54 using namespace WrtDeviceApis::CommonsJavaScript;
56 JSClassDefinition JSContactEmailAddressTypeArray::m_classInfo = {
58 kJSClassAttributeNone,
68 NULL, //deleteProperty,
69 NULL, //getPropertyNames,
70 NULL, //callAsFunction,
71 NULL, //callAsConstructor,
73 NULL, //convertToType,
76 JSStaticValue JSContactEmailAddressTypeArray::m_property[] = {
77 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
81 JSStaticFunction JSContactEmailAddressTypeArray::m_function[] = {
82 { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
83 { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
84 { FUNCTION_POP, pop, kJSPropertyAttributeNone },
85 { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
86 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
87 { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
88 { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
89 { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
90 { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
91 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
92 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
93 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
97 JSClassRef JSContactEmailAddressTypeArray::m_jsClassRef = JSClassCreate(
98 JSContactEmailAddressTypeArray::getClassInfo());
100 JSValueRef JSContactEmailAddressTypeArray::getLength(JSContextRef context,
102 JSStringRef propertyName,
103 JSValueRef* exception)
108 JSContactEmailAddressTypeArrayPriv* priv =
109 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
111 Throw(NullPointerException);
113 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
115 BasicConverter converter = BasicConverterFactory::getConverter(context);
116 return converter->toJSValueRef(typeArray->size());
121 LogError("invalid conversion");
123 return JSValueMakeUndefined(context);
126 JSObjectRef JSContactEmailAddressTypeArray::createArray(JSContextRef context,
127 const ContactEmailAddressTypeArrayPtr &typeArray)
129 JSContactEmailAddressTypeArrayPriv *priv = new JSContactEmailAddressTypeArrayPriv(context, typeArray);
130 return JSObjectMake(context, getClassRef(), priv);
133 bool JSContactEmailAddressTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
135 return JSValueIsObjectOfClass(context, value, getClassRef());
138 ContactEmailAddressTypeArrayPtr JSContactEmailAddressTypeArray::getContactEmailAddressTypeArray(JSContextRef context, JSValueRef value)
140 if (!isObjectOfClass(context, value)) {
141 Throw(InvalidArgumentException);
143 JSObjectRef object = JSValueToObject(context, value, NULL);
145 Throw(InvalidArgumentException);
147 JSContactEmailAddressTypeArrayPriv *priv = static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
149 Throw(NullPointerException);
151 return priv->getObject();
154 const JSClassDefinition* JSContactEmailAddressTypeArray::getClassInfo()
156 return &(m_classInfo);
159 JSClassRef JSContactEmailAddressTypeArray::getClassRef()
162 m_jsClassRef = JSClassCreate(&m_classInfo);
167 void JSContactEmailAddressTypeArray::initialize(JSContextRef context,
173 void JSContactEmailAddressTypeArray::finalize(JSObjectRef object)
176 JSContactEmailAddressTypeArrayPriv* priv =
177 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
179 JSObjectSetPrivate(object, NULL);
182 bool JSContactEmailAddressTypeArray::hasProperty(JSContextRef context,
184 JSStringRef propertyName)
187 BasicConverter converter = BasicConverterFactory::getConverter(context);
190 size_t index = converter->toSizeT(propertyName);
191 JSContactEmailAddressTypeArrayPriv* priv =
192 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
194 Throw(NullPointerException);
196 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
197 if (index < typeArray->size()) {
203 //not reporting error is intended
208 JSValueRef JSContactEmailAddressTypeArray::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 JSContactEmailAddressTypeArrayPriv* priv =
220 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
222 Throw(NullPointerException);
224 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
225 if (index < typeArray->size()) {
226 std::string result = converter->toContactEmailAddressTypeStr(typeArray->at(index));
227 if (!result.empty()) {
228 return converter->toJSValueRef(result);
234 LogError("invalid property");
236 return JSValueMakeUndefined(context);
239 bool JSContactEmailAddressTypeArray::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 JSContactEmailAddressTypeArrayPriv* priv =
256 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
258 Throw(NullPointerException);
260 ContactEmailAddressTypeArrayPtr typeArray = priv->getObject();
262 Throw(NullPointerException);
264 if (typeArray->size() <= index) {
265 typeArray->resize(index + 1);
267 (*typeArray)[index] = converter->toContactEmailAddressType(str);
272 LogError("error occured");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
278 JSValueRef JSContactEmailAddressTypeArray::concat(JSContextRef context,
279 JSObjectRef function,
280 JSObjectRef thisObject,
281 size_t argumentCount,
282 const JSValueRef arguments[],
283 JSValueRef* exception)
288 ContactEmailAddressTypeArrayPtr typeArray = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
289 JSContactEmailAddressTypeArrayPriv *newPrivateObject = new JSContactEmailAddressTypeArrayPriv(
292 JSValueRef result = JSObjectMake(context,
293 getClassRef(), newPrivateObject);
295 //copy current typeArray
296 JSContactEmailAddressTypeArrayPriv* priv =
297 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
298 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
299 for (size_t i = 0; i < currentStrings->size(); ++i) {
300 typeArray->push_back(currentStrings->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(ConversionException);
310 // process array of strings
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->toContactEmailAddressType(att));
322 LogError("error occured");
324 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
327 JSValueRef JSContactEmailAddressTypeArray::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 BasicConverter converter = BasicConverterFactory::getConverter(context);
340 JSContactEmailAddressTypeArrayPriv* priv =
341 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
342 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
343 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
344 separator = converter->toString(arguments[0]);
346 for (size_t i = 0; i < currentStrings->size(); ++i) {
350 result += currentStrings->at(i);
352 return converter->toJSValueRef(result);
356 LogError("error occured");
358 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
361 JSValueRef JSContactEmailAddressTypeArray::pop(JSContextRef context,
362 JSObjectRef function,
363 JSObjectRef thisObject,
364 size_t argumentCount,
365 const JSValueRef arguments[],
366 JSValueRef* exception)
368 //LogDebug("entered");
371 ContactConverterFactory::ConverterType converter =
372 ContactConverterFactory::getConverter(context);
373 JSContactEmailAddressTypeArrayPriv* priv =
374 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
375 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
376 if (currentStrings->size() > 0) {
377 std::string result = converter->toContactEmailAddressTypeStr(currentStrings->at(currentStrings->size() - 1));
378 currentStrings->pop_back();
379 return converter->toJSValueRef(result);
384 LogError("error occured");
386 return JSValueMakeUndefined(context);
389 JSValueRef JSContactEmailAddressTypeArray::push(JSContextRef context,
390 JSObjectRef function,
391 JSObjectRef thisObject,
392 size_t argumentCount,
393 const JSValueRef arguments[],
394 JSValueRef* exception)
396 //LogDebug("entered");
399 ContactConverterFactory::ConverterType converter =
400 ContactConverterFactory::getConverter(context);
401 JSContactEmailAddressTypeArrayPriv* priv =
402 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
403 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
404 for (size_t i = 0; i < argumentCount; ++i) {
405 currentStrings->push_back(converter->toContactEmailAddressType(arguments[i]));
407 return converter->toJSValueRef(currentStrings->size());
411 LogError("error occured");
413 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
416 JSValueRef JSContactEmailAddressTypeArray::reverse(JSContextRef context,
417 JSObjectRef function,
418 JSObjectRef thisObject,
419 size_t argumentCount,
420 const JSValueRef arguments[],
421 JSValueRef* exception)
423 //LogDebug("entered");
426 Converter converter(context);
427 JSContactEmailAddressTypeArrayPriv* priv =
428 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
429 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
430 std::reverse(currentStrings->begin(), currentStrings->end());
435 LogError("error occured");
437 return JSValueMakeUndefined(context);
440 JSValueRef JSContactEmailAddressTypeArray::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 JSContactEmailAddressTypeArrayPriv* priv =
453 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
454 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
455 if (currentStrings->size() > 0) {
456 std::string result = converter->toContactEmailAddressTypeStr(currentStrings->at(0));
457 currentStrings->erase(currentStrings->begin());
458 return converter->toJSValueRef(result);
463 LogError("error occured");
465 return JSValueMakeUndefined(context);
468 JSValueRef JSContactEmailAddressTypeArray::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 BasicConverter converter = BasicConverterFactory::getConverter(context);
482 ContactEmailAddressTypeArrayPtr typeArray = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
483 JSContactEmailAddressTypeArrayPriv *newPrivateObject = new JSContactEmailAddressTypeArrayPriv(
486 JSValueRef result = JSObjectMake(context,
487 getClassRef(), newPrivateObject);
489 //copy current typeArray
490 JSContactEmailAddressTypeArrayPriv* priv =
491 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
492 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
493 std::size_t first = converter->toSizeT(arguments[0]);
494 std::size_t last = currentStrings->size() - 1;
495 if (argumentCount > 1) {
496 last = converter->toSizeT(arguments[1]);
497 if (last >= currentStrings->size()) {
498 last = currentStrings->size() - 1;
504 for (size_t i = first; i <= last; ++i) {
505 typeArray->push_back(currentStrings->at(i));
512 LogError("error occured");
514 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
517 JSValueRef JSContactEmailAddressTypeArray::sort(JSContextRef context,
518 JSObjectRef function,
519 JSObjectRef thisObject,
520 size_t argumentCount,
521 const JSValueRef arguments[],
522 JSValueRef* exception)
524 //LogDebug("entered");
527 BasicConverter converter = BasicConverterFactory::getConverter(context);
528 JSContactEmailAddressTypeArrayPriv* priv =
529 static_cast<JSContactEmailAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
530 ContactEmailAddressTypeArrayPtr currentStrings = priv->getObject();
531 std::sort(currentStrings->begin(), currentStrings->end());
536 LogError("error occured");
538 return JSValueMakeUndefined(context);
541 JSValueRef JSContactEmailAddressTypeArray::splice(JSContextRef context,
542 JSObjectRef function,
543 JSObjectRef thisObject,
544 size_t argumentCount,
545 const JSValueRef arguments[],
546 JSValueRef* exception)
548 //LogDebug("entered");
549 return JSValueMakeUndefined(context);
552 JSValueRef JSContactEmailAddressTypeArray::toString(JSContextRef context,
553 JSObjectRef function,
554 JSObjectRef thisObject,
555 size_t argumentCount,
556 const JSValueRef arguments[],
557 JSValueRef* exception)
559 //LogDebug("entered");
560 return join(context, function, thisObject, 0, arguments, exception);
563 JSValueRef JSContactEmailAddressTypeArray::unshift(JSContextRef context,
564 JSObjectRef function,
565 JSObjectRef thisObject,
566 size_t argumentCount,
567 const JSValueRef arguments[],
568 JSValueRef* exception)
570 //LogDebug("entered");
571 return JSValueMakeUndefined(context);
574 JSValueRef JSContactEmailAddressTypeArray::valueOf(JSContextRef context,
575 JSObjectRef function,
576 JSObjectRef thisObject,
577 size_t argumentCount,
578 const JSValueRef arguments[],
579 JSValueRef* exception)
581 //LogDebug("entered");
582 return JSValueMakeUndefined(context);