merge wrt-plugins-tizen_0.2.0-2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Tizen / JSSimpleCoordinates.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file        JSSimpleCoordinates.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSSimpleCoordinates class
22  */
23
24 #include <string>
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <API/Filter/SimpleCoordinates.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include "FilterConverter.h"
33 #include "JSSimpleCoordinates.h"
34
35 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
36
37 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
38 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
39
40 namespace TizenApis {
41 namespace Tizen1_0 {
42 namespace Tizen {
43
44 using namespace TizenApis::Commons;
45 using namespace TizenApis::Api::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48
49 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
50
51 JSClassDefinition JSSimpleCoordinates::m_classInfo =
52 {
53         0,
54         kJSClassAttributeNone,
55         ATTRIBUTE_FILTER_CLASS_NAME,
56         NULL,
57         m_property,
58         m_functions,
59         Initialize,
60         Finalize,
61         NULL, //hasProperty,
62         NULL, //GetProperty,
63         NULL, //SetProperty,
64         NULL, //DeleteProperty,
65         NULL, //getPropertyNames,
66         NULL, //CallAsFunction,
67         constructor, //CallAsConstructor,
68         NULL, //HasInstance,
69         NULL, //ConvertToType,
70 };
71
72 JSStaticValue JSSimpleCoordinates::m_property[] = {
73         { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
74         { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
75         { 0, 0, 0, 0 }
76 };
77
78 JSStaticFunction JSSimpleCoordinates::m_functions[] =
79 {
80         { 0, 0, 0 }
81 };
82
83 JSClassRef JSSimpleCoordinates::getClassRef() {
84         if (!m_classRef) {
85                 m_classRef = JSClassCreate(&m_classInfo);
86         }
87         return m_classRef;
88 }
89
90 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
91 {
92         return JSValueIsObjectOfClass(context, value, getClassRef());
93 }
94
95 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
96 {
97         if (!isObjectOfClass(context, value)) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSObjectRef object = JSValueToObject(context, value, NULL);
101         if (!object) {
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103         }
104         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
105         if (!priv) {
106                 Throw(WrtDeviceApis::Commons::NullPointerException);
107         }
108         return priv->getObject();
109 }
110
111 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
112 {
113         JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, privateData);
114         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
115         if (NULL == jsObjectRef) {
116                 LogError("object creation error");
117                 return NULL;
118         }
119         return jsObjectRef;
120 }
121
122 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
123 {
124         if (!JSObjectGetPrivate(object))
125         {
126                 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
127                 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
128                 if (!JSObjectSetPrivate(object, priv)) {
129                         delete priv;
130                 }
131         }
132 }
133
134 void JSSimpleCoordinates::Finalize(JSObjectRef object)
135 {
136         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
137
138         if (priv != NULL)
139         {
140                 delete (priv);
141         }
142
143         priv = NULL;
144 }
145
146 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
147 {
148         LogDebug("entered");
149         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
150         if (!priv) {
151                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152         }
153         SimpleCoordinatesPtr result = priv->getObject();
154         if (!result) {
155                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
156         }
157         return result;
158 }
159
160 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
161                 JSObjectRef constructor,
162                 size_t argumentCount,
163                 const JSValueRef arguments[],
164                 JSValueRef* exception)
165 {
166         LogDebug("entered");
167
168 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
169 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
170
171         JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(constructor));
172         if (!priv) {
173                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
174         }
175         JSContextRef gContext = priv->getContext();
176
177 //      JSGlobalContextRef gContext = JSGlobalContextFactory::getInstance()->get();
178
179         BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
180         Try {
181                 if (argumentCount != 2)
182                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
183
184                 if (!JSValueIsNumber(gContext, arguments[0]))
185                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
186
187                 if (!JSValueIsNumber(gContext, arguments[1]))
188                         ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
189
190         } Catch(Exception ) {
191                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
192                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
193                 return NULL;
194         }
195
196         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
197
198         double latitude;
199         double longitude;
200
201         Try {
202                 latitude = converter->toDouble(arguments[0]);
203         } Catch(Exception) {
204                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
205                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
206                 return NULL;
207         }
208
209         Try {
210                 longitude = converter->toDouble(arguments[1]);
211         } Catch(Exception) {
212                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
213                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
214                 return NULL;
215         }
216         printf("latitude : %f\n\n", latitude);
217         SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
218
219         JSObjectRef jsobject;
220
221         Try {
222                 jsobject = createJSObject(gContext, simpleCoordinates);
223         } Catch(Exception) {
224                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
225                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
226                 return NULL;
227         }
228
229         return jsobject;
230 }
231
232 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
233                 JSObjectRef object,
234                 JSStringRef propertyName,
235                 JSValueRef* exception)
236 {
237         LogDebug("entered");
238         Try
239         {
240                 FilterConverterFactory::ConverterType converter =
241                                 FilterConverterFactory::getConverter(context);
242                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
243                 return converter->toJSValueRef(simpleCoordinates->getLatitude());
244         }
245         Catch(WrtDeviceApis::Commons::Exception)
246         {
247                 LogWarning("trying to get incorrect value");
248         }
249         return JSValueMakeUndefined(context);
250 }
251
252 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
253                 JSObjectRef object,
254                 JSStringRef propertyName,
255                 JSValueRef value,
256                 JSValueRef* exception)
257 {
258         Try
259         {
260                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
261                 FilterConverterFactory::ConverterType converter =
262                                 FilterConverterFactory::getConverter(context);
263                 simpleCoordinates->setLatitude(converter->toDouble(value));
264                 return true;
265         }
266         Catch(WrtDeviceApis::Commons::Exception)
267         {
268                 LogWarning("trying to set incorrect value");
269         }
270
271         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
272         return false;
273 }
274
275 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
276                 JSObjectRef object,
277                 JSStringRef propertyName,
278                 JSValueRef* exception)
279 {
280         LogDebug("entered");
281         Try
282         {
283                 FilterConverterFactory::ConverterType converter =
284                                 FilterConverterFactory::getConverter(context);
285                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
286                 return converter->toJSValueRef(simpleCoordinates->getLongitude());
287         }
288         Catch(WrtDeviceApis::Commons::Exception)
289         {
290                 LogWarning("trying to get incorrect value");
291         }
292         return JSValueMakeUndefined(context);
293 }
294
295 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
296                 JSObjectRef object,
297                 JSStringRef propertyName,
298                 JSValueRef value,
299                 JSValueRef* exception)
300 {
301         Try
302         {
303                 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
304                 FilterConverterFactory::ConverterType converter =
305                                 FilterConverterFactory::getConverter(context);
306                 simpleCoordinates->setLongitude(converter->toDouble(value));
307                 return true;
308         }
309         Catch(WrtDeviceApis::Commons::Exception)
310         {
311                 LogWarning("trying to set incorrect value");
312         }
313
314         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
315         return false;
316 }
317
318 } // Tizen
319 } // Tizen1_0
320 } // TizenApis