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