wrt-plugins-tizen_0.4.23
[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 "JSSimpleCoordinates.h"
26
27 #include <string>
28 #include <dpl/shared_ptr.h>
29 #include <JSWebAPIException.h>
30 #include <JSUtil.h>
31 #include <ArgumentValidator.h>
32 #include <Logger.h>
33
34 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
35
36 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
37 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
38
39 namespace DeviceAPI {\rnamespace Tizen {
40
41 using namespace DeviceAPI::Common;
42 using namespace DeviceAPI::Tizen;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45
46 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
47
48 JSClassDefinition JSSimpleCoordinates::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         ATTRIBUTE_FILTER_CLASS_NAME,
53         NULL,
54         m_property,
55         m_functions,
56         Initialize,
57         Finalize,
58         NULL, //hasProperty,
59         NULL, //GetProperty,
60         NULL, //SetProperty,
61         NULL, //DeleteProperty,
62         NULL, //getPropertyNames,
63         NULL, //CallAsFunction,
64         NULL, //CallAsConstructor,
65         NULL, //HasInstance,
66         NULL, //ConvertToType,
67 };
68
69 JSStaticValue JSSimpleCoordinates::m_property[] = {
70         { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
71         { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
72         { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSSimpleCoordinates::m_functions[] =
76 {
77         { 0, 0, 0 }
78 };
79
80 JSClassRef JSSimpleCoordinates::getClassRef() {
81         if (!m_classRef) {
82                 m_classRef = JSClassCreate(&m_classInfo);
83         }
84         return m_classRef;
85 }
86
87 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
88 {
89         return JSValueIsObjectOfClass(context, value, getClassRef());
90 }
91
92 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
93 {
94         if (!isObjectOfClass(context, value))
95                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
96
97         JSObjectRef object = JSValueToObject(context, value, NULL);
98         if (!object)
99                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100
101         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
102         if (!priv)
103                 Throw(WrtDeviceApis::Commons::NullPointerException);
104
105         return priv->getObject();
106 }
107
108 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
109 {
110         JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, privateData);
111         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
112         if (NULL == jsObjectRef)
113         {
114                 LoggerE("object creation error");
115                 return NULL;
116         }
117         return jsObjectRef;
118 }
119
120 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
121                 JSObjectRef constructor,
122                 size_t argumentCount,
123                 const JSValueRef arguments[],
124                 JSValueRef* exception)
125 {
126         LoggerD("entered");
127
128         ArgumentValidator validator(context, argumentCount, arguments);
129
130         double latitude;
131         double longitude;
132
133         try
134         {
135                 latitude = validator.toDouble(0, false);
136                 longitude = validator.toDouble(1, false);
137         }
138         catch(BasePlatformException &e)
139         {
140                 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(context, e);
141                 *exception = exceptionObj;
142                 return exceptionObj;
143         }
144
145         SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
146
147         JSObjectRef jsobject;
148
149         Try
150         {
151                 jsobject = createJSObject(context, simpleCoordinates);
152         }
153         Catch(Exception)
154         {
155                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
156                 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(
157                                 context, TypeMismatchException("Error occurred while creating object"));
158                 *exception = exceptionObj;
159                 return exceptionObj;
160         }
161
162         return jsobject;
163 }
164
165 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
166 {
167         if (!JSObjectGetPrivate(object))
168         {
169                 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
170                 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
171                 if (!JSObjectSetPrivate(object, priv))
172                 {
173                         delete priv;
174                 }
175         }
176 }
177
178 void JSSimpleCoordinates::Finalize(JSObjectRef object)
179 {
180         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
181
182         if (priv != NULL)
183         {
184                 delete (priv);
185         }
186
187         priv = NULL;
188 }
189
190 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
191 {
192         LoggerD("entered");
193         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
194         if (!priv)
195                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
196
197         SimpleCoordinatesPtr result = priv->getObject();
198         if (!result)
199                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
200
201         return result;
202 }
203
204 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
205                 JSObjectRef object,
206                 JSStringRef propertyName,
207                 JSValueRef* exception)
208 {
209         LoggerD("entered");
210
211         try
212         {
213                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
214                 return JSUtil::toJSValueRef(context, simpleCoordinates->getLatitude());
215         }
216         catch(BasePlatformException &e)
217         {
218                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
219         }
220
221         return JSValueMakeUndefined(context);
222 }
223
224 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
225                 JSObjectRef object,
226                 JSStringRef propertyName,
227                 JSValueRef value,
228                 JSValueRef* exception)
229 {
230         LoggerD("entered");
231
232         try
233         {
234                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
235                 simpleCoordinates->setLatitude(JSUtil::JSValueToDouble(context, value));
236         }
237         catch(BasePlatformException &e)
238         {
239                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
240         }
241
242         return true;
243 }
244
245 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
246                 JSObjectRef object,
247                 JSStringRef propertyName,
248                 JSValueRef* exception)
249 {
250         LoggerD("entered");
251
252         try
253         {
254                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
255                 return JSUtil::toJSValueRef(context, simpleCoordinates->getLongitude());
256         }
257         catch(BasePlatformException &e)
258         {
259                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
260         }
261
262         return JSValueMakeUndefined(context);
263 }
264
265 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
266                 JSObjectRef object,
267                 JSStringRef propertyName,
268                 JSValueRef value,
269                 JSValueRef* exception)
270 {
271         LoggerD("entered");
272
273         try
274         {
275                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
276                 simpleCoordinates->setLongitude(JSUtil::JSValueToDouble(context, value));
277         }
278         catch(BasePlatformException &e)
279         {
280                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
281         }
282
283         return true;
284 }
285
286 } // Tizen
287 } // DeviceAPI