Update change log and spec for wrt-plugins-tizen_0.4.11
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSSimpleCoordinates.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        JSSimpleCoordinates.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSSimpleCoordinates class
23  */
24
25 #include <string>
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"
35
36 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
37
38 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
39 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
40
41 namespace DeviceAPI {\rnamespace Tizen {
42
43 using namespace DeviceAPI::Common;
44 using namespace DeviceAPI::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47
48 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
49
50 JSClassDefinition JSSimpleCoordinates::m_classInfo =
51 {
52         0,
53         kJSClassAttributeNone,
54         ATTRIBUTE_FILTER_CLASS_NAME,
55         NULL,
56         m_property,
57         m_functions,
58         Initialize,
59         Finalize,
60         NULL, //hasProperty,
61         NULL, //GetProperty,
62         NULL, //SetProperty,
63         NULL, //DeleteProperty,
64         NULL, //getPropertyNames,
65         NULL, //CallAsFunction,
66         NULL, //CallAsConstructor,
67         NULL, //HasInstance,
68         NULL, //ConvertToType,
69 };
70
71 JSStaticValue JSSimpleCoordinates::m_property[] = {
72         { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
73         { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
74         { 0, 0, 0, 0 }
75 };
76
77 JSStaticFunction JSSimpleCoordinates::m_functions[] =
78 {
79         { 0, 0, 0 }
80 };
81
82 JSClassRef JSSimpleCoordinates::getClassRef() {
83         if (!m_classRef) {
84                 m_classRef = JSClassCreate(&m_classInfo);
85         }
86         return m_classRef;
87 }
88
89 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
90 {
91         return JSValueIsObjectOfClass(context, value, getClassRef());
92 }
93
94 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
95 {
96         if (!isObjectOfClass(context, value)) {
97                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98         }
99         JSObjectRef object = JSValueToObject(context, value, NULL);
100         if (!object) {
101                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102         }
103         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
104         if (!priv) {
105                 Throw(WrtDeviceApis::Commons::NullPointerException);
106         }
107         return priv->getObject();
108 }
109
110 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
111 {
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");
116                 return NULL;
117         }
118         return jsObjectRef;
119 }
120
121 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
122                 JSObjectRef constructor,
123                 size_t argumentCount,
124                 const JSValueRef arguments[],
125                 JSValueRef* exception)
126 {
127         LogDebug("entered");
128
129         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
130         Try {
131                 if (argumentCount < 2)
132                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
133
134                 if (!JSValueIsNumber(context, arguments[0]))
135                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
136
137                 if (!JSValueIsNumber(context, arguments[1]))
138                         ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
139
140         } Catch(Exception ) {
141                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
142                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
143                 return NULL;
144         }
145
146         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
147
148         double latitude;
149         double longitude;
150
151         Try {
152                 latitude = converter->toDouble(arguments[0]);
153         } Catch(Exception) {
154                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
155                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
156                 return NULL;
157         }
158
159         Try {
160                 longitude = converter->toDouble(arguments[1]);
161         } Catch(Exception) {
162                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
163                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
164                 return NULL;
165         }
166
167         SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
168
169         JSObjectRef jsobject;
170
171         Try {
172                 jsobject = createJSObject(context, simpleCoordinates);
173         } Catch(Exception) {
174                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
175                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
176                 return NULL;
177         }
178
179         return jsobject;
180 }
181
182 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
183 {
184         if (!JSObjectGetPrivate(object))
185         {
186                 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
187                 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
188                 if (!JSObjectSetPrivate(object, priv)) {
189                         delete priv;
190                 }
191         }
192 }
193
194 void JSSimpleCoordinates::Finalize(JSObjectRef object)
195 {
196         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
197
198         if (priv != NULL)
199         {
200                 delete (priv);
201         }
202
203         priv = NULL;
204 }
205
206 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
207 {
208         LogDebug("entered");
209         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
210         if (!priv) {
211                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
212         }
213         SimpleCoordinatesPtr result = priv->getObject();
214         if (!result) {
215                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
216         }
217         return result;
218 }
219
220 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
221                 JSObjectRef object,
222                 JSStringRef propertyName,
223                 JSValueRef* exception)
224 {
225         LogDebug("entered");
226         Try
227         {
228                 FilterConverterFactory::ConverterType converter =
229                                 FilterConverterFactory::getConverter(context);
230                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
231                 return converter->toJSValueRef(simpleCoordinates->getLatitude());
232         }
233         Catch(WrtDeviceApis::Commons::Exception)
234         {
235                 LogWarning("trying to get incorrect value");
236         }
237         return JSValueMakeUndefined(context);
238 }
239
240 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
241                 JSObjectRef object,
242                 JSStringRef propertyName,
243                 JSValueRef value,
244                 JSValueRef* exception)
245 {
246         Try
247         {
248                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
249                 FilterConverterFactory::ConverterType converter =
250                                 FilterConverterFactory::getConverter(context);
251                 simpleCoordinates->setLatitude(converter->toDouble(value));
252                 return true;
253         }
254         Catch(WrtDeviceApis::Commons::Exception)
255         {
256                 LogWarning("trying to set incorrect value");
257         }
258
259         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
260         return false;
261 }
262
263 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
264                 JSObjectRef object,
265                 JSStringRef propertyName,
266                 JSValueRef* exception)
267 {
268         LogDebug("entered");
269         Try
270         {
271                 FilterConverterFactory::ConverterType converter =
272                                 FilterConverterFactory::getConverter(context);
273                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
274                 return converter->toJSValueRef(simpleCoordinates->getLongitude());
275         }
276         Catch(WrtDeviceApis::Commons::Exception)
277         {
278                 LogWarning("trying to get incorrect value");
279         }
280         return JSValueMakeUndefined(context);
281 }
282
283 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
284                 JSObjectRef object,
285                 JSStringRef propertyName,
286                 JSValueRef value,
287                 JSValueRef* exception)
288 {
289         Try
290         {
291                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
292                 FilterConverterFactory::ConverterType converter =
293                                 FilterConverterFactory::getConverter(context);
294                 simpleCoordinates->setLongitude(converter->toDouble(value));
295                 return true;
296         }
297         Catch(WrtDeviceApis::Commons::Exception)
298         {
299                 LogWarning("trying to set incorrect value");
300         }
301
302         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
303         return false;
304 }
305
306 } // Tizen
307 } // DeviceAPI
308