wrt-plugins-tizen_0.4.23
[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_UID, getPropertyUid, NULL, kJSPropertyAttributeReadOnly},
71 //    { TIZEN_CONTENT_IMAGE_GEOLOCATION, getPropertyGeoLocation, setPropertyGeolocation, kJSPropertyAttributeNone},
72     { TIZEN_CONTENT_IMAGE_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
73     { TIZEN_CONTENT_IMAGE_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_CONTENT_IMAGE_ORIENTATION, getPropertyOrientation, setPropertyOrientation, kJSPropertyAttributeNone},
75     { 0, 0, 0, 0 }
76 };
77
78
79 JSClassRef JSImage::m_jsClassRef = JSClassCreate(JSImage::getClassInfo());
80
81 void JSImage::initialize(JSContextRef context, JSObjectRef object)
82 {
83     LoggerD( "entered" );
84     ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) );
85
86     if (!priv) {
87         MediacontentImagePtr privateData(new MediacontentImage());
88         priv = new ImagePrivObject(context, privateData);
89         JSObjectSetPrivate(object, static_cast<void*>(priv));
90         LoggerD("new private object is created" );
91     }
92     else {
93         LoggerD("private object already exists");
94                 MediacontentImagePtr image = getImageObject(object);
95                 DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(image->getImageLatitude(),image->getImageLongitude()));
96                 JSUtil::setProperty(context, object, TIZEN_CONTENT_IMAGE_GEOLOCATION,
97                                         JSSimpleCoordinates::createJSObject(context,geoPtr),
98                                         kJSPropertyAttributeNone);
99     }
100 }
101
102 void JSImage::finalize(JSObjectRef object)
103 {
104     LoggerD( "entered" );
105         ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) ) ;
106         if (priv != NULL)
107         {
108                 delete (priv);
109                 priv = NULL;
110                 JSObjectSetPrivate(object, NULL);
111         }
112 }
113
114 const JSClassRef JSImage::getClassRef()
115 {
116         LoggerD("JSImage::getClassRef()");
117
118     if (!m_jsClassRef) {
119             m_jsClassRef = JSClassCreate(&m_classInfo);
120     }
121     return m_jsClassRef;
122 }
123
124 const JSClassDefinition* JSImage::getClassInfo()
125 {
126     return &m_classInfo;
127 }
128
129 MediacontentImagePtr JSImage::getImageObject(JSObjectRef object)
130 {
131     LoggerD("entered");
132     ImagePrivObject *priv = static_cast<ImagePrivObject*>(JSObjectGetPrivate(object));
133     if(!priv) {
134         ThrowMsg(NullPointerException, "Private object is null");
135     }
136     MediacontentImagePtr result = priv->getObject();
137     if (!result) {
138         ThrowMsg(NullPointerException, "Private object is null");
139     }
140     return result;
141 }
142
143 JSValueRef JSImage::getPropertyGeoLocation(
144                 JSContextRef context,
145         JSObjectRef object,
146         JSStringRef propertyName,
147         JSValueRef* exception)
148 {
149
150     LoggerD("entered");
151     Try
152     {
153         Converter converter(context);
154         MediacontentImagePtr image = getImageObject(object);
155
156                 LoggerD("lati :"  << image->getImageLatitude());
157                 LoggerD("longi :"  << image->getImageLongitude());
158                 DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(image->getImageLatitude(),image->getImageLongitude()));
159                 return JSSimpleCoordinates::createJSObject(context,geoPtr);
160
161     }
162     Catch(Exception)
163     {
164         LoggerW("trying to get incorrect value");
165     }
166     return JSValueMakeNull(context);
167 }
168
169
170 JSValueRef JSImage::getPropertyWidth(
171                 JSContextRef context,
172         JSObjectRef object,
173         JSStringRef propertyName,
174         JSValueRef* exception)
175 {
176     LoggerD("entered");
177     Try
178     {
179         Converter converter(context);
180         MediacontentImagePtr image = getImageObject(object);
181         return converter.toJSValueRef(image->getImageWidth());
182     }
183     Catch(Exception)
184     {
185         LoggerW("trying to get incorrect value");
186     }
187     return JSValueMakeUndefined(context);
188 }
189
190
191 JSValueRef JSImage::getPropertyHeight(
192                 JSContextRef context,
193         JSObjectRef object,
194         JSStringRef propertyName,
195         JSValueRef* exception)
196 {
197     LoggerD("entered");
198     Try
199     {
200         Converter converter(context);
201         MediacontentImagePtr image = getImageObject(object);
202         return converter.toJSValueRef(image->getImageHeight());
203     }
204     Catch(Exception)
205     {
206         LoggerW("trying to get incorrect value");
207     }
208     return JSValueMakeUndefined(context);
209 }
210
211 JSValueRef JSImage::getPropertyOrientation(
212                 JSContextRef context,
213         JSObjectRef object,
214         JSStringRef propertyName,
215         JSValueRef* exception)
216 {
217     LoggerD("entered");
218     Try
219     {
220         Converter converter(context);
221         MediacontentImagePtr image = getImageObject(object);
222         if(!(image->getImageOrientation().empty())){
223             return converter.toJSValueRef(image->getImageOrientation());
224         }
225     }
226     Catch(Exception)
227     {
228         LoggerW("trying to get incorrect value");
229     }
230     return JSValueMakeNull(context);
231 }
232
233 bool    JSImage::setPropertyOrientation(
234                                 JSContextRef context,
235                                 JSObjectRef object,
236                                 JSStringRef propertyName,
237                                 JSValueRef value,
238                                 JSValueRef* exception)
239 {
240          LoggerD("entered");
241          Try
242             {
243                 Converter converter(context);
244                 MediacontentImagePtr objImg = getImageObject(object);
245
246                 string orientation = converter.toString(value);
247                 LoggerD("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                 LoggerW("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         LoggerD("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                 LoggerW("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