256deb3f11ba8bc85730094aef085e64690d5baf
[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 <JSUtil.h>
30 #include <ArgumentValidator.h>
31 #include <JSWebAPIErrorFactory.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                 return JSWebAPIErrorFactory::postException(context, exception, e);
141         }
142
143         SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
144
145         JSObjectRef jsobject;
146
147         Try
148         {
149                 jsobject = createJSObject(context, simpleCoordinates);
150         }
151         Catch(Exception)
152         {
153                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
154                 return JSWebAPIErrorFactory::postException(context, exception,
155                                 TypeMismatchException("Error occurred while creating object"));
156         }
157
158         return jsobject;
159 }
160
161 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
162 {
163         if (!JSObjectGetPrivate(object))
164         {
165                 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
166                 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
167                 if (!JSObjectSetPrivate(object, priv))
168                 {
169                         delete priv;
170                 }
171         }
172 }
173
174 void JSSimpleCoordinates::Finalize(JSObjectRef object)
175 {
176         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
177
178         if (priv != NULL)
179         {
180                 delete (priv);
181         }
182
183         priv = NULL;
184 }
185
186 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
187 {
188         LoggerD("entered");
189         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
190         if (!priv)
191                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
192
193         SimpleCoordinatesPtr result = priv->getObject();
194         if (!result)
195                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
196
197         return result;
198 }
199
200 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
201                 JSObjectRef object,
202                 JSStringRef propertyName,
203                 JSValueRef* exception)
204 {
205         LoggerD("entered");
206
207         try
208         {
209                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
210                 return JSUtil::toJSValueRef(context, simpleCoordinates->getLatitude());
211         }
212         catch(BasePlatformException &e)
213         {
214                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
215         }
216
217         return JSValueMakeUndefined(context);
218 }
219
220 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
221                 JSObjectRef object,
222                 JSStringRef propertyName,
223                 JSValueRef value,
224                 JSValueRef* exception)
225 {
226         LoggerD("entered");
227
228         try
229         {
230                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
231                 simpleCoordinates->setLatitude(JSUtil::JSValueToDouble(context, value));
232         }
233         catch(BasePlatformException &e)
234         {
235                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
236         }
237
238         return true;
239 }
240
241 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
242                 JSObjectRef object,
243                 JSStringRef propertyName,
244                 JSValueRef* exception)
245 {
246         LoggerD("entered");
247
248         try
249         {
250                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
251                 return JSUtil::toJSValueRef(context, simpleCoordinates->getLongitude());
252         }
253         catch(BasePlatformException &e)
254         {
255                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
256         }
257
258         return JSValueMakeUndefined(context);
259 }
260
261 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
262                 JSObjectRef object,
263                 JSStringRef propertyName,
264                 JSValueRef value,
265                 JSValueRef* exception)
266 {
267         LoggerD("entered");
268
269         try
270         {
271                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
272                 simpleCoordinates->setLongitude(JSUtil::JSValueToDouble(context, value));
273         }
274         catch(BasePlatformException &e)
275         {
276                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
277         }
278
279         return true;
280 }
281
282 } // Tizen
283 } // DeviceAPI