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