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 JSSimpleCoordinates.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSSimpleCoordinates class
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <API/Filter/SimpleCoordinates.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include "FilterConverter.h"
33 #include "JSSimpleCoordinates.h"
35 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
37 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
38 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
44 using namespace TizenApis::Commons;
45 using namespace TizenApis::Api::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
49 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
51 JSClassDefinition JSSimpleCoordinates::m_classInfo =
54 kJSClassAttributeNone,
55 ATTRIBUTE_FILTER_CLASS_NAME,
64 NULL, //DeleteProperty,
65 NULL, //getPropertyNames,
66 NULL, //CallAsFunction,
67 constructor, //CallAsConstructor,
69 NULL, //ConvertToType,
72 JSStaticValue JSSimpleCoordinates::m_property[] = {
73 { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
74 { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
78 JSStaticFunction JSSimpleCoordinates::m_functions[] =
83 JSClassRef JSSimpleCoordinates::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
97 if (!isObjectOfClass(context, value)) {
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSObjectRef object = JSValueToObject(context, value, NULL);
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
113 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, privateData);
114 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
115 if (NULL == jsObjectRef) {
116 LogError("object creation error");
122 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
124 if (!JSObjectGetPrivate(object))
126 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
127 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
128 if (!JSObjectSetPrivate(object, priv)) {
134 void JSSimpleCoordinates::Finalize(JSObjectRef object)
136 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
146 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
149 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 SimpleCoordinatesPtr result = priv->getObject();
155 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
161 JSObjectRef constructor,
162 size_t argumentCount,
163 const JSValueRef arguments[],
164 JSValueRef* exception)
168 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
169 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
171 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(constructor));
173 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175 JSContextRef gContext = priv->getContext();
177 // JSGlobalContextRef gContext = JSGlobalContextFactory::getInstance()->get();
179 BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
181 if (argumentCount != 2)
182 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
184 if (!JSValueIsNumber(gContext, arguments[0]))
185 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
187 if (!JSValueIsNumber(gContext, arguments[1]))
188 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
190 } Catch(Exception ) {
191 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
192 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
196 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
202 latitude = converter->toDouble(arguments[0]);
204 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
205 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
210 longitude = converter->toDouble(arguments[1]);
212 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
213 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
216 printf("latitude : %f\n\n", latitude);
217 SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
219 JSObjectRef jsobject;
222 jsobject = createJSObject(gContext, simpleCoordinates);
224 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
225 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
232 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
234 JSStringRef propertyName,
235 JSValueRef* exception)
240 FilterConverterFactory::ConverterType converter =
241 FilterConverterFactory::getConverter(context);
242 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
243 return converter->toJSValueRef(simpleCoordinates->getLatitude());
245 Catch(WrtDeviceApis::Commons::Exception)
247 LogWarning("trying to get incorrect value");
249 return JSValueMakeUndefined(context);
252 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
254 JSStringRef propertyName,
256 JSValueRef* exception)
260 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
261 FilterConverterFactory::ConverterType converter =
262 FilterConverterFactory::getConverter(context);
263 simpleCoordinates->setLatitude(converter->toDouble(value));
266 Catch(WrtDeviceApis::Commons::Exception)
268 LogWarning("trying to set incorrect value");
271 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
275 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
277 JSStringRef propertyName,
278 JSValueRef* exception)
283 FilterConverterFactory::ConverterType converter =
284 FilterConverterFactory::getConverter(context);
285 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
286 return converter->toJSValueRef(simpleCoordinates->getLongitude());
288 Catch(WrtDeviceApis::Commons::Exception)
290 LogWarning("trying to get incorrect value");
292 return JSValueMakeUndefined(context);
295 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
297 JSStringRef propertyName,
299 JSValueRef* exception)
303 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
304 FilterConverterFactory::ConverterType converter =
305 FilterConverterFactory::getConverter(context);
306 simpleCoordinates->setLongitude(converter->toDouble(value));
309 Catch(WrtDeviceApis::Commons::Exception)
311 LogWarning("trying to set incorrect value");
314 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");