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