Tizen 2.1 base
[platform/framework/web/wrt-plugins-common.git] / src / js-overlay / JSClass / JSSoftKeyboardChangeEvent.cpp
1 /*
2  * Copyright (c) 2012 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  * @file        JSSoftKeyboardChangeEvent.cpp
19  * @author      Yunchan Cho (yunchan.cho@samsung.com)
20  * @version     0.1
21  */
22
23 #include "JSSoftKeyboardChangeEvent.h"
24 #include <dpl/log/log.h>
25 #include <dpl/assert.h>
26 #include <SoftKeyboardChangeEvent/ISoftKeyboardChangeEvent.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
29
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace WrtDeviceApis::SoftKeyboardChangeEvent::Api;
34
35 #define WIDGET_PLUGIN_NAME              "SoftKeyboardChangeEvent"
36 #define SOFTKEYBOARD_CHANGE_EVENT_PROPERTY_STATE   "state"
37 #define SOFTKEYBOARD_CHANGE_EVENT_PROPERTY_WIDTH   "width"
38 #define SOFTKEYBOARD_CHANGE_EVENT_PROPERTY_HEIGHT  "height"
39
40 #define CATCH_EXCEPTION_CONVERSION \
41     Catch(Commons::ConversionException) {\
42         LogError("Error on conversion");\
43         return JSDOMExceptionFactory::\
44             UnknownException.make(context, exception);\
45     }
46
47 #define CATCH_EXCEPTION_NULL_PTR \
48     Catch(Commons::NullPointerException) {\
49         LogError("Error on pointer, null value");\
50         return JSDOMExceptionFactory::\
51             UnknownException.make(context, exception);\
52     }
53
54 #define CATCH_EXCEPTION_PLATFORM_ERROR \
55     Catch(Commons::PlatformException){\
56         LogError("PlatformException occured");\
57         return JSDOMExceptionFactory::\
58             UnknownException.make(context, exception);\
59     }
60
61 namespace WrtPlugins {
62 namespace Tizen {
63 JSClassDefinition JSSoftKeyboardChangeEvent::m_classInfo = {
64     0,
65     kJSClassAttributeNone,
66     WIDGET_PLUGIN_NAME,
67     0,
68     m_property,
69     NULL,
70     initialize,
71     finalize,
72     NULL, //HasProperty,
73     NULL, //GetProperty,
74     NULL, //SetProperty,
75     NULL, //DeleteProperty,
76     NULL, //GetPropertyNames,
77     NULL, //CallAsFunction,
78     NULL, //CallAsConstructor,
79     NULL, //HasInstance,
80     NULL, //ConvertToType,
81 };
82
83 JSStaticValue JSSoftKeyboardChangeEvent::m_property[] = {
84     { SOFTKEYBOARD_CHANGE_EVENT_PROPERTY_STATE, JSSoftKeyboardChangeEvent::getState,
85       0, kJSPropertyAttributeReadOnly },
86     { SOFTKEYBOARD_CHANGE_EVENT_PROPERTY_WIDTH, JSSoftKeyboardChangeEvent::getWidth,
87       0, kJSPropertyAttributeReadOnly },
88     { SOFTKEYBOARD_CHANGE_EVENT_PROPERTY_HEIGHT, JSSoftKeyboardChangeEvent::getHeight,
89       0, kJSPropertyAttributeReadOnly },
90     { 0, 0, 0, 0 }
91 };
92
93 const JSClassRef JSSoftKeyboardChangeEvent::getClassRef()
94 {
95     if (!m_jsClassRef) {
96         m_jsClassRef = JSClassCreate(&m_classInfo);
97     }
98     return m_jsClassRef;
99 }
100
101 const JSClassDefinition* JSSoftKeyboardChangeEvent::getClassInfo()
102 {
103     return &m_classInfo;
104 }
105
106 JSClassRef JSSoftKeyboardChangeEvent::m_jsClassRef = JSClassCreate(JSSoftKeyboardChangeEvent::getClassInfo());
107
108 void JSSoftKeyboardChangeEvent::initialize(JSContextRef context, JSObjectRef object)
109 {
110     LogDebug("entered");
111
112     JSSoftKeyboardChangeEventPrivateObject* priv =
113         static_cast<JSSoftKeyboardChangeEventPrivateObject*>(JSObjectGetPrivate(object));
114
115     Assert(priv && "Missing private object");
116 }
117
118 void JSSoftKeyboardChangeEvent::finalize(JSObjectRef object)
119 {
120     LogDebug("entered");
121     JSSoftKeyboardChangeEventPrivateObject* priv =
122         static_cast<JSSoftKeyboardChangeEventPrivateObject*>(JSObjectGetPrivate(object));
123
124     delete priv;
125     LogDebug("private object is realised");
126
127 }
128
129 JSValueRef JSSoftKeyboardChangeEvent::getState(
130         JSContextRef context,
131         JSObjectRef object,
132         JSStringRef propertyName,
133         JSValueRef* exception)
134 {
135     LogDebug("entered!");
136
137     Try
138     {
139         Converter converter(context);
140         return converter.toJSValueRef(getPrivateObject(object)->getState());
141     }
142     CATCH_EXCEPTION_CONVERSION
143     CATCH_EXCEPTION_NULL_PTR
144     CATCH_EXCEPTION_PLATFORM_ERROR
145 }
146
147 JSValueRef JSSoftKeyboardChangeEvent::getWidth(
148         JSContextRef context,
149         JSObjectRef object,
150         JSStringRef propertyName,
151         JSValueRef* exception)
152 {
153     LogDebug("entered!");
154
155     Try
156     {
157         Converter converter(context);
158         return converter.toJSValueRef(getPrivateObject(object)->getWidth());
159     }
160     CATCH_EXCEPTION_CONVERSION
161     CATCH_EXCEPTION_NULL_PTR
162     CATCH_EXCEPTION_PLATFORM_ERROR
163 }
164
165 JSValueRef JSSoftKeyboardChangeEvent::getHeight(
166         JSContextRef context,
167         JSObjectRef object,
168         JSStringRef propertyName,
169         JSValueRef* exception)
170 {
171     LogDebug("entered");
172
173     Try
174     {
175         Converter converter(context);
176         return converter.toJSValueRef(getPrivateObject(object)->getHeight());
177     }
178     CATCH_EXCEPTION_CONVERSION
179     CATCH_EXCEPTION_NULL_PTR
180     CATCH_EXCEPTION_PLATFORM_ERROR
181 }
182
183 ISoftKeyboardChangeEventPtr JSSoftKeyboardChangeEvent::getPrivateObject(JSObjectRef arg)
184 {
185     JSSoftKeyboardChangeEventPrivateObject* priv =
186         static_cast<JSSoftKeyboardChangeEventPrivateObject*>(JSObjectGetPrivate(arg));
187
188     if (!priv) {
189         LogError("Private object not initialized");
190         ThrowMsg(Commons::NullPointerException,
191                  "Private object not initialized");
192     }
193
194     return priv->getObject();
195 }
196
197 #undef CATCH_EXCEPTION_CONVERSION
198 #undef CATCH_EXCEPTION_NULL_PTR
199 #undef CATCH_EXCEPTION_PLATFORM_ERROR
200
201 }
202 }