Fix genlist style
[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 (!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);
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         evas_object_show(layout);
371
372         return layout;
373 }
374
375 static void __common_utils_del_popup(void *data, Evas_Object *obj, void *event_info)
376 {
377         Evas_Object *popup = (Evas_Object *)data;
378         evas_object_del(popup);
379 }
380
381 Evas_Object *common_utils_show_info_popup(Evas_Object *parent,
382                 popup_btn_info_t *popup_data)
383 {
384         __COMMON_FUNC_ENTER__;
385
386         Evas_Object *popup = elm_popup_add(parent);
387         char *txt = NULL;
388
389         if (!popup) {
390                 ERROR_LOG(UG_NAME_ERR, "Could not add popup");
391                 return NULL;
392         }
393
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);
396
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);
401                 g_free(txt);
402         }
403
404         if (popup_data->info_txt) {
405                 elm_object_domain_translatable_text_set(popup, PACKAGE,
406                                 popup_data->info_txt);
407         }
408
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);
420                 } else {
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);
425                 }
426         }
427
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);
441                 } else {
442                         evas_object_smart_callback_add(btn_2, "clicked",
443                                         __common_utils_del_popup, popup);
444                 }
445         }
446
447         evas_object_show(popup);
448         elm_object_focus_set(popup, EINA_TRUE);
449
450         return popup;
451 }
452
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)
456 {
457         popup_btn_info_t popup_data;
458
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;
464
465         return common_utils_show_info_popup(win, &popup_data);
466 }
467
468 Evas_Object *common_utils_show_info_timeout_popup(Evas_Object *win,
469                 const char* info_text, const double timeout)
470 {
471         Evas_Object *popup = elm_popup_add(win);
472
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);
480
481         return popup;
482 }
483
484 int common_utils_get_rotate_angle(enum appcore_rm rotate_mode)
485 {
486         int rotate_angle;
487         if (APPCORE_RM_UNKNOWN == rotate_mode) {
488                 appcore_get_rotation_state(&rotate_mode);
489         }
490
491         DEBUG_LOG(SP_NAME_NORMAL, "rotate_mode = %d", rotate_mode);
492
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");
496                 rotate_angle = 0;
497                 break;
498
499         case APPCORE_RM_PORTRAIT_REVERSE:         /**< Portrait upside down mode */
500                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_REVERSE");
501                 rotate_angle = 180;
502                 break;
503
504         case APPCORE_RM_LANDSCAPE_NORMAL:         /**< Left handed landscape mode */
505                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_NORMAL");
506                 rotate_angle = 270;
507                 break;
508
509         case APPCORE_RM_LANDSCAPE_REVERSE:          /**< Right handed landscape mode */
510                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_REVERSE");
511                 rotate_angle = 90;
512                 break;
513
514         default:
515                 ERROR_LOG(SP_NAME_ERR, "Invalid rotate mode. The default value (0) is set to 'rotate_angle'.");
516                 rotate_angle = 0;
517                 break;
518         }
519
520         return rotate_angle;
521 }
522
523 wlan_security_mode_type_t common_utils_get_sec_mode(wifi_security_type_e sec_type)
524 {
525         switch (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;
536         default:
537                 return WLAN_SEC_MODE_NONE;
538         }
539
540         return WLAN_SEC_MODE_NONE;
541 }
542
543 int common_utils_send_message_to_net_popup(const char *title,
544                 const char *content, const char *type, const char *ssid)
545 {
546         int ret = 0;
547         bundle *b = bundle_create();
548
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);
553
554         ret = aul_launch_app("net.netpopup", b);
555
556         bundle_free(b);
557
558         return ret;
559 }
560
561 int common_util_set_system_registry(const char *key, int value)
562 {
563         __COMMON_FUNC_ENTER__;
564
565         if (vconf_set_int(key, value) < 0) {
566                 ERROR_LOG(UG_NAME_NORMAL, "Failed to set vconf");
567
568                 __COMMON_FUNC_EXIT__;
569                 return -1;
570         }
571
572         __COMMON_FUNC_EXIT__;
573         return 0;
574 }
575
576 int common_util_get_system_registry(const char *key)
577 {
578         __COMMON_FUNC_ENTER__;
579
580         int value = 0;
581
582         if (vconf_get_int(key, &value) < 0) {
583                 ERROR_LOG(UG_NAME_NORMAL, "Failed to get vconf");
584
585                 __COMMON_FUNC_EXIT__;
586                 return -1;
587         }
588
589         __COMMON_FUNC_EXIT__;
590         return value;
591 }
592
593 static void __common_util_managed_idle_destroy_cb(gpointer data)
594 {
595         if (!data) {
596                 return;
597         }
598
599         managed_idler_list = g_slist_remove(managed_idler_list, data);
600         g_free(data);
601 }
602
603 static gboolean __common_util_managed_idle_cb(gpointer user_data)
604 {
605         struct managed_idle_data *data = (struct managed_idle_data *)user_data;
606
607         if (!data) {
608                 return FALSE;
609         }
610
611         return data->func(data->user_data);
612 }
613
614 guint common_util_managed_idle_add(GSourceFunc func, gpointer user_data)
615 {
616         guint id;
617         struct managed_idle_data *data;
618
619         if (!func) {
620                 return 0;
621         }
622
623         data = g_try_new0(struct managed_idle_data, 1);
624         if (!data) {
625                 return 0;
626         }
627
628         data->func = func;
629         data->user_data = user_data;
630
631         id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __common_util_managed_idle_cb,
632                         data, __common_util_managed_idle_destroy_cb);
633         if (!id) {
634                 g_free(data);
635                 return id;
636         }
637
638         data->id = id;
639
640         managed_idler_list = g_slist_append(managed_idler_list, data);
641
642         return id;
643 }
644
645 void common_util_managed_idle_cleanup(void)
646 {
647         if (managed_idler_list == NULL) {
648                 return;
649         }
650
651         GSList *cur = managed_idler_list;
652         GSource *src;
653         struct managed_idle_data *data;
654
655         while (cur) {
656                 GSList *next = cur->next;
657                 data = (struct managed_idle_data *)cur->data;
658
659                 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
660                 if (src) {
661                         g_source_destroy(src);
662                         cur = managed_idler_list;
663                 } else {
664                         cur = next;
665                 }
666         }
667
668         g_slist_free(managed_idler_list);
669         managed_idler_list = NULL;
670 }
671
672 void common_util_managed_ecore_scan_update_timer_add(double interval,
673                 common_util_scan_update_cb callback, void *data)
674 {
675         if (callback == NULL)
676                 return;
677
678         common_util_managed_ecore_scan_update_timer_del();
679
680         scan_update_timer = ecore_timer_add(interval, callback, data);
681 }
682
683 void common_util_managed_ecore_scan_update_timer_del(void)
684 {
685         if (scan_update_timer != NULL) {
686                 ecore_timer_del(scan_update_timer);
687                 scan_update_timer = NULL;
688         }
689 }
690
691 void common_util_manager_ecore_scan_update_timer_reset(void)
692 {
693         scan_update_timer = NULL;
694 }
695
696 static GDBusConnection *common_util_get_gdbus_conn(void)
697 {
698         GError *error = NULL;
699
700         if (gdbus_conn.connection != NULL)
701                 return gdbus_conn.connection;
702
703 #if !GLIB_CHECK_VERSION(2,36,0)
704         g_type_init();
705 #endif
706
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);
711                 g_error_free(error);
712                 return NULL;
713         }
714
715         return gdbus_conn.connection;
716 }
717
718 gboolean common_util_subscribe_scanning_signal(GDBusSignalCallback callback)
719 {
720         GDBusConnection *connection;
721
722         connection = common_util_get_gdbus_conn();
723         if (connection == NULL) {
724                 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
725                 return FALSE;
726         }
727
728         /* Create supplicant service connection */
729         gdbus_conn.subscribe_id_supplicant = g_dbus_connection_signal_subscribe(
730                         connection,
731                         SUPPLICANT_SERVICE,
732                         SUPPLICANT_SERVICE_INTERFACE,
733                         "PropertiesChanged",
734                         NULL,
735                         NULL,
736                         G_DBUS_SIGNAL_FLAGS_NONE,
737                         callback,
738                         NULL,
739                         NULL);
740
741         if (gdbus_conn.subscribe_id_supplicant == 0) {
742                 ERROR_LOG(UG_NAME_NORMAL, "Failed register signals "
743                                 "supplicant(%d)\n",
744                                 gdbus_conn.subscribe_id_supplicant);
745                 return FALSE;
746         }
747
748         return TRUE;
749 }
750
751 gboolean common_util_unsubscribe_scanning_signal(void)
752 {
753         GDBusConnection *connection;
754
755         connection = common_util_get_gdbus_conn();
756         if (connection == NULL) {
757                 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
758                 return FALSE;
759         }
760
761         g_dbus_connection_signal_unsubscribe(connection,
762                         gdbus_conn.subscribe_id_supplicant);
763
764         g_object_unref(gdbus_conn.connection);
765         gdbus_conn.connection = NULL;
766
767         return TRUE;
768 }
769
770 int common_utils_get_sim_state(void)
771 {
772         int value = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
773         int sim_count = 0;
774
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) {
778                 return value;
779         }
780
781         sim_count = common_util_get_system_registry(
782                         VCONFKEY_TELEPHONY_SIM_SLOT_COUNT);
783         if (sim_count == 1)
784                 return value;
785
786         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT2);
787         INFO_LOG(UG_NAME_NORMAL, "SIM slot 2 state : %d", value);
788
789         return value;
790 }