tizen 2.3.1 release
[framework/web/mobile/wrt.git] / src / view / webkit / view_logic_authentication_challenge_support.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  * @file    view_logic_authentication_challenge_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  */
20
21 #include "view_logic_authentication_challenge_support.h"
22
23 #include <string>
24
25 #include <dpl/assert.h>
26 #include <dpl/availability.h>
27 #include <dpl/log/wrt_log.h>
28 #include <efl_assist.h>
29 #include <Elementary.h>
30 #include <EWebKit.h>
31 #include <EWebKit_internal.h>
32
33 #include <widget_string.h>
34
35 namespace ViewModule {
36 namespace {
37 struct authenticationData {
38     Ewk_Auth_Challenge* m_authChallenge;
39     std::string m_bodyText;
40     Evas_Object* m_navi;
41     Evas_Object* m_idEdit;
42     Evas_Object* m_pwEdit;
43 };
44
45 // function declare
46 void askUserInformation(authenticationData* authData);
47 void loginAuthentication(authenticationData* authData);
48 void cancelAuthentication(authenticationData* authData);
49 Evas_Object* getEvasObjectByWidgetName(Evas_Object* obj, const char* name);
50 Evas_Object* createEdit(Evas_Object* parent, bool isIdEdit);
51 void eaKeyCallback(void* data, Evas_Object* obj, void* eventInfo);
52 void buttonClickedCallback(void* data, Evas_Object* obj, void* eventInfo);
53 void editActivatedCallback(void* data, Evas_Object* obj, void* eventInfo);
54
55 void askUserInformation(authenticationData* authData)
56 {
57     Evas_Object* popup = elm_popup_add(authData->m_navi);
58
59     elm_object_style_set(popup, "popup/default");
60     evas_object_size_hint_weight_set(popup , EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
61     evas_object_size_hint_align_set(popup , EVAS_HINT_FILL, EVAS_HINT_FILL);
62     elm_object_domain_translatable_part_text_set(popup, "title,text", "wrt", WRT_BODY_AUTHENTICATION_REQUIRED_IDS);
63
64     ea_object_event_callback_add(popup, EA_CALLBACK_BACK, eaKeyCallback, authData);
65
66     Evas_Object* layout = elm_layout_add(popup);
67     elm_layout_file_set(layout, WRT_EDJ_PATH, "authChallengePopup");
68     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
69     evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
70
71     Evas_Object* label = elm_label_add(layout);
72     elm_label_line_wrap_set(label , ELM_WRAP_WORD);
73     elm_object_text_set(label, authData->m_bodyText.c_str());
74     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
75     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
76
77     Evas_Object* idEdit = createEdit(layout, true);
78     authData->m_idEdit = idEdit;
79
80     Evas_Object* pwEdit = createEdit(layout, false);
81     authData->m_pwEdit = pwEdit;
82
83     elm_object_part_content_set(layout, "elm.swallow.label", label);
84     elm_object_part_content_set(layout, "elm.swallow.idfield", idEdit);
85     elm_object_part_content_set(layout, "elm.swallow.pwfield", pwEdit);
86     edje_object_part_text_set(elm_layout_edje_get(layout), "elm.swallow.idtext", WRT_BODY_AUTHUSERNAME);
87     edje_object_part_text_set(elm_layout_edje_get(layout), "elm.swallow.pwtext", WRT_BODY_PASSWORD);
88     elm_object_content_set(popup, layout);
89
90     Evas_Object* cancelButton = elm_button_add(popup);
91     elm_object_domain_translatable_part_text_set(cancelButton, 0, "wrt", WRT_SK_CANCEL_IDS);
92     elm_object_style_set(cancelButton, "popup");
93     elm_object_part_content_set(popup, "button1", cancelButton);
94     evas_object_smart_callback_add(cancelButton, "clicked", buttonClickedCallback, static_cast<void*>(authData));
95
96     Evas_Object* loginButton = elm_button_add(popup);
97     elm_object_domain_translatable_part_text_set(loginButton, 0, "sys_string", WRT_SK_LOGIN_IDS);
98     elm_object_style_set(loginButton, "popup");
99     elm_object_part_content_set(popup, "button2", loginButton);
100     evas_object_smart_callback_add(loginButton, "clicked", buttonClickedCallback, static_cast<void*>(authData));
101
102     evas_object_show(popup);
103 }
104
105 void loginAuthentication(authenticationData* authData)
106 {
107     WrtLogD("called");
108
109     Assert(authData);
110
111     const char* id = elm_entry_entry_get(authData->m_idEdit);
112     const char* pw = elm_entry_entry_get(authData->m_pwEdit);
113     ewk_auth_challenge_credential_use(authData->m_authChallenge, const_cast<char*>(id), const_cast<char*>(pw));
114
115     Evas_Object* popup = getEvasObjectByWidgetName(authData->m_idEdit, "elm_popup");
116     if (popup) {
117         evas_object_hide(popup);
118         evas_object_del(popup);
119     }
120 }
121
122 void cancelAuthentication(authenticationData* authData)
123 {
124     WrtLogD("called");
125
126     Assert(authData);
127
128     ewk_auth_challenge_credential_cancel(authData->m_authChallenge);
129
130     Evas_Object* popup = getEvasObjectByWidgetName(authData->m_idEdit, "elm_popup");
131     if (popup) {
132         evas_object_hide(popup);
133         evas_object_del(popup);
134     }
135 }
136
137 Evas_Object* getEvasObjectByWidgetName(Evas_Object* obj, const char* name)
138 {
139     Assert(obj);
140     Evas_Object* current = elm_object_parent_widget_get(obj);
141     while (strcmp(elm_object_widget_type_get(current), name)) {
142         current = elm_object_parent_widget_get(current);
143         if (!current) {
144             return NULL;
145         }
146     }
147     return current;
148 }
149
150 Evas_Object* createEdit(Evas_Object* parent, bool isIdEdit)
151 {
152     Evas_Object* edit = ea_editfield_add(parent, EA_EDITFIELD_SCROLL_SINGLELINE);
153     elm_entry_cnp_mode_set(edit, ELM_CNP_MODE_PLAINTEXT);
154     elm_object_style_set(edit, "editfield/password/popup");
155     elm_entry_single_line_set(edit, EINA_TRUE);
156     elm_entry_scrollable_set(edit, EINA_TRUE);
157     elm_entry_prediction_allow_set(edit, EINA_FALSE);
158     elm_object_signal_emit(edit, "elm,action,hide,search_icon", "");
159     elm_entry_autocapital_type_set(edit, ELM_AUTOCAPITAL_TYPE_NONE);
160
161     if (isIdEdit) {
162         evas_object_smart_callback_add(edit, "activated", editActivatedCallback, NULL);
163     } else {
164         elm_entry_password_set(edit, EINA_TRUE);
165         elm_entry_input_panel_layout_set(edit, ELM_INPUT_PANEL_LAYOUT_PASSWORD);
166     }
167
168     return edit;
169 }
170
171 void eaKeyCallback(void* data, Evas_Object* obj, void* eventInfo)
172 {
173     WrtLogD("called");
174
175     DPL_UNUSED_PARAM(obj);
176     DPL_UNUSED_PARAM(eventInfo);
177
178     Assert(data);
179
180     authenticationData* authData = static_cast<authenticationData*>(data);
181     cancelAuthentication(authData);
182 }
183
184 void buttonClickedCallback(void* data, Evas_Object* obj, void* eventInfo)
185 {
186     WrtLogD("called");
187
188     DPL_UNUSED_PARAM(eventInfo);
189
190     Assert(data);
191     Assert(obj);
192
193     bool isLogin = !strcmp(WRT_SK_LOGIN, elm_object_text_get(obj));
194
195     authenticationData* authData = static_cast<authenticationData*>(data);
196     if (isLogin) {
197         loginAuthentication(authData);
198     } else {
199         cancelAuthentication(authData);
200     }
201 }
202
203 void editActivatedCallback(void* data, Evas_Object* obj, void* eventInfo)
204 {
205     DPL_UNUSED_PARAM(eventInfo);
206     DPL_UNUSED_PARAM(data);
207
208     Assert(obj);
209     elm_object_focus_set(obj, EINA_TRUE);
210 }
211 } // namespace
212
213 void AuthenticationChallengeSupport::authenticationChallengeRequest(
214     Evas_Object* webview,
215     std::string url,
216     void* data)
217 {
218     WrtLogD("called");
219     authenticationData* authData = new authenticationData();
220     Assert(webview);
221     authData->m_navi = getEvasObjectByWidgetName(webview, "elm_naviframe");
222
223     Assert(data);
224     authData->m_authChallenge = static_cast<Ewk_Auth_Challenge*>(data);
225
226     const char* authRealm =
227         ewk_auth_challenge_realm_get(authData->m_authChallenge);
228     authData->m_bodyText = url;
229     if (authRealm != NULL) {
230         authData->m_bodyText += "<br>";
231         authData->m_bodyText += std::string(authRealm);
232     }
233     ewk_auth_challenge_suspend(authData->m_authChallenge);
234
235     // ask to user
236     askUserInformation(authData);
237 }
238 } // namespace ViewModule