Fixed SVACE issues and Code cleanup
[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.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://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 #include <libxml/xmlmemory.h>
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28
29 #include "common.h"
30 #include "ug_wifi.h"
31 #include "common_utils.h"
32 #include "i18nmanager.h"
33
34 #define SUPPLICANT_SERVICE                              "fi.w1.wpa_supplicant1"
35 #define SUPPLICANT_SERVICE_INTERFACE    SUPPLICANT_SERVICE ".Interface"
36 #define COLOR_TABLE "/usr/apps/wifi-efl-ug/shared/res/tables/ug-wifi-efl_ChangeableColorTable.xml"
37 #define FONT_TABLE "/usr/apps/wifi-efl-ug/shared/res/tables/ug-wifi-efl_FontInfoTable.xml"
38
39 #define MODEL_CONFIG_FILE "/etc/config/model-config.xml"
40 #define TYPE_FIELD "string"
41 #define FEATURE_TAG "platform"
42 #define MODEL_CONFIG_TAG "model-config"
43
44 typedef enum {
45         TIZEN_MODEL_UNKNOWN = 0,
46         TIZEN_MODEL_EMULATOR = 0x1,
47         TIZEN_MODEL_NOT_EMULATOR = 0x2,
48 } tizen_model_t;
49
50 static tizen_model_t emulator = TIZEN_MODEL_UNKNOWN;
51
52 typedef struct {
53         char *title_str;
54         char *info_str;
55 } two_line_disp_data_t;
56
57 struct managed_idle_data {
58         GSourceFunc func;
59         gpointer user_data;
60         guint id;
61 };
62
63 struct gdbus_connection_data {
64         GDBusConnection *connection;
65         guint subscribe_id_supplicant;
66 };
67
68 static struct gdbus_connection_data gdbus_conn = { NULL, 0 };
69 static GSList *managed_idler_list = NULL;
70 static int (*rotate_cb)(enum appcore_rm, void*, Eina_Bool, Eina_Bool) = NULL;
71 static void *rotate_cb_data = NULL;
72 static Eina_Bool is_wps = EINA_FALSE;
73 static Eina_Bool is_setting = EINA_FALSE;
74 static Eina_Bool is_portrait_mode = EINA_TRUE;
75 static Ecore_Timer *scan_update_timer = NULL;
76
77 static char *__common_utils_2line_text_get(void *data, Evas_Object *obj, const char *part)
78 {
79         two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
80         if (!strcmp("elm.text", part))
81                 return g_strdup(item_data->title_str);
82         else if (!strcmp("elm.text.sub", part))
83                 return g_strdup(item_data->info_str);
84
85         return NULL;
86 }
87
88 static void __common_utils_2line_text_del(void *data, Evas_Object *obj)
89 {
90         two_line_disp_data_t *item_data = (two_line_disp_data_t *)data;
91         if (item_data) {
92                 g_free(item_data->info_str);
93                 g_free(item_data->title_str);
94                 g_free(item_data);
95         }
96 }
97
98 static void __common_utils_separator_del(void *data, Evas_Object *obj)
99 {
100         elm_genlist_item_class_free(data);
101         return;
102 }
103
104 Eina_Bool common_utils_is_portrait_mode(void)
105 {
106         return is_portrait_mode;
107 }
108
109 static void __common_utils_set_portrait_mode(Eina_Bool on)
110 {
111         is_portrait_mode = on;
112 }
113
114 void common_utils_set_rotate_cb(int (*func)(enum appcore_rm, void*, Eina_Bool, Eina_Bool),
115                 void *data, Eina_Bool wps_value, Eina_Bool setting_value)
116 {
117         rotate_cb = func;
118         rotate_cb_data = data;
119         is_wps = wps_value;
120         is_setting = setting_value;
121 }
122
123 static void __common_utils_rotate_popup(enum appcore_rm rotmode)
124 {
125         if (rotate_cb)
126                 rotate_cb(rotmode, rotate_cb_data, is_wps, is_setting);
127 }
128
129 void common_utils_contents_rotation_adjust(int event)
130 {
131         if (event == UG_EVENT_ROTATE_PORTRAIT ||
132                         event == UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN) {
133                 __common_utils_rotate_popup(APPCORE_RM_PORTRAIT_NORMAL);
134                 __common_utils_set_portrait_mode(EINA_TRUE);
135         } else {
136                 __common_utils_rotate_popup(APPCORE_RM_LANDSCAPE_NORMAL);
137                 __common_utils_set_portrait_mode(EINA_FALSE);
138         }
139 }
140
141 Elm_Object_Item* common_utils_add_dialogue_separator(Evas_Object* genlist, const char *separator_style)
142 {
143         assertm_if(NULL == genlist, "NULL!!");
144
145         static Elm_Genlist_Item_Class *separator_itc;
146         separator_itc = elm_genlist_item_class_new();
147         separator_itc->item_style = separator_style;
148         separator_itc->func.text_get = NULL;
149         separator_itc->func.content_get = NULL;
150         separator_itc->func.state_get = NULL;
151         separator_itc->func.del = __common_utils_separator_del;
152
153         Elm_Object_Item* sep = elm_genlist_item_append(
154                                         genlist,
155                                         separator_itc,
156                                         NULL,
157                                         NULL,
158                                         ELM_GENLIST_ITEM_GROUP,
159                                         NULL,
160                                         NULL);
161
162         assertm_if(NULL == sep, "NULL!!");
163
164         elm_genlist_item_select_mode_set(sep, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
165
166         return sep;
167 }
168
169 char *common_utils_get_ap_security_type_info_txt(const char *pkg_name,
170                 wifi_device_info_t *device_info, bool check_fav)
171 {
172         bool favorite = false;
173         char *temp = NULL;
174         char *status_txt = NULL;
175
176         /* TODO: temporary disabled */
177         /*
178         wifi_manager_connection_state_e state;
179         wifi_connection_error_e error;
180
181         wifi_manager_ap_get_connection_state(device_info->ap, &state);
182         if (state == WIFI_MANAGER_CONNECTION_STATE_FAILURE) {
183                 error = wlan_manager_get_connection_error_state(device_info->ap);
184
185                 if (error == WIFI_MANAGER_CONNECTION_ERROR_INVALID_KEY)
186                         return g_strdup(sc(pkg_name,
187                                         I18N_TYPE_WIFI_AUTHENTICATION_ERROR_OCCURRED));
188
189                 if (error == WIFI_MANAGER_CONNECTION_ERROR_DHCP_FAILED)
190                         return g_strdup(sc(pkg_name,
191                                         I18N_TYPE_WIFI_FAILED_TO_OBTAIN_IP_ADDRESS));
192         }
193         */
194
195         switch (device_info->security_mode) {
196         case WLAN_SEC_MODE_NONE:                /** Security disabled */
197                 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Open));
198                 break;
199         case WLAN_SEC_MODE_IEEE8021X:   /** EAP */
200                 status_txt = g_strdup_printf("%s (%s)", sc(pkg_name, I18N_TYPE_Secured),
201                                 sc(pkg_name, I18N_TYPE_EAP));
202                 break;
203         case WLAN_SEC_MODE_WEP:                 /** WEP */
204         case WLAN_SEC_MODE_WPA_PSK:             /** WPA-PSK */
205         case WLAN_SEC_MODE_WPA2_PSK:    /** WPA2-PSK */
206                 if (TRUE == device_info->wps_mode) {
207                         status_txt = g_strdup_printf("%s (%s)", sc(pkg_name,
208                                         I18N_TYPE_Secured), sc(pkg_name, I18N_TYPE_WPS_Available));
209                 } else {
210                         status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Secured));
211                 }
212                 break;
213         default:                                                /** Unknown */
214                 status_txt = g_strdup(sc(pkg_name, I18N_TYPE_Unknown));
215                 break;
216         }
217
218         if (true == check_fav) {
219                 wifi_manager_ap_is_favorite(device_info->ap, &favorite);
220                 if (true == favorite) {
221                         temp = status_txt;
222                         status_txt = g_strdup_printf("%s, %s",
223                                         sc(pkg_name, I18N_TYPE_Saved), temp);
224                         g_free(temp);
225                 }
226         }
227
228         return status_txt;
229 }
230
231 void common_utils_get_device_icon(wifi_device_info_t *device_info, char **icon_path)
232 {
233         char buf[MAX_DEVICE_ICON_PATH_STR_LEN] = {'\0', };
234
235         g_strlcat(buf, "A01-3_icon", sizeof(buf));
236
237         if (device_info->security_mode != WLAN_SEC_MODE_NONE)
238                 g_strlcat(buf, "_lock", sizeof(buf));
239
240         switch (wlan_manager_get_signal_strength(device_info->rssi)) {
241         case SIGNAL_STRENGTH_TYPE_EXCELLENT:
242                 g_strlcat(buf, "_03", sizeof(buf));
243                 break;
244         case SIGNAL_STRENGTH_TYPE_GOOD:
245                 g_strlcat(buf, "_02", sizeof(buf));
246                 break;
247         case SIGNAL_STRENGTH_TYPE_WEAK:
248                 g_strlcat(buf, "_01", sizeof(buf));
249                 break;
250         case SIGNAL_STRENGTH_TYPE_VERY_WEAK:
251         case SIGNAL_STRENGTH_TYPE_NULL:
252         default:
253                 g_strlcat(buf, "_00", sizeof(buf));
254                 break;
255         }
256
257         if (icon_path)
258                 *icon_path = g_strdup_printf("%s", buf);
259 }
260
261 char *common_utils_get_rssi_text(const char *str_pkg_name, int rssi)
262 {
263         switch (wlan_manager_get_signal_strength(rssi)) {
264         case SIGNAL_STRENGTH_TYPE_EXCELLENT:
265                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Excellent));
266         case SIGNAL_STRENGTH_TYPE_GOOD:
267                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Good));
268         default:
269                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Weak));
270         }
271 }
272
273 void common_utils_set_edit_box_imf_panel_evnt_cb(Elm_Object_Item *item,
274                                                 imf_ctxt_panel_cb_t input_panel_cb, void *user_data)
275 {
276         __COMMON_FUNC_ENTER__;
277         common_utils_entry_info_t *entry_info;
278         entry_info = elm_object_item_data_get(item);
279         if (!entry_info)
280                 return;
281
282         entry_info->input_panel_cb = input_panel_cb;
283         entry_info->input_panel_cb_data = user_data;
284
285         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
286         Ecore_IMF_Context *imf_ctxt = elm_entry_imf_context_get(entry);
287         if (imf_ctxt && entry_info->input_panel_cb) {
288                 /* Deleting the previously attached callback */
289                 ecore_imf_context_input_panel_event_callback_del(imf_ctxt,
290                                 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
291                                 entry_info->input_panel_cb);
292                 ecore_imf_context_input_panel_event_callback_add(imf_ctxt,
293                                 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
294                                 entry_info->input_panel_cb,
295                                 entry_info->input_panel_cb_data);
296                 DEBUG_LOG(UG_NAME_NORMAL, "set the imf ctxt cbs");
297         }
298
299         __COMMON_FUNC_EXIT__;
300         return;
301 }
302
303 void common_utils_edit_box_focus_set(Elm_Object_Item *item, Eina_Bool focus_set)
304 {
305         __COMMON_FUNC_ENTER__;
306         if (!item)
307                 return;
308
309         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
310
311         if (entry) {
312                 elm_object_focus_set(entry, focus_set);
313                 elm_object_focus_allow_set(entry, focus_set);
314         }
315
316         __COMMON_FUNC_EXIT__;
317         return;
318 }
319
320 void common_utils_edit_box_allow_focus_set(Elm_Object_Item *item,
321                 Eina_Bool focus_set)
322 {
323         __COMMON_FUNC_ENTER__;
324         if (!item)
325                 return;
326
327         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
328         elm_object_focus_allow_set(entry, focus_set);
329
330         __COMMON_FUNC_EXIT__;
331         return;
332 }
333
334 Elm_Object_Item *common_utils_add_2_line_txt_disabled_item(
335                 Evas_Object* view_list, const char *style_name,
336                 const char *line1_txt, const char *line2_txt)
337 {
338         static Elm_Genlist_Item_Class two_line_display_itc;
339         two_line_disp_data_t *two_line_data = NULL;
340         Elm_Object_Item *item = NULL;
341
342         two_line_display_itc.item_style = style_name;
343         two_line_display_itc.func.text_get = __common_utils_2line_text_get;
344         two_line_display_itc.func.content_get = NULL;
345         two_line_display_itc.func.state_get = NULL;
346         two_line_display_itc.func.del = __common_utils_2line_text_del;
347
348         two_line_data = g_new0(two_line_disp_data_t, 1);
349         two_line_data->title_str = g_strdup(line1_txt);
350         two_line_data->info_str = g_strdup(line2_txt);
351         SECURE_INFO_LOG(UG_NAME_NORMAL, "title_str = %s info_str = %s",
352                         two_line_data->title_str, two_line_data->info_str);
353
354         item = elm_genlist_item_append(view_list, &two_line_display_itc,
355                         two_line_data, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
356         elm_object_item_disabled_set(item, TRUE);
357
358         return item;
359 }
360
361 char *common_utils_get_list_item_entry_txt(Elm_Object_Item *entry_item)
362 {
363         common_utils_entry_info_t *entry_info =
364                         (common_utils_entry_info_t *)elm_object_item_data_get(entry_item);
365         if (entry_info == NULL)
366                 return NULL;
367
368         DEBUG_LOG(UG_NAME_NORMAL, "entry_info: 0x%x", entry_info);
369
370         return g_strdup(entry_info->entry_txt);
371 }
372
373 Evas_Object *common_utils_create_layout(Evas_Object *navi_frame)
374 {
375         Evas_Object *layout;
376         layout = elm_layout_add(navi_frame);
377         elm_layout_theme_set(layout, "layout", "application", "noindicator");
378         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
379         evas_object_show(layout);
380
381         return layout;
382 }
383
384 static void __common_utils_del_popup(void *data, Evas_Object *obj, void *event_info)
385 {
386         Evas_Object *popup = (Evas_Object *)data;
387         evas_object_del(popup);
388 }
389
390 Evas_Object *common_utils_show_info_popup(Evas_Object *parent,
391                 popup_btn_info_t *popup_data)
392 {
393         __COMMON_FUNC_ENTER__;
394
395         Evas_Object *popup = elm_popup_add(parent);
396         char *txt = NULL;
397
398         if (!popup) {
399                 ERROR_LOG(UG_NAME_ERR, "Could not add popup");
400                 return NULL;
401         }
402
403         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
404         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
405
406         if (popup_data->title_txt) {
407                 txt = evas_textblock_text_utf8_to_markup(NULL, popup_data->title_txt);
408                 elm_object_domain_translatable_part_text_set(popup,
409                                 "title,text", PACKAGE, txt);
410                 g_free(txt);
411         }
412
413         if (popup_data->info_txt) {
414                 elm_object_domain_translatable_text_set(popup, PACKAGE,
415                                 popup_data->info_txt);
416         }
417
418         if (popup_data->btn1_txt) {
419                 Evas_Object *btn_1 = elm_button_add(popup);
420                 elm_object_style_set(btn_1, "popup");
421                 elm_object_domain_translatable_text_set(btn_1, PACKAGE ,
422                                 popup_data->btn1_txt);
423                 elm_object_part_content_set(popup, "button1", btn_1);
424                 if (popup_data->btn1_cb) {
425                         evas_object_smart_callback_add(btn_1, "clicked",
426                                         popup_data->btn1_cb, popup_data->btn1_data);
427                         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
428                                         popup_data->btn1_cb, popup_data->btn1_data);
429                 } else {
430                         evas_object_smart_callback_add(btn_1, "clicked",
431                                         __common_utils_del_popup, popup);
432                         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
433                                         __common_utils_del_popup, popup);
434                 }
435         }
436
437         if (popup_data->btn2_txt) {
438                 Evas_Object *btn_2 = elm_button_add(popup);
439                 elm_object_style_set(btn_2, "popup");
440                 /* This button reference used in case of hidden AP case */
441                 popup_data->btn = btn_2;
442                 elm_object_domain_translatable_text_set(btn_2, PACKAGE,
443                                 popup_data->btn2_txt);
444                 elm_object_part_content_set(popup, "button2", btn_2);
445                 //evas_object_smart_callback_add(btn_2, "clicked", popup_data->btn2_cb, NULL);
446                 evas_object_show(popup);
447                 if (popup_data->btn2_cb) {
448                         evas_object_smart_callback_add(btn_2, "clicked",
449                                         popup_data->btn2_cb, popup_data->btn2_data);
450                 } else {
451                         evas_object_smart_callback_add(btn_2, "clicked",
452                                         __common_utils_del_popup, popup);
453                 }
454         }
455
456         evas_object_show(popup);
457         elm_object_focus_set(popup, EINA_TRUE);
458
459         return popup;
460 }
461
462 Evas_Object *common_utils_show_info_ok_popup(Evas_Object *win,
463                 const char *str_pkg_name, const char *info_txt,
464                 Evas_Smart_Cb ok_cb, void *cb_data)
465 {
466         popup_btn_info_t popup_data;
467
468         memset(&popup_data, 0, sizeof(popup_data));
469         popup_data.info_txt = (char *)info_txt;
470         popup_data.btn1_txt = sc(str_pkg_name, I18N_TYPE_Ok);
471         popup_data.btn1_cb = ok_cb;
472         popup_data.btn1_data = cb_data;
473
474         return common_utils_show_info_popup(win, &popup_data);
475 }
476
477 Evas_Object *common_utils_show_info_timeout_popup(Evas_Object *win,
478                 const char* info_text, const double timeout)
479 {
480         Evas_Object *popup = elm_popup_add(win);
481
482         elm_object_text_set(popup, info_text);
483         elm_popup_timeout_set(popup, timeout);
484         evas_object_smart_callback_add(popup, "timeout",
485                         __common_utils_del_popup, popup);
486         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
487         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
488         evas_object_show(popup);
489
490         return popup;
491 }
492
493 int common_utils_get_rotate_angle(enum appcore_rm rotate_mode)
494 {
495         int rotate_angle;
496         if (APPCORE_RM_UNKNOWN == rotate_mode)
497                 appcore_get_rotation_state(&rotate_mode);
498
499         DEBUG_LOG(SP_NAME_NORMAL, "rotate_mode = %d", rotate_mode);
500
501         switch (rotate_mode) {
502         case APPCORE_RM_PORTRAIT_NORMAL:         /**< Portrait mode */
503                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_NORMAL");
504                 rotate_angle = 0;
505                 break;
506
507         case APPCORE_RM_PORTRAIT_REVERSE:         /**< Portrait upside down mode */
508                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_REVERSE");
509                 rotate_angle = 180;
510                 break;
511
512         case APPCORE_RM_LANDSCAPE_NORMAL:         /**< Left handed landscape mode */
513                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_NORMAL");
514                 rotate_angle = 270;
515                 break;
516
517         case APPCORE_RM_LANDSCAPE_REVERSE:          /**< Right handed landscape mode */
518                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_REVERSE");
519                 rotate_angle = 90;
520                 break;
521
522         default:
523                 ERROR_LOG(SP_NAME_ERR, "Invalid rotate mode. The default value (0) is set to 'rotate_angle'.");
524                 rotate_angle = 0;
525                 break;
526         }
527
528         return rotate_angle;
529 }
530
531 wlan_security_mode_type_t common_utils_get_sec_mode(wifi_manager_security_type_e sec_type)
532 {
533         switch (sec_type) {
534         case WIFI_MANAGER_SECURITY_TYPE_NONE:
535                 return WLAN_SEC_MODE_NONE;
536         case WIFI_MANAGER_SECURITY_TYPE_WEP:
537                 return WLAN_SEC_MODE_WEP;
538         case WIFI_MANAGER_SECURITY_TYPE_WPA_PSK:
539                 return WLAN_SEC_MODE_WPA_PSK;
540         case WIFI_MANAGER_SECURITY_TYPE_WPA2_PSK:
541                 return WLAN_SEC_MODE_WPA_PSK;
542         case WIFI_MANAGER_SECURITY_TYPE_EAP:
543                 return WLAN_SEC_MODE_IEEE8021X;
544         default:
545                 return WLAN_SEC_MODE_NONE;
546         }
547
548         return WLAN_SEC_MODE_NONE;
549 }
550
551 int common_utils_send_message_to_net_popup(const char *title,
552                 const char *content, const char *type, const char *ssid)
553 {
554         int ret = 0;
555         bundle *b = bundle_create();
556
557         bundle_add_str(b, "_SYSPOPUP_TITLE_", title);
558         bundle_add_str(b, "_SYSPOPUP_CONTENT_", content);
559         bundle_add_str(b, "_SYSPOPUP_TYPE_", type);
560         bundle_add_str(b, "_AP_NAME_", ssid);
561
562         ret = aul_launch_app("net.netpopup", b);
563
564         bundle_free(b);
565
566         return ret;
567 }
568
569 int common_utils_send_restriction_to_net_popup(const char *title,
570                 const char *type, const char *restriction)
571 {
572         int ret = 0;
573         bundle *b = bundle_create();
574
575         bundle_add_str(b, "_SYSPOPUP_TITLE_", title);
576         bundle_add_str(b, "_SYSPOPUP_CONTENT_", "security restriction");
577         bundle_add_str(b, "_SYSPOPUP_TYPE_", type);
578         bundle_add_str(b, "_RESTRICTED_TYPE_", restriction);
579
580         ret = aul_launch_app("net.netpopup", b);
581
582         bundle_free(b);
583
584         return ret;
585 }
586
587 int common_util_set_system_registry(const char *key, int value)
588 {
589         __COMMON_FUNC_ENTER__;
590
591         if (vconf_set_int(key, value) < 0) {
592                 ERROR_LOG(UG_NAME_NORMAL, "Failed to set vconf");
593
594                 __COMMON_FUNC_EXIT__;
595                 return -1;
596         }
597
598         __COMMON_FUNC_EXIT__;
599         return 0;
600 }
601
602 int common_util_get_system_registry(const char *key)
603 {
604         __COMMON_FUNC_ENTER__;
605
606         int value = 0;
607
608         if (vconf_get_int(key, &value) < 0) {
609                 ERROR_LOG(UG_NAME_NORMAL, "Failed to get vconf");
610
611                 __COMMON_FUNC_EXIT__;
612                 return -1;
613         }
614
615         __COMMON_FUNC_EXIT__;
616         return value;
617 }
618
619 static void __common_util_managed_idle_destroy_cb(gpointer data)
620 {
621         if (!data)
622                 return;
623
624         managed_idler_list = g_slist_remove(managed_idler_list, data);
625         g_free(data);
626 }
627
628 static gboolean __common_util_managed_idle_cb(gpointer user_data)
629 {
630         struct managed_idle_data *data = (struct managed_idle_data *)user_data;
631
632         if (!data)
633                 return FALSE;
634
635         return data->func(data->user_data);
636 }
637
638 guint common_util_managed_idle_add(GSourceFunc func, gpointer user_data)
639 {
640         guint id;
641         struct managed_idle_data *data;
642
643         if (!func)
644                 return 0;
645
646         data = g_try_new0(struct managed_idle_data, 1);
647         if (!data)
648                 return 0;
649
650         data->func = func;
651         data->user_data = user_data;
652
653         id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __common_util_managed_idle_cb,
654                         data, __common_util_managed_idle_destroy_cb);
655         if (!id) {
656                 g_free(data);
657                 return id;
658         }
659
660         data->id = id;
661
662         managed_idler_list = g_slist_append(managed_idler_list, data);
663
664         return id;
665 }
666
667 void common_util_managed_idle_cleanup(void)
668 {
669         if (managed_idler_list == NULL)
670                 return;
671
672         GSList *cur = managed_idler_list;
673         GSource *src;
674         struct managed_idle_data *data;
675
676         while (cur) {
677                 GSList *next = cur->next;
678                 data = (struct managed_idle_data *)cur->data;
679
680                 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
681                 if (src) {
682                         g_source_destroy(src);
683                         cur = managed_idler_list;
684                 } else {
685                         cur = next;
686                 }
687         }
688
689         g_slist_free(managed_idler_list);
690         managed_idler_list = NULL;
691 }
692
693 void common_util_managed_ecore_scan_update_timer_add(double interval,
694                 common_util_scan_update_cb callback, void *data)
695 {
696         if (callback == NULL)
697                 return;
698
699         common_util_managed_ecore_scan_update_timer_del();
700
701         scan_update_timer = ecore_timer_add(interval, callback, data);
702 }
703
704 void common_util_managed_ecore_scan_update_timer_del(void)
705 {
706         if (scan_update_timer != NULL) {
707                 ecore_timer_del(scan_update_timer);
708                 scan_update_timer = NULL;
709         }
710 }
711
712 void common_util_manager_ecore_scan_update_timer_reset(void)
713 {
714         scan_update_timer = NULL;
715 }
716
717 static GDBusConnection *common_util_get_gdbus_conn(void)
718 {
719         GError *error = NULL;
720
721         if (gdbus_conn.connection != NULL)
722                 return gdbus_conn.connection;
723
724 #if !GLIB_CHECK_VERSION(2, 36, 0)
725         g_type_init();
726 #endif
727
728         gdbus_conn.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
729         if (gdbus_conn.connection == NULL) {
730                 ERROR_LOG(UG_NAME_NORMAL,
731                                 "Failed to connect to the D-BUS daemon: [%s]\n", error->message);
732                 g_error_free(error);
733                 return NULL;
734         }
735
736         return gdbus_conn.connection;
737 }
738
739 gboolean common_util_subscribe_scanning_signal(GDBusSignalCallback callback)
740 {
741         GDBusConnection *connection;
742
743         connection = common_util_get_gdbus_conn();
744         if (connection == NULL) {
745                 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
746                 return FALSE;
747         }
748
749         /* Create supplicant service connection */
750         gdbus_conn.subscribe_id_supplicant = g_dbus_connection_signal_subscribe(
751                         connection,
752                         SUPPLICANT_SERVICE,
753                         SUPPLICANT_SERVICE_INTERFACE,
754                         "PropertiesChanged",
755                         NULL,
756                         NULL,
757                         G_DBUS_SIGNAL_FLAGS_NONE,
758                         callback,
759                         NULL,
760                         NULL);
761
762         if (gdbus_conn.subscribe_id_supplicant == 0) {
763                 ERROR_LOG(UG_NAME_NORMAL, "Failed register signals "
764                                 "supplicant(%d)\n",
765                                 gdbus_conn.subscribe_id_supplicant);
766                 return FALSE;
767         }
768
769         return TRUE;
770 }
771
772 gboolean common_util_unsubscribe_scanning_signal(void)
773 {
774         GDBusConnection *connection;
775
776         connection = common_util_get_gdbus_conn();
777         if (connection == NULL) {
778                 ERROR_LOG(UG_NAME_NORMAL, "failed to get gdbus_conn");
779                 return FALSE;
780         }
781
782         g_dbus_connection_signal_unsubscribe(connection,
783                         gdbus_conn.subscribe_id_supplicant);
784
785         g_object_unref(gdbus_conn.connection);
786         gdbus_conn.connection = NULL;
787
788         return TRUE;
789 }
790
791 int common_utils_get_sim_state(void)
792 {
793         int value = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
794         int sim_count = 0;
795
796         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT);
797         INFO_LOG(UG_NAME_NORMAL, "SIM slot 1 state : %d", value);
798         if (value == VCONFKEY_TELEPHONY_SIM_INSERTED)
799                 return value;
800
801         sim_count = common_util_get_system_registry(
802                         VCONFKEY_TELEPHONY_SIM_SLOT_COUNT);
803         if (sim_count == 1)
804                 return value;
805
806         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT2);
807         INFO_LOG(UG_NAME_NORMAL, "SIM slot 2 state : %d", value);
808
809         return value;
810 }
811
812 static inline int __get_model_from_model_config_xml(const char *field, char **value)
813 {
814         char *node_name = NULL;
815         char *node_value = NULL;
816         xmlNode *cur_node = NULL;
817         xmlNodePtr cur_ptr = NULL;
818         xmlNodePtr model_ptr = NULL;
819         xmlDocPtr xml_doc = NULL;
820
821         xml_doc = xmlParseFile(MODEL_CONFIG_FILE);
822         if (xml_doc == NULL)
823                 return -1;
824
825         cur_ptr = xmlDocGetRootElement(xml_doc);
826         if (cur_ptr == NULL) {
827                 xmlFreeDoc(xml_doc);
828                 return -1;
829         }
830
831         for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
832                 if (!xmlStrcmp(cur_ptr->name, (const xmlChar*)MODEL_CONFIG_TAG))
833                         break;
834         }
835
836         cur_ptr = cur_ptr->xmlChildrenNode;
837         for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
838                 if (!xmlStrcmp(cur_node->name, (const xmlChar*)FEATURE_TAG)) {
839                         model_ptr = cur_node;
840                         break;
841                 }
842         }
843
844         if (model_ptr == NULL) {
845                 xmlFreeDoc(xml_doc);
846                 return -1;
847         }
848
849         if (model_ptr) {
850                 cur_ptr = model_ptr->xmlChildrenNode;
851
852                 for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
853                         if (cur_node->type == XML_ELEMENT_NODE) {
854                                 node_name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");
855                                 if (node_name == NULL) {
856                                         xmlFreeDoc(xml_doc);
857                                         return -1;
858                                 }
859
860                                 if (!strncmp(node_name, field, strlen(node_name))) {
861                                         node_value = (char *)xmlNodeListGetString(xml_doc, cur_node->xmlChildrenNode, 1);
862                                         if (node_value) {
863                                                 *value = strdup(node_value);
864                                                 free(node_name);
865                                                 free(node_value);
866                                                 break;
867                                         }
868                                 }
869                                 free(node_name);
870                         }
871                 }
872         }
873
874         xmlFreeDoc(xml_doc);
875         return 0;
876 }
877
878 Eina_Bool _is_emulator(void)
879 {
880         char *model_name = NULL;
881
882         if (emulator == TIZEN_MODEL_EMULATOR)
883                 return EINA_TRUE;
884         else if (emulator == TIZEN_MODEL_NOT_EMULATOR)
885                 return EINA_FALSE;
886
887         if (__get_model_from_model_config_xml("tizen.org/system/model_name", &model_name) < 0)
888                 return EINA_FALSE;
889
890         if (model_name == NULL)
891                 return EINA_FALSE;
892
893         if (strncmp(model_name, "Emulator", strlen("Emulator")) == 0) {
894                 free(model_name);
895                 emulator = TIZEN_MODEL_EMULATOR;
896                 return EINA_TRUE;
897         } else {
898                 free(model_name);
899                 emulator = TIZEN_MODEL_NOT_EMULATOR;
900                 return EINA_FALSE;
901         }
902 }