-//
-// Tizen Web Device API
-// Copyright (c) 2012 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Apache License, Version 2.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-
-#include <algorithm>
-#include <sstream>
-#include <dpl/log/log.h>
-#include <CommonsJavaScript/Converter.h>
-#include <CommonsJavaScript/ScopedJSStringRef.h>
-#include <JSTizenExceptionFactory.h>
-#include <JSTizenException.h>
-#include <CommonsJavaScript/Converter.h>
-#include "JSTZDateArray.h"
-#include <TimeUtilConverter.h>
-
-#define FUNCTION_CONCAT "concat"
-#define FUNCTION_JOIN "join"
-#define FUNCTION_POP "pop"
-#define FUNCTION_PUSH "push"
-#define FUNCTION_REVERSE "reverse"
-#define FUNCTION_SHIFT "shift"
-#define FUNCTION_SLICE "slice"
-#define FUNCTION_SORT "sort"
-#define FUNCTION_SPLICE "splice"
-#define FUNCTION_TOSTRING "toString"
-#define FUNCTION_UNSHIFT "unshift"
-#define FUNCTION_VALUEOF "valueOf"
-#define ARRAY "Array"
-#define ATTRIBUTE_LENGTH "length"
-
-namespace DeviceAPI {
-namespace Calendar {
-
-
-using namespace DeviceAPI::Common;
-using namespace DeviceAPI::Time;
-using namespace WrtDeviceApis::CommonsJavaScript;
-
-JSClassDefinition JSTZDateArray::m_classInfo = {
- 0,
- kJSClassAttributeNone,
- ARRAY,
- 0,
- m_property,
- m_function,
- initialize,
- finalize,
- hasProperty,
- getProperty,
- setProperty,
- NULL, //deleteProperty,
- getPropertyNames,
- NULL, //callAsFunction,
- NULL, //callAsConstructor,
- NULL, //hasInstance,
- NULL, //convertToType,
-};
-
-JSStaticValue JSTZDateArray::m_property[] = {
- { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { 0, 0, 0, 0 }
-};
-
-JSStaticFunction JSTZDateArray::m_function[] = {
- { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { 0, 0, 0 }
-};
-
-JSClassRef JSTZDateArray::m_jsClassRef = JSClassCreate(
- JSTZDateArray::getClassInfo());
-
-JSValueRef JSTZDateArray::getLength(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName,
- JSValueRef* exception)
-{
- Try
- {
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- NumberArrayPtr numbers = priv->getObject();
- if (numbers) {
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- return converter->toJSValueRef(numbers->size());
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("invalid conversion");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSObjectRef JSTZDateArray::createArray(JSContextRef context,
- const NumberArrayPtr &numbers)
-{
- JSTZDateArrayPriv *priv = new JSTZDateArrayPriv(context, numbers);
- return JSObjectMake(context, getClassRef(), priv);
-}
-
-const JSClassDefinition* JSTZDateArray::getClassInfo()
-{
- return &(m_classInfo);
-}
-
-JSClassRef JSTZDateArray::getClassRef()
-{
- if (!m_jsClassRef) {
- m_jsClassRef = JSClassCreate(&m_classInfo);
- }
- return m_jsClassRef;
-}
-
-bool JSTZDateArray::isObjectOfClass(JSContextRef context, JSValueRef value)
-{
- return JSValueIsObjectOfClass(context, value, getClassRef());
-}
-
-NumberArrayPtr JSTZDateArray::getTZDateArray(JSContextRef context, JSValueRef value)
-{
- if (!isObjectOfClass(context, value)) {
- Throw(WrtDeviceApis::Commons::InvalidArgumentException);
- }
- JSObjectRef object = JSValueToObject(context, value, NULL);
- if (!object) {
- Throw(WrtDeviceApis::Commons::InvalidArgumentException);
- }
- JSTZDateArrayPriv* priv = static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- return priv->getObject();
-}
-
-void JSTZDateArray::initialize(JSContextRef context,
- JSObjectRef object)
-{
-}
-
-void JSTZDateArray::finalize(JSObjectRef object)
-{
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- delete priv;
- JSObjectSetPrivate(object, NULL);
-}
-
-bool JSTZDateArray::hasProperty(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName)
-{
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- Try
- {
- size_t index = converter->toSizeT(propertyName);
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- NumberArrayPtr numbers = priv->getObject();
- if (index < numbers->size()) {
- return true;
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- //not reporting error is intended
- }
- return false;
-}
-
-JSValueRef JSTZDateArray::getProperty(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName,
- JSValueRef* exception)
-{
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- TimeUtilConverter timeConverter(context);
-
- Try
- {
- size_t index = converter->toSizeT(propertyName);
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- NumberArrayPtr numbers = priv->getObject();
- if (index < numbers->size()) {
- long long int result = numbers->at(index);
- LogInfo("index: "<<index<<", result: "<<result);
- return timeConverter.toJSValueRefTZDate((double)(result*1000.0), "");
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("invalid property");
- }
- return JSValueMakeUndefined(context);
-}
-
-bool JSTZDateArray::setProperty(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName,
- JSValueRef value,
- JSValueRef* exception)
-{
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- TimeUtilConverter timeConverter(context);
-
- Try
- {
- size_t index = converter->toSizeT(propertyName);
- long long int number = 0;
- if (!JSValueIsUndefined(context, value)) {
- number = timeConverter.getTimeInMilliseconds(value)/1000;
- }
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- NumberArrayPtr numbers = priv->getObject();
- if (!numbers) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- if (numbers->size() <= index) {
- numbers->resize(index + 1);
- }
- (*numbers)[index] = number;
- return true;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
- }
- return false;
-}
-
-void JSTZDateArray::getPropertyNames(JSContextRef context,
- JSObjectRef object,
- JSPropertyNameAccumulatorRef propertyNames)
-{
- Converter converter(context);
-
- Try
- {
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- NumberArrayPtr numbers = priv->getObject();
-
- int count = numbers->size();
-
- for(int i=0; i < count; i++)
- {
- ScopedJSStringRef name(converter.toJSStringRef(converter.toString(i)));
- JSPropertyNameAccumulatorAddName(propertyNames, name.get());
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("invalid property");
- }
-}
-
-JSValueRef JSTZDateArray::concat(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- NumberArrayPtr numbers = NumberArrayPtr(new NumberArray());
- JSTZDateArrayPriv *newPrivateObject = new JSTZDateArrayPriv(
- context,
- numbers);
- JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
-
- //copy current numbers
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- for (size_t i = 0; i < currentNumbers->size(); ++i) {
- numbers->push_back(currentNumbers->at(i));
- }
-
- //copy submitted arrays
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- TimeUtilConverter timeConverter(context);
- for (size_t i = 0; i < argumentCount; ++i) {
- if (!JSIsArrayValue(context, arguments[i])) {
- Throw(WrtDeviceApis::Commons::ConversionException);
- }
- // process array of numbers
- JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
- unsigned int len = JSGetArrayLength(context, arrayObj);
- for (unsigned int e = 0; e < len; ++e) {
- JSValueRef att = JSGetArrayElement(context, arrayObj, e);
-
- long long int number = 0;
- if (!JSValueIsUndefined(context, att)) {
- number = timeConverter.getTimeInMilliseconds(att)/1000;
- }
- LogDebug("Concatenating a number: "<<number);
- numbers->push_back(number);
- }
- }
- return result;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSTZDateArray::join(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- std::string result;
- std::string separator(",");
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- JSTZDateArrayPriv* priv = static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- TimeUtilConverter timeConverter(context);
-
- NumberArrayPtr currentNumbers = priv->getObject();
- if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
- separator = converter->toString(arguments[0]);
- LogDebug("Separator: "<<separator);
- }
- for (size_t i = 0; i < currentNumbers->size(); ++i) {
- if (i != 0) {
- result += separator;
- }
- std::stringstream ss;
- ss<<currentNumbers->at(i);
- result += ss.str();
- }
- LogDebug("Resulting string: "<<result);
- return converter->toJSValueRef(result);
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSTZDateArray::pop(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- TimeUtilConverter timeConverter(context);
-
- JSTZDateArrayPriv* priv = static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- if (currentNumbers->size() > 0) {
- long long int result = currentNumbers->at(currentNumbers->size() - 1);
- currentNumbers->pop_back();
- return timeConverter.toJSValueRefTZDate((double)(result*1000.0), "");
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSTZDateArray::push(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- TimeUtilConverter timeConverter(context);
-
- JSTZDateArrayPriv* priv = static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- for (size_t i = 0; i < argumentCount; ++i) {
- long long int number = 0;
- if (!JSValueIsUndefined(context, arguments[i])) {
- number = timeConverter.getTimeInMilliseconds(arguments[i])/1000;
- }
- LogDebug("Pushing a TZDate number: "<<number<<" for index: "<<i);
- currentNumbers->push_back(number);
- }
- return timeConverter.toJSValueRef(currentNumbers->size());
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSTZDateArray::reverse(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- std::reverse(currentNumbers->begin(), currentNumbers->end());
- return thisObject;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSTZDateArray::shift(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- if (currentNumbers->size() > 0) {
- long long int result = currentNumbers->at(0);
- currentNumbers->erase(currentNumbers->begin());
- return converter->toJSValueRef(result);
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSTZDateArray::slice(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- if (argumentCount < 1) {
- return JSValueMakeUndefined(context);
- }
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- NumberArrayPtr numbers = NumberArrayPtr(new NumberArray());
- JSTZDateArrayPriv *newPrivateObject = new JSTZDateArrayPriv(context, numbers);
- JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
-
- //copy current numbers
- JSTZDateArrayPriv* priv = static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- std::size_t first = converter->toSizeT(arguments[0]);
- std::size_t last = currentNumbers->size() - 1;
- if (argumentCount > 1) {
- last = converter->toSizeT(arguments[1]);
- if (last >= currentNumbers->size()) {
- last = currentNumbers->size() - 1;
- }
- }
-
- for (size_t i = first; i <= last; ++i) {
- numbers->push_back(currentNumbers->at(i));
- }
-
- return result;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSTZDateArray::sort(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- Try
- {
- WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
- WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
- JSTZDateArrayPriv* priv =
- static_cast<JSTZDateArrayPriv*>(JSObjectGetPrivate(thisObject));
- NumberArrayPtr currentNumbers = priv->getObject();
- std::sort(currentNumbers->begin(), currentNumbers->end());
- return thisObject;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSTZDateArray::splice(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSTZDateArray::toString(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- return join(context, function, thisObject, 0, arguments, exception);
-}
-
-JSValueRef JSTZDateArray::unshift(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSTZDateArray::valueOf(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- return JSValueMakeUndefined(context);
-}
-
-} // Calendar
-} // DeviceAPI