Remove hardcoded path for multiuser support
[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 <JSWebAPIErrorFactory.h>
25 #include <SecurityExceptions.h>
26
27 #include "JSUtil.h"
28 #include "JSImage.h"
29 #include <Logger.h>
30
31 #define TIZEN_CONTENT_IMAGE_ATTRIBUTENAME               "ImageContent"
32 #define TIZEN_CONTENT_IMAGE_GEOLOCATION                 "geolocation"
33 #define TIZEN_CONTENT_IMAGE_WIDTH                       "width"
34 #define TIZEN_CONTENT_IMAGE_HEIGHT                      "height"
35 #define TIZEN_CONTENT_IMAGE_ORIENTATION                 "orientation"
36
37 using namespace DeviceAPI::Tizen;
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::Commons;
40 using namespace DeviceAPI::Tizen;
41
42 namespace DeviceAPI {
43 namespace Content {
44
45
46 JSClassDefinition JSImage::m_classInfo =
47 {
48         0,
49         kJSClassAttributeNone,
50         TIZEN_CONTENT_IMAGE_ATTRIBUTENAME,
51         JSMedia::getClassRef(),
52         m_property,
53         NULL, //    m_function,
54         initialize,
55         finalize,
56         NULL, //hasProperty,
57         NULL, //getProperty,
58         setProperty, //setProperty,
59         NULL, //DeleteProperty,
60         NULL, //GetPropertyNames,
61         NULL, //CallAsFunction,
62         NULL, //CallAsConstructor,
63         NULL, //HasInstance,
64         NULL  //ConvertToType
65 };
66
67 JSStaticValue JSImage::m_property[] =
68 {
69     { TIZEN_CONTENT_IMAGE_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
70     { TIZEN_CONTENT_IMAGE_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},
71     { TIZEN_CONTENT_IMAGE_ORIENTATION, getPropertyOrientation, setPropertyOrientation, kJSPropertyAttributeNone},
72     { 0, 0, 0, 0 }
73 };
74
75
76 JSClassRef JSImage::m_jsClassRef = JSClassCreate(JSImage::getClassInfo());
77
78 void JSImage::initialize(JSContextRef context, JSObjectRef object)
79 {
80
81     ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) );
82
83     if (!priv) {
84         MediacontentImagePtr privateData(new MediacontentImage());
85         priv = new ImagePrivObject(context, privateData);
86         JSObjectSetPrivate(object, static_cast<void*>(priv));
87     }
88     else {
89         MediacontentImagePtr image = getImageObject(object);
90         if(image->getImageLatitude() != DEFAULT_GEOLOCATION  && 
91             image->getImageLongitude() != DEFAULT_GEOLOCATION){
92             DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(image->getImageLatitude(),image->getImageLongitude()));
93             JSUtil::setProperty(context, object, TIZEN_CONTENT_IMAGE_GEOLOCATION,
94                     JSSimpleCoordinates::createJSObject(context,geoPtr),
95                     kJSPropertyAttributeNone);
96         }
97         else
98         {
99             JSUtil::setProperty(context, object, TIZEN_CONTENT_IMAGE_GEOLOCATION,
100                         JSValueMakeNull(context),kJSPropertyAttributeNone);
101         }
102     }
103 }
104
105 void JSImage::finalize(JSObjectRef object)
106 {
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     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     ImagePrivObject *priv = static_cast<ImagePrivObject*>(JSObjectGetPrivate(object));
132     if(!priv) {
133         ThrowMsg(NullPointerException, "Private object is null");
134     }
135     MediacontentImagePtr result = priv->getObject();
136     if (!result) {
137         ThrowMsg(NullPointerException, "Private object is null");
138     }
139     return result;
140 }
141
142 JSValueRef JSImage::getPropertyWidth(
143                 JSContextRef context,
144         JSObjectRef object,
145         JSStringRef propertyName,
146         JSValueRef* exception)
147 {
148     Try
149     {
150         Converter converter(context);
151         MediacontentImagePtr image = getImageObject(object);
152         return converter.toJSValueRef(image->getImageWidth());
153     }
154     Catch(Exception)
155     {
156         LoggerW("trying to get incorrect value");
157     }
158     return JSValueMakeUndefined(context);
159 }
160
161
162 JSValueRef JSImage::getPropertyHeight(
163                 JSContextRef context,
164         JSObjectRef object,
165         JSStringRef propertyName,
166         JSValueRef* exception)
167 {
168     Try
169     {
170         Converter converter(context);
171         MediacontentImagePtr image = getImageObject(object);
172         return converter.toJSValueRef(image->getImageHeight());
173     }
174     Catch(Exception)
175     {
176         LoggerW("trying to get incorrect value");
177     }
178     return JSValueMakeUndefined(context);
179 }
180
181 JSValueRef JSImage::getPropertyOrientation(
182                 JSContextRef context,
183         JSObjectRef object,
184         JSStringRef propertyName,
185         JSValueRef* exception)
186 {
187     Try
188     {
189         Converter converter(context);
190         MediacontentImagePtr image = getImageObject(object);
191         if(!(image->getImageOrientation().empty())){
192             return converter.toJSValueRef(image->getImageOrientation());
193         }
194     }
195     Catch(Exception)
196     {
197         LoggerW("trying to get incorrect value");
198     }
199     return JSValueMakeNull(context);
200 }
201
202 bool    JSImage::setPropertyOrientation(
203                                 JSContextRef context,
204                                 JSObjectRef object,
205                                 JSStringRef propertyName,
206                                 JSValueRef value,
207                                 JSValueRef* exception)
208 {
209     try
210     {
211         Converter converter(context);
212         MediacontentImagePtr objImg = getImageObject(object);
213
214         string orientation = converter.toString(value);
215         if (orientation != "NORMAL" && orientation != "FLIP_HORIZONTAL" && 
216             orientation != "ROTATE_180" && orientation != "FLIP_VERTICAL" &&
217             orientation != "TRANSPOSE" && orientation != "ROTATE_90" &&
218             orientation != "TRANSVERSE" && orientation != "ROTATE_270"){
219             throw TypeMismatchException("Invalid orientation type");
220         }
221
222         if ((objImg->getImageOrientation()).compare(orientation) !=0 )
223         {
224             objImg->setImageOrientation(orientation, true);
225         }
226
227         return true;                    
228     }
229     catch(...)
230     {
231         LoggerW("trying to get incorrect value");
232     }
233
234     return false;
235 }
236
237
238 bool JSImage::setProperty(JSContextRef context, JSObjectRef object,
239         JSStringRef propertyName, JSValueRef value,  JSValueRef* exception)
240 {
241     try {
242         // check geolocation
243         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CONTENT_IMAGE_GEOLOCATION)) {
244             if(JSValueIsUndefined(context, value)){
245                 throw TypeMismatchException("Invalid geolocation");
246             }
247         }
248     } catch (const BasePlatformException &err) {
249        LOGE("setProperty error< %s : %s> " , err.getName().c_str(), err.getMessage().c_str());
250        return true;
251     }
252     return false;
253 }
254
255
256 }
257 }
258