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 JSContactOrganizationArray.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 "JSContactOrganizationArray.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 JSContactOrganizationArray::m_classInfo = {
55 kJSClassAttributeNone,
65 NULL, //deleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //callAsFunction,
68 NULL, //callAsConstructor,
70 NULL, //convertToType,
73 JSStaticValue JSContactOrganizationArray::m_property[] = {
74 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
78 JSStaticFunction JSContactOrganizationArray::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 JSContactOrganizationArray::m_jsClassRef = JSClassCreate(
95 JSContactOrganizationArray::getClassInfo());
97 JSValueRef JSContactOrganizationArray::getLength(JSContextRef context,
99 JSStringRef propertyName,
100 JSValueRef* exception)
105 JSContactOrganizationArrayPriv* priv =
106 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 ContactOrganizationArrayPtr organizations = priv->getObject();
112 ContactConverterFactory::ConverterType converter =
113 ContactConverterFactory::getConverter(context);
114 return converter->toJSValueRef(organizations->size());
117 Catch(WrtDeviceApis::Commons::Exception)
119 LogError("invalid conversion");
121 return JSValueMakeUndefined(context);
124 JSObjectRef JSContactOrganizationArray::createArray(JSContextRef context,
125 const ContactOrganizationArrayPtr &organizations)
127 JSContactOrganizationArrayPriv *priv = new JSContactOrganizationArrayPriv(context, organizations);
128 return JSObjectMake(context, getClassRef(), priv);
131 bool JSContactOrganizationArray::isObjectOfClass(JSContextRef context, JSValueRef value)
133 return JSValueIsObjectOfClass(context, value, getClassRef());
136 ContactOrganizationArrayPtr JSContactOrganizationArray::getContactOrganizationArray(JSContextRef context, JSValueRef value)
138 if (!isObjectOfClass(context, value)) {
139 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
141 JSObjectRef object = JSValueToObject(context, value, NULL);
143 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
145 JSContactOrganizationArrayPriv *priv = static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(object));
147 Throw(WrtDeviceApis::Commons::NullPointerException);
149 return priv->getObject();
151 const JSClassDefinition* JSContactOrganizationArray::getClassInfo()
153 return &(m_classInfo);
156 JSClassRef JSContactOrganizationArray::getClassRef()
159 m_jsClassRef = JSClassCreate(&m_classInfo);
164 void JSContactOrganizationArray::initialize(JSContextRef context,
170 void JSContactOrganizationArray::finalize(JSObjectRef object)
173 JSContactOrganizationArrayPriv* priv =
174 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(object));
176 JSObjectSetPrivate(object, NULL);
179 bool JSContactOrganizationArray::hasProperty(JSContextRef context,
181 JSStringRef propertyName)
184 ContactConverterFactory::ConverterType converter =
185 ContactConverterFactory::getConverter(context);
188 size_t index = converter->toSizeT(propertyName);
189 JSContactOrganizationArrayPriv* priv =
190 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(object));
192 Throw(WrtDeviceApis::Commons::NullPointerException);
194 ContactOrganizationArrayPtr organizations = priv->getObject();
195 if (index < organizations->size()) {
199 Catch(WrtDeviceApis::Commons::Exception)
201 //not reporting error is intended
206 JSValueRef JSContactOrganizationArray::getProperty(JSContextRef context,
208 JSStringRef propertyName,
209 JSValueRef* exception)
212 ContactConverterFactory::ConverterType converter =
213 ContactConverterFactory::getConverter(context);
216 size_t index = converter->toSizeT(propertyName);
217 JSContactOrganizationArrayPriv* priv =
218 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(object));
220 Throw(WrtDeviceApis::Commons::NullPointerException);
222 ContactOrganizationArrayPtr organizations = priv->getObject();
223 if (index < organizations->size()) {
224 ContactOrganizationPtr result = organizations->at(index);
226 return converter->toJSValueRef(result);
230 Catch(WrtDeviceApis::Commons::Exception)
232 LogError("invalid property");
234 return JSValueMakeUndefined(context);
237 bool JSContactOrganizationArray::setProperty(JSContextRef context,
239 JSStringRef propertyName,
241 JSValueRef* exception)
244 ContactConverterFactory::ConverterType converter =
245 ContactConverterFactory::getConverter(context);
248 size_t index = converter->toSizeT(propertyName);
249 ContactOrganizationPtr organization;
250 if (!JSValueIsUndefined(context, value)) {
251 organization = converter->toContactOrganization(value);
253 JSContactOrganizationArrayPriv* priv =
254 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(object));
256 Throw(WrtDeviceApis::Commons::NullPointerException);
258 ContactOrganizationArrayPtr organizations = priv->getObject();
259 if (!organizations) {
260 Throw(WrtDeviceApis::Commons::NullPointerException);
262 if (organizations->size() <= index) {
263 organizations->resize(index + 1);
265 (*organizations)[index] = organization;
268 Catch(WrtDeviceApis::Commons::Exception)
270 LogError("error occured");
271 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
276 JSValueRef JSContactOrganizationArray::concat(JSContextRef context,
277 JSObjectRef function,
278 JSObjectRef thisObject,
279 size_t argumentCount,
280 const JSValueRef arguments[],
281 JSValueRef* exception)
286 ContactOrganizationArrayPtr organizations = ContactOrganizationArrayPtr(new ContactOrganizationArray());
287 JSContactOrganizationArrayPriv *newPrivateObject = new JSContactOrganizationArrayPriv(context, organizations);
288 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
290 //copy current organizations
291 JSContactOrganizationArrayPriv* priv =
292 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
293 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
294 for (size_t i = 0; i < currentOrganizations->size(); ++i) {
295 organizations->push_back(currentOrganizations->at(i));
298 //copy submitted arrays
299 ContactConverterFactory::ConverterType converter =
300 ContactConverterFactory::getConverter(context);
301 for (size_t i = 0; i < argumentCount; ++i) {
302 if (!JSIsArrayValue(context, arguments[i])) {
303 Throw(WrtDeviceApis::Commons::ConversionException);
305 // process array of strings
306 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
307 unsigned int len = JSGetArrayLength(context, arrayObj);
308 for (unsigned int e = 0; e < len; ++e) {
309 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
310 organizations->push_back(converter->toContactOrganization(att));
315 Catch(WrtDeviceApis::Commons::Exception)
317 LogError("error occured");
319 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
322 JSValueRef JSContactOrganizationArray::join(JSContextRef context,
323 JSObjectRef function,
324 JSObjectRef thisObject,
325 size_t argumentCount,
326 const JSValueRef arguments[],
327 JSValueRef* exception)
329 //LogDebug("entered");
333 std::string separator(",");
334 ContactConverterFactory::ConverterType converter =
335 ContactConverterFactory::getConverter(context);
336 JSContactOrganizationArrayPriv* priv =
337 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
338 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
339 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
340 separator = converter->toString(arguments[0]);
342 for (size_t i = 0; i < currentOrganizations->size(); ++i) {
346 //FIXME : to be changed to support join
347 //result += currentOrganizations->at(i);
349 return converter->toJSValueRef(result);
351 Catch(WrtDeviceApis::Commons::Exception)
353 LogError("error occured");
355 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
358 JSValueRef JSContactOrganizationArray::pop(JSContextRef context,
359 JSObjectRef function,
360 JSObjectRef thisObject,
361 size_t argumentCount,
362 const JSValueRef arguments[],
363 JSValueRef* exception)
365 //LogDebug("entered");
368 ContactConverterFactory::ConverterType converter =
369 ContactConverterFactory::getConverter(context);
370 JSContactOrganizationArrayPriv* priv =
371 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
372 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
373 if (currentOrganizations->size() > 0) {
374 ContactOrganizationPtr result = currentOrganizations->at(
375 currentOrganizations->size() - 1);
376 currentOrganizations->pop_back();
377 return converter->toJSValueRef(result);
380 Catch(WrtDeviceApis::Commons::Exception)
382 LogError("error occured");
384 return JSValueMakeUndefined(context);
387 JSValueRef JSContactOrganizationArray::push(JSContextRef context,
388 JSObjectRef function,
389 JSObjectRef thisObject,
390 size_t argumentCount,
391 const JSValueRef arguments[],
392 JSValueRef* exception)
394 //LogDebug("entered");
397 ContactConverterFactory::ConverterType converter =
398 ContactConverterFactory::getConverter(context);
399 JSContactOrganizationArrayPriv* priv =
400 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
401 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
402 for (size_t i = 0; i < argumentCount; ++i) {
403 currentOrganizations->push_back(converter->toContactOrganization(arguments[i]));
405 return converter->toJSValueRef(currentOrganizations->size());
407 Catch(WrtDeviceApis::Commons::Exception)
409 LogError("error occured");
411 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
414 JSValueRef JSContactOrganizationArray::reverse(JSContextRef context,
415 JSObjectRef function,
416 JSObjectRef thisObject,
417 size_t argumentCount,
418 const JSValueRef arguments[],
419 JSValueRef* exception)
421 //LogDebug("entered");
424 ContactConverterFactory::ConverterType converter =
425 ContactConverterFactory::getConverter(context);
426 JSContactOrganizationArrayPriv* priv =
427 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
428 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
429 std::reverse(currentOrganizations->begin(), currentOrganizations->end());
432 Catch(WrtDeviceApis::Commons::Exception)
434 LogError("error occured");
436 return JSValueMakeUndefined(context);
439 JSValueRef JSContactOrganizationArray::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 =
450 ContactConverterFactory::getConverter(context);
451 JSContactOrganizationArrayPriv* priv =
452 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
453 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
454 if (currentOrganizations->size() > 0) {
455 ContactOrganizationPtr result = currentOrganizations->at(0);
456 currentOrganizations->erase(currentOrganizations->begin());
457 return converter->toJSValueRef(result);
460 Catch(WrtDeviceApis::Commons::Exception)
462 LogError("error occured");
464 return JSValueMakeUndefined(context);
467 JSValueRef JSContactOrganizationArray::slice(JSContextRef context,
468 JSObjectRef function,
469 JSObjectRef thisObject,
470 size_t argumentCount,
471 const JSValueRef arguments[],
472 JSValueRef* exception)
477 if (argumentCount < 1) {
478 return JSValueMakeUndefined(context);
480 ContactConverterFactory::ConverterType converter =
481 ContactConverterFactory::getConverter(context);
482 ContactOrganizationArrayPtr organizations = ContactOrganizationArrayPtr(new ContactOrganizationArray());
483 JSContactOrganizationArrayPriv *newPrivateObject = new JSContactOrganizationArrayPriv(
486 JSValueRef result = JSObjectMake(context,
487 getClassRef(), newPrivateObject);
489 //copy current organizations
490 JSContactOrganizationArrayPriv* priv =
491 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
492 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
493 std::size_t first = converter->toSizeT(arguments[0]);
494 std::size_t last = currentOrganizations->size() - 1;
495 if (argumentCount > 1) {
496 last = converter->toSizeT(arguments[1]);
497 if (last >= currentOrganizations->size()) {
498 last = currentOrganizations->size() - 1;
504 for (size_t i = first; i <= last; ++i) {
505 organizations->push_back(currentOrganizations->at(i));
510 Catch(WrtDeviceApis::Commons::Exception)
512 LogError("error occured");
514 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
517 JSValueRef JSContactOrganizationArray::sort(JSContextRef context,
518 JSObjectRef function,
519 JSObjectRef thisObject,
520 size_t argumentCount,
521 const JSValueRef arguments[],
522 JSValueRef* exception)
524 //LogDebug("entered");
527 ContactConverterFactory::ConverterType converter =
528 ContactConverterFactory::getConverter(context);
529 JSContactOrganizationArrayPriv* priv =
530 static_cast<JSContactOrganizationArrayPriv*>(JSObjectGetPrivate(thisObject));
531 ContactOrganizationArrayPtr currentOrganizations = priv->getObject();
532 std::sort(currentOrganizations->begin(), currentOrganizations->end());
535 Catch(WrtDeviceApis::Commons::Exception)
537 LogError("error occured");
539 return JSValueMakeUndefined(context);
542 JSValueRef JSContactOrganizationArray::splice(JSContextRef context,
543 JSObjectRef function,
544 JSObjectRef thisObject,
545 size_t argumentCount,
546 const JSValueRef arguments[],
547 JSValueRef* exception)
549 //LogDebug("entered");
550 return JSValueMakeUndefined(context);
553 JSValueRef JSContactOrganizationArray::toString(JSContextRef context,
554 JSObjectRef function,
555 JSObjectRef thisObject,
556 size_t argumentCount,
557 const JSValueRef arguments[],
558 JSValueRef* exception)
560 //LogDebug("entered");
561 return join(context, function, thisObject, 0, arguments, exception);
564 JSValueRef JSContactOrganizationArray::unshift(JSContextRef context,
565 JSObjectRef function,
566 JSObjectRef thisObject,
567 size_t argumentCount,
568 const JSValueRef arguments[],
569 JSValueRef* exception)
571 //LogDebug("entered");
572 return JSValueMakeUndefined(context);
575 JSValueRef JSContactOrganizationArray::valueOf(JSContextRef context,
576 JSObjectRef function,
577 JSObjectRef thisObject,
578 size_t argumentCount,
579 const JSValueRef arguments[],
580 JSValueRef* exception)
582 //LogDebug("entered");
583 return JSValueMakeUndefined(context);