Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSImage.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 #include <dpl/log/log.h>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include "JSMediacontent.h"
24 #include "JSImage.h"
25
26
27
28 #define TIZEN_MEDIACONTENT_IMAGE_ATTRIBUTENAME          "Image"
29 #define TIZEN_MEDIACONTENT_IMAGE_GEOLOCATION            "geolocation"
30 #define TIZEN_MEDIACONTENT_IMAGE_WIDTH                          "width"
31 #define TIZEN_MEDIACONTENT_IMAGE_HEIGHT                         "height"
32 #define TIZEN_MEDIACONTENT_IMAGE_ORIENTATION            "orientation"
33
34
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38 namespace Mediacontent {
39
40
41 JSClassDefinition JSImage::m_classInfo =
42 {
43         0,
44         kJSClassAttributeNone,
45         TIZEN_MEDIACONTENT_IMAGE_ATTRIBUTENAME,
46         JSMedia::getClassRef(),
47         m_property,
48         NULL, //    m_function,
49         initialize,
50         finalize,
51         NULL, //hasProperty,
52         NULL, //getProperty,
53         NULL, //setProperty,
54         NULL, //DeleteProperty,
55         NULL, //GetPropertyNames,
56         NULL, //CallAsFunction,
57         NULL, //CallAsConstructor,
58         NULL, //HasInstance,
59         NULL  //ConvertToType
60 };
61
62 JSStaticValue JSImage::m_property[] =
63 {
64 //    { TIZEN_MEDIACONTENT_IMAGE_UID, getPropertyUid, NULL, kJSPropertyAttributeReadOnly},
65     { TIZEN_MEDIACONTENT_IMAGE_GEOLOCATION, getPropertyGeoLocation, NULL, kJSPropertyAttributeReadOnly},
66     { TIZEN_MEDIACONTENT_IMAGE_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
67     { TIZEN_MEDIACONTENT_IMAGE_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},    
68     { TIZEN_MEDIACONTENT_IMAGE_ORIENTATION, getPropertyOrientation, NULL, kJSPropertyAttributeReadOnly},  
69     { 0, 0, 0, 0 }
70 };
71
72
73 JSClassRef JSImage::m_jsClassRef = JSClassCreate(JSImage::getClassInfo());
74
75 void JSImage::initialize(JSContextRef context, JSObjectRef object)
76 {
77     LogDebug( "entered" );
78     ImagePrivObject *priv =
79             static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) );
80     if (!priv) {
81         MediacontentImagePtr privateData(new MediacontentImage());
82         priv = new ImagePrivObject(context, privateData);
83         JSObjectSetPrivate(object, static_cast<void*>(priv));
84         LogDebug("new event is created" );
85     }
86     else {
87         LogDebug("private object already exists");
88     }
89 }
90
91 void JSImage::finalize(JSObjectRef object)
92 {
93     LogDebug( "entered" );
94         ImagePrivObject *priv =
95         static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) ) ;
96     delete priv;
97 }
98
99 const JSClassRef JSImage::getClassRef()
100 {
101         LogDebug("JSImage::getClassRef()");
102
103     if (!m_jsClassRef) {
104             m_jsClassRef = JSClassCreate(&m_classInfo);
105     }
106     return m_jsClassRef;
107 }
108
109 const JSClassDefinition* JSImage::getClassInfo()
110 {
111     return &m_classInfo;
112 }
113
114 MediacontentImagePtr JSImage::getImageObject(JSObjectRef object)
115 {
116     LogDebug("entered");
117     ImagePrivObject *priv = static_cast<ImagePrivObject*>(JSObjectGetPrivate(object));
118     if(!priv) {
119         ThrowMsg(NullPointerException, "Private object is null");
120     }
121     MediacontentImagePtr result = priv->getObject();
122     if (!result) {
123         ThrowMsg(NullPointerException, "Private object is null");
124     }
125     return result;
126 }
127                                         
128 JSValueRef JSImage::getPropertyGeoLocation(
129                 JSContextRef context,
130         JSObjectRef object,
131         JSStringRef propertyName,
132         JSValueRef* exception)
133 {
134     LogDebug("entered");
135     Try
136     {
137         Converter converter(context);
138         MediacontentImagePtr event = getImageObject(object);
139
140         //Todo. change to getPropertyGeoLocation
141         return converter.toJSValueRef(event->getImageLongitude());
142     }
143     Catch(Exception)
144     {
145         LogWarning("trying to get incorrect value");
146     }
147     return JSValueMakeUndefined(context);
148 }
149
150
151
152 JSValueRef JSImage::getPropertyWidth(
153                 JSContextRef context,
154         JSObjectRef object, 
155         JSStringRef propertyName, 
156         JSValueRef* exception)
157 {
158     LogDebug("entered");
159     Try
160     {
161         Converter converter(context);
162         MediacontentImagePtr event = getImageObject(object);
163         return converter.toJSValueRef(event->getImageWidth());
164     }
165     Catch(Exception)
166     {
167         LogWarning("trying to get incorrect value");
168     }
169     return JSValueMakeUndefined(context);
170 }
171
172
173 JSValueRef JSImage::getPropertyHeight(
174                 JSContextRef context,
175         JSObjectRef object,
176         JSStringRef propertyName,
177         JSValueRef* exception)
178 {
179     LogDebug("entered");
180     Try
181     {
182         Converter converter(context);
183         MediacontentImagePtr event = getImageObject(object);
184         return converter.toJSValueRef(event->getImageHeight());
185     }
186     Catch(Exception)
187     {
188         LogWarning("trying to get incorrect value");
189     }
190     return JSValueMakeUndefined(context);
191 }
192
193 JSValueRef JSImage::getPropertyOrientation(
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 event = getImageObject(object);
204         return converter.toJSValueRef(event->getImageOrientation());
205     }
206     Catch(Exception)
207     {
208         LogWarning("trying to get incorrect value");
209     }
210     return JSValueMakeUndefined(context);
211 }
212
213
214
215 }
216 }
217 }