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