upload tizen1.0 source
[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 <Tizen/Tizen/JSSimpleCoordinates.h>
24 #include "JSMediacontent.h"
25 #include "JSImage.h"
26
27
28
29 #define TIZEN_MEDIACONTENT_IMAGE_ATTRIBUTENAME          "Image"
30 #define TIZEN_MEDIACONTENT_IMAGE_GEOLOCATION            "geolocation"
31 #define TIZEN_MEDIACONTENT_IMAGE_WIDTH                          "width"
32 #define TIZEN_MEDIACONTENT_IMAGE_HEIGHT                         "height"
33 #define TIZEN_MEDIACONTENT_IMAGE_ORIENTATION            "orientation"
34
35 using namespace TizenApis::Tizen1_0::Tizen;
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 namespace Mediacontent {
40
41
42 JSClassDefinition JSImage::m_classInfo =
43 {
44         0,
45         kJSClassAttributeNone,
46         TIZEN_MEDIACONTENT_IMAGE_ATTRIBUTENAME,
47         JSMedia::getClassRef(),
48         m_property,
49         NULL, //    m_function,
50         initialize,
51         finalize,
52         NULL, //hasProperty,
53         NULL, //getProperty,
54         NULL, //setProperty,
55         NULL, //DeleteProperty,
56         NULL, //GetPropertyNames,
57         NULL, //CallAsFunction,
58         NULL, //CallAsConstructor,
59         NULL, //HasInstance,
60         NULL  //ConvertToType
61 };
62
63 JSStaticValue JSImage::m_property[] =
64 {
65 //    { TIZEN_MEDIACONTENT_IMAGE_UID, getPropertyUid, NULL, kJSPropertyAttributeReadOnly},
66     { TIZEN_MEDIACONTENT_IMAGE_GEOLOCATION, getPropertyGeoLocation, NULL, kJSPropertyAttributeReadOnly},
67     { TIZEN_MEDIACONTENT_IMAGE_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
68     { TIZEN_MEDIACONTENT_IMAGE_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},    
69     { TIZEN_MEDIACONTENT_IMAGE_ORIENTATION, getPropertyOrientation, NULL, kJSPropertyAttributeReadOnly},  
70     { 0, 0, 0, 0 }
71 };
72
73
74 JSClassRef JSImage::m_jsClassRef = JSClassCreate(JSImage::getClassInfo());
75
76 void JSImage::initialize(JSContextRef context, JSObjectRef object)
77 {
78     LogDebug( "entered" );
79     ImagePrivObject *priv =
80             static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) );
81     if (!priv) {
82         MediacontentImagePtr privateData(new MediacontentImage());
83         priv = new ImagePrivObject(context, privateData);
84         JSObjectSetPrivate(object, static_cast<void*>(priv));
85         LogDebug("new event is created" );
86     }
87     else {
88         LogDebug("private object already exists");
89     }
90 }
91
92 void JSImage::finalize(JSObjectRef object)
93 {
94     LogDebug( "entered" );
95         ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) ) ;
96         if (priv != NULL)
97         {
98                 delete (priv);
99                 priv = NULL;
100                 JSObjectSetPrivate(object, NULL);               
101         }
102 }
103
104 const JSClassRef JSImage::getClassRef()
105 {
106         LogDebug("JSImage::getClassRef()");
107
108     if (!m_jsClassRef) {
109             m_jsClassRef = JSClassCreate(&m_classInfo);
110     }
111     return m_jsClassRef;
112 }
113
114 const JSClassDefinition* JSImage::getClassInfo()
115 {
116     return &m_classInfo;
117 }
118
119 MediacontentImagePtr JSImage::getImageObject(JSObjectRef object)
120 {
121     LogDebug("entered");
122     ImagePrivObject *priv = static_cast<ImagePrivObject*>(JSObjectGetPrivate(object));
123     if(!priv) {
124         ThrowMsg(NullPointerException, "Private object is null");
125     }
126     MediacontentImagePtr result = priv->getObject();
127     if (!result) {
128         ThrowMsg(NullPointerException, "Private object is null");
129     }
130     return result;
131 }
132                                         
133 JSValueRef JSImage::getPropertyGeoLocation(
134                 JSContextRef context,
135         JSObjectRef object,
136         JSStringRef propertyName,
137         JSValueRef* exception)
138 {
139     LogDebug("entered");
140     Try
141     {
142         Converter converter(context);
143         MediacontentImagePtr event = getImageObject(object);
144
145                 LogDebug("lati :"  << event->getImageLatitude());
146                 LogDebug("longi :"  << event->getImageLongitude());             
147                 SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(event->getImageLatitude(),event->getImageLongitude()));
148                 return JSSimpleCoordinates::createJSObject(context,geoPtr);
149                 //return converter.toJSValueRef(event->getImageWidth());
150
151     }
152     Catch(Exception)
153     {
154         LogWarning("trying to get incorrect value");
155     }
156     return JSValueMakeUndefined(context);
157 }
158
159
160 JSValueRef JSImage::getPropertyWidth(
161                 JSContextRef context,
162         JSObjectRef object, 
163         JSStringRef propertyName, 
164         JSValueRef* exception)
165 {
166     LogDebug("entered");
167     Try
168     {
169         Converter converter(context);
170         MediacontentImagePtr event = getImageObject(object);
171         return converter.toJSValueRef(event->getImageWidth());
172     }
173     Catch(Exception)
174     {
175         LogWarning("trying to get incorrect value");
176     }
177     return JSValueMakeUndefined(context);
178 }
179
180
181 JSValueRef JSImage::getPropertyHeight(
182                 JSContextRef context,
183         JSObjectRef object,
184         JSStringRef propertyName,
185         JSValueRef* exception)
186 {
187     LogDebug("entered");
188     Try
189     {
190         Converter converter(context);
191         MediacontentImagePtr event = getImageObject(object);
192         return converter.toJSValueRef(event->getImageHeight());
193     }
194     Catch(Exception)
195     {
196         LogWarning("trying to get incorrect value");
197     }
198     return JSValueMakeUndefined(context);
199 }
200
201 JSValueRef JSImage::getPropertyOrientation(
202                 JSContextRef context,
203         JSObjectRef object,
204         JSStringRef propertyName,
205         JSValueRef* exception)
206 {
207     LogDebug("entered");
208     Try
209     {
210         Converter converter(context);
211         MediacontentImagePtr event = getImageObject(object);
212         return converter.toJSValueRef(event->getImageOrientation());
213     }
214     Catch(Exception)
215     {
216         LogWarning("trying to get incorrect value");
217     }
218     return JSValueMakeUndefined(context);
219 }
220
221
222
223 }
224 }
225 }