Fix WPS popup label length
[apps/native/ug-wifi-efl.git] / sources / libraries / Common / common_pswd_popup.c
1 /*
2  * Wi-Fi
3  *
4  * Copyright 2012-2013 Samsung Electronics Co., Ltd
5  *
6  * Licensed under the Flora License, Version 1.1 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://floralicense.org/license
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "common.h"
21 #include "common_pswd_popup.h"
22 #include "i18nmanager.h"
23 #include "common_utils.h"
24 #include "ug_wifi.h"
25
26 #define PBC_TIMEOUT_MSG_STR     "one click connection failed"
27 #define MAX_PBC_TIMEOUT_SECS    120     // Time in seconds
28
29 static void __popup_entry_changed_cb(void* data, Evas_Object* obj, void* event_info)
30 {
31         if (elm_object_focus_get(data)) {
32                 if (elm_entry_is_empty(obj))
33                         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
34                 else
35                         elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
36         }
37 }
38
39 static void __popup_entry_focused_cb(void *data, Evas_Object *obj, void *event_info)
40 {
41         if (!elm_entry_is_empty(obj))
42                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
43         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
44 }
45
46 static void __popup_entry_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
47 {
48         if (elm_entry_is_empty(obj))
49                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
50         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
51 }
52
53 static void __popup_eraser_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
54 {
55         elm_entry_entry_set(data, "");
56 }
57
58 static void _check_changed_cb(void *data, Evas_Object *obj, void *event_info)
59 {
60         if (obj == NULL)
61                 return;
62
63         Eina_Bool state = elm_check_state_get(obj);
64         common_utils_entry_password_set(data, !state);
65 }
66
67 static void __common_pbc_popup_destroy(pbc_popup_t *pbc_popup_data)
68 {
69         __COMMON_FUNC_ENTER__;
70         if (!pbc_popup_data)
71                 return;
72
73         if (pbc_popup_data->checker == 0) {
74                 pbc_popup_data->checker = 1;
75
76                 if (pbc_popup_data->timer != NULL) {
77                         ecore_timer_del(pbc_popup_data->timer);
78                         pbc_popup_data->timer = NULL;
79                 }
80                 if (pbc_popup_data->popup != NULL) {
81                         evas_object_hide(pbc_popup_data->popup);
82                         evas_object_del(pbc_popup_data->popup);
83                         pbc_popup_data->popup = NULL;
84                 }
85                 g_free(pbc_popup_data);
86         }
87         __COMMON_FUNC_EXIT__;
88         return;
89 }
90
91 static Eina_Bool _fn_pb_timer_bar(void *data)
92 {
93         pswd_popup_t *pswd_popup_data = (pswd_popup_t *)data;
94         pbc_popup_t *pbc_popup_data = pswd_popup_data->pbc_popup_data;
95         if (!pbc_popup_data || pbc_popup_data->timer == NULL || pbc_popup_data->progressbar == NULL) {
96                 return ECORE_CALLBACK_CANCEL;
97         }
98         const double diff = (double)1/(double)MAX_PBC_TIMEOUT_SECS;
99         pbc_popup_data->value = elm_progressbar_value_get(pbc_popup_data->progressbar);
100         pbc_popup_data->value += diff;
101         if (pbc_popup_data->value >= 1) {
102                 if (pbc_popup_data->checker == 0) {
103                         __COMMON_FUNC_ENTER__;
104                         common_utils_show_info_timeout_popup(pswd_popup_data->win, PBC_TIMEOUT_MSG_STR, 3.0f);
105                         Evas_Object *cancel_btn = elm_object_part_content_get(pbc_popup_data->popup, "button1");
106                         evas_object_smart_callback_call(cancel_btn, "clicked", NULL);
107                         __COMMON_FUNC_EXIT__;
108                 }
109                 return ECORE_CALLBACK_CANCEL;
110         }
111
112         int remain_mins = (int)(MAX_PBC_TIMEOUT_SECS * (1 - pbc_popup_data->value));
113         int remain_secs = 0;
114         remain_secs = remain_mins % 60;
115         remain_mins /= 60;
116
117         char *remaining_time_str = g_strdup_printf("<font_size=40><align=center>%02d:%02d</align></font_size>", remain_mins, remain_secs);
118         elm_object_text_set(pbc_popup_data->timer_label, remaining_time_str);
119         //INFO_LOG(UG_NAME_NORMAL, "pbc_popup_data->value = %lf; remain_mins = %d; remain_secs = %d; remaining_time_str = %s", pbc_popup_data->value, remain_mins, remain_secs, remaining_time_str);
120         g_free(remaining_time_str);
121
122         elm_progressbar_value_set(pbc_popup_data->progressbar, pbc_popup_data->value);
123
124         return ECORE_CALLBACK_RENEW;
125 }
126
127 static Eina_Bool _enable_scan_updates_cb(void *data)
128 {
129         /* Lets enable the scan updates */
130         wlan_manager_enable_scan_result_update();
131
132         return ECORE_CALLBACK_CANCEL;
133 }
134
135 void create_pbc_popup(pswd_popup_t *pswd_popup_data, Evas_Smart_Cb cancel_cb, void *cancel_cb_data)
136 {
137         if (!pswd_popup_data) {
138                 return;
139         }
140         Evas_Object *popup = NULL,*progressbar = NULL;
141         Evas_Object *label = NULL, *timer_label = NULL;
142         Evas_Object *separator1 = NULL, *separator2 = NULL;
143
144         pbc_popup_t *pbc_popup_data = NULL;
145         pbc_popup_data = g_new0(pbc_popup_t, 1);
146
147         popup_btn_info_t popup_btn_data;
148         memset(&popup_btn_data, 0, sizeof(popup_btn_data));
149         popup_btn_data.btn1_txt = sc(pswd_popup_data->str_pkg_name, I18N_TYPE_Cancel);
150         popup_btn_data.btn1_cb = cancel_cb;
151         popup_btn_data.btn1_data = cancel_cb_data;
152         popup = common_utils_show_info_popup(pswd_popup_data->win, &popup_btn_data);
153
154         label = elm_label_add(popup);
155         elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
156         elm_object_text_set(label, sc(pswd_popup_data->str_pkg_name,I18N_TYPE_Press_WPS_On_Your_Wi_Fi_Access_Point));
157         evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
158         evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
159         evas_object_show(label);
160
161         separator1 = elm_separator_add(popup);
162
163         progressbar = elm_progressbar_add(popup);
164         elm_object_style_set(progressbar, "list_progress");
165         elm_progressbar_horizontal_set(progressbar, EINA_TRUE);
166         evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL, EVAS_HINT_FILL);
167         evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
168         elm_progressbar_value_set(progressbar, 0.0);
169
170         separator2 = elm_separator_add(popup);
171
172         timer_label = elm_label_add(popup);
173         elm_object_style_set(timer_label, "label3");
174         elm_label_line_wrap_set(timer_label, ELM_WRAP_MIXED);
175         elm_object_text_set(timer_label, _("<font_size=40><align=center>02:00</align></font_size>"));
176         evas_object_size_hint_weight_set(timer_label, EVAS_HINT_EXPAND, 0.0);
177         evas_object_size_hint_align_set(timer_label, EVAS_HINT_FILL, EVAS_HINT_FILL);
178         evas_object_show(timer_label);
179
180         pbc_popup_data->checker = 0;
181         pbc_popup_data->value = 0.0;
182         pbc_popup_data->progressbar = progressbar;
183         pbc_popup_data->timer_label = timer_label;
184         pbc_popup_data->popup = popup;
185         pbc_popup_data->timer = ecore_timer_add(1.0, _fn_pb_timer_bar, pswd_popup_data);
186         evas_object_show(progressbar);
187
188         Evas_Object *box = elm_box_add(popup);
189         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
190         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
191         evas_object_show(box);
192         elm_box_pack_end(box, label);
193         elm_box_pack_end(box, separator1);
194         elm_box_pack_end(box, progressbar);
195         elm_box_pack_end(box, separator2);
196         elm_box_pack_end(box, timer_label);
197
198         evas_object_show(box);
199         elm_object_content_set(popup, box);
200         pswd_popup_data->pbc_popup_data = pbc_popup_data;
201
202         /* Delete the password popup */
203         evas_object_hide(pswd_popup_data->popup);
204         evas_object_del(pswd_popup_data->popup);
205
206         return;
207 }
208
209 pswd_popup_t *create_passwd_popup(Evas_Object *win_main,
210                 const char *pkg_name, pswd_popup_create_req_data_t *popup_info)
211 {
212         __COMMON_FUNC_ENTER__;
213
214         if (!win_main || !popup_info || !pkg_name)
215                 return NULL;
216
217         pswd_popup_t *pswd_popup_data = g_new0(pswd_popup_t, 1);
218
219         if (popup_info->ap) {
220                 if (WIFI_ERROR_NONE !=
221                                 wifi_ap_clone(&(pswd_popup_data->ap), popup_info->ap)) {
222                         g_free(pswd_popup_data);
223
224                         return NULL;
225                 }
226         } else {
227                 /* It can be NULL in case of hidden AP */
228         }
229         /* Lets disable the scan updates so that the UI is not refreshed un necessarily */
230         wlan_manager_disable_scan_result_update();
231
232         popup_btn_info_t popup_btn_data;
233         memset(&popup_btn_data, 0, sizeof(popup_btn_data));
234
235         popup_btn_data.title_txt = popup_info->title;
236         popup_btn_data.btn2_cb = popup_info->ok_cb;
237         popup_btn_data.btn2_data = popup_info->cb_data;
238         popup_btn_data.btn1_cb = popup_info->cancel_cb;
239         popup_btn_data.btn1_data = popup_info->cb_data;
240         popup_btn_data.btn2_txt = sc(pkg_name, I18N_TYPE_Ok);
241         popup_btn_data.btn1_txt = sc(pkg_name, I18N_TYPE_Cancel);
242         Evas_Object *passpopup = common_utils_show_info_popup(win_main, &popup_btn_data);
243
244         Evas_Object *eo = elm_layout_edje_get(passpopup);
245         const Evas_Object *po = edje_object_part_object_get(eo, "elm.text.title");
246         Evas_Object *ao = elm_access_object_get(po);
247         elm_access_info_set(ao, ELM_ACCESS_INFO, popup_info->title);
248
249         Evas_Object *box = elm_box_add(passpopup);
250         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
251         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
252         evas_object_show(box);
253
254         Evas_Object *entry_ly = elm_layout_add(box);
255         Evas_Object *entry = NULL;
256         Elm_Entry_Filter_Limit_Size limit_filter_data;
257
258         elm_layout_file_set(entry_ly, CUSTOM_EDITFIELD_PATH, "custom_editfield");
259         evas_object_size_hint_weight_set(entry_ly, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
260         evas_object_size_hint_align_set(entry_ly, EVAS_HINT_FILL, EVAS_HINT_FILL);
261         elm_object_part_text_set(entry_ly, "elm.text", sc(pkg_name, I18N_TYPE_Enter_password));
262
263         entry = elm_entry_add(entry_ly);
264         elm_entry_scrollable_set(entry, EINA_TRUE);
265         elm_entry_single_line_set(entry, EINA_TRUE);
266         elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_PASSWORD);
267         elm_object_part_content_set(entry_ly, "elm.swallow.content", entry);
268
269         limit_filter_data.max_char_count = 32;
270         elm_entry_markup_filter_append(entry,
271                         elm_entry_filter_limit_size, &limit_filter_data);
272
273         evas_object_smart_callback_add(entry, "changed",
274                         __popup_entry_changed_cb, entry_ly);
275         evas_object_smart_callback_add(entry, "focused",
276                         __popup_entry_focused_cb, entry_ly);
277         evas_object_smart_callback_add(entry, "unfocused",
278                         __popup_entry_unfocused_cb, entry_ly);
279         elm_object_signal_callback_add(entry_ly, "elm,eraser,clicked",
280                         "elm", __popup_eraser_clicked_cb, entry);
281         evas_object_show(entry);
282
283         elm_entry_password_set(entry, EINA_TRUE);
284         evas_object_show(entry_ly);
285         elm_box_pack_end(box, entry_ly);
286
287         Evas_Object *check = elm_check_add(box);
288         elm_object_text_set(check, sc(pkg_name, I18N_TYPE_Show_password));
289         elm_object_focus_allow_set(check, EINA_FALSE);
290         evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
291         evas_object_size_hint_align_set(check, EVAS_HINT_FILL, EVAS_HINT_FILL);
292         evas_object_smart_callback_add(check, "changed", _check_changed_cb, entry_ly);
293
294         evas_object_show(check);
295         elm_box_pack_end(box, check);
296
297         if (popup_info->show_wps_btn) {
298                 Evas_Object *btn = elm_button_add(box);
299                 elm_object_text_set(btn, sc(pkg_name, I18N_TYPE_WPS_Button_Connection));
300                 evas_object_size_hint_weight_set(btn,
301                                 EVAS_HINT_EXPAND,
302                                 EVAS_HINT_EXPAND);
303                 evas_object_size_hint_align_set(btn,
304                                 EVAS_HINT_FILL,
305                                 EVAS_HINT_FILL);
306                 evas_object_smart_callback_add(btn, "clicked",
307                                 popup_info->wps_btn_cb, popup_info->cb_data);
308                 elm_box_pack_end(box, btn);
309                 evas_object_show(btn);
310         }
311
312         elm_object_content_set(passpopup, box);
313         evas_object_show(passpopup);
314         pswd_popup_data->win = win_main;
315         pswd_popup_data->str_pkg_name = pkg_name;
316         pswd_popup_data->popup = passpopup;
317         pswd_popup_data->popup_entry_lyt = entry_ly;
318         elm_object_focus_set(entry, EINA_TRUE);
319
320         __COMMON_FUNC_EXIT__;
321         return pswd_popup_data;
322 }
323
324 char *passwd_popup_get_txt(pswd_popup_t *pswd_popup_data)
325 {
326         if (pswd_popup_data)
327                 return common_utils_entry_layout_get_text(
328                                         pswd_popup_data->popup_entry_lyt);
329
330         return NULL;
331 }
332
333 wifi_ap_h passwd_popup_get_ap(pswd_popup_t *pswd_popup_data)
334 {
335         if (pswd_popup_data)
336                 return pswd_popup_data->ap;
337
338         return NULL;
339 }
340
341 void passwd_popup_free(pswd_popup_t *pswd_popup_data)
342 {
343         if (pswd_popup_data == NULL)
344                 return;
345
346         if (pswd_popup_data->pbc_popup_data) {
347                 __common_pbc_popup_destroy(pswd_popup_data->pbc_popup_data);
348                 pswd_popup_data->pbc_popup_data = NULL;
349         }
350
351         evas_object_hide(pswd_popup_data->popup);
352         evas_object_del(pswd_popup_data->popup);
353         pswd_popup_data->popup = NULL;
354         pswd_popup_data->popup_entry_lyt = NULL;
355
356         wifi_ap_destroy(pswd_popup_data->ap);
357
358         g_free(pswd_popup_data);
359
360         /* A delay is needed to get the smooth Input panel closing animation effect */
361         ecore_timer_add(0.1, _enable_scan_updates_cb, NULL);
362 }