637ad7c8fc173717238363907183d9d1d16e6361
[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         default:
252                 g_strlcat(buf, "_00", sizeof(buf));
253                 break;
254         }
255
256         if (icon_path)
257                 *icon_path = g_strdup_printf("%s", buf);
258 }
259
260 char *common_utils_get_rssi_text(const char *str_pkg_name, int rssi)
261 {
262         switch (wlan_manager_get_signal_strength(rssi)) {
263         case SIGNAL_STRENGTH_TYPE_EXCELLENT:
264                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Excellent));
265         case SIGNAL_STRENGTH_TYPE_GOOD:
266                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Good));
267         default:
268                 return g_strdup(sc(str_pkg_name, I18N_TYPE_Weak));
269         }
270 }
271
272 void common_utils_set_edit_box_imf_panel_evnt_cb(Elm_Object_Item *item,
273                                                 imf_ctxt_panel_cb_t input_panel_cb, void *user_data)
274 {
275         __COMMON_FUNC_ENTER__;
276         common_utils_entry_info_t *entry_info;
277         entry_info = elm_object_item_data_get(item);
278         if (!entry_info)
279                 return;
280
281         entry_info->input_panel_cb = input_panel_cb;
282         entry_info->input_panel_cb_data = user_data;
283
284         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
285         Ecore_IMF_Context *imf_ctxt = elm_entry_imf_context_get(entry);
286         if (imf_ctxt && entry_info->input_panel_cb) {
287                 /* Deleting the previously attached callback */
288                 ecore_imf_context_input_panel_event_callback_del(imf_ctxt,
289                                 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
290                                 entry_info->input_panel_cb);
291                 ecore_imf_context_input_panel_event_callback_add(imf_ctxt,
292                                 ECORE_IMF_INPUT_PANEL_STATE_EVENT,
293                                 entry_info->input_panel_cb,
294                                 entry_info->input_panel_cb_data);
295                 DEBUG_LOG(UG_NAME_NORMAL, "set the imf ctxt cbs");
296         }
297
298         __COMMON_FUNC_EXIT__;
299         return;
300 }
301
302 void common_utils_edit_box_focus_set(Elm_Object_Item *item, Eina_Bool focus_set)
303 {
304         __COMMON_FUNC_ENTER__;
305         if (!item)
306                 return;
307
308         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
309
310         if (entry) {
311                 elm_object_focus_set(entry, focus_set);
312                 elm_object_focus_allow_set(entry, focus_set);
313         }
314
315         __COMMON_FUNC_EXIT__;
316         return;
317 }
318
319 void common_utils_edit_box_allow_focus_set(Elm_Object_Item *item,
320                 Eina_Bool focus_set)
321 {
322         __COMMON_FUNC_ENTER__;
323         if (!item)
324                 return;
325
326         Evas_Object *entry = elm_object_item_part_content_get(item, "elm.icon.entry");
327         elm_object_focus_allow_set(entry, focus_set);
328
329         __COMMON_FUNC_EXIT__;
330         return;
331 }
332
333 Elm_Object_Item *common_utils_add_2_line_txt_disabled_item(
334                 Evas_Object* view_list, const char *style_name,
335                 const char *line1_txt, const char *line2_txt)
336 {
337         static Elm_Genlist_Item_Class two_line_display_itc;
338         two_line_disp_data_t *two_line_data = NULL;
339         Elm_Object_Item *item = NULL;
340
341         two_line_display_itc.item_style = style_name;
342         two_line_display_itc.func.text_get = __common_utils_2line_text_get;
343         two_line_display_itc.func.content_get = NULL;
344         two_line_display_itc.func.state_get = NULL;
345         two_line_display_itc.func.del = __common_utils_2line_text_del;
346
347         two_line_data = g_new0(two_line_disp_data_t, 1);
348         two_line_data->title_str = g_strdup(line1_txt);
349         two_line_data->info_str = g_strdup(line2_txt);
350         SECURE_INFO_LOG(UG_NAME_NORMAL, "title_str = %s info_str = %s",
351                         two_line_data->title_str, two_line_data->info_str);
352
353         item = elm_genlist_item_append(view_list, &two_line_display_itc,
354                         two_line_data, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
355         elm_object_item_disabled_set(item, TRUE);
356
357         return item;
358 }
359
360 char *common_utils_get_list_item_entry_txt(Elm_Object_Item *entry_item)
361 {
362         common_utils_entry_info_t *entry_info =
363                         (common_utils_entry_info_t *)elm_object_item_data_get(entry_item);
364         if (entry_info == NULL)
365                 return NULL;
366
367         DEBUG_LOG(UG_NAME_NORMAL, "entry_info: 0x%x", entry_info);
368
369         return g_strdup(entry_info->entry_txt);
370 }
371
372 Evas_Object *common_utils_create_layout(Evas_Object *navi_frame)
373 {
374         Evas_Object *layout;
375         layout = elm_layout_add(navi_frame);
376         elm_layout_theme_set(layout, "layout", "application", "noindicator");
377         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
378         evas_object_show(layout);
379
380         return layout;
381 }
382
383 static void __common_utils_del_popup(void *data, Evas_Object *obj, void *event_info)
384 {
385         Evas_Object *popup = (Evas_Object *)data;
386         evas_object_del(popup);
387 }
388
389 Evas_Object *common_utils_show_info_popup(Evas_Object *parent,
390                 popup_btn_info_t *popup_data)
391 {
392         __COMMON_FUNC_ENTER__;
393
394         Evas_Object *popup = elm_popup_add(parent);
395         char *txt = NULL;
396
397         if (!popup) {
398                 ERROR_LOG(UG_NAME_ERR, "Could not add popup");
399                 return NULL;
400         }
401
402         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
403         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
404
405         if (popup_data->title_txt) {
406                 txt = evas_textblock_text_utf8_to_markup(NULL, popup_data->title_txt);
407                 elm_object_domain_translatable_part_text_set(popup,
408                                 "title,text", PACKAGE, txt);
409                 g_free(txt);
410         }
411
412         if (popup_data->info_txt) {
413                 elm_object_domain_translatable_text_set(popup, PACKAGE,
414                                 popup_data->info_txt);
415         }
416
417         if (popup_data->btn1_txt) {
418                 Evas_Object *btn_1 = elm_button_add(popup);
419                 elm_object_style_set(btn_1, "popup");
420                 elm_object_domain_translatable_text_set(btn_1, PACKAGE ,
421                                 popup_data->btn1_txt);
422                 elm_object_part_content_set(popup, "button1", btn_1);
423                 if (popup_data->btn1_cb) {
424                         evas_object_smart_callback_add(btn_1, "clicked",
425                                         popup_data->btn1_cb, popup_data->btn1_data);
426                         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
427                                         popup_data->btn1_cb, popup_data->btn1_data);
428                 } else {
429                         evas_object_smart_callback_add(btn_1, "clicked",
430                                         __common_utils_del_popup, popup);
431                         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK,
432                                         __common_utils_del_popup, popup);
433                 }
434         }
435
436         if (popup_data->btn2_txt) {
437                 Evas_Object *btn_2 = elm_button_add(popup);
438                 elm_object_style_set(btn_2, "popup");
439                 /* This button reference used in case of hidden AP case */
440                 popup_data->btn = btn_2;
441                 elm_object_domain_translatable_text_set(btn_2, PACKAGE,
442                                 popup_data->btn2_txt);
443                 elm_object_part_content_set(popup, "button2", btn_2);
444                 //evas_object_smart_callback_add(btn_2, "clicked", popup_data->btn2_cb, NULL);
445                 evas_object_show(popup);
446                 if (popup_data->btn2_cb) {
447                         evas_object_smart_callback_add(btn_2, "clicked",
448                                         popup_data->btn2_cb, popup_data->btn2_data);
449                 } else {
450                         evas_object_smart_callback_add(btn_2, "clicked",
451                                         __common_utils_del_popup, popup);
452                 }
453         }
454
455         evas_object_show(popup);
456         elm_object_focus_set(popup, EINA_TRUE);
457
458         return popup;
459 }
460
461 Evas_Object *common_utils_show_info_ok_popup(Evas_Object *win,
462                 const char *str_pkg_name, const char *info_txt,
463                 Evas_Smart_Cb ok_cb, void *cb_data)
464 {
465         popup_btn_info_t popup_data;
466
467         memset(&popup_data, 0, sizeof(popup_data));
468         popup_data.info_txt = (char *)info_txt;
469         popup_data.btn1_txt = sc(str_pkg_name, I18N_TYPE_Ok);
470         popup_data.btn1_cb = ok_cb;
471         popup_data.btn1_data = cb_data;
472
473         return common_utils_show_info_popup(win, &popup_data);
474 }
475
476 Evas_Object *common_utils_show_info_timeout_popup(Evas_Object *win,
477                 const char* info_text, const double timeout)
478 {
479         Evas_Object *popup = elm_popup_add(win);
480
481         elm_object_text_set(popup, info_text);
482         elm_popup_timeout_set(popup, timeout);
483         evas_object_smart_callback_add(popup, "timeout",
484                         __common_utils_del_popup, popup);
485         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
486         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
487         evas_object_show(popup);
488
489         return popup;
490 }
491
492 int common_utils_get_rotate_angle(enum appcore_rm rotate_mode)
493 {
494         int rotate_angle;
495         if (APPCORE_RM_UNKNOWN == rotate_mode)
496                 appcore_get_rotation_state(&rotate_mode);
497
498         DEBUG_LOG(SP_NAME_NORMAL, "rotate_mode = %d", rotate_mode);
499
500         switch (rotate_mode) {
501         case APPCORE_RM_PORTRAIT_NORMAL:         /**< Portrait mode */
502                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_NORMAL");
503                 rotate_angle = 0;
504                 break;
505
506         case APPCORE_RM_PORTRAIT_REVERSE:         /**< Portrait upside down mode */
507                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_PORTRAIT_REVERSE");
508                 rotate_angle = 180;
509                 break;
510
511         case APPCORE_RM_LANDSCAPE_NORMAL:         /**< Left handed landscape mode */
512                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_NORMAL");
513                 rotate_angle = 270;
514                 break;
515
516         case APPCORE_RM_LANDSCAPE_REVERSE:          /**< Right handed landscape mode */
517                 DEBUG_LOG(SP_NAME_NORMAL, "rotate mode is APPCORE_RM_LANDSCAPE_REVERSE");
518                 rotate_angle = 90;
519                 break;
520
521         default:
522                 ERROR_LOG(SP_NAME_ERR, "Invalid rotate mode. The default value (0) is set to 'rotate_angle'.");
523                 rotate_angle = 0;
524                 break;
525         }
526
527         return rotate_angle;
528 }
529
530 wlan_security_mode_type_t common_utils_get_sec_mode(wifi_manager_security_type_e sec_type)
531 {
532         switch (sec_type) {
533         case WIFI_MANAGER_SECURITY_TYPE_NONE:
534                 return WLAN_SEC_MODE_NONE;
535         case WIFI_MANAGER_SECURITY_TYPE_WEP:
536                 return WLAN_SEC_MODE_WEP;
537         case WIFI_MANAGER_SECURITY_TYPE_WPA_PSK:
538                 return WLAN_SEC_MODE_WPA_PSK;
539         case WIFI_MANAGER_SECURITY_TYPE_WPA2_PSK:
540                 return WLAN_SEC_MODE_WPA_PSK;
541         case WIFI_MANAGER_SECURITY_TYPE_EAP:
542                 return WLAN_SEC_MODE_IEEE8021X;
543         default:
544                 return WLAN_SEC_MODE_NONE;
545         }
546
547         return WLAN_SEC_MODE_NONE;
548 }
549
550 int common_utils_send_message_to_net_popup(const char *title,
551                 const char *content, const char *type, const char *ssid)
552 {
553         int ret = 0;
554         bundle *b = bundle_create();
555
556         bundle_add_str(b, "_SYSPOPUP_TITLE_", title);
557         bundle_add_str(b, "_SYSPOPUP_CONTENT_", content);
558         bundle_add_str(b, "_SYSPOPUP_TYPE_", type);
559         bundle_add_str(b, "_AP_NAME_", ssid);
560
561         ret = aul_launch_app("net.netpopup", b);
562
563         bundle_free(b);
564
565         return ret;
566 }
567
568 int common_utils_send_restriction_to_net_popup(const char *title,
569                 const char *type, const char *restriction)
570 {
571         int ret = 0;
572         bundle *b = bundle_create();
573
574         bundle_add_str(b, "_SYSPOPUP_TITLE_", title);
575         bundle_add_str(b, "_SYSPOPUP_CONTENT_", "security restriction");
576         bundle_add_str(b, "_SYSPOPUP_TYPE_", type);
577         bundle_add_str(b, "_RESTRICTED_TYPE_", restriction);
578
579         ret = aul_launch_app("net.netpopup", b);
580
581         bundle_free(b);
582
583         return ret;
584 }
585
586 int common_util_set_system_registry(const char *key, int value)
587 {
588         __COMMON_FUNC_ENTER__;
589
590         if (vconf_set_int(key, value) < 0) {
591                 ERROR_LOG(UG_NAME_NORMAL, "Failed to set vconf");
592
593                 __COMMON_FUNC_EXIT__;
594                 return -1;
595         }
596
597         __COMMON_FUNC_EXIT__;
598         return 0;
599 }
600
601 int common_util_get_system_registry(const char *key)
602 {
603         __COMMON_FUNC_ENTER__;
604
605         int value = 0;
606
607         if (vconf_get_int(key, &value) < 0) {
608                 ERROR_LOG(UG_NAME_NORMAL, "Failed to get vconf");
609
610                 __COMMON_FUNC_EXIT__;
611                 return -1;
612         }
613
614         __COMMON_FUNC_EXIT__;
615         return value;
616 }
617
618 static void __common_util_managed_idle_destroy_cb(gpointer data)
619 {
620         if (!data)
621                 return;
622
623         managed_idler_list = g_slist_remove(managed_idler_list, data);
624         g_free(data);
625 }
626
627 static gboolean __common_util_managed_idle_cb(gpointer user_data)
628 {
629         struct managed_idle_data *data = (struct managed_idle_data *)user_data;
630
631         if (!data)
632                 return FALSE;
633
634         return data->func(data->user_data);
635 }
636
637 guint common_util_managed_idle_add(GSourceFunc func, gpointer user_data)
638 {
639         guint id;
640         struct managed_idle_data *data;
641
642         if (!func)
643                 return 0;
644
645         data = g_try_new0(struct managed_idle_data, 1);
646         if (!data)
647                 return 0;
648
649         data->func = func;
650         data->user_data = user_data;
651
652         id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __common_util_managed_idle_cb,
653                         data, __common_util_managed_idle_destroy_cb);
654         if (!id) {
655                 g_free(data);
656                 return id;
657         }
658
659         data->id = id;
660
661         managed_idler_list = g_slist_append(managed_idler_list, data);
662
663         return id;
664 }
665
666 void common_util_managed_idle_cleanup(void)
667 {
668         if (managed_idler_list == NULL)
669                 return;
670
671         GSList *cur = managed_idler_list;
672         GSource *src;
673         struct managed_idle_data *data;
674
675         while (cur) {
676                 GSList *next = cur->next;
677                 data = (struct managed_idle_data *)cur->data;
678
679                 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
680                 if (src) {
681                         g_source_destroy(src);
682                         cur = managed_idler_list;
683                 } else {
684                         cur = next;
685                 }
686         }
687
688         g_slist_free(managed_idler_list);
689         managed_idler_list = NULL;
690 }
691
692 void common_util_managed_ecore_scan_update_timer_add(double interval,
693                 common_util_scan_update_cb callback, void *data)
694 {
695         if (callback == NULL)
696                 return;
697
698         common_util_managed_ecore_scan_update_timer_del();
699
700         scan_update_timer = ecore_timer_add(interval, callback, data);
701 }
702
703 void common_util_managed_ecore_scan_update_timer_del(void)
704 {
705         if (scan_update_timer != NULL) {
706                 ecore_timer_del(scan_update_timer);
707                 scan_update_timer = NULL;
708         }
709 }
710
711 void common_util_manager_ecore_scan_update_timer_reset(void)
712 {
713         scan_update_timer = NULL;
714 }
715
716 static GDBusConnection *common_util_get_gdbus_conn(void)
717 {
718         GError *error = NULL;
719
720         if (gdbus_conn.connection != NULL)
721                 return gdbus_conn.connection;
722
723 #if !GLIB_CHECK_VERSION(2, 36, 0)
724         g_type_init();
725 #endif
726
727         gdbus_conn.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
728         if (gdbus_conn.connection == NULL) {
729                 ERROR_LOG(UG_NAME_NORMAL,
730                                 "Failed to connect to the D-BUS daemon: [%s]\n", error->message);
731                 g_error_free(error);
732                 return NULL;
733         }
734
735         return gdbus_conn.connection;
736 }
737
738 int common_utils_get_sim_state(void)
739 {
740         int value = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
741         int sim_count = 0;
742
743         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT);
744         INFO_LOG(UG_NAME_NORMAL, "SIM slot 1 state : %d", value);
745         if (value == VCONFKEY_TELEPHONY_SIM_INSERTED)
746                 return value;
747
748         sim_count = common_util_get_system_registry(
749                         VCONFKEY_TELEPHONY_SIM_SLOT_COUNT);
750         if (sim_count == 1)
751                 return value;
752
753         value = common_util_get_system_registry(VCONFKEY_TELEPHONY_SIM_SLOT2);
754         INFO_LOG(UG_NAME_NORMAL, "SIM slot 2 state : %d", value);
755
756         return value;
757 }
758
759 static inline int __get_model_from_model_config_xml(const char *field, char **value)
760 {
761         char *node_name = NULL;
762         char *node_value = NULL;
763         xmlNode *cur_node = NULL;
764         xmlNodePtr cur_ptr = NULL;
765         xmlNodePtr model_ptr = NULL;
766         xmlDocPtr xml_doc = NULL;
767
768         xml_doc = xmlParseFile(MODEL_CONFIG_FILE);
769         if (xml_doc == NULL)
770                 return -1;
771
772         cur_ptr = xmlDocGetRootElement(xml_doc);
773         if (cur_ptr == NULL) {
774                 xmlFreeDoc(xml_doc);
775                 return -1;
776         }
777
778         for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
779                 if (!xmlStrcmp(cur_ptr->name, (const xmlChar*)MODEL_CONFIG_TAG))
780                         break;
781         }
782
783         cur_ptr = cur_ptr->xmlChildrenNode;
784         for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
785                 if (!xmlStrcmp(cur_node->name, (const xmlChar*)FEATURE_TAG)) {
786                         model_ptr = cur_node;
787                         break;
788                 }
789         }
790
791         if (model_ptr == NULL) {
792                 xmlFreeDoc(xml_doc);
793                 return -1;
794         }
795
796         if (model_ptr) {
797                 cur_ptr = model_ptr->xmlChildrenNode;
798
799                 for (cur_node = cur_ptr; cur_node; cur_node = cur_node->next) {
800                         if (cur_node->type == XML_ELEMENT_NODE) {
801                                 node_name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");
802                                 if (node_name == NULL) {
803                                         xmlFreeDoc(xml_doc);
804                                         return -1;
805                                 }
806
807                                 if (!strncmp(node_name, field, strlen(node_name))) {
808                                         node_value = (char *)xmlNodeListGetString(xml_doc, cur_node->xmlChildrenNode, 1);
809                                         if (node_value) {
810                                                 *value = strdup(node_value);
811                                                 free(node_name);
812                                                 free(node_value);
813                                                 break;
814                                         }
815                                 }
816                                 free(node_name);
817                         }
818                 }
819         }
820
821         xmlFreeDoc(xml_doc);
822         return 0;
823 }
824
825 Eina_Bool _is_emulator(void)
826 {
827         char *model_name = NULL;
828
829         if (emulator == TIZEN_MODEL_EMULATOR)
830                 return EINA_TRUE;
831         else if (emulator == TIZEN_MODEL_NOT_EMULATOR)
832                 return EINA_FALSE;
833
834         if (__get_model_from_model_config_xml("tizen.org/system/model_name", &model_name) < 0)
835                 return EINA_FALSE;
836
837         if (model_name == NULL)
838                 return EINA_FALSE;
839
840         if (strncmp(model_name, "Emulator", strlen("Emulator")) == 0) {
841                 free(model_name);
842                 emulator = TIZEN_MODEL_EMULATOR;
843                 return EINA_TRUE;
844         } else {
845                 free(model_name);
846                 emulator = TIZEN_MODEL_NOT_EMULATOR;
847                 return EINA_FALSE;
848         }
849 }