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