summaryrefslogtreecommitdiff
path: root/mobile_src/Content/JSImage.cpp
blob: 064467266d746a1e3169b1ee582ddb40871569cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// Tizen Web Device API
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <CommonsJavaScript/PrivateObject.h>
#include <CommonsJavaScript/Converter.h>
#include <CommonsJavaScript/JSUtils.h>
#include <CommonsJavaScript/JSDOMExceptionFactory.h>
#include <JSSimpleCoordinates.h>

#include <JSWebAPIErrorFactory.h>
#include <SecurityExceptions.h>

#include "JSUtil.h"
#include "JSImage.h"
#include <Logger.h>

#define TIZEN_CONTENT_IMAGE_ATTRIBUTENAME 		"ImageContent"
#define TIZEN_CONTENT_IMAGE_GEOLOCATION			"geolocation"
#define TIZEN_CONTENT_IMAGE_WIDTH			"width"
#define TIZEN_CONTENT_IMAGE_HEIGHT			"height"
#define TIZEN_CONTENT_IMAGE_ORIENTATION			"orientation"

using namespace DeviceAPI::Tizen;
using namespace DeviceAPI::Common;
using namespace WrtDeviceApis::Commons;
using namespace DeviceAPI::Tizen;

namespace DeviceAPI {
namespace Content {


JSClassDefinition JSImage::m_classInfo =
{
	0,
	kJSClassAttributeNone,
	TIZEN_CONTENT_IMAGE_ATTRIBUTENAME,
	JSMedia::getClassRef(),
	m_property,
	NULL, //    m_function,
	initialize,
	finalize,
	NULL, //hasProperty,
	NULL, //getProperty,
	setProperty, //setProperty,
	NULL, //DeleteProperty,
	NULL, //GetPropertyNames,
	NULL, //CallAsFunction,
	NULL, //CallAsConstructor,
	NULL, //HasInstance,
	NULL  //ConvertToType
};

JSStaticValue JSImage::m_property[] =
{
    { TIZEN_CONTENT_IMAGE_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
    { TIZEN_CONTENT_IMAGE_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},
    { TIZEN_CONTENT_IMAGE_ORIENTATION, getPropertyOrientation, setPropertyOrientation, kJSPropertyAttributeNone},
    { 0, 0, 0, 0 }
};


JSClassRef JSImage::m_jsClassRef = JSClassCreate(JSImage::getClassInfo());

void JSImage::initialize(JSContextRef context, JSObjectRef object)
{

    ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) );

    if (!priv) {
        MediacontentImagePtr privateData(new MediacontentImage());
        priv = new ImagePrivObject(context, privateData);
        JSObjectSetPrivate(object, static_cast<void*>(priv));
    }
    else {
        MediacontentImagePtr image = getImageObject(object);
        if(image->getImageLatitude() != DEFAULT_GEOLOCATION  && 
            image->getImageLongitude() != DEFAULT_GEOLOCATION){
            DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(image->getImageLatitude(),image->getImageLongitude()));
            JSUtil::setProperty(context, object, TIZEN_CONTENT_IMAGE_GEOLOCATION,
                    JSSimpleCoordinates::createJSObject(context,geoPtr),
                    kJSPropertyAttributeNone);
        }
        else
        {
            JSUtil::setProperty(context, object, TIZEN_CONTENT_IMAGE_GEOLOCATION,
      			JSValueMakeNull(context),kJSPropertyAttributeNone);
        }
    }
}

void JSImage::finalize(JSObjectRef object)
{
    ImagePrivObject *priv = static_cast<ImagePrivObject*>( JSObjectGetPrivate( object ) ) ;
    if (priv != NULL)
    {
        delete (priv);
        priv = NULL;
        JSObjectSetPrivate(object, NULL);
    }
}

const JSClassRef JSImage::getClassRef()
{
    if (!m_jsClassRef) {
        m_jsClassRef = JSClassCreate(&m_classInfo);
    }
    return m_jsClassRef;
}

const JSClassDefinition* JSImage::getClassInfo()
{
    return &m_classInfo;
}

MediacontentImagePtr JSImage::getImageObject(JSObjectRef object)
{
    ImagePrivObject *priv = static_cast<ImagePrivObject*>(JSObjectGetPrivate(object));
    if(!priv) {
        ThrowMsg(NullPointerException, "Private object is null");
    }
    MediacontentImagePtr result = priv->getObject();
    if (!result) {
        ThrowMsg(NullPointerException, "Private object is null");
    }
    return result;
}

JSValueRef JSImage::getPropertyWidth(
		JSContextRef context,
        JSObjectRef object,
        JSStringRef propertyName,
        JSValueRef* exception)
{
    Try
    {
        Converter converter(context);
        MediacontentImagePtr image = getImageObject(object);
        return converter.toJSValueRef(image->getImageWidth());
    }
    Catch(Exception)
    {
        LoggerW("trying to get incorrect value");
    }
    return JSValueMakeUndefined(context);
}


JSValueRef JSImage::getPropertyHeight(
		JSContextRef context,
        JSObjectRef object,
        JSStringRef propertyName,
        JSValueRef* exception)
{
    Try
    {
        Converter converter(context);
        MediacontentImagePtr image = getImageObject(object);
        return converter.toJSValueRef(image->getImageHeight());
    }
    Catch(Exception)
    {
        LoggerW("trying to get incorrect value");
    }
    return JSValueMakeUndefined(context);
}

JSValueRef JSImage::getPropertyOrientation(
		JSContextRef context,
        JSObjectRef object,
        JSStringRef propertyName,
        JSValueRef* exception)
{
    Try
    {
        Converter converter(context);
        MediacontentImagePtr image = getImageObject(object);
        if(!(image->getImageOrientation().empty())){
            return converter.toJSValueRef(image->getImageOrientation());
        }
    }
    Catch(Exception)
    {
        LoggerW("trying to get incorrect value");
    }
    return JSValueMakeNull(context);
}

bool 	JSImage::setPropertyOrientation(
				JSContextRef context,
				JSObjectRef object,
				JSStringRef propertyName,
				JSValueRef value,
				JSValueRef* exception)
{
    try
    {
        Converter converter(context);
        MediacontentImagePtr objImg = getImageObject(object);

        string orientation = converter.toString(value);
        if (orientation != "NORMAL" && orientation != "FLIP_HORIZONTAL" && 
            orientation != "ROTATE_180" && orientation != "FLIP_VERTICAL" &&
            orientation != "TRANSPOSE" && orientation != "ROTATE_90" &&
            orientation != "TRANSVERSE" && orientation != "ROTATE_270"){
            throw TypeMismatchException("Invalid orientation type");
        }

        if ((objImg->getImageOrientation()).compare(orientation) !=0 )
        {
            objImg->setImageOrientation(orientation, true);
        }

        return true;			
    }
    catch(...)
    {
        LoggerW("trying to get incorrect value");
    }

    return false;
}


bool JSImage::setProperty(JSContextRef context, JSObjectRef object,
        JSStringRef propertyName, JSValueRef value,  JSValueRef* exception)
{
    try {
        // check geolocation
        if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CONTENT_IMAGE_GEOLOCATION)) {
            if(JSValueIsUndefined(context, value)){
                throw TypeMismatchException("Invalid geolocation");
            }
        }
    } catch (const BasePlatformException &err) {
       LOGE("setProperty error< %s : %s> " , err.getName().c_str(), err.getMessage().c_str());
       return true;
    }
    return false;
}


}
}