2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <CommonsJavaScript/ScopedJSStringRef.h>
20 #include <JSTizenExceptionFactory.h>
21 #include <JSTizenException.h>
22 #include "NotificationConverter.h"
23 #include "JSNotificationLineArray.h"
26 #define FUNCTION_CONCAT "concat"
27 #define FUNCTION_JOIN "join"
28 #define FUNCTION_POP "pop"
29 #define FUNCTION_PUSH "push"
30 #define FUNCTION_REVERSE "reverse"
31 #define FUNCTION_SHIFT "shift"
32 #define FUNCTION_SLICE "slice"
33 #define FUNCTION_SORT "sort"
34 #define FUNCTION_SPLICE "splice"
35 #define FUNCTION_TOSTRING "toString"
36 #define FUNCTION_UNSHIFT "unshift"
37 #define FUNCTION_VALUEOF "valueOf"
39 #define ATTRIBUTE_LENGTH "length"
42 namespace Notification{
44 using namespace DeviceAPI::Common;
45 using namespace WrtDeviceApis::CommonsJavaScript;
47 JSClassDefinition JSNotificationLineArray::m_classInfo = {
49 kJSClassAttributeNone,
61 NULL, //callAsFunction,
62 NULL, //callAsConstructor,
64 NULL, //convertToType,
67 JSStaticValue JSNotificationLineArray::m_property[] = {
68 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
72 JSStaticFunction JSNotificationLineArray::m_function[] = {
73 { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
74 { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
75 { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
76 { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
77 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
78 { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
79 { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
80 { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
81 { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
82 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
83 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
84 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
88 JSClassRef JSNotificationLineArray::m_jsClassRef = JSClassCreate(
89 JSNotificationLineArray::getClassInfo());
91 JSValueRef JSNotificationLineArray::getLength(JSContextRef context,
93 JSStringRef propertyName,
94 JSValueRef* exception)
98 JSNotificationLineArrayPriv* priv =
99 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
101 Throw(WrtDeviceApis::Commons::NullPointerException);
103 NotificationLineArrayPtr lines = priv->getObject();
105 NotificationConverterFactory::ConverterType converter =
106 NotificationConverterFactory::getConverter(context);
107 return converter->toJSValueRef(lines->size());
110 Catch(WrtDeviceApis::Commons::Exception)
112 LogError("invalid conversion");
114 return JSValueMakeUndefined(context);
117 bool JSNotificationLineArray::isObjectOfClass(JSContextRef context, JSValueRef value)
119 return JSValueIsObjectOfClass(context, value, getClassRef());
122 NotificationLineArrayPtr JSNotificationLineArray::getNotificationLineArray(JSContextRef context, JSValueRef value)
124 if (!isObjectOfClass(context, value)) {
125 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
127 JSObjectRef object = JSValueToObject(context, value, NULL);
129 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
131 JSNotificationLineArrayPriv *priv = static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
133 Throw(WrtDeviceApis::Commons::NullPointerException);
135 return priv->getObject();
138 JSObjectRef JSNotificationLineArray::createArray(JSContextRef context,
139 const NotificationLineArrayPtr &lines)
141 JSNotificationLineArrayPriv *priv = new JSNotificationLineArrayPriv(context, lines);
142 return JSObjectMake(context, getClassRef(), priv);
145 const JSClassDefinition* JSNotificationLineArray::getClassInfo()
147 return &(m_classInfo);
150 JSClassRef JSNotificationLineArray::getClassRef()
153 m_jsClassRef = JSClassCreate(&m_classInfo);
158 void JSNotificationLineArray::initialize(JSContextRef context,
163 void JSNotificationLineArray::finalize(JSObjectRef object)
165 JSNotificationLineArrayPriv* priv =
166 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
168 JSObjectSetPrivate(object, NULL);
171 bool JSNotificationLineArray::hasProperty(JSContextRef context,
173 JSStringRef propertyName)
175 NotificationConverterFactory::ConverterType converter =
176 NotificationConverterFactory::getConverter(context);
179 size_t index = converter->toSizeT(propertyName);
180 JSNotificationLineArrayPriv* priv =
181 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
183 Throw(WrtDeviceApis::Commons::NullPointerException);
185 NotificationLineArrayPtr lines = priv->getObject();
186 if (index < lines->size()) {
190 Catch(WrtDeviceApis::Commons::Exception)
192 //not reporting error is intended
197 JSValueRef JSNotificationLineArray::getProperty(JSContextRef context,
199 JSStringRef propertyName,
200 JSValueRef* exception)
202 NotificationConverterFactory::ConverterType converter =
203 NotificationConverterFactory::getConverter(context);
206 size_t index = converter->toSizeT(propertyName);
207 JSNotificationLineArrayPriv* priv =
208 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
210 Throw(WrtDeviceApis::Commons::NullPointerException);
212 NotificationLineArrayPtr lines = priv->getObject();
213 if (index < lines->size()) {
214 NotificationLinePtr result = lines->at(index);
216 return converter->toJSValueRef(result);
220 Catch(WrtDeviceApis::Commons::Exception)
222 LogError("invalid property");
224 return JSValueMakeUndefined(context);
227 bool JSNotificationLineArray::setProperty(JSContextRef context,
229 JSStringRef propertyName,
231 JSValueRef* exception)
233 NotificationConverterFactory::ConverterType converter =
234 NotificationConverterFactory::getConverter(context);
237 size_t index = converter->toSizeT(propertyName);
238 NotificationLinePtr line(NULL);
239 if (!JSValueIsUndefined(context, value)) {
240 line = converter->toNotificationLine(value);
242 JSNotificationLineArrayPriv* priv =
243 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
245 Throw(WrtDeviceApis::Commons::NullPointerException);
247 NotificationLineArrayPtr lines = priv->getObject();
249 Throw(WrtDeviceApis::Commons::NullPointerException);
251 if (lines->size() <= index) {
252 lines->resize(index + 1);
254 (*lines)[index] = line;
257 Catch(WrtDeviceApis::Commons::Exception)
259 LogError("error occured");
260 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
265 bool JSNotificationLineArray::deleteProperty(JSContextRef context,
267 JSStringRef propertyName,
268 JSValueRef* exception)
270 NotificationConverterFactory::ConverterType converter =
271 NotificationConverterFactory::getConverter(context);
274 size_t index = converter->toSizeT(propertyName);
275 NotificationLinePtr line(NULL);
276 JSNotificationLineArrayPriv* priv =
277 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
279 Throw(WrtDeviceApis::Commons::NullPointerException);
281 NotificationLineArrayPtr lines = priv->getObject();
283 Throw(WrtDeviceApis::Commons::NullPointerException);
285 if (lines->size() > index) {
286 (*lines)[index] = line;
290 Catch(WrtDeviceApis::Commons::Exception)
292 LogError("error occured");
293 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
298 void JSNotificationLineArray::getPropertyNames(JSContextRef context,
300 JSPropertyNameAccumulatorRef propertyNames)
302 NotificationConverterFactory::ConverterType converter =
303 NotificationConverterFactory::getConverter(context);
306 JSNotificationLineArrayPriv* priv =
307 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(object));
309 Throw(WrtDeviceApis::Commons::NullPointerException);
311 NotificationLineArrayPtr lines = priv->getObject();
313 int count = lines->size();
315 for(int i=0; i < count; i++)
317 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
318 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
321 Catch(WrtDeviceApis::Commons::Exception)
323 LogError("invalid property");
327 JSValueRef JSNotificationLineArray::concat(JSContextRef context,
328 JSObjectRef function,
329 JSObjectRef thisObject,
330 size_t argumentCount,
331 const JSValueRef arguments[],
332 JSValueRef* exception)
336 NotificationLineArrayPtr lines = NotificationLineArrayPtr(new NotificationLineArray());
337 JSNotificationLineArrayPriv *newPrivateObject = new JSNotificationLineArrayPriv(context, lines);
338 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
341 JSNotificationLineArrayPriv* priv =
342 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
343 NotificationLineArrayPtr currentLines = priv->getObject();
344 for (size_t i = 0; i < currentLines->size(); ++i) {
345 lines->push_back(currentLines->at(i));
348 //copy submitted arrays
349 NotificationConverterFactory::ConverterType converter =
350 NotificationConverterFactory::getConverter(context);
351 for (size_t i = 0; i < argumentCount; ++i) {
352 if (!JSIsArrayValue(context, arguments[i])) {
353 Throw(WrtDeviceApis::Commons::ConversionException);
355 // process array of strings
356 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
357 unsigned int len = JSGetArrayLength(context, arrayObj);
358 for (unsigned int e = 0; e < len; ++e) {
359 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
360 lines->push_back(converter->toNotificationLine(att));
365 Catch(WrtDeviceApis::Commons::Exception)
367 LogError("error occured");
369 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
372 JSValueRef JSNotificationLineArray::join(JSContextRef context,
373 JSObjectRef function,
374 JSObjectRef thisObject,
375 size_t argumentCount,
376 const JSValueRef arguments[],
377 JSValueRef* exception)
382 std::string separator(",");
383 NotificationConverterFactory::ConverterType converter =
384 NotificationConverterFactory::getConverter(context);
385 JSNotificationLineArrayPriv* priv =
386 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
387 NotificationLineArrayPtr currentLines = priv->getObject();
388 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
389 separator = converter->toString(arguments[0]);
391 for (size_t i = 0; i < currentLines->size(); ++i) {
395 //FIXME : to be changed to support join
396 //result += currentLines->at(i);
398 return converter->toJSValueRef(result);
400 Catch(WrtDeviceApis::Commons::Exception)
402 LogError("error occured");
404 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
407 JSValueRef JSNotificationLineArray::pop(JSContextRef context,
408 JSObjectRef function,
409 JSObjectRef thisObject,
410 size_t argumentCount,
411 const JSValueRef arguments[],
412 JSValueRef* exception)
416 NotificationConverterFactory::ConverterType converter =
417 NotificationConverterFactory::getConverter(context);
418 JSNotificationLineArrayPriv* priv =
419 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
420 NotificationLineArrayPtr currentLines = priv->getObject();
421 if (currentLines->size() > 0) {
422 NotificationLinePtr result = currentLines->at(currentLines->size() - 1);
423 currentLines->pop_back();
424 return converter->toJSValueRef(result);
427 Catch(WrtDeviceApis::Commons::Exception)
429 LogError("error occured");
431 return JSValueMakeUndefined(context);
434 JSValueRef JSNotificationLineArray::push(JSContextRef context,
435 JSObjectRef function,
436 JSObjectRef thisObject,
437 size_t argumentCount,
438 const JSValueRef arguments[],
439 JSValueRef* exception)
443 NotificationConverterFactory::ConverterType converter =
444 NotificationConverterFactory::getConverter(context);
445 JSNotificationLineArrayPriv* priv =
446 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
447 NotificationLineArrayPtr currentLines = priv->getObject();
448 for (size_t i = 0; i < argumentCount; ++i) {
449 currentLines->push_back(converter->toNotificationLine(arguments[i]));
451 return converter->toJSValueRef(currentLines->size());
453 Catch(WrtDeviceApis::Commons::Exception)
455 LogError("error occured");
457 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
460 JSValueRef JSNotificationLineArray::reverse(JSContextRef context,
461 JSObjectRef function,
462 JSObjectRef thisObject,
463 size_t argumentCount,
464 const JSValueRef arguments[],
465 JSValueRef* exception)
469 NotificationConverterFactory::ConverterType converter =
470 NotificationConverterFactory::getConverter(context);
471 JSNotificationLineArrayPriv* priv =
472 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
473 NotificationLineArrayPtr currentLines = priv->getObject();
474 std::reverse(currentLines->begin(), currentLines->end());
477 Catch(WrtDeviceApis::Commons::Exception)
479 LogError("error occured");
481 return JSValueMakeUndefined(context);
484 JSValueRef JSNotificationLineArray::shift(JSContextRef context,
485 JSObjectRef function,
486 JSObjectRef thisObject,
487 size_t argumentCount,
488 const JSValueRef arguments[],
489 JSValueRef* exception)
493 NotificationConverterFactory::ConverterType converter =
494 NotificationConverterFactory::getConverter(context);
495 JSNotificationLineArrayPriv* priv =
496 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
497 NotificationLineArrayPtr currentLines = priv->getObject();
498 if (currentLines->size() > 0) {
499 NotificationLinePtr result = currentLines->at(0);
500 currentLines->erase(currentLines->begin());
501 return converter->toJSValueRef(result);
504 Catch(WrtDeviceApis::Commons::Exception)
506 LogError("error occured");
508 return JSValueMakeUndefined(context);
511 JSValueRef JSNotificationLineArray::slice(JSContextRef context,
512 JSObjectRef function,
513 JSObjectRef thisObject,
514 size_t argumentCount,
515 const JSValueRef arguments[],
516 JSValueRef* exception)
520 if (argumentCount < 1) {
521 return JSValueMakeUndefined(context);
523 NotificationConverterFactory::ConverterType converter =
524 NotificationConverterFactory::getConverter(context);
525 NotificationLineArrayPtr lines = NotificationLineArrayPtr(new NotificationLineArray());
526 JSNotificationLineArrayPriv *newPrivateObject = new JSNotificationLineArrayPriv(
529 JSValueRef result = JSObjectMake(context,
530 getClassRef(), newPrivateObject);
533 JSNotificationLineArrayPriv* priv =
534 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
535 NotificationLineArrayPtr currentLines = priv->getObject();
536 std::size_t first = converter->toSizeT(arguments[0]);
537 std::size_t last = currentLines->size() - 1;
538 if (argumentCount > 1) {
539 last = converter->toSizeT(arguments[1]);
540 if (last >= currentLines->size()) {
541 last = currentLines->size() - 1;
544 for (size_t i = first; i <= last; ++i) {
545 lines->push_back(currentLines->at(i));
550 Catch(WrtDeviceApis::Commons::Exception)
552 LogError("error occured");
554 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
557 JSValueRef JSNotificationLineArray::sort(JSContextRef context,
558 JSObjectRef function,
559 JSObjectRef thisObject,
560 size_t argumentCount,
561 const JSValueRef arguments[],
562 JSValueRef* exception)
566 NotificationConverterFactory::ConverterType converter =
567 NotificationConverterFactory::getConverter(context);
568 JSNotificationLineArrayPriv* priv =
569 static_cast<JSNotificationLineArrayPriv*>(JSObjectGetPrivate(thisObject));
570 NotificationLineArrayPtr currentLines = priv->getObject();
571 std::sort(currentLines->begin(), currentLines->end());
574 Catch(WrtDeviceApis::Commons::Exception)
576 LogError("error occured");
578 return JSValueMakeUndefined(context);
581 JSValueRef JSNotificationLineArray::splice(JSContextRef context,
582 JSObjectRef function,
583 JSObjectRef thisObject,
584 size_t argumentCount,
585 const JSValueRef arguments[],
586 JSValueRef* exception)
588 return JSValueMakeUndefined(context);
591 JSValueRef JSNotificationLineArray::toString(JSContextRef context,
592 JSObjectRef function,
593 JSObjectRef thisObject,
594 size_t argumentCount,
595 const JSValueRef arguments[],
596 JSValueRef* exception)
598 return join(context, function, thisObject, 0, arguments, exception);
601 JSValueRef JSNotificationLineArray::unshift(JSContextRef context,
602 JSObjectRef function,
603 JSObjectRef thisObject,
604 size_t argumentCount,
605 const JSValueRef arguments[],
606 JSValueRef* exception)
608 return JSValueMakeUndefined(context);
611 JSValueRef JSNotificationLineArray::valueOf(JSContextRef context,
612 JSObjectRef function,
613 JSObjectRef thisObject,
614 size_t argumentCount,
615 const JSValueRef arguments[],
616 JSValueRef* exception)
618 return JSValueMakeUndefined(context);