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 * @file JSSimpleCoordinates.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSSimpleCoordinates class
26 #include <dpl/log/log.h>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <CommonsJavaScript/Validator.h>
30 #include <JSTizenExceptionFactory.h>
31 #include <JSTizenException.h>
32 #include "SimpleCoordinates.h"
33 #include "FilterConverter.h"
34 #include "JSSimpleCoordinates.h"
36 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
38 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
39 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
41 namespace DeviceAPI {
\rnamespace Tizen {
43 using namespace DeviceAPI::Common;
44 using namespace DeviceAPI::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
48 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
50 JSClassDefinition JSSimpleCoordinates::m_classInfo =
53 kJSClassAttributeNone,
54 ATTRIBUTE_FILTER_CLASS_NAME,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
65 NULL, //CallAsFunction,
66 NULL, //CallAsConstructor,
68 NULL, //ConvertToType,
71 JSStaticValue JSSimpleCoordinates::m_property[] = {
72 { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
73 { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
77 JSStaticFunction JSSimpleCoordinates::m_functions[] =
82 JSClassRef JSSimpleCoordinates::getClassRef() {
84 m_classRef = JSClassCreate(&m_classInfo);
89 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
91 return JSValueIsObjectOfClass(context, value, getClassRef());
94 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
96 if (!isObjectOfClass(context, value)) {
97 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99 JSObjectRef object = JSValueToObject(context, value, NULL);
101 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
105 Throw(WrtDeviceApis::Commons::NullPointerException);
107 return priv->getObject();
110 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
112 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, privateData);
113 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
114 if (NULL == jsObjectRef) {
115 LogError("object creation error");
121 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
122 JSObjectRef constructor,
123 size_t argumentCount,
124 const JSValueRef arguments[],
125 JSValueRef* exception)
129 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
131 if (argumentCount < 2)
132 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
134 if (!JSValueIsNumber(context, arguments[0]))
135 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
137 if (!JSValueIsNumber(context, arguments[1]))
138 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
140 } Catch(Exception ) {
141 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
142 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
146 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
152 latitude = converter->toDouble(arguments[0]);
154 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
155 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
160 longitude = converter->toDouble(arguments[1]);
162 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
163 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
167 SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
169 JSObjectRef jsobject;
172 jsobject = createJSObject(context, simpleCoordinates);
174 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
175 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
182 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
184 if (!JSObjectGetPrivate(object))
186 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
187 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
188 if (!JSObjectSetPrivate(object, priv)) {
194 void JSSimpleCoordinates::Finalize(JSObjectRef object)
196 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
206 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
209 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
211 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
213 SimpleCoordinatesPtr result = priv->getObject();
215 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
220 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
222 JSStringRef propertyName,
223 JSValueRef* exception)
228 FilterConverterFactory::ConverterType converter =
229 FilterConverterFactory::getConverter(context);
230 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
231 return converter->toJSValueRef(simpleCoordinates->getLatitude());
233 Catch(WrtDeviceApis::Commons::Exception)
235 LogWarning("trying to get incorrect value");
237 return JSValueMakeUndefined(context);
240 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
248 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
249 FilterConverterFactory::ConverterType converter =
250 FilterConverterFactory::getConverter(context);
251 simpleCoordinates->setLatitude(converter->toDouble(value));
254 Catch(WrtDeviceApis::Commons::Exception)
256 LogWarning("trying to set incorrect value");
259 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
263 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
265 JSStringRef propertyName,
266 JSValueRef* exception)
271 FilterConverterFactory::ConverterType converter =
272 FilterConverterFactory::getConverter(context);
273 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
274 return converter->toJSValueRef(simpleCoordinates->getLongitude());
276 Catch(WrtDeviceApis::Commons::Exception)
278 LogWarning("trying to get incorrect value");
280 return JSValueMakeUndefined(context);
283 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
285 JSStringRef propertyName,
287 JSValueRef* exception)
291 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
292 FilterConverterFactory::ConverterType converter =
293 FilterConverterFactory::getConverter(context);
294 simpleCoordinates->setLongitude(converter->toDouble(value));
297 Catch(WrtDeviceApis::Commons::Exception)
299 LogWarning("trying to set incorrect value");
302 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");