30345537fe629e70c38c10fec0cc629797fa2189
[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         constructor, //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 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
122 {
123         if (!JSObjectGetPrivate(object))
124         {
125                 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
126                 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
127                 if (!JSObjectSetPrivate(object, priv)) {
128                         delete priv;
129                 }
130         }
131 }
132
133 void JSSimpleCoordinates::Finalize(JSObjectRef object)
134 {
135         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
136
137         if (priv != NULL)
138         {
139                 delete (priv);
140         }
141
142         priv = NULL;
143 }
144
145 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
146 {
147         LogDebug("entered");
148         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
149         if (!priv) {
150                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151         }
152         SimpleCoordinatesPtr result = priv->getObject();
153         if (!result) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155         }
156         return result;
157 }
158
159 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
160                 JSObjectRef constructor,
161                 size_t argumentCount,
162                 const JSValueRef arguments[],
163                 JSValueRef* exception)
164 {
165         LogDebug("entered");
166
167 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
168 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
169
170         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(constructor));
171         if (!priv) {
172                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
173         }
174         JSContextRef gContext = priv->getContext();
175
176 //      JSGlobalContextRef gContext = JSGlobalContextFactory::getInstance()->get();
177
178         BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
179         Try {
180                 if (argumentCount < 2)
181                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
182
183                 if (!JSValueIsNumber(gContext, arguments[0]))
184                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
185
186                 if (!JSValueIsNumber(gContext, arguments[1]))
187                         ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
188
189         } Catch(Exception ) {
190                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
191                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
192                 return NULL;
193         }
194
195         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
196
197         double latitude;
198         double longitude;
199
200         Try {
201                 latitude = converter->toDouble(arguments[0]);
202         } Catch(Exception) {
203                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
204                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
205                 return NULL;
206         }
207
208         Try {
209                 longitude = converter->toDouble(arguments[1]);
210         } Catch(Exception) {
211                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
212                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
213                 return NULL;
214         }
215
216         SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
217
218         JSObjectRef jsobject;
219
220         Try {
221                 jsobject = createJSObject(gContext, simpleCoordinates);
222         } Catch(Exception) {
223                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
224                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
225                 return NULL;
226         }
227
228         return jsobject;
229 }
230
231 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
232                 JSObjectRef object,
233                 JSStringRef propertyName,
234                 JSValueRef* exception)
235 {
236         LogDebug("entered");
237         Try
238         {
239                 FilterConverterFactory::ConverterType converter =
240                                 FilterConverterFactory::getConverter(context);
241                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
242                 return converter->toJSValueRef(simpleCoordinates->getLatitude());
243         }
244         Catch(WrtDeviceApis::Commons::Exception)
245         {
246                 LogWarning("trying to get incorrect value");
247         }
248         return JSValueMakeUndefined(context);
249 }
250
251 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
252                 JSObjectRef object,
253                 JSStringRef propertyName,
254                 JSValueRef value,
255                 JSValueRef* exception)
256 {
257         Try
258         {
259                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
260                 FilterConverterFactory::ConverterType converter =
261                                 FilterConverterFactory::getConverter(context);
262                 simpleCoordinates->setLatitude(converter->toDouble(value));
263                 return true;
264         }
265         Catch(WrtDeviceApis::Commons::Exception)
266         {
267                 LogWarning("trying to set incorrect value");
268         }
269
270         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
271         return false;
272 }
273
274 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
275                 JSObjectRef object,
276                 JSStringRef propertyName,
277                 JSValueRef* exception)
278 {
279         LogDebug("entered");
280         Try
281         {
282                 FilterConverterFactory::ConverterType converter =
283                                 FilterConverterFactory::getConverter(context);
284                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
285                 return converter->toJSValueRef(simpleCoordinates->getLongitude());
286         }
287         Catch(WrtDeviceApis::Commons::Exception)
288         {
289                 LogWarning("trying to get incorrect value");
290         }
291         return JSValueMakeUndefined(context);
292 }
293
294 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
295                 JSObjectRef object,
296                 JSStringRef propertyName,
297                 JSValueRef value,
298                 JSValueRef* exception)
299 {
300         Try
301         {
302                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
303                 FilterConverterFactory::ConverterType converter =
304                                 FilterConverterFactory::getConverter(context);
305                 simpleCoordinates->setLongitude(converter->toDouble(value));
306                 return true;
307         }
308         Catch(WrtDeviceApis::Commons::Exception)
309         {
310                 LogWarning("trying to set incorrect value");
311         }
312
313         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
314         return false;
315 }
316
317 } // Tizen
318 } // DeviceAPI
319