Implement hardware key event #2
[apps/native/ug-wifi-efl.git] / sources / libraries / Common / common_utils.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 <vconf.h>
21 #include <syspopup_caller.h>
22 #include <aul.h>
23 #include <Ecore_X.h>
24 #include <efl_assist.h>
25
26 #include "common.h"
27 #include "common_utils.h"
28 #include "i18nmanager.h"
29
30 #define POPUP_HEAD_AREA 134
31 #define POPUP_BUTTON_AREA 80
32
33 typedef struct {
34         char *title_str;
35         char *info_str;
36 } two_line_disp_data_t;
37
38 static GSList *managed_idler_list = NULL;
39
40 static char *__common_utils_2line_text_get(void *data, Evas_Object *obj, const char *part)
41 {
42         two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
43         if (!strcmp(part, "elm.text.1")) {
44                 return g_strdup(item_data->info_str);
45         } else if (!strcmp(part, "elm.text.2")) {
46                 return g_strdup(item_data->title_str);
47         }
48         return NULL;
49 }
50
51 static void __common_utils_2line_text_del(void *data, Evas_Object *obj)
52 {
53         two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
54         if (item_data) {
55                 g_free(item_data->info_str);
56                 g_free(item_data->title_str);
57                 g_free(item_data);
58         }
59 }
60
61 static void __common_utils_separator_del(void *data, Evas_Object *obj)
62 {
63         elm_genlist_item_class_free(data);
64         return;
65 }
66
67 Elm_Object_Item* common_utils_add_dialogue_separator(Evas_Object* genlist, const char *separator_style)
68 {
69         assertm_if(NULL == genlist, "NULL!!");
70
71         static Elm_Genlist_Item_Class *separator_itc;
72         separator_itc = elm_genlist_item_class_new();
73         separator_itc->item_style = separator_style;
74         separator_itc->func.text_get = NULL;
75         separator_itc->func.content_get = NULL;
76         separator_itc->func.state_get = NULL;
77         separator_itc->func.del = __common_utils_separator_del;
78
79         Elm_Object_Item* sep = elm_genlist_item_append(
80                                         genlist,
81                                         separator_itc,
82                                         separator_itc,
83                                         NULL,
84                                         ELM_GENLIST_ITEM_GROUP,
85                                         NULL,
86                                         NULL);
87
88         assertm_if(NULL == sep, "NULL!!");
89
90         elm_genlist_item_select_mode_set(sep, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
91
92         return sep;
93 }
94
95 char *common_utils_get_ap_security_type_info_txt(const char *pkg_name, wifi_device_info_t *device_info)
96 {
97         char *status_txt = NULL;
98         switch (device_info->security_mode)
99         {
100                 case WLAN_SEC_MODE_NONE:                /** Security disabled */
101                         status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Open));
102                         break;
103                 case WLAN_SEC_MODE_IEEE8021X:   /** EAP */
104                         status_txt = g_strdup_printf("%s (%s)", sc(pkg_name, I18N_TYPE_Secured), sc(pkg_name, I18N_TYPE_EAP));
105                         break;
106                 case WLAN_SEC_MODE_WEP:                 /** WEP */
107                 case WLAN_SEC_MODE_WPA_PSK:             /** WPA-PSK */
108                 case WLAN_SEC_MODE_WPA2_PSK:    /** WPA2-PSK */
109                         if (TRUE == device_info->wps_mode) {
110                                 status_txt = g_strdup_printf("%s (%s)", sc(pkg_name, I18N_TYPE_Secured), sc(pkg_name, I18N_TYPE_WPS_Available));
111                         } else {
112                                 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Secured));
113                         }
114                         break;
115                 default:                                                /** Unknown */
116                         status_txt = g_strdup(WIFI_UNKNOWN_DEVICE_STATUS_STR);
117                         break;
118         }
119         return status_txt;
120 }
121
122 void common_utils_get_device_icon(const char *image_path_dir, wifi_device_info_t *device_info, char **icon_path)
123 {
124         char buf[MAX_DEVICE_ICON_PATH_STR_LEN] = {'\0', };
125
126         g_strlcpy(buf, image_path_dir, sizeof(buf));
127         g_strlcat(buf, "/37_wifi_icon", sizeof(buf));
128
129         if (device_info->security_mode != WLAN_SEC_MODE_NONE) {
130                 g_strlcat(buf, "_lock", sizeof(buf));
131         }
132
133         switch (wlan_manager_get_signal_strength(device_info->rssi)) {
134         case SIGNAL_STRENGTH_TYPE_EXCELLENT:
135                 g_strlcat(buf, "_03", sizeof(buf));
136                 break;
137         case SIGNAL_STRENGTH_TYPE_GOOD:
138                 g_strlcat(buf, "_02", sizeof(buf));
139                 break;
140         case SIGNAL_STRENGTH_TYPE_WEAK:
141                 g_strlcat(buf, "_01", sizeof(buf));
142                 break;
143         case SIGNAL_STRENGTH_TYPE_VERY_WEAK:
144         case SIGNAL_STRENGTH_TYPE_NULL:
145         default:
146                 g_strlcat(buf, "_00", sizeof(buf));
147                 break;
148         }
149
150         if (icon_path) {
151                 *icon_path = g_strdup_printf("%s", buf);
152         }
153 }
154
155 char *common_utils_get_rssi_text(const char *str_pkg_name, int rssi)
156 {
157         switch (wlan_manager_get_signal_strength(rssi)) {
158         case SIGNAL_STRENGTH_TYPE_EXCELLENT:
159                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Excellent));
160         case SIGNAL_STRENGTH_TYPE_GOOD:
161                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Good));
162         default:
163                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Week));
164         }
165 }
166
167 Evas_Object *common_utils_entry_layout_get_entry(Evas_Object *layout)
168 {
169         return elm_object_part_content_get(layout, "elm.swallow.content");
170 }
171
172 char *common_utils_entry_layout_get_text(Evas_Object *layout)
173 {
174         Evas_Object *entry = elm_object_part_content_get(layout, "elm.swallow.content");
175         return elm_entry_markup_to_utf8(elm_entry_entry_get(entry));
176 }
177
178 void common_popup_size_get(Ecore_IMF_Context *target_imf, int *width, int *height)
179 {
180         __COMMON_FUNC_ENTER__;
181
182         int window_width, window_height;
183         int start_x, start_y, imf_width, imf_height;
184         int rotate_angle;
185         float resize_scale = 0.7f;
186
187         rotate_angle = common_utils_get_rotate_angle(APPCORE_RM_UNKNOWN);
188         ecore_x_window_size_get(ecore_x_window_root_first_get(), &window_width, &window_height);
189
190         *width = window_width;
191
192         if (rotate_angle == 0 || rotate_angle == 180) {
193                 *height = window_height * resize_scale;
194         }else
195                 *height = window_width;
196
197         if (target_imf != NULL) {
198                 ecore_imf_context_input_panel_geometry_get(target_imf, &start_x, &start_y, &imf_width, &imf_height);
199                 *height = start_y * resize_scale;
200         }
201
202         *height = *height-POPUP_HEAD_AREA-POPUP_BUTTON_AREA;
203
204         __COMMON_FUNC_EXIT__;
205 }
206
207 void common_utils_entry_password_set(Evas_Object *layout, Eina_Bool pswd_set)
208 {
209         Evas_Object *entry = elm_object_part_content_get(layout, "elm.swallow.content");
210         elm_entry_password_set(entry, pswd_set);
211 }
212
213 void common_utils_set_edit_box_imf_panel_evnt_cb(Elm_Object_Item *item,
214                                                 imf_ctxt_panel_cb_t input_panel_cb, void *user_data)
215 {
216         __COMMON_FUNC_ENTER__;
217         common_utils_entry_info_t *entry_info;
218         entry_info = elm_object_item_data_get(item);
219         if (!entry_info)
220                 return;
221
222         entry_info->input_panel_cb = input_panel_cb;
223         entry_info->input_panel_cb_data = user_data;
224
225         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
226         Ecore_IMF_Context *imf_ctxt = elm_entry_imf_context_get(entry);
227         if (imf_ctxt && entry_info->input_panel_cb) {
228                 /* Deleting the previously attached callback */
229                 ecore_imf_context_input_panel_event_callback_del(imf_ctxt,
230                                 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
231                                 entry_info->input_panel_cb);
232                 ecore_imf_context_input_panel_event_callback_add(imf_ctxt,
233                                 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
234                                 entry_info->input_panel_cb,
235                                 entry_info->input_panel_cb_data);
236                 DEBUG_LOG(UG_NAME_NORMAL, "set the imf ctxt cbs");
237         }
238
239         __COMMON_FUNC_EXIT__;
240         return;
241 }
242
243 void common_utils_edit_box_focus_set(Elm_Object_Item *item, Eina_Bool focus_set)
244 {
245         __COMMON_FUNC_ENTER__;
246         if (!item)
247                 return;
248
249         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
250         elm_object_focus_set(entry, focus_set);
251
252         __COMMON_FUNC_EXIT__;
253         return;
254 }
255
256 Elm_Object_Item *common_utils_add_2_line_txt_disabled_item(Evas_Object* view_list, const char *style_name, const char *line1_txt, const char *line2_txt)
257 {
258         static Elm_Genlist_Item_Class two_line_display_itc;
259         two_line_disp_data_t *two_line_data = NULL;
260         Elm_Object_Item *item = NULL;
261
262         two_line_display_itc.item_style = style_name;
263         two_line_display_itc.func.text_get = __common_utils_2line_text_get;
264         two_line_display_itc.func.content_get = NULL;
265         two_line_display_itc.func.state_get = NULL;
266         two_line_display_itc.func.del = __common_utils_2line_text_del;
267
268         two_line_data = g_new0(two_line_disp_data_t, 1);
269         two_line_data->title_str = g_strdup(line1_txt);
270         two_line_data->info_str = g_strdup(line2_txt);
271         INFO_LOG(UG_NAME_NORMAL, "title_str = %s info_str = %s", two_line_data->title_str, two_line_data->info_str);
272
273         item = elm_genlist_item_append(view_list, &two_line_display_itc, two_line_data, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
274         elm_object_item_disabled_set(item, TRUE);
275
276         return item;
277 }
278
279 char *common_utils_get_list_item_entry_txt(Elm_Object_Item *entry_item)
280 {
281         common_utils_entry_info_t *entry_info =
282                         (common_utils_entry_info_t *)elm_object_item_data_get(entry_item);
283         if (entry_info == NULL)
284                 return NULL;
285
286         DEBUG_LOG(UG_NAME_NORMAL, "entry_info: 0x%x", entry_info);
287
288         return g_strdup(entry_info->entry_txt);
289 }
290
291 Evas_Object *common_utils_create_radio_button(Evas_Object *parent, const int value)
292 {
293         Evas_Object *radio = elm_radio_add(parent);
294         elm_radio_state_value_set(radio, value);
295 //      elm_radio_group_add(radio, radio_main);
296         evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND,
297                 EVAS_HINT_EXPAND);
298         evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
299
300         return radio;
301 }
302
303 Evas_Object *common_utils_create_layout(Evas_Object *navi_frame)
304 {
305         Evas_Object *layout;
306         layout = elm_layout_add(navi_frame);
307         elm_layout_theme_set(layout, "layout", "application", "noindicator");
308         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
309
310         Evas_Object* bg = elm_bg_add(layout);
311         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
312         elm_object_style_set(bg, "group_list");
313         elm_object_part_content_set(layout, "elm.swallow.bg", bg);
314
315         evas_object_show(layout);
316
317         return layout;
318 }
319
320 static void __common_utils_del_popup(void *data, Evas_Object *obj, void *event_info)
321 {
322         Evas_Object *popup = (Evas_Object *)data;
323         evas_object_del(popup);
324 }
325
326 Evas_Object *common_utils_show_info_popup(Evas_Object *parent, popup_btn_info_t *popup_data)
327 {
328         __COMMON_FUNC_ENTER__;
329         Evas_Object *popup = elm_popup_add(parent);
330
331         ea_object_event_callback_add(popup, EA_CALLBACK_BACK, ea_popup_back_cb, NULL);
332
333         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
334         if (popup_data->title_txt)
335                 elm_object_part_text_set(popup, "title,text", popup_data->title_txt);
336         if (popup_data->info_txt)
337                 elm_object_text_set(popup, popup_data->info_txt);
338         if (popup_data->btn1_txt) {
339                 Evas_Object *btn_1 = elm_button_add(popup);
340                 elm_object_text_set(btn_1, popup_data->btn1_txt);
341                 elm_object_part_content_set(popup, "button1", btn_1);
342                 if (popup_data->btn1_cb) {
343                         evas_object_smart_callback_add(btn_1, "clicked", popup_data->btn1_cb, popup_data->btn1_data);
344                 } else {        // set the default callback
345                         evas_object_smart_callback_add(btn_1, "clicked", __common_utils_del_popup, popup);
346                 }
347         }
348         if (popup_data->btn2_txt) {
349                 Evas_Object *btn_2 = elm_button_add(popup);
350                 elm_object_text_set(btn_2, popup_data->btn2_txt);
351                 elm_object_part_content_set(popup, "button2", btn_2);
352                 evas_object_smart_callback_add(btn_2, "clicked", popup_data->btn2_cb, NULL);
353                 evas_object_show(popup);
354                 if (popup_data->btn2_cb) {
355                         evas_object_smart_callback_add(btn_2, "clicked", popup_data->btn2_cb, popup_data->btn2_data);
356                 } else {        // set the default callback
357                         evas_object_smart_callback_add(btn_2, "clicked", __common_utils_del_popup, popup);
358                 }
359         }
360
361         elm_object_focus_set(popup, EINA_TRUE);
362         evas_object_show(popup);
363
364         return popup;
365 }
366
367 Evas_Object *common_utils_show_info_ok_popup(Evas_Object *win,
368                 const char *str_pkg_name, const char *info_txt)
369 {
370         popup_btn_info_t popup_data;
371
372         memset(&popup_data, 0, sizeof(popup_data));
373         popup_data.info_txt = (char *)info_txt;
374         popup_data.btn1_txt = sc(str_pkg_name, I18N_TYPE_Ok);
375
376         return common_utils_show_info_popup(win, &popup_data);
377 }
378
379 Evas_Object *common_utils_show_info_timeout_popup(Evas_Object *win,
380                 const char* info_text, const double timeout)
381 {
382         Evas_Object *popup = elm_popup_add(win);
383
384         elm_object_text_set(popup, info_text);
385         elm_popup_timeout_set(popup, timeout);
386         evas_object_smart_callback_add(popup, "timeout",
387                         __common_utils_del_popup, popup);
388         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
389         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
390         evas_object_show(popup);
391
392         return popup;
393 }
394
395 int common_utils_get_rotate_angle(enum appcore_rm rotate_mode)
396 {
397         int rotate_angle = 0;
398         if (APPCORE_RM_UNKNOWN == rotate_mode) {
399                 appcore_get_rotation_state(&rotate_mode);
400         }
401
402         DEBUG_LOG(SP_NAME_NORMAL, "rotate_mode = %d", rotate_mode);
403
404         switch (rotate_mode) {
405         case APPCORE_RM_PORTRAIT_NORMAL:         /**< Portrait mode */
406                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_NORMAL");
407                 rotate_angle = 0;
408                 break;
409
410         case APPCORE_RM_PORTRAIT_REVERSE:         /**< Portrait upside down mode */
411                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_REVERSE");
412                 rotate_angle = 180;
413                 break;
414
415         case APPCORE_RM_LANDSCAPE_NORMAL:         /**< Left handed landscape mode */
416                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_NORMAL");
417                 rotate_angle = 270;
418                 break;
419
420         case APPCORE_RM_LANDSCAPE_REVERSE:          /**< Right handed landscape mode */
421                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_REVERSE");
422                 rotate_angle = 90;
423                 break;
424
425         default:
426                 ERROR_LOG(SP_NAME_ERR, "Invalid rotate mode. The default value (0) is set to 'rotate_angle'.");
427                 rotate_angle = 0;
428                 break;
429         }
430
431         return rotate_angle;
432 }
433
434 wlan_security_mode_type_t common_utils_get_sec_mode(wifi_security_type_e sec_type)
435 {
436         switch (sec_type) {
437         case WIFI_SECURITY_TYPE_NONE:
438                 return WLAN_SEC_MODE_NONE;
439         case WIFI_SECURITY_TYPE_WEP:
440                 return WLAN_SEC_MODE_WEP;
441         case WIFI_SECURITY_TYPE_WPA_PSK:
442                 return WLAN_SEC_MODE_WPA_PSK;
443         case WIFI_SECURITY_TYPE_WPA2_PSK:
444                 return WLAN_SEC_MODE_WPA_PSK;
445         case WIFI_SECURITY_TYPE_EAP:
446                 return WLAN_SEC_MODE_IEEE8021X;
447         default:
448                 return WLAN_SEC_MODE_NONE;
449         }
450         return WLAN_SEC_MODE_NONE;
451 }
452
453 int common_utils_send_message_to_net_popup(const char *title, const char *content, const char *type, const char *ssid)
454 {
455         int ret = 0;
456         bundle *b = bundle_create();
457
458         bundle_add(b, "_SYSPOPUP_TITLE_", title);
459         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
460         bundle_add(b, "_SYSPOPUP_TYPE_", type);
461         bundle_add(b, "_AP_NAME_", ssid);
462
463         ret = aul_launch_app("net.netpopup", b);
464
465         bundle_free(b);
466
467         return ret;
468 }
469
470 int common_util_set_system_registry(const char *key, int value)
471 {
472         __COMMON_FUNC_ENTER__;
473
474         if (vconf_set_int(key, value) < 0) {
475                 ERROR_LOG(UG_NAME_NORMAL, "Failed to set vconf");
476
477                 __COMMON_FUNC_EXIT__;
478                 return -1;
479         }
480
481         __COMMON_FUNC_EXIT__;
482         return 0;
483 }
484
485 int common_util_get_system_registry(const char *key)
486 {
487         __COMMON_FUNC_ENTER__;
488
489         int value = 0;
490
491         if (vconf_get_int(key, &value) < 0) {
492                 ERROR_LOG(UG_NAME_NORMAL, "Failed to get vconf");
493
494                 __COMMON_FUNC_EXIT__;
495                 return -1;
496         }
497
498         __COMMON_FUNC_EXIT__;
499         return value;
500 }
501
502 struct managed_idle_data {
503         GSourceFunc func;
504         gpointer user_data;
505         guint id;
506 };
507
508 static void __managed_idle_destroy_cb(gpointer data)
509 {
510         if (!data)
511                 return;
512
513         managed_idler_list = g_slist_remove(managed_idler_list, data);
514 }
515
516 static gboolean __managed_idle_hook_cb(gpointer user_data)
517 {
518         struct managed_idle_data *data = user_data;
519
520         if (!data)
521                 return FALSE;
522
523         return data->func(data->user_data);
524 }
525
526 guint common_util_managed_idle_add(GSourceFunc func, gpointer user_data)
527 {
528         guint id;
529         struct managed_idle_data *data;
530
531         if (!func)
532                 return 0;
533
534         data = g_new0(struct managed_idle_data, 1);
535         if (!data)
536                 return 0;
537
538         data->func = func;
539         data->user_data = user_data;
540
541         id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __managed_idle_hook_cb, data,
542                         __managed_idle_destroy_cb);
543         if (!id) {
544                 g_free(data);
545                 return id;
546         }
547
548         data->id = id;
549
550         managed_idler_list = g_slist_append(managed_idler_list, data);
551
552         return id;
553 }
554
555 void common_util_managed_idle_cleanup(void)
556 {
557         GSList *cur = managed_idler_list;
558         GSource *src;
559         struct managed_idle_data *data;
560
561         for (;cur; cur = cur->next) {
562                 data = cur->data;
563
564                 src = g_main_context_find_source_by_id(g_main_context_default(),
565                                 data->id);
566                 if (src) {
567                         g_source_destroy(src);
568                 }
569
570                 g_free(data);
571         }
572
573         g_slist_free (managed_idler_list);
574         managed_idler_list = NULL;
575
576 }