d9fa61c398ad9feb0a55e652fea05eb783733d37
[framework/web/wrt-plugins-tizen.git] / src / Content / JSImage.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 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <JSSimpleCoordinates.h>
25
26 #include <JSTizenExceptionFactory.h>
27 #include <JSTizenException.h>
28 #include <SecurityExceptions.h>
29
30 #include "JSImage.h"
31
32
33 #define TIZEN_CONTENT_IMAGE_ATTRIBUTENAME               "ImageContent"
34 #define TIZEN_CONTENT_IMAGE_GEOLOCATION                 "geolocation"
35 #define TIZEN_CONTENT_IMAGE_WIDTH                       "width"
36 #define TIZEN_CONTENT_IMAGE_HEIGHT                      "height"
37 #define TIZEN_CONTENT_IMAGE_ORIENTATION                 "orientation"
38
39 using namespace DeviceAPI::Tizen;
40 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace DeviceAPI::Tizen;
43
44 namespace DeviceAPI {
45 namespace Content {
46
47
48 JSClassDefinition JSImage::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         TIZEN_CONTENT_IMAGE_ATTRIBUTENAME,
53         JSMedia::getClassRef(),
54         m_property,
55         NULL, //    m_function,
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 JSImage::m_property[] =
70 {
71 //    { TIZEN_CONTENT_IMAGE_UID, getPropertyUid, NULL, kJSPropertyAttributeReadOnly},
72     { TIZEN_CONTENT_IMAGE_GEOLOCATION, getPropertyGeoLocation, setPropertyGeolocation, kJSPropertyAttributeNone},
73     { TIZEN_CONTENT_IMAGE_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_CONTENT_IMAGE_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},    
75     { TIZEN_CONTENT_IMAGE_ORIENTATION, getPropertyOrientation, setPropertyOrientation, kJSPropertyAttributeNone},  
76     { 0, 0, 0, 0 }
77 };
78
79
80 JSClassRef JSImage::m_jsClassRef = JSClassCreate(JSImage::getClassInfo());
81
82 void JSImage::initialize(JSContextRef context, JSObjectRef object)
83 {
84     LogDebug( "entered" );
85     ImagePrivObject *priv =
86             static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) );
87     if (!priv) {
88         MediacontentImagePtr privateData(new MediacontentImage());
89         priv = new ImagePrivObject(context, privateData);
90         JSObjectSetPrivate(object, static_cast<void*>(priv));
91         LogDebug("new private object is created" );
92     }
93     else {
94         LogDebug("private object already exists");
95     }
96 }
97
98 void JSImage::finalize(JSObjectRef object)
99 {
100     LogDebug( "entered" );
101         ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) ) ;
102         if (priv != NULL)
103         {
104                 delete (priv);
105                 priv = NULL;
106                 JSObjectSetPrivate(object, NULL);               
107         }
108 }
109
110 const JSClassRef JSImage::getClassRef()
111 {
112         LogDebug("JSImage::getClassRef()");
113
114     if (!m_jsClassRef) {
115             m_jsClassRef = JSClassCreate(&m_classInfo);
116     }
117     return m_jsClassRef;
118 }
119
120 const JSClassDefinition* JSImage::getClassInfo()
121 {
122     return &m_classInfo;
123 }
124
125 MediacontentImagePtr JSImage::getImageObject(JSObjectRef object)
126 {
127     LogDebug("entered");
128     ImagePrivObject *priv = static_cast<ImagePrivObject*>(JSObjectGetPrivate(object));
129     if(!priv) {
130         ThrowMsg(NullPointerException, "Private object is null");
131     }
132     MediacontentImagePtr result = priv->getObject();
133     if (!result) {
134         ThrowMsg(NullPointerException, "Private object is null");
135     }
136     return result;
137 }
138                                         
139 JSValueRef JSImage::getPropertyGeoLocation(
140                 JSContextRef context,
141         JSObjectRef object,
142         JSStringRef propertyName,
143         JSValueRef* exception)
144 {
145
146     LogDebug("entered");
147     Try
148     {
149         Converter converter(context);
150         MediacontentImagePtr image = getImageObject(object);
151
152                 LogDebug("lati :"  << image->getImageLatitude());
153                 LogDebug("longi :"  << image->getImageLongitude());             
154                 DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(image->getImageLatitude(),image->getImageLongitude()));
155                 return JSSimpleCoordinates::createJSObject(context,geoPtr);
156
157     }
158     Catch(Exception)
159     {
160         LogWarning("trying to get incorrect value");
161     }
162     return JSValueMakeUndefined(context);
163 }
164
165
166 JSValueRef JSImage::getPropertyWidth(
167                 JSContextRef context,
168         JSObjectRef object, 
169         JSStringRef propertyName, 
170         JSValueRef* exception)
171 {
172     LogDebug("entered");
173     Try
174     {
175         Converter converter(context);
176         MediacontentImagePtr image = getImageObject(object);
177         return converter.toJSValueRef(image->getImageWidth());
178     }
179     Catch(Exception)
180     {
181         LogWarning("trying to get incorrect value");
182     }
183     return JSValueMakeUndefined(context);
184 }
185
186
187 JSValueRef JSImage::getPropertyHeight(
188                 JSContextRef context,
189         JSObjectRef object,
190         JSStringRef propertyName,
191         JSValueRef* exception)
192 {
193     LogDebug("entered");
194     Try
195     {
196         Converter converter(context);
197         MediacontentImagePtr image = getImageObject(object);
198         return converter.toJSValueRef(image->getImageHeight());
199     }
200     Catch(Exception)
201     {
202         LogWarning("trying to get incorrect value");
203     }
204     return JSValueMakeUndefined(context);
205 }
206
207 JSValueRef JSImage::getPropertyOrientation(
208                 JSContextRef context,
209         JSObjectRef object,
210         JSStringRef propertyName,
211         JSValueRef* exception)
212 {
213     LogDebug("entered");
214     Try
215     {
216         Converter converter(context);
217         MediacontentImagePtr image = getImageObject(object);
218         return converter.toJSValueRef(image->getImageOrientation());
219     }
220     Catch(Exception)
221     {
222         LogWarning("trying to get incorrect value");
223     }
224     return JSValueMakeUndefined(context);
225 }
226
227 bool    JSImage::setPropertyOrientation(
228                                 JSContextRef context,
229                                 JSObjectRef object,
230                                 JSStringRef propertyName,
231                                 JSValueRef value,
232                                 JSValueRef* exception)
233 {
234          LogDebug("entered");
235          Try
236             {
237                 Converter converter(context);
238                 MediacontentImagePtr objImg = getImageObject(object);
239
240                 string orientation = converter.toString(value);
241                 LogDebug("Inserted value : " << orientation);
242
243                 if ((objImg->getImageOrientation()).compare(orientation) !=0 )
244                 {
245                         objImg->setImageOrientation(orientation, true);
246                 }
247                 
248                 return true;                    
249             }
250             Catch(Exception)
251             {
252                 LogWarning("trying to get incorrect value");
253                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);
254             }
255
256            return false;
257 }
258
259 bool    JSImage::setPropertyGeolocation(
260                                 JSContextRef context,
261                                 JSObjectRef object,
262                                 JSStringRef propertyName,
263                                 JSValueRef value,
264                                 JSValueRef* exception)
265 {
266         LogDebug("entered");
267
268         Try
269         {
270                 Converter converter(context);
271                 MediacontentImagePtr objImg = getImageObject(object);
272
273                 DeviceAPI::Tizen::SimpleCoordinatesPtr geoLocation =
274                         DeviceAPI::Tizen::JSSimpleCoordinates::getSimpleCoordinates(context, value);
275
276                 objImg->setImageLatitude(geoLocation->getLatitude());
277                 objImg->setImageLongitude(geoLocation->getLongitude());
278         
279                 return true;
280         }
281         Catch(Exception)
282         {
283                 LogWarning("trying to get incorrect value");
284                 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
285         }
286
287         return false;
288
289 }
290
291 }
292 }
293