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