Tizen directory path migration
[apps/native/ug-wifi-direct.git] / ug-wifidirect / src / wfd_ug_rename_popup.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 <Elementary.h>
21 #include <vconf.h>
22 #include <wifi-direct.h>
23 #include <efl_extension.h>
24
25 #include "wfd_ug.h"
26 #include "wfd_ug_view.h"
27 #include "wfd_client.h"
28
29 #define WSC_SPEC_DEVICE_NAME_MAX_LEN 32
30 static char special_char[30] = "'!@$%^&*()-_=+[];:,<.>/?";
31
32 static bool is_space_str(const char *str)
33 {
34         while (str) {
35                 if (*str != '\0' && *str != ' ') {
36                         return FALSE;
37                 } else if (*str == '\0') {
38                         return TRUE;
39                 }
40                 str++;
41         }
42         return TRUE;
43 }
44
45 /**
46  *      This function check the max length of the device name
47  *      @return   void
48  *      @param[in] data the pointer to the main data structure
49  */
50 static void _rename_popop_entry_changed_cb(void *data, Evas_Object *obj, void *event_info)
51 {
52         __FUNC_ENTER__;
53         struct ug_data *ugd = (struct ug_data *) data;
54         WFD_RET_IF(ugd == NULL || obj == NULL, "Incorrect parameter(NULL)\n");
55         WFD_RET_IF(ugd->rename_button == NULL, "ugd->rename_button(NULL)\n");
56
57         const char *entry_text  = NULL;
58         char *input_str                 = NULL;
59         bool is_space_string = FALSE;
60
61         entry_text = elm_entry_entry_get(obj);
62         input_str = elm_entry_markup_to_utf8(entry_text);
63
64         if (elm_object_part_content_get(obj, "elm.swallow.clear")) {
65                 if (elm_object_focus_get(obj)) {
66                         if (elm_entry_is_empty(obj))
67                                 elm_object_signal_emit(obj, "elm,state,clear,hidden", "");
68                         else
69                                 elm_object_signal_emit(obj, "elm,state,clear,visible", "");
70                 }
71         }
72
73         if (input_str == NULL || (strlen(input_str) == 0)) {
74                 elm_object_disabled_set(ugd->rename_button, TRUE);
75                 elm_entry_input_panel_return_key_disabled_set(ugd->rename_entry, TRUE);
76                 WFD_IF_FREE_MEM(input_str);
77                 return;
78         }
79
80         is_space_string = is_space_str(input_str);
81         if (is_space_string) {
82                 elm_object_disabled_set(ugd->rename_button, TRUE);
83                 elm_entry_input_panel_return_key_disabled_set(ugd->rename_entry, TRUE);
84                 WFD_IF_FREE_MEM(input_str);
85                 return;
86         }
87
88         if (strlen(input_str) > 0) {
89                 elm_object_disabled_set(ugd->rename_button, FALSE);
90                 elm_entry_input_panel_return_key_disabled_set(ugd->rename_entry, FALSE);
91         }
92
93         WFD_IF_FREE_MEM(input_str);
94         __FUNC_EXIT__;
95 }
96
97 static void _rename_popup_cancel_cb(void *data, Evas_Object *obj, void *event_info)
98 {
99         __FUNC_ENTER__;
100         struct ug_data *ugd = (struct ug_data *) data;
101
102         WFD_IF_DEL_OBJ(ugd->rename_popup);
103         WFD_IF_DEL_OBJ(ugd->rename_entry);
104
105         __FUNC_EXIT__;
106 }
107
108 int _check_device_name(const char *device_name)
109 {
110         __FUNC_ENTER__;
111
112         int i = 0;
113         int j = 0;
114         bool is_unavailable_char_found = true;
115
116         if ((device_name == NULL) || (strlen(device_name) == 0)) {
117                 return -1;
118         }
119
120         for (i = 0; i < strlen(device_name); i++) {
121                 if ((device_name[i] >= '0' && device_name[i] <= '9') ||
122                         (device_name[i] >= 'a' && device_name[i] <= 'z') ||
123                         (device_name[i] >= 'A' && device_name[i] <= 'Z') ||
124                         device_name[i] == ' ') {
125                         continue;
126                 }
127                 is_unavailable_char_found = true;
128                 for (j = 0; j < strlen(special_char); j++) {
129                         if (special_char[j] == device_name[i]) {
130                                 is_unavailable_char_found = false;
131                                 break;
132                         }
133                 }
134
135                 if (is_unavailable_char_found) {
136                         DBG(LOG_ERROR, "Unavailable char:[%c]\n", device_name[i]);
137                         return -1;
138                 }
139         }
140
141         __FUNC_EXIT__;
142         return 0;
143 }
144
145 static void _rename_popup_ok_cb(void *data, Evas_Object *obj, void *event_info)
146 {
147         __FUNC_ENTER__;
148         struct ug_data *ugd = (struct ug_data *) data;
149
150         const char *entry_str = elm_entry_entry_get(ugd->rename_entry);
151         char *device_name_str = NULL;
152         device_name_str = elm_entry_markup_to_utf8(entry_str);
153
154         DBG(LOG_INFO, "New device name:[%s]\n", device_name_str);
155         _check_device_name(device_name_str);
156
157         if (0 != vconf_set_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR, device_name_str)) {
158                 DBG(LOG_ERROR, "Set vconf[%s] failed\n",VCONFKEY_SETAPPL_DEVICE_NAME_STR);
159         }
160 #ifdef WFD_ON_OFF_GENLIST
161         wfd_ug_view_refresh_glitem(ugd->item_wifi_onoff);
162 #endif
163         wfd_ug_view_refresh_glitem(ugd->device_name_item);
164         WFD_IF_DEL_OBJ(ugd->rename_popup);
165         WFD_IF_DEL_OBJ(ugd->rename_entry);
166         WFD_IF_FREE_MEM(device_name_str);
167         __FUNC_EXIT__;
168 }
169
170 static void _eraser_btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
171 {
172         __FUNC_ENTER__;
173         struct ug_data *ugd = (struct ug_data *) data;
174         WFD_RET_IF(ugd == NULL, "Incorrect parameter(NULL)\n");
175         WFD_RET_IF(ugd->rename_entry == NULL, "ugd->rename_entry(NULL)\n");
176         elm_entry_entry_set(ugd->rename_entry, "");
177         elm_object_domain_translatable_part_text_set(ugd->rename_entry, "elm.guide",
178                          PACKAGE, "IDS_STU_HEADER_ENTER_DEVICE_NAME");
179
180         WFD_RET_IF(ugd->rename_button == NULL, "ugd->rename_button(NULL)\n");
181         elm_object_disabled_set(ugd->rename_button, TRUE);
182         elm_entry_input_panel_return_key_disabled_set(ugd->rename_entry, TRUE);
183         __FUNC_EXIT__;
184 }
185
186 static void __device_name_maxlength_reached_cb(void *data, Evas_Object *obj,
187                 void *event_info)
188 {
189         __FUNC_ENTER__;
190
191         if (data == NULL || obj == NULL) {
192                 DBG(LOG_ERROR, "The param is NULL\n");
193                 return;
194         }
195
196         int ret = notification_status_message_post(
197                         D_("IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED"));
198         if (ret != NOTIFICATION_ERROR_NONE) {
199                 DBG(LOG_ERROR, "notification_status_message_post() ERROR [%d]", ret);
200         }
201
202         __FUNC_EXIT__;
203 }
204
205 static void _entry_focused_cb(void *data, Evas_Object *obj, void *event_info)
206 {
207         if (elm_object_part_content_get(obj, "elm.swallow.clear")) {
208                 if (!elm_entry_is_empty(obj))
209                         elm_object_signal_emit(obj, "elm,state,clear,visible", "");
210                 else
211                         elm_object_signal_emit(obj, "elm,state,clear,hidden", "");
212         }
213         elm_object_signal_emit(obj, "elm,state,focus,on", "");
214 }
215
216 static void _rename_entry_keydown_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
217 {
218         __FUNC_ENTER__;
219
220         Evas_Event_Key_Down *ev;
221         Evas_Object *entry = obj;
222
223         WFD_RET_IF(data == NULL, "Incorrect parameter data(NULL)\n");
224         WFD_RET_IF(event_info == NULL, "Incorrect parameter event_info(NULL)\n");
225         WFD_RET_IF(entry == NULL, "Incorrect parameter entry(NULL)\n");
226
227         ev = (Evas_Event_Key_Down *)event_info;
228         DBG(LOG_INFO, "ENTER ev->key:%s", ev->key);
229
230         if (g_strcmp0(ev->key, "KP_Enter") == 0 || g_strcmp0(ev->key, "Return") == 0) {
231                 Ecore_IMF_Context *imf_context;
232
233                 imf_context = (Ecore_IMF_Context*)elm_entry_imf_context_get(entry);
234                 if (imf_context) {
235                         ecore_imf_context_input_panel_hide(imf_context);
236                 }
237
238                 elm_object_focus_set(entry, EINA_FALSE);
239         }
240 }
241
242 static char *__wfd_main_rename_desc_label_get(void *data, Evas_Object *obj,
243                                               const char *part)
244 {
245         __FUNC_ENTER__;
246
247         DBG(LOG_INFO,"Part = %s", part);
248         if (obj == NULL || part == NULL) {
249                 return NULL;
250         }
251
252         if (!strcmp("elm.text.multiline", part)) {
253                 char buf[WFD_GLOBALIZATION_STR_LENGTH] = {0, };
254                 snprintf(buf, WFD_GLOBALIZATION_STR_LENGTH, "<font_size=30>%s</font_size>",
255                         D_("IDS_ST_POP_DEVICE_NAMES_ARE_DISPLAYED_TO_DISTINGUISH_EACH_OF_THE_DEVICES_AVAILABLE_MSG"));
256                 return g_strdup(buf);
257         }
258         __FUNC_EXIT__;
259         return NULL;
260 }
261
262 static Evas_Object *__wfd_main_rename_entry_icon_get(
263                                 void *data, Evas_Object *obj, const char *part)
264 {
265         __FUNC_ENTER__;
266         struct ug_data *ugd = (struct ug_data *)data;
267         WFD_RETV_IF(ugd == NULL, NULL, "Incorrect parameter(NULL)\n");
268         Evas_Object *entry = NULL;
269         Evas_Object *layout = NULL;
270         Evas_Object *button = NULL;
271         Ecore_IMF_Context *imf_context;
272         char *name_value = NULL;
273
274         static Elm_Entry_Filter_Limit_Size limit_filter_data;
275
276         if (!strcmp(part, "elm.icon.entry")) {
277
278                 name_value = elm_entry_utf8_to_markup(ugd->dev_name);
279                 entry = elm_entry_add(obj);
280                 elm_entry_single_line_set(entry, EINA_TRUE);
281                 elm_object_style_set(entry, "editfield");
282                 elm_entry_scrollable_set(entry, EINA_TRUE);
283                 limit_filter_data.max_char_count = WSC_SPEC_DEVICE_NAME_MAX_LEN;
284                 elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
285
286                 evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
287                 evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
288                 eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
289                 elm_object_signal_emit(entry, "elm,action,hide,search_icon", "");
290                 elm_object_domain_translatable_part_text_set(entry, "elm.guide",
291                                  PACKAGE, "IDS_STU_HEADER_ENTER_DEVICE_NAME");
292
293                 imf_context = (Ecore_IMF_Context*)elm_entry_imf_context_get(entry);
294                 if (imf_context) {
295                         ecore_imf_context_input_panel_return_key_type_set(imf_context,
296                         ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
297                 }
298
299                 ugd->rename_entry = entry;
300                 elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
301                 elm_entry_entry_set(entry, name_value);
302                 WFD_IF_FREE_MEM(name_value);
303                 elm_entry_cursor_end_set(entry);
304
305                 evas_object_smart_callback_add(entry, "changed,user", _rename_popop_entry_changed_cb, ugd);
306                 evas_object_smart_callback_add(entry, "changed", _rename_popop_entry_changed_cb, ugd);
307                 evas_object_smart_callback_add(entry, "preedit,changed", _rename_popop_entry_changed_cb, ugd);
308                 evas_object_smart_callback_add(entry, "maxlength,reached",
309                                 __device_name_maxlength_reached_cb, ugd);
310
311                 button = elm_button_add(obj);
312                 elm_object_style_set(button, "search_clear");
313                 elm_object_focus_allow_set(button, EINA_FALSE);
314                 elm_object_part_content_set(entry, "elm.swallow.clear", button);
315                 evas_object_smart_callback_add(button, "clicked", _eraser_btn_clicked_cb, ugd);
316
317                 evas_object_smart_callback_add(entry, "focused", _entry_focused_cb, NULL);
318                 evas_object_event_callback_add(entry, EVAS_CALLBACK_KEY_DOWN, _rename_entry_keydown_cb, ugd);
319                 elm_object_part_content_set(layout, "entry_part", entry);
320                 evas_object_show(entry);
321                 elm_object_focus_set(entry, EINA_TRUE);
322
323                 __FUNC_EXIT__;
324                 return entry;
325         }
326         __FUNC_EXIT__;
327         return NULL;
328 }
329
330
331 /**
332  *      This function let the ug create rename popup
333  *      @return   void
334  *      @param[in] data the pointer to the main data structure
335  *      @param[in] obj the pointer to the evas object
336  *      @param[in] event_info the pointer to the event information
337  */
338 void _gl_rename_device_sel(void *data, Evas_Object *obj, void *event_info)
339 {
340         __FUNC_ENTER__;
341         struct ug_data *ugd = (struct ug_data *) data;
342         WFD_RET_IF(ugd == NULL, "The param is NULL\n");
343         WFD_IF_DEL_OBJ(ugd->ctxpopup);
344         WFD_IF_DEL_OBJ(ugd->rename_popup);
345         Evas_Object *popup;
346         Evas_Object *button;
347         Evas_Object *genlist = NULL;
348         Evas_Object *layout = NULL;
349         Elm_Object_Item *git = NULL;
350
351         popup = elm_popup_add(ugd->base);
352         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
353         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, _rename_popup_cancel_cb, ugd);
354         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
355         elm_object_domain_translatable_part_text_set(popup, "title,text",
356                          PACKAGE, "IDS_ST_HEADER_RENAME_DEVICE");
357
358         layout = elm_layout_add(popup);
359         elm_layout_file_set(layout, WFD_UG_EDJ_PATH, "main_rename_device_ly");
360         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
361
362         genlist = elm_genlist_add(layout);
363         evas_object_size_hint_weight_set(genlist,
364                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
365         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
366         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
367         elm_scroller_content_min_limit(genlist, EINA_FALSE, EINA_TRUE);
368
369         /* Entry genlist item */
370         ugd->rename_entry_itc = elm_genlist_item_class_new();
371         if(ugd->rename_entry_itc != NULL) {
372                 ugd->rename_entry_itc->item_style = "entry";
373                 ugd->rename_entry_itc->func.text_get = NULL;
374                 ugd->rename_entry_itc->func.content_get = __wfd_main_rename_entry_icon_get;
375                 ugd->rename_entry_itc->func.state_get = NULL;
376                 ugd->rename_entry_itc->func.del = NULL;
377
378                 elm_genlist_item_append(genlist, ugd->rename_entry_itc, ugd,
379                                 NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
380         }
381
382         ugd->rename_desc_itc = elm_genlist_item_class_new();
383         if(ugd->rename_desc_itc != NULL) {
384                 ugd->rename_desc_itc->item_style = WFD_GENLIST_MULTILINE_TEXT_STYLE;
385                 ugd->rename_desc_itc->func.text_get = __wfd_main_rename_desc_label_get;
386                 ugd->rename_desc_itc->func.content_get = NULL;
387                 ugd->rename_desc_itc->func.state_get = NULL;
388                 ugd->rename_desc_itc->func.del = NULL;
389
390                 git = elm_genlist_item_append(genlist, ugd->rename_desc_itc, NULL, NULL,
391                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
392                 if(git != NULL)
393                         elm_genlist_item_select_mode_set(git,
394                                                  ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
395         }
396
397         button = elm_button_add(popup);
398         elm_object_style_set(button, "popup");
399         elm_object_domain_translatable_text_set(button, PACKAGE,
400                         "IDS_BR_SK_CANCEL");
401         elm_object_part_content_set(popup, "button1", button);
402         evas_object_smart_callback_add(button, "clicked", _rename_popup_cancel_cb, ugd);
403
404         button = elm_button_add(popup);
405         elm_object_style_set(button, "popup");
406         elm_object_domain_translatable_text_set(button, PACKAGE,
407                         "IDS_ST_SK_RENAME");
408         elm_object_part_content_set(popup, "button2", button);
409         evas_object_smart_callback_add(button, "clicked", _rename_popup_ok_cb, ugd);
410         ugd->rename_button = button;
411
412 #if defined(GENLIST_REALIZATION_MOTE_SET)
413         elm_genlist_realization_mode_set(genlist, EINA_TRUE);
414 #endif
415         evas_object_show(genlist);
416         elm_object_part_content_set(layout, "elm.swallow.layout", genlist);
417         evas_object_show(layout);
418         elm_object_content_set(popup, layout);
419         evas_object_show(popup);
420         elm_object_focus_set(ugd->rename_entry, EINA_TRUE);
421         ugd->rename_popup = popup;
422
423         __FUNC_EXIT__;
424 }
425