793ba463c1301a7b70ec731f1a75aed13c1b346f
[apps/native/ug-wifi-efl.git] / sources / libraries / Common / common_utils.c
1 /*
2  * Wi-Fi
3  *
4  * Copyright 2012 Samsung Electronics Co., Ltd
5  *
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
9  *
10  * http://www.tizenopensource.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 <aul.h>
22 #include <ui-gadget-module.h>
23 #include <bundle_internal.h>
24 #include <efl_extension.h>
25
26 #include "common.h"
27 #include "ug_wifi.h"
28 #include "common_utils.h"
29 #include "i18nmanager.h"
30
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"
35
36 typedef struct {
37         char *title_str;
38         char *info_str;
39 } two_line_disp_data_t;
40
41 struct managed_idle_data {
42         GSourceFunc func;
43         gpointer user_data;
44         guint id;
45 };
46
47 struct gdbus_connection_data {
48         GDBusConnection *connection;
49         guint subscribe_id_supplicant;
50 };
51
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;
60
61 static char *__common_utils_2line_text_get(void *data, Evas_Object *obj, const char *part)
62 {
63         two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
64         if (!g_strcmp0(part, "elm.text.sub.left.bottom")) {
65                 return g_strdup(item_data->info_str);
66         } else if (!g_strcmp0(part, "elm.text.main.left.top")) {
67                 return g_strdup(item_data->title_str);
68         }
69         return NULL;
70 }
71
72 static void __common_utils_2line_text_del(void *data, Evas_Object *obj)
73 {
74         two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
75         if (item_data) {
76                 g_free(item_data->info_str);
77                 g_free(item_data->title_str);
78                 g_free(item_data);
79         }
80 }
81
82 static void __common_utils_separator_del(void *data, Evas_Object *obj)
83 {
84         elm_genlist_item_class_free(data);
85         return;
86 }
87
88 Eina_Bool common_utils_is_portrait_mode(void)
89 {
90         return is_portrait_mode;
91 }
92
93 static void __common_utils_set_portrait_mode(Eina_Bool on)
94 {
95         is_portrait_mode = on;
96 }
97
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)
100 {
101         rotate_cb = func;
102         rotate_cb_data = data;
103         is_wps = wps_value;
104         is_setting = setting_value;
105 }
106
107 static void __common_utils_rotate_popup(enum appcore_rm rotmode)
108 {
109         if (rotate_cb) {
110                 rotate_cb(rotmode, rotate_cb_data, is_wps, is_setting);
111         }
112 }
113
114 void common_utils_contents_rotation_adjust(int event)
115 {
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);
120         } else {
121                 __common_utils_rotate_popup(APPCORE_RM_LANDSCAPE_NORMAL);
122                 __common_utils_set_portrait_mode(EINA_FALSE);
123         }
124 }
125
126 Elm_Object_Item* common_utils_add_dialogue_separator(Evas_Object* genlist, const char *separator_style)
127 {
128         assertm_if(NULL == genlist, "NULL!!");
129
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;
137
138         Elm_Object_Item* sep = elm_genlist_item_append(
139                                         genlist,
140                                         separator_itc,
141                                         NULL,
142                                         NULL,
143                                         ELM_GENLIST_ITEM_GROUP,
144                                         NULL,
145                                         NULL);
146
147         assertm_if(NULL == sep, "NULL!!");
148
149         elm_genlist_item_select_mode_set(sep, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
150
151         return sep;
152 }
153
154 char *common_utils_get_ap_security_type_info_txt(const char *pkg_name,
155                 wifi_device_info_t *device_info, bool check_fav)
156 {
157         bool favorite = false;
158         char *temp = NULL;
159         char *status_txt = NULL;
160
161         /* TODO: temporary disabled */
162         /*
163         wifi_connection_state_e state;
164         wifi_connection_error_e error;
165
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);
169
170                 if (error == WIFI_CONNECTION_ERROR_INVALID_KEY)
171                         return g_strdup(sc(pkg_name,
172                                         I18N_TYPE_WIFI_AUTHENTICATION_ERROR_OCCURRED));
173
174                 if (error == WIFI_CONNECTION_ERROR_DHCP_FAILED)
175                         return g_strdup(sc(pkg_name,
176                                         I18N_TYPE_WIFI_FAILED_TO_OBTAIN_IP_ADDRESS));
177         }
178         */
179
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));
183                 break;
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));
187                 break;
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));
194                 } else {
195                         status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Secured));
196                 }
197                 break;
198         default:                                                /** Unknown */
199                 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Unknown));
200                 break;
201         }
202
203         if (true == check_fav) {
204                 wifi_ap_is_favorite(device_info->ap, &favorite);
205                 if (true == favorite) {
206                         temp = status_txt;
207                         status_txt = g_strdup_printf("%s, %s",
208                                         sc(pkg_name, I18N_TYPE_Saved), temp);
209                         g_free(temp);
210                 }
211         }
212
213         return status_txt;
214 }
215
216 void common_utils_get_device_icon(wifi_device_info_t *device_info, char **icon_path)
217 {
218         char buf[MAX_DEVICE_ICON_PATH_STR_LEN] = {'\0', };
219
220         g_strlcat(buf, "A01-3_icon", sizeof(buf));
221
222         if (device_info->security_mode != WLAN_SEC_MODE_NONE) {
223                 g_strlcat(buf, "_lock", sizeof(buf));
224         }
225
226         switch (wlan_manager_get_signal_strength(device_info->rssi)) {
227         case SIGNAL_STRENGTH_TYPE_EXCELLENT:
228                 g_strlcat(buf, "_03", sizeof(buf));
229                 break;
230         case SIGNAL_STRENGTH_TYPE_GOOD:
231                 g_strlcat(buf, "_02", sizeof(buf));
232                 break;
233         case SIGNAL_STRENGTH_TYPE_WEAK:
234                 g_strlcat(buf, "_01", sizeof(buf));
235                 break;
236         case SIGNAL_STRENGTH_TYPE_VERY_WEAK:
237         case SIGNAL_STRENGTH_TYPE_NULL:
238         default:
239                 g_strlcat(buf, "_00", sizeof(buf));
240                 break;
241         }
242
243         if (icon_path) {
244                 *icon_path = g_strdup_printf("%s", buf);
245         }
246 }
247
248 char *common_utils_get_rssi_text(const char *str_pkg_name, int rssi)
249 {
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));
255         default:
256                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Weak));
257         }
258 }
259
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)
262 {
263         __COMMON_FUNC_ENTER__;
264         common_utils_entry_info_t *entry_info;
265         entry_info = elm_object_item_data_get(item);
266         if (!entry_info) {
267                 return;
268         }
269
270         entry_info->input_panel_cb = input_panel_cb;
271         entry_info->input_panel_cb_data = user_data;
272
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");
285         }
286
287         __COMMON_FUNC_EXIT__;
288         return;
289 }
290
291 void common_utils_edit_box_focus_set(Elm_Object_Item *item, Eina_Bool focus_set)
292 {
293         __COMMON_FUNC_ENTER__;
294         if (!item) {
295                 return;
296         }
297
298         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
299
300         if (entry) {
301                 elm_object_focus_set(entry, focus_set);
302                 elm_object_focus_allow_set(entry, focus_set);
303         }
304
305         __COMMON_FUNC_EXIT__;
306         return;
307 }
308
309 void common_utils_edit_box_allow_focus_set(Elm_Object_Item *item,
310                 Eina_Bool focus_set)
311 {
312         __COMMON_FUNC_ENTER__;
313         if (!item) {
314                 return;
315         }
316
317         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
318         elm_object_focus_allow_set(entry, focus_set);
319
320         __COMMON_FUNC_EXIT__;
321         return;
322 }
323
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)
327 {
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;
331
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;
337
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);
343
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);
347
348         return item;
349 }
350
351 char *common_utils_get_list_item_entry_txt(Elm_Object_Item *entry_item)
352 {
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) {
356                 return NULL;
357         }
358
359         DEBUG_LOG(UG_NAME_NORMAL, "entry_info: 0x%x", entry_info);
360
361         return g_strdup(entry_info->entry_txt);
362 }
363
364 Evas_Object *common_utils_create_layout(Evas_Object *navi_frame)
365 {
366         Evas_Object *layout;
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
371         Evas_Object* bg = elm_bg_add(layout);
372         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
373         elm_object_style_set(bg, "group_list");
374         elm_object_part_content_set(layout, "elm.swallow.bg", bg);
375
376         evas_object_show(layout);
377
378         return layout;
379 }
380
381 static void __common_utils_del_popup(void *data, Evas_Object *obj, void *event_info)
382 {
383         Evas_Object *popup = (Evas_Object *)data;
384         evas_object_del(popup);
385 }
386
387 Evas_Object *common_utils_show_info_popup(Evas_Object *parent,
388                 popup_btn_info_t *popup_data)
389 {
390         __COMMON_FUNC_ENTER__;
391
392         Evas_Object *popup = elm_popup_add(parent);
393         char *txt = NULL;
394
395         if (!popup) {
396                 ERROR_LOG(UG_NAME_ERR, "Could not add popup");
397                 return NULL;
398         }
399
400         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
401         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
402
403         if (popup_data->title_txt) {
404                 txt = evas_textblock_text_utf8_to_markup(NULL, popup_data->title_txt);
405                 elm_object_domain_translatable_part_text_set(popup,
406                                 "title,text", PACKAGE, txt);
407                 g_free(txt);
408         }
409
410         if (popup_data->info_txt) {
411                 elm_object_domain_translatable_text_set(popup, PACKAGE,
412                                 popup_data->info_txt);
413         }
414
415         if (popup_data->btn1_txt) {
416                 Evas_Object *btn_1 = elm_button_add(popup);
417                 elm_object_style_set(btn_1, "popup");
418                 elm_object_domain_translatable_text_set(btn_1, PACKAGE ,
419                                 popup_data->btn1_txt );
420                 elm_object_part_content_set(popup, "button1", btn_1);
421                 if (popup_data->btn1_cb) {
422                         evas_object_smart_callback_add(btn_1, "clicked",
423                                         popup_data->btn1_cb, popup_data->btn1_data);
424                         eext_object_event_callback_add(popup, EA_CALLBACK_BACK,
425                                         popup_data->btn1_cb, popup_data->btn1_data);
426                 } else {
427                         evas_object_smart_callback_add(btn_1, "clicked",
428                                         __common_utils_del_popup, popup);
429                         eext_object_event_callback_add(popup, EA_CALLBACK_BACK,
430                                         __common_utils_del_popup, popup);
431                 }
432         }
433
434         if (popup_data->btn2_txt) {
435                 Evas_Object *btn_2 = elm_button_add(popup);
436                 elm_object_style_set(btn_2, "popup");
437                 /* This button reference used in case of hidden AP case */
438                 popup_data->btn = btn_2;
439                 elm_object_domain_translatable_text_set(btn_2, PACKAGE,
440                                 popup_data->btn2_txt);
441                 elm_object_part_content_set(popup, "button2", btn_2);
442                 //evas_object_smart_callback_add(btn_2, "clicked", popup_data->btn2_cb, NULL);
443                 evas_object_show(popup);
444                 if (popup_data->btn2_cb) {
445                         evas_object_smart_callback_add(btn_2, "clicked",
446                                         popup_data->btn2_cb, popup_data->btn2_data);
447                 } else {
448                         evas_object_smart_callback_add(btn_2, "clicked",
449                                         __common_utils_del_popup, popup);
450                 }
451         }
452
453         evas_object_show(popup);
454         elm_object_focus_set(popup, EINA_TRUE);
455
456         return popup;
457 }
458
459 Evas_Object *common_utils_show_info_ok_popup(Evas_Object *win,
460                 const char *str_pkg_name, const char *info_txt,
461                 Evas_Smart_Cb ok_cb, void *cb_data)
462 {
463         popup_btn_info_t popup_data;
464
465         memset(&popup_data, 0, sizeof(popup_data));
466         popup_data.info_txt = (char *)info_txt;
467         popup_data.btn1_txt = sc(str_pkg_name, I18N_TYPE_Ok);
468         popup_data.btn1_cb = ok_cb;
469         popup_data.btn1_data = cb_data;
470
471         return common_utils_show_info_popup(win, &popup_data);
472 }
473
474 Evas_Object *common_utils_show_info_timeout_popup(Evas_Object *win,
475                 const char* info_text, const double timeout)
476 {
477         Evas_Object *popup = elm_popup_add(win);
478
479         elm_object_text_set(popup, info_text);
480         elm_popup_timeout_set(popup, timeout);
481         evas_object_smart_callback_add(popup, "timeout",
482                         __common_utils_del_popup, popup);
483         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
484         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
485         evas_object_show(popup);
486
487         return popup;
488 }
489
490 int common_utils_get_rotate_angle(enum appcore_rm rotate_mode)
491 {
492         int rotate_angle;
493         if (APPCORE_RM_UNKNOWN == rotate_mode) {
494                 appcore_get_rotation_state(&rotate_mode);
495         }
496
497         DEBUG_LOG(SP_NAME_NORMAL, "rotate_mode = %d", rotate_mode);
498
499         switch (rotate_mode) {
500         case APPCORE_RM_PORTRAIT_NORMAL:         /**< Portrait mode */
501                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_NORMAL");
502                 rotate_angle = 0;
503                 break;
504
505         case APPCORE_RM_PORTRAIT_REVERSE:         /**< Portrait upside down mode */
506                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_REVERSE");
507                 rotate_angle = 180;
508                 break;
509
510         case APPCORE_RM_LANDSCAPE_NORMAL:         /**< Left handed landscape mode */
511                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_NORMAL");
512                 rotate_angle = 270;
513                 break;
514
515         case APPCORE_RM_LANDSCAPE_REVERSE:          /**< Right handed landscape mode */
516                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_REVERSE");
517                 rotate_angle = 90;
518                 break;
519
520         default:
521                 ERROR_LOG(SP_NAME_ERR, "Invalid rotate mode. The default value (0) is set to 'rotate_angle'.");
522                 rotate_angle = 0;
523                 break;
524         }
525
526         return rotate_angle;
527 }
528
529 wlan_security_mode_type_t common_utils_get_sec_mode(wifi_security_type_e sec_type)
530 {
531         switch (sec_type) {
532         case WIFI_SECURITY_TYPE_NONE:
533                 return WLAN_SEC_MODE_NONE;
534         case WIFI_SECURITY_TYPE_WEP:
535                 return WLAN_SEC_MODE_WEP;
536         case WIFI_SECURITY_TYPE_WPA_PSK:
537                 return WLAN_SEC_MODE_WPA_PSK;
538         case WIFI_SECURITY_TYPE_WPA2_PSK:
539                 return WLAN_SEC_MODE_WPA_PSK;
540         case WIFI_SECURITY_TYPE_EAP:
541                 return WLAN_SEC_MODE_IEEE8021X;
542         default:
543                 return WLAN_SEC_MODE_NONE;
544         }
545
546         return WLAN_SEC_MODE_NONE;
547 }
548
549 int common_utils_send_message_to_net_popup(const char *title,
550                 const char *content, const char *type, const char *ssid)
551 {
552         int ret = 0;
553         bundle *b = bundle_create();
554
555         bundle_add(b, "_SYSPOPUP_TITLE_", title);
556         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
557         bundle_add(b, "_SYSPOPUP_TYPE_", type);
558         bundle_add(b, "_AP_NAME_", ssid);
559
560         ret = aul_launch_app("net.netpopup", b);
561
562         bundle_free(b);
563
564         return ret;
565 }
566
567 int common_util_set_system_registry(const char *key, int value)
568 {
569         __COMMON_FUNC_ENTER__;
570
571         if (vconf_set_int(key, value) < 0) {
572                 ERROR_LOG(UG_NAME_NORMAL, "Failed to set vconf");
573
574                 __COMMON_FUNC_EXIT__;
575                 return -1;
576         }
577
578         __COMMON_FUNC_EXIT__;
579         return 0;
580 }
581
582 int common_util_get_system_registry(const char *key)
583 {
584         __COMMON_FUNC_ENTER__;
585
586         int value = 0;
587
588         if (vconf_get_int(key, &value) < 0) {
589                 ERROR_LOG(UG_NAME_NORMAL, "Failed to get vconf");
590
591                 __COMMON_FUNC_EXIT__;
592                 return -1;
593         }
594
595         __COMMON_FUNC_EXIT__;
596         return value;
597 }
598
599 static void __common_util_managed_idle_destroy_cb(gpointer data)
600 {
601         if (!data) {
602                 return;
603         }
604
605         managed_idler_list = g_slist_remove(managed_idler_list, data);
606         g_free(data);
607 }
608
609 static gboolean __common_util_managed_idle_cb(gpointer user_data)
610 {
611         struct managed_idle_data *data = (struct managed_idle_data *)user_data;
612
613         if (!data) {
614                 return FALSE;
615         }
616
617         return data->func(data->user_data);
618 }
619
620 guint common_util_managed_idle_add(GSourceFunc func, gpointer user_data)
621 {
622         guint id;
623         struct managed_idle_data *data;
624
625         if (!func) {
626                 return 0;
627         }
628
629         data = g_try_new0(struct managed_idle_data, 1);
630         if (!data) {
631                 return 0;
632         }
633
634         data->func = func;
635         data->user_data = user_data;
636
637         id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __common_util_managed_idle_cb,
638                         data, __common_util_managed_idle_destroy_cb);
639         if (!id) {
640                 g_free(data);
641                 return id;
642         }
643
644         data->id = id;
645
646         managed_idler_list = g_slist_append(managed_idler_list, data);
647
648         return id;
649 }
650
651 void common_util_managed_idle_cleanup(void)
652 {
653         if (managed_idler_list == NULL) {
654                 return;
655         }
656
657         GSList *cur = managed_idler_list;
658         GSource *src;
659         struct managed_idle_data *data;
660
661         while (cur) {
662                 GSList *next = cur->next;
663                 data = (struct managed_idle_data *)cur->data;
664
665                 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
666                 if (src) {
667                         g_source_destroy(src);
668                         cur = managed_idler_list;
669                 } else {
670                         cur = next;
671                 }
672         }
673
674         g_slist_free(managed_idler_list);
675         managed_idler_list = NULL;
676 }
677
678 void common_util_managed_ecore_scan_update_timer_add(double interval,
679                 common_util_scan_update_cb callback, void *data)
680 {
681         if (callback == NULL)
682                 return;
683
684         common_util_managed_ecore_scan_update_timer_del();
685
686         scan_update_timer = ecore_timer_add(interval, callback, data);
687 }
688
689 void common_util_managed_ecore_scan_update_timer_del(void)
690 {
691         if (scan_update_timer != NULL) {
692                 ecore_timer_del(scan_update_timer);
693                 scan_update_timer = NULL;
694         }
695 }
696
697 void common_util_manager_ecore_scan_update_timer_reset(void)
698 {
699         scan_update_timer = NULL;
700 }
701
702 static GDBusConnection *common_util_get_gdbus_conn(void)
703 {
704         GError *error = NULL;
705
706         if (gdbus_conn.connection != NULL)
707                 return gdbus_conn.connection;
708
709 #if !GLIB_CHECK_VERSION(2,36,0)
710         g_type_init();
711 #endif
712
713         gdbus_conn.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
714         if (gdbus_conn.connection == NULL) {
715                 ERROR_LOG(UG_NAME_NORMAL,
716                                 "Failed to connect to the D-BUS daemon: [%s]\n", error->message);
717                 g_error_free(error);
718                 return NULL;
719         }
720
721         return gdbus_conn.connection;
722 }
723
724 gboolean common_util_subscribe_scanning_signal(GDBusSignalCallback callback)
725 {
726         GDBusConnection *connection;
727
728         connection = common_util_get_gdbus_conn();
729         if (connection == NULL) {
730                 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
731                 return FALSE;
732         }
733
734         /* Create supplicant service connection */
735         gdbus_conn.subscribe_id_supplicant = g_dbus_connection_signal_subscribe(
736                         connection,
737                         SUPPLICANT_SERVICE,
738                         SUPPLICANT_SERVICE_INTERFACE,
739                         "PropertiesChanged",
740                         NULL,
741                         NULL,
742                         G_DBUS_SIGNAL_FLAGS_NONE,
743                         callback,
744                         NULL,
745                         NULL);
746
747         if (gdbus_conn.subscribe_id_supplicant == 0) {
748                 ERROR_LOG(UG_NAME_NORMAL, "Failed register signals "
749                                 "supplicant(%d)\n",
750                                 gdbus_conn.subscribe_id_supplicant);
751                 return FALSE;
752         }
753
754         return TRUE;
755 }
756
757 gboolean common_util_unsubscribe_scanning_signal(void)
758 {
759         GDBusConnection *connection;
760
761         connection = common_util_get_gdbus_conn();
762         if (connection == NULL) {
763                 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
764                 return FALSE;
765         }
766
767         g_dbus_connection_signal_unsubscribe(connection,
768                         gdbus_conn.subscribe_id_supplicant);
769
770         g_object_unref(gdbus_conn.connection);
771         gdbus_conn.connection = NULL;
772
773         return TRUE;
774 }
775
776 int common_utils_get_sim_state(void)
777 {
778         int value = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
779         int sim_count = 0;
780
781         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT);
782         INFO_LOG(UG_NAME_NORMAL, "SIM slot 1 state : %d", value);
783         if (value == VCONFKEY_TELEPHONY_SIM_INSERTED) {
784                 return value;
785         }
786
787         sim_count = common_util_get_system_registry(
788                         VCONFKEY_TELEPHONY_SIM_SLOT_COUNT);
789         if (sim_count == 1)
790                 return value;
791
792         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT2);
793         INFO_LOG(UG_NAME_NORMAL, "SIM slot 2 state : %d", value);
794
795         return value;
796 }