4 * Copyright 2012 Samsung Electronics Co., Ltd
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
10 * http://www.tizenopensource.org/license
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.
22 #include <ui-gadget-module.h>
23 #include <bundle_internal.h>
24 #include <efl_extension.h>
28 #include "common_utils.h"
29 #include "i18nmanager.h"
31 #define SUPPLICANT_SERVICE "fi.w1.wpa_supplicant1"
32 #define SUPPLICANT_SERVICE_INTERFACE SUPPLICANT_SERVICE ".Interface"
33 #define COLOR_TABLE "/usr/apps/wifi-efl-ug/shared/res/tables/ug-wifi-efl_ChangeableColorTable.xml"
34 #define FONT_TABLE "/usr/apps/wifi-efl-ug/shared/res/tables/ug-wifi-efl_FontInfoTable.xml"
39 } two_line_disp_data_t;
41 struct managed_idle_data {
47 struct gdbus_connection_data {
48 GDBusConnection *connection;
49 guint subscribe_id_supplicant;
52 static __thread struct gdbus_connection_data gdbus_conn = { NULL, 0 };
53 static GSList *managed_idler_list = NULL;
54 static int (*rotate_cb)(enum appcore_rm, void*, Eina_Bool, Eina_Bool) = NULL;
55 static void *rotate_cb_data = NULL;
56 static Eina_Bool is_wps = EINA_FALSE;
57 static Eina_Bool is_setting = EINA_FALSE;
58 static Eina_Bool is_portrait_mode = EINA_TRUE;
59 static Ecore_Timer *scan_update_timer = NULL;
61 static char *__common_utils_2line_text_get(void *data, Evas_Object *obj, const char *part)
63 two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
64 if (!strcmp("elm.text", part)) {
65 return g_strdup(item_data->title_str);
66 } else if (!strcmp("elm.text.sub", part)) {
67 return g_strdup(item_data->info_str);
72 static void __common_utils_2line_text_del(void *data, Evas_Object *obj)
74 two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
76 g_free(item_data->info_str);
77 g_free(item_data->title_str);
82 static void __common_utils_separator_del(void *data, Evas_Object *obj)
84 elm_genlist_item_class_free(data);
88 Eina_Bool common_utils_is_portrait_mode(void)
90 return is_portrait_mode;
93 static void __common_utils_set_portrait_mode(Eina_Bool on)
95 is_portrait_mode = on;
98 void common_utils_set_rotate_cb(int (*func)(enum appcore_rm, void*, Eina_Bool, Eina_Bool),
99 void *data, Eina_Bool wps_value, Eina_Bool setting_value)
102 rotate_cb_data = data;
104 is_setting = setting_value;
107 static void __common_utils_rotate_popup(enum appcore_rm rotmode)
110 rotate_cb(rotmode, rotate_cb_data, is_wps, is_setting);
114 void common_utils_contents_rotation_adjust(int event)
116 if (event == UG_EVENT_ROTATE_PORTRAIT ||
117 event == UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN) {
118 __common_utils_rotate_popup(APPCORE_RM_PORTRAIT_NORMAL);
119 __common_utils_set_portrait_mode(EINA_TRUE);
121 __common_utils_rotate_popup(APPCORE_RM_LANDSCAPE_NORMAL);
122 __common_utils_set_portrait_mode(EINA_FALSE);
126 Elm_Object_Item* common_utils_add_dialogue_separator(Evas_Object* genlist, const char *separator_style)
128 assertm_if(NULL == genlist, "NULL!!");
130 static Elm_Genlist_Item_Class *separator_itc;
131 separator_itc = elm_genlist_item_class_new();
132 separator_itc->item_style = separator_style;
133 separator_itc->func.text_get = NULL;
134 separator_itc->func.content_get = NULL;
135 separator_itc->func.state_get = NULL;
136 separator_itc->func.del = __common_utils_separator_del;
138 Elm_Object_Item* sep = elm_genlist_item_append(
143 ELM_GENLIST_ITEM_GROUP,
147 assertm_if(NULL == sep, "NULL!!");
149 elm_genlist_item_select_mode_set(sep, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
154 char *common_utils_get_ap_security_type_info_txt(const char *pkg_name,
155 wifi_device_info_t *device_info, bool check_fav)
157 bool favorite = false;
159 char *status_txt = NULL;
161 /* TODO: temporary disabled */
163 wifi_connection_state_e state;
164 wifi_connection_error_e error;
166 wifi_ap_get_connection_state(device_info->ap, &state);
167 if (state == WIFI_CONNECTION_STATE_FAILURE) {
168 error = wlan_manager_get_connection_error_state(device_info->ap);
170 if (error == WIFI_CONNECTION_ERROR_INVALID_KEY)
171 return g_strdup(sc(pkg_name,
172 I18N_TYPE_WIFI_AUTHENTICATION_ERROR_OCCURRED));
174 if (error == WIFI_CONNECTION_ERROR_DHCP_FAILED)
175 return g_strdup(sc(pkg_name,
176 I18N_TYPE_WIFI_FAILED_TO_OBTAIN_IP_ADDRESS));
180 switch (device_info->security_mode) {
181 case WLAN_SEC_MODE_NONE: /** Security disabled */
182 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Open));
184 case WLAN_SEC_MODE_IEEE8021X: /** EAP */
185 status_txt = g_strdup_printf("%s (%s)", sc(pkg_name, I18N_TYPE_Secured),
186 sc(pkg_name, I18N_TYPE_EAP));
188 case WLAN_SEC_MODE_WEP: /** WEP */
189 case WLAN_SEC_MODE_WPA_PSK: /** WPA-PSK */
190 case WLAN_SEC_MODE_WPA2_PSK: /** WPA2-PSK */
191 if (TRUE == device_info->wps_mode) {
192 status_txt = g_strdup_printf("%s (%s)", sc(pkg_name,
193 I18N_TYPE_Secured), sc(pkg_name, I18N_TYPE_WPS_Available));
195 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Secured));
198 default: /** Unknown */
199 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Unknown));
203 if (true == check_fav) {
204 wifi_ap_is_favorite(device_info->ap, &favorite);
205 if (true == favorite) {
207 status_txt = g_strdup_printf("%s, %s",
208 sc(pkg_name, I18N_TYPE_Saved), temp);
216 void common_utils_get_device_icon(wifi_device_info_t *device_info, char **icon_path)
218 char buf[MAX_DEVICE_ICON_PATH_STR_LEN] = {'\0', };
220 g_strlcat(buf, "A01-3_icon", sizeof(buf));
222 if (device_info->security_mode != WLAN_SEC_MODE_NONE) {
223 g_strlcat(buf, "_lock", sizeof(buf));
226 switch (wlan_manager_get_signal_strength(device_info->rssi)) {
227 case SIGNAL_STRENGTH_TYPE_EXCELLENT:
228 g_strlcat(buf, "_03", sizeof(buf));
230 case SIGNAL_STRENGTH_TYPE_GOOD:
231 g_strlcat(buf, "_02", sizeof(buf));
233 case SIGNAL_STRENGTH_TYPE_WEAK:
234 g_strlcat(buf, "_01", sizeof(buf));
236 case SIGNAL_STRENGTH_TYPE_VERY_WEAK:
237 case SIGNAL_STRENGTH_TYPE_NULL:
239 g_strlcat(buf, "_00", sizeof(buf));
244 *icon_path = g_strdup_printf("%s", buf);
248 char *common_utils_get_rssi_text(const char *str_pkg_name, int rssi)
250 switch (wlan_manager_get_signal_strength(rssi)) {
251 case SIGNAL_STRENGTH_TYPE_EXCELLENT:
252 return g_strdup(sc(str_pkg_name, I18N_TYPE_Excellent));
253 case SIGNAL_STRENGTH_TYPE_GOOD:
254 return g_strdup(sc(str_pkg_name, I18N_TYPE_Good));
256 return g_strdup(sc(str_pkg_name, I18N_TYPE_Weak));
260 void common_utils_set_edit_box_imf_panel_evnt_cb(Elm_Object_Item *item,
261 imf_ctxt_panel_cb_t input_panel_cb, void *user_data)
263 __COMMON_FUNC_ENTER__;
264 common_utils_entry_info_t *entry_info;
265 entry_info = elm_object_item_data_get(item);
270 entry_info->input_panel_cb = input_panel_cb;
271 entry_info->input_panel_cb_data = user_data;
273 Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
274 Ecore_IMF_Context *imf_ctxt = elm_entry_imf_context_get(entry);
275 if (imf_ctxt && entry_info->input_panel_cb) {
276 /* Deleting the previously attached callback */
277 ecore_imf_context_input_panel_event_callback_del(imf_ctxt,
278 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
279 entry_info->input_panel_cb);
280 ecore_imf_context_input_panel_event_callback_add(imf_ctxt,
281 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
282 entry_info->input_panel_cb,
283 entry_info->input_panel_cb_data);
284 DEBUG_LOG(UG_NAME_NORMAL, "set the imf ctxt cbs");
287 __COMMON_FUNC_EXIT__;
291 void common_utils_edit_box_focus_set(Elm_Object_Item *item, Eina_Bool focus_set)
293 __COMMON_FUNC_ENTER__;
298 Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
301 elm_object_focus_set(entry, focus_set);
302 elm_object_focus_allow_set(entry, focus_set);
305 __COMMON_FUNC_EXIT__;
309 void common_utils_edit_box_allow_focus_set(Elm_Object_Item *item,
312 __COMMON_FUNC_ENTER__;
317 Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
318 elm_object_focus_allow_set(entry, focus_set);
320 __COMMON_FUNC_EXIT__;
324 Elm_Object_Item *common_utils_add_2_line_txt_disabled_item(
325 Evas_Object* view_list, const char *style_name,
326 const char *line1_txt, const char *line2_txt)
328 static Elm_Genlist_Item_Class two_line_display_itc;
329 two_line_disp_data_t *two_line_data = NULL;
330 Elm_Object_Item *item = NULL;
332 two_line_display_itc.item_style = style_name;
333 two_line_display_itc.func.text_get = __common_utils_2line_text_get;
334 two_line_display_itc.func.content_get = NULL;
335 two_line_display_itc.func.state_get = NULL;
336 two_line_display_itc.func.del = __common_utils_2line_text_del;
338 two_line_data = g_new0(two_line_disp_data_t, 1);
339 two_line_data->title_str = g_strdup(line1_txt);
340 two_line_data->info_str = g_strdup(line2_txt);
341 SECURE_INFO_LOG(UG_NAME_NORMAL, "title_str = %s info_str = %s",
342 two_line_data->title_str, two_line_data->info_str);
344 item = elm_genlist_item_append(view_list, &two_line_display_itc,
345 two_line_data, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
346 elm_object_item_disabled_set(item, TRUE);
351 char *common_utils_get_list_item_entry_txt(Elm_Object_Item *entry_item)
353 common_utils_entry_info_t *entry_info =
354 (common_utils_entry_info_t *)elm_object_item_data_get(entry_item);
355 if (entry_info == NULL) {
359 DEBUG_LOG(UG_NAME_NORMAL, "entry_info: 0x%x", entry_info);
361 return g_strdup(entry_info->entry_txt);
364 Evas_Object *common_utils_create_layout(Evas_Object *navi_frame)
367 layout = elm_layout_add(navi_frame);
368 elm_layout_theme_set(layout, "layout", "application", "noindicator");
369 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
370 evas_object_show(layout);
375 static void __common_utils_del_popup(void *data, Evas_Object *obj, void *event_info)
377 Evas_Object *popup = (Evas_Object *)data;
378 evas_object_del(popup);
381 Evas_Object *common_utils_show_info_popup(Evas_Object *parent,
382 popup_btn_info_t *popup_data)
384 __COMMON_FUNC_ENTER__;
386 Evas_Object *popup = elm_popup_add(parent);
390 ERROR_LOG(UG_NAME_ERR, "Could not add popup");
394 elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
395 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
397 if (popup_data->title_txt) {
398 txt = evas_textblock_text_utf8_to_markup(NULL, popup_data->title_txt);
399 elm_object_domain_translatable_part_text_set(popup,
400 "title,text", PACKAGE, txt);
404 if (popup_data->info_txt) {
405 elm_object_domain_translatable_text_set(popup, PACKAGE,
406 popup_data->info_txt);
409 if (popup_data->btn1_txt) {
410 Evas_Object *btn_1 = elm_button_add(popup);
411 elm_object_style_set(btn_1, "popup");
412 elm_object_domain_translatable_text_set(btn_1, PACKAGE ,
413 popup_data->btn1_txt );
414 elm_object_part_content_set(popup, "button1", btn_1);
415 if (popup_data->btn1_cb) {
416 evas_object_smart_callback_add(btn_1, "clicked",
417 popup_data->btn1_cb, popup_data->btn1_data);
418 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
419 popup_data->btn1_cb, popup_data->btn1_data);
421 evas_object_smart_callback_add(btn_1, "clicked",
422 __common_utils_del_popup, popup);
423 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
424 __common_utils_del_popup, popup);
428 if (popup_data->btn2_txt) {
429 Evas_Object *btn_2 = elm_button_add(popup);
430 elm_object_style_set(btn_2, "popup");
431 /* This button reference used in case of hidden AP case */
432 popup_data->btn = btn_2;
433 elm_object_domain_translatable_text_set(btn_2, PACKAGE,
434 popup_data->btn2_txt);
435 elm_object_part_content_set(popup, "button2", btn_2);
436 //evas_object_smart_callback_add(btn_2, "clicked", popup_data->btn2_cb, NULL);
437 evas_object_show(popup);
438 if (popup_data->btn2_cb) {
439 evas_object_smart_callback_add(btn_2, "clicked",
440 popup_data->btn2_cb, popup_data->btn2_data);
442 evas_object_smart_callback_add(btn_2, "clicked",
443 __common_utils_del_popup, popup);
447 evas_object_show(popup);
448 elm_object_focus_set(popup, EINA_TRUE);
453 Evas_Object *common_utils_show_info_ok_popup(Evas_Object *win,
454 const char *str_pkg_name, const char *info_txt,
455 Evas_Smart_Cb ok_cb, void *cb_data)
457 popup_btn_info_t popup_data;
459 memset(&popup_data, 0, sizeof(popup_data));
460 popup_data.info_txt = (char *)info_txt;
461 popup_data.btn1_txt = sc(str_pkg_name, I18N_TYPE_Ok);
462 popup_data.btn1_cb = ok_cb;
463 popup_data.btn1_data = cb_data;
465 return common_utils_show_info_popup(win, &popup_data);
468 Evas_Object *common_utils_show_info_timeout_popup(Evas_Object *win,
469 const char* info_text, const double timeout)
471 Evas_Object *popup = elm_popup_add(win);
473 elm_object_text_set(popup, info_text);
474 elm_popup_timeout_set(popup, timeout);
475 evas_object_smart_callback_add(popup, "timeout",
476 __common_utils_del_popup, popup);
477 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
478 elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
479 evas_object_show(popup);
484 int common_utils_get_rotate_angle(enum appcore_rm rotate_mode)
487 if (APPCORE_RM_UNKNOWN == rotate_mode) {
488 appcore_get_rotation_state(&rotate_mode);
491 DEBUG_LOG(SP_NAME_NORMAL, "rotate_mode = %d", rotate_mode);
493 switch (rotate_mode) {
494 case APPCORE_RM_PORTRAIT_NORMAL: /**< Portrait mode */
495 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_NORMAL");
499 case APPCORE_RM_PORTRAIT_REVERSE: /**< Portrait upside down mode */
500 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_REVERSE");
504 case APPCORE_RM_LANDSCAPE_NORMAL: /**< Left handed landscape mode */
505 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_NORMAL");
509 case APPCORE_RM_LANDSCAPE_REVERSE: /**< Right handed landscape mode */
510 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_REVERSE");
515 ERROR_LOG(SP_NAME_ERR, "Invalid rotate mode. The default value (0) is set to 'rotate_angle'.");
523 wlan_security_mode_type_t common_utils_get_sec_mode(wifi_security_type_e sec_type)
526 case WIFI_SECURITY_TYPE_NONE:
527 return WLAN_SEC_MODE_NONE;
528 case WIFI_SECURITY_TYPE_WEP:
529 return WLAN_SEC_MODE_WEP;
530 case WIFI_SECURITY_TYPE_WPA_PSK:
531 return WLAN_SEC_MODE_WPA_PSK;
532 case WIFI_SECURITY_TYPE_WPA2_PSK:
533 return WLAN_SEC_MODE_WPA_PSK;
534 case WIFI_SECURITY_TYPE_EAP:
535 return WLAN_SEC_MODE_IEEE8021X;
537 return WLAN_SEC_MODE_NONE;
540 return WLAN_SEC_MODE_NONE;
543 int common_utils_send_message_to_net_popup(const char *title,
544 const char *content, const char *type, const char *ssid)
547 bundle *b = bundle_create();
549 bundle_add(b, "_SYSPOPUP_TITLE_", title);
550 bundle_add(b, "_SYSPOPUP_CONTENT_", content);
551 bundle_add(b, "_SYSPOPUP_TYPE_", type);
552 bundle_add(b, "_AP_NAME_", ssid);
554 ret = aul_launch_app("net.netpopup", b);
561 int common_util_set_system_registry(const char *key, int value)
563 __COMMON_FUNC_ENTER__;
565 if (vconf_set_int(key, value) < 0) {
566 ERROR_LOG(UG_NAME_NORMAL, "Failed to set vconf");
568 __COMMON_FUNC_EXIT__;
572 __COMMON_FUNC_EXIT__;
576 int common_util_get_system_registry(const char *key)
578 __COMMON_FUNC_ENTER__;
582 if (vconf_get_int(key, &value) < 0) {
583 ERROR_LOG(UG_NAME_NORMAL, "Failed to get vconf");
585 __COMMON_FUNC_EXIT__;
589 __COMMON_FUNC_EXIT__;
593 static void __common_util_managed_idle_destroy_cb(gpointer data)
599 managed_idler_list = g_slist_remove(managed_idler_list, data);
603 static gboolean __common_util_managed_idle_cb(gpointer user_data)
605 struct managed_idle_data *data = (struct managed_idle_data *)user_data;
611 return data->func(data->user_data);
614 guint common_util_managed_idle_add(GSourceFunc func, gpointer user_data)
617 struct managed_idle_data *data;
623 data = g_try_new0(struct managed_idle_data, 1);
629 data->user_data = user_data;
631 id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __common_util_managed_idle_cb,
632 data, __common_util_managed_idle_destroy_cb);
640 managed_idler_list = g_slist_append(managed_idler_list, data);
645 void common_util_managed_idle_cleanup(void)
647 if (managed_idler_list == NULL) {
651 GSList *cur = managed_idler_list;
653 struct managed_idle_data *data;
656 GSList *next = cur->next;
657 data = (struct managed_idle_data *)cur->data;
659 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
661 g_source_destroy(src);
662 cur = managed_idler_list;
668 g_slist_free(managed_idler_list);
669 managed_idler_list = NULL;
672 void common_util_managed_ecore_scan_update_timer_add(double interval,
673 common_util_scan_update_cb callback, void *data)
675 if (callback == NULL)
678 common_util_managed_ecore_scan_update_timer_del();
680 scan_update_timer = ecore_timer_add(interval, callback, data);
683 void common_util_managed_ecore_scan_update_timer_del(void)
685 if (scan_update_timer != NULL) {
686 ecore_timer_del(scan_update_timer);
687 scan_update_timer = NULL;
691 void common_util_manager_ecore_scan_update_timer_reset(void)
693 scan_update_timer = NULL;
696 static GDBusConnection *common_util_get_gdbus_conn(void)
698 GError *error = NULL;
700 if (gdbus_conn.connection != NULL)
701 return gdbus_conn.connection;
703 #if !GLIB_CHECK_VERSION(2,36,0)
707 gdbus_conn.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
708 if (gdbus_conn.connection == NULL) {
709 ERROR_LOG(UG_NAME_NORMAL,
710 "Failed to connect to the D-BUS daemon: [%s]\n", error->message);
715 return gdbus_conn.connection;
718 gboolean common_util_subscribe_scanning_signal(GDBusSignalCallback callback)
720 GDBusConnection *connection;
722 connection = common_util_get_gdbus_conn();
723 if (connection == NULL) {
724 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
728 /* Create supplicant service connection */
729 gdbus_conn.subscribe_id_supplicant = g_dbus_connection_signal_subscribe(
732 SUPPLICANT_SERVICE_INTERFACE,
736 G_DBUS_SIGNAL_FLAGS_NONE,
741 if (gdbus_conn.subscribe_id_supplicant == 0) {
742 ERROR_LOG(UG_NAME_NORMAL, "Failed register signals "
744 gdbus_conn.subscribe_id_supplicant);
751 gboolean common_util_unsubscribe_scanning_signal(void)
753 GDBusConnection *connection;
755 connection = common_util_get_gdbus_conn();
756 if (connection == NULL) {
757 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
761 g_dbus_connection_signal_unsubscribe(connection,
762 gdbus_conn.subscribe_id_supplicant);
764 g_object_unref(gdbus_conn.connection);
765 gdbus_conn.connection = NULL;
770 int common_utils_get_sim_state(void)
772 int value = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
775 value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT);
776 INFO_LOG(UG_NAME_NORMAL, "SIM slot 1 state : %d", value);
777 if (value == VCONFKEY_TELEPHONY_SIM_INSERTED) {
781 sim_count = common_util_get_system_registry(
782 VCONFKEY_TELEPHONY_SIM_SLOT_COUNT);
786 value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT2);
787 INFO_LOG(UG_NAME_NORMAL, "SIM slot 2 state : %d", value);