Remove unused type (TETHERING_TYPE_RESERVED)
[apps/native/ug-mobile-ap.git] / src / mh_device_rename.c
1 /*
2  * mh_device_rename.c
3  *
4  *  Created on: 26-May-2014
5  *      Author: sach.sharma
6  */
7
8 #include <Elementary.h>
9 #include <vconf.h>
10 #include <efl_extension.h>
11
12 #include "mh_view_main.h"
13 #include "mh_string.h"
14 #include "mh_popup.h"
15
16 static Eina_Bool __is_portrait_mode = EINA_TRUE;
17 static int (*rotate_cb)(Eina_Bool , void*) = NULL;
18 static void *rotate_cb_data = NULL;
19 static void __set_rotate_cb(int (*cb)(Eina_Bool, void*) , void *data);
20
21 static void __rename_popup_keyback_cb(void *data, Evas_Object *obj,
22                 void *event_info)
23 {
24         __MOBILE_AP_FUNC_ENTER__;
25
26         mh_appdata_t *ad = (mh_appdata_t *)data;
27
28         elm_object_focus_set(ad->rename_entry, EINA_FALSE);
29
30         if (ad->rename_popup) {
31                 evas_object_del(ad->rename_popup);
32         }
33         ad->rename_popup = NULL;
34         ad->rename_entry = NULL;
35         __set_rotate_cb(NULL, NULL);
36
37         __MOBILE_AP_FUNC_EXIT__;
38 }
39
40 static void __rename_popup_mouseup_cb(void *data,
41                 Evas *e, Evas_Object *obj, void *event_info)
42 {
43         __MOBILE_AP_FUNC_ENTER__;
44
45         Evas_Event_Mouse_Up *ev = event_info;
46         mh_appdata_t *ad = (mh_appdata_t *)data;
47
48         if (ev->button == 3) {
49                 elm_object_focus_set(ad->rename_entry, EINA_FALSE);
50                 if (ad->rename_popup) {
51                         evas_object_del(ad->rename_popup);
52                 }
53                 ad->rename_popup = NULL;
54                 ad->rename_entry = NULL;
55         }
56         __MOBILE_AP_FUNC_EXIT__;
57         return;
58 }
59
60 static bool __is_space_str(const char *str)
61 {
62         while (str) {
63                 if (*str != '\0' && *str != ' ') {
64                         return FALSE;
65                 } else if (*str == '\0') {
66                         return TRUE;
67                 }
68                 str++;
69         }
70         return TRUE;
71 }
72
73 static void __rename_popop_entry_changed_cb(void *data, Evas_Object *obj,
74                                 void *event_info)
75 {
76         __MOBILE_AP_FUNC_ENTER__;
77         mh_appdata_t *ad = (mh_appdata_t *)data;
78
79         const char *entry_text  = NULL;
80         char *input_str                 = NULL;
81         bool is_space_string = FALSE;
82
83         entry_text = elm_entry_entry_get(obj);
84         input_str = elm_entry_markup_to_utf8(entry_text);
85
86         if (elm_object_part_content_get(obj, "elm.swallow.clear")) {
87                 if (elm_object_focus_get(obj)) {
88                         if (elm_entry_is_empty(obj))
89                                 elm_object_signal_emit(obj, "elm,state,clear,hidden", "");
90                         else
91                                 elm_object_signal_emit(obj, "elm,state,clear,visible", "");
92                 }
93         }
94
95
96         if (input_str == NULL || (strlen(input_str) == 0)) {
97                 elm_object_disabled_set(ad->rename_button, TRUE);
98                 elm_entry_input_panel_return_key_disabled_set(obj, TRUE);
99                 if (input_str) {
100                         free(input_str);
101                 }
102                 return;
103         }
104
105         is_space_string = __is_space_str(input_str);
106         if (is_space_string) {
107                 elm_object_disabled_set(ad->rename_button, TRUE);
108                 elm_entry_input_panel_return_key_disabled_set(ad->rename_entry, TRUE);
109                 free(input_str);
110                 return;
111         }
112
113         if (strlen(input_str) > 0) {
114                 elm_object_disabled_set(ad->rename_button, FALSE);
115                 elm_entry_input_panel_return_key_disabled_set(ad->rename_entry, FALSE);
116         }
117         if (input_str) {
118                 free(input_str);
119         }
120         __MOBILE_AP_FUNC_EXIT__;
121 }
122
123 static void __device_name_maxlength_reached_cb(void *data, Evas_Object *obj,
124                 void *event_info)
125 {
126         __MOBILE_AP_FUNC_ENTER__;
127
128         if (data == NULL || obj == NULL) {
129                 ERR("The param is NULL\n");
130                 return;
131         }
132
133         char buf[MH_LABEL_LENGTH_MAX] = { 0, };
134         notification_error_e ret;
135
136         snprintf(buf, sizeof(buf), STR_PW_MAX_LEN_WARN_MSG, DEVICE_NAME_LENGTH_MAX);
137         ret = notification_status_message_post(buf);
138         if (ret != NOTIFICATION_ERROR_NONE) {
139                 ERR("notification_status_message_post() is failed : %d\n", ret);
140         }
141
142         __MOBILE_AP_FUNC_EXIT__;
143 }
144
145 static void __entry_focused_cb(void *data, Evas_Object *obj, void *event_info)
146 {
147         __MOBILE_AP_FUNC_ENTER__;
148
149         if (elm_object_part_content_get(obj, "elm.swallow.clear")) {
150                 if (!elm_entry_is_empty(obj))
151                         elm_object_signal_emit(obj, "elm,state,clear,visible", "");
152                 else
153                         elm_object_signal_emit(obj, "elm,state,clear,hidden", "");
154         }
155         elm_object_signal_emit(obj, "elm,state,focus,on", "");
156
157         __MOBILE_AP_FUNC_EXIT__;
158 }
159
160 static void __entry_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
161 {
162         __MOBILE_AP_FUNC_ENTER__;
163
164         if (elm_object_part_content_get(obj, "elm.swallow.clear"))
165                 elm_object_signal_emit(obj, "elm,state,clear,hidden", "");
166         elm_object_signal_emit(obj, "elm,state,focus,off", "");
167
168         __MOBILE_AP_FUNC_EXIT__;
169 }
170
171 static void __eraser_btn_clicked_cb(void *data, Evas_Object *obj,
172                 void *event_info)
173 {
174         __MOBILE_AP_FUNC_ENTER__;
175
176         if (data == NULL) {
177                 ERR("data is null\n");
178                 return;
179         }
180         mh_appdata_t *ad = (mh_appdata_t*) data;
181
182         elm_entry_entry_set(ad->rename_entry, "");
183         elm_object_disabled_set(ad->rename_button, TRUE);
184         elm_entry_input_panel_return_key_disabled_set(ad->rename_entry, TRUE);
185
186         __MOBILE_AP_FUNC_EXIT__;
187 }
188
189 static void __rename_popup_cancel_cb(void *data, Evas_Object *obj,
190                 void *event_info)
191 {
192         __MOBILE_AP_FUNC_ENTER__;
193
194         if (data == NULL || obj == NULL) {
195                 ERR("data is null\n");
196                 return;
197         }
198         mh_appdata_t *ad = (mh_appdata_t*) data;
199
200         if (ad->rename_popup) {
201                 evas_object_del(ad->rename_popup);
202                 ad->rename_popup = NULL;
203         }
204         if (ad->rename_entry) {
205                 evas_object_del(ad->rename_entry);
206                 ad->rename_entry = NULL;
207         }
208         __set_rotate_cb(NULL, NULL);
209
210         __MOBILE_AP_FUNC_EXIT__;
211         return;
212 }
213
214 static void __rename_popup_ok_cb(void *data, Evas_Object *obj,
215                 void *event_info)
216 {
217         __MOBILE_AP_FUNC_ENTER__;
218
219         if (data == NULL || obj == NULL) {
220                 ERR("data is null\n");
221                 return;
222         }
223         mh_appdata_t *ad = (mh_appdata_t*) data;
224         char *device_name_str = NULL;
225         const char *entry_str = elm_entry_entry_get(ad->rename_entry);
226
227         device_name_str = elm_entry_markup_to_utf8(entry_str);
228         if (device_name_str == NULL) {
229                 ERR("elm_entry_utf8_to_markup is failed\n");
230                 return;
231         }
232
233         DBG("New device name:[%s]\n", device_name_str);
234         __set_rotate_cb(NULL, NULL);
235         if (ad->rename_popup) {
236                 evas_object_del(ad->rename_popup);
237                 ad->rename_popup = NULL;
238         }
239         if (ad->rename_entry) {
240                 evas_object_del(ad->rename_entry);
241                 ad->rename_entry = NULL;
242         }
243         if (g_strcmp0(ad->setup.device_name, device_name_str)) {
244                 if (vconf_set_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR, device_name_str) != 0) {
245                         DBG("Set vconf[%s] failed\n",VCONFKEY_SETAPPL_DEVICE_NAME_STR);
246                 }
247
248                 g_strlcpy(ad->setup.device_name, entry_str,
249                                 sizeof(ad->setup.device_name));
250
251                 if (ad->main.help_item != NULL) {
252                         if (ad->main.help_item)
253                                 elm_genlist_item_update(ad->main.help_item);
254                 }
255
256                 if (ad->setup.name_item != NULL) {
257                         if (ad->setup.name_item)
258                                 elm_genlist_item_update(ad->setup.name_item);
259                 }
260         }
261         free(device_name_str);
262
263         __MOBILE_AP_FUNC_EXIT__;
264         return;
265 }
266
267 static void __enter_key_down_cb(void *data, Evas *evas, Evas_Object *obj,
268                                 void *event_info)
269 {
270         __MOBILE_AP_FUNC_ENTER__;
271
272         Evas_Event_Key_Down *ev = event_info;
273
274         if (g_strcmp0(ev->key, "KP_Enter") == 0 ||
275                         g_strcmp0(ev->key, "Return") == 0) {
276                 Ecore_IMF_Context *imf_context;
277                 elm_entry_cursor_end_set(obj);
278                 imf_context = (Ecore_IMF_Context*)elm_entry_imf_context_get(obj);
279                 if (imf_context)
280                         ecore_imf_context_input_panel_hide(imf_context);
281                 elm_object_focus_set(obj, EINA_FALSE);
282         }
283         __MOBILE_AP_FUNC_EXIT__;
284 }
285
286 static void __set_rotate_cb(int (*cb)(Eina_Bool, void*), void *data)
287 {
288         rotate_cb = cb;
289         rotate_cb_data = data;
290 }
291
292 Eina_Bool __is_current_mode_portrait(void)
293 {
294         return __is_portrait_mode;
295 }
296
297 static void __set_portrait_mode(Eina_Bool is_portrait)
298 {
299         __is_portrait_mode = is_portrait;
300 }
301
302
303 void _hadnle_rename_popup_rotation(Eina_Bool is_portrait)
304 {
305         __MOBILE_AP_FUNC_ENTER__;
306
307         __set_portrait_mode(is_portrait);
308         if (rotate_cb) {
309                 rotate_cb(is_portrait, rotate_cb_data);
310         }
311         __MOBILE_AP_FUNC_EXIT__;
312 }
313
314 static Evas_Object *__rename_entry_icon_get(void *data, Evas_Object *obj, const char *part)
315 {
316         __MOBILE_AP_FUNC_ENTER__;
317
318         Evas_Object *entry = NULL;
319         Evas_Object *button = NULL;
320         char *ptr = NULL;
321         char *device_name_utf = NULL;
322         static Elm_Entry_Filter_Limit_Size limit_filter_data;
323
324         if (data == NULL) {
325                 ERR("data is null\n");
326                 return NULL;
327         }
328         mh_appdata_t *ad = (mh_appdata_t*) data;
329
330         if (strcmp(part, "elm.icon.entry")) {
331                 return NULL;
332         }
333
334         entry = elm_entry_add(obj);
335         elm_entry_single_line_set(entry, EINA_TRUE);
336         elm_entry_scrollable_set(entry, EINA_TRUE);
337         limit_filter_data.max_char_count = DEVICE_NAME_LENGTH_MAX;
338         elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
339
340         evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
341         evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
342         eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
343         elm_object_signal_emit(entry, "elm,action,hide,search_icon", "");
344         elm_object_domain_translatable_part_text_set(entry, "elm.guide", PACKAGE,
345                         STR_DEVICE_NAME);
346
347         elm_entry_input_panel_return_key_type_set(entry,
348                         ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
349         elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
350         ad->rename_entry = entry;
351
352         device_name_utf = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
353         if (device_name_utf == NULL) {
354                 ERR("vconf_get_str failed \n");
355                 return NULL;
356         }
357         ptr = elm_entry_utf8_to_markup(device_name_utf);
358         if (ptr == NULL) {
359                 ERR("elm_entry_utf8_to_markup is failed\n");
360                 free(device_name_utf);
361                 return NULL;
362         } else {
363                 elm_entry_entry_set(entry, ptr);
364         }
365         free(device_name_utf);
366         free(ptr);
367         elm_entry_cursor_end_set(entry);
368
369         evas_object_smart_callback_add(entry, "changed,user", __rename_popop_entry_changed_cb, ad);
370         evas_object_smart_callback_add(entry, "changed", __rename_popop_entry_changed_cb, ad);
371         evas_object_smart_callback_add(entry, "preedit,changed",
372                         __rename_popop_entry_changed_cb, ad);
373
374         evas_object_smart_callback_add(entry, "maxlength,reached",
375                         __device_name_maxlength_reached_cb, ad);
376         evas_object_smart_callback_add(entry, "focused", __entry_focused_cb, NULL);
377         evas_object_smart_callback_add(entry, "unfocused", __entry_unfocused_cb, NULL);
378         evas_object_event_callback_add(entry, EVAS_CALLBACK_KEY_DOWN,
379                         __enter_key_down_cb, ad);
380
381         button = elm_button_add(obj);
382         elm_object_style_set(button, "search_clear");
383         elm_object_focus_allow_set(button, EINA_FALSE);
384         elm_object_part_content_set(entry, "elm.swallow.clear", button);
385         evas_object_smart_callback_add(button, "clicked", __eraser_btn_clicked_cb, ad);
386         evas_object_show(entry);
387         elm_object_focus_set(entry, EINA_TRUE);
388         return entry;
389 }
390
391 static char *__rename_desc_label_get(void *data, Evas_Object *obj,
392                                         const char *part)
393 {
394         if (!strcmp(part, "elm.text.multiline" )) {
395                 return g_strdup(STR_RENAME_DEVICE_MSG);
396         }
397         return NULL;
398 }
399
400 void _create_rename_device_popup(void *data)
401 {
402         __MOBILE_AP_FUNC_ENTER__;
403
404         if (data == NULL) {
405                 ERR("data is null\n");
406                 __MOBILE_AP_FUNC_EXIT__;
407                 return;
408         }
409
410         Evas_Object *popup = NULL;
411         Evas_Object *button = NULL;
412         Evas_Object *rename_btn = NULL;
413         Evas_Object *genlist = NULL;
414         Elm_Object_Item *item = NULL;
415         Evas_Object *layout = NULL;
416         mh_appdata_t *ad = (mh_appdata_t*) data;
417
418         if (ad->rename_popup) {
419                 evas_object_del(ad->rename_popup);
420                 ad->rename_popup = NULL;
421         }
422
423         popup = elm_popup_add(ad->layout);
424         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
425         elm_object_domain_translatable_part_text_set(popup, "title,text", PACKAGE,
426                         "IDS_ST_HEADER_RENAME_DEVICE");
427         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
428                         __rename_popup_keyback_cb, (void *)ad);
429         evas_object_event_callback_add(popup, EVAS_CALLBACK_MOUSE_UP,
430                         __rename_popup_mouseup_cb, (void *)ad);
431
432         layout = elm_layout_add(popup);
433         elm_layout_file_set(layout, FILE_PATH_OF_EDC, "rename_device_ly");
434         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
435
436         genlist = elm_genlist_add(layout);
437         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
438         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
439         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
440         elm_scroller_content_min_limit(genlist, EINA_FALSE, EINA_TRUE);
441
442         /* Entry genlist item */
443         ad->setup.rename_entry_itc = elm_genlist_item_class_new();
444         if (ad->setup.rename_entry_itc == NULL) {
445                 ERR("elm_genlist_item_class_new failed\n");
446                 __MOBILE_AP_FUNC_EXIT__;
447                 return;
448         }
449         ad->setup.rename_entry_itc->item_style = "entry";
450         ad->setup.rename_entry_itc->func.text_get = NULL;
451         ad->setup.rename_entry_itc->func.content_get = __rename_entry_icon_get;
452         ad->setup.rename_entry_itc->func.state_get = NULL;
453         ad->setup.rename_entry_itc->func.del = NULL;
454
455         elm_genlist_item_append(genlist, ad->setup.rename_entry_itc, ad,
456                                 NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
457
458         /* Description genlist item */
459         ad->setup.rename_descp_itc = elm_genlist_item_class_new();
460         if (ad->setup.rename_descp_itc == NULL) {
461                 ERR("elm_genlist_item_class_new failed\n");
462                 __MOBILE_AP_FUNC_EXIT__;
463                 return;
464         }
465         ad->setup.rename_descp_itc->item_style = "multiline_sub";
466         ad->setup.rename_descp_itc->func.text_get = __rename_desc_label_get;
467         ad->setup.rename_descp_itc->func.content_get = NULL;
468         ad->setup.rename_descp_itc->func.state_get = NULL;
469         ad->setup.rename_descp_itc->func.del = NULL;
470         item = elm_genlist_item_append(genlist, ad->setup.rename_descp_itc, NULL, NULL,
471                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
472         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
473
474         button = elm_button_add(popup);
475         elm_object_style_set(button, "popup");
476         elm_object_text_set(button, STR_CANCEL);
477         elm_object_domain_translatable_text_set(button, PACKAGE, STR_CANCEL);
478         elm_object_part_content_set(popup, "button1", button);
479         evas_object_smart_callback_add(button, "clicked",
480                         __rename_popup_cancel_cb, ad);
481         evas_object_show(button);
482
483         rename_btn = elm_button_add(popup);
484         ad->rename_button = rename_btn;
485         elm_object_style_set(rename_btn, "popup");
486         elm_object_domain_translatable_text_set(rename_btn, PACKAGE, STR_RENAME_DEVICE);
487         elm_object_part_content_set(popup, "button2", rename_btn);
488         evas_object_smart_callback_add(rename_btn, "clicked", __rename_popup_ok_cb, ad);
489         evas_object_show(rename_btn);
490
491         elm_genlist_realization_mode_set(genlist, EINA_TRUE);
492         evas_object_show(genlist);
493         elm_object_part_content_set(layout, "elm.swallow.layout", genlist);
494         evas_object_show(layout);
495         elm_object_content_set(popup, layout);
496
497         evas_object_show(popup);
498         ad->rename_popup = popup;
499         elm_object_focus_set(ad->rename_entry, EINA_TRUE);
500
501         __MOBILE_AP_FUNC_EXIT__;
502         return;
503 }