eeefa9565c71b011b70d541c57c8c7e0dacfdbd1
[apps/core/preloaded/quickpanel.git] / daemon / settings / modules / bluetooth.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <Elementary.h>
19
20 #include <bluetooth.h>
21 #include <vconf.h>
22 #include <bluetooth_internal.h>
23 #include <tzsh.h>
24 #include <tzsh_quickpanel_service.h>
25 #include <E_DBus.h>
26
27 #include "common.h"
28 #include "quickpanel-ui.h"
29 #include "settings.h"
30 #include "setting_utils.h"
31 #include "setting_module_api.h"
32 #include "settings_icon_common.h"
33
34 #define BUTTON_LABEL _("IDS_ST_BUTTON2_BLUETOOTH_ABB")
35 #define BUTTON_ICON_NORMAL "quick_icon_bluetooth.png"
36 #define BUTTON_ICON_HIGHLIGHT NULL
37 #define BUTTON_ICON_DIM NULL
38 #define PACKAGE_SETTING_MENU "ug-bluetooth-efl-single"
39
40 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
41
42 static const char *_label_get(void)
43 {
44         return BUTTON_LABEL;
45 }
46
47 static const char *_icon_get(qp_setting_icon_image_type type)
48 {
49         if (type == QP_SETTING_ICON_NORMAL) {
50                 return BUTTON_ICON_NORMAL;
51         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
52                 return BUTTON_ICON_HIGHLIGHT;
53         } else if (type == QP_SETTING_ICON_DIM) {
54 #ifdef BUTTON_ICON_DIM
55                 return BUTTON_ICON_DIM;
56 #endif
57         }
58
59         return NULL;
60 }
61
62 static void _long_press_cb(void *data)
63 {
64 #ifdef PACKAGE_SETTING_MENU
65         quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
66 #endif
67 }
68
69 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
70 {
71         Evas_Object *image = NULL;
72         const char *icon_path = NULL;
73
74         quickpanel_setting_icon_state_set(view, state);
75
76         if (state == ICON_VIEW_STATE_ON) {
77 #ifdef BUTTON_ICON_HIGHLIGHT
78                 icon_path = BUTTON_ICON_HIGHLIGHT;
79 #endif
80         } else if (state == ICON_VIEW_STATE_DIM) {
81 #ifdef BUTTON_ICON_DIM
82                 icon_path = BUTTON_ICON_DIM;
83 #endif
84         } else {
85                 icon_path = BUTTON_ICON_NORMAL;
86         }
87
88         if (icon_path == NULL) {
89                 icon_path = BUTTON_ICON_NORMAL;
90         }
91         image = quickpanel_setting_icon_image_new(view, icon_path);
92         quickpanel_setting_icon_content_set(view, image);
93         quickpanel_setting_icon_text_set(view, BUTTON_LABEL, state);
94 }
95
96 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
97 {
98         int ret = 0;
99         bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
100         retif(module == NULL, , "Invalid parameter!");
101
102         ret = bt_adapter_get_state(&adapter_state);
103         retif(ret != BT_ERROR_NONE, , "bt_adapter_get_state failed");
104
105         if (adapter_state == BT_ADAPTER_ENABLED) {
106                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
107         } else {
108                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
109         }
110
111         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
112         quickpanel_setting_module_icon_timer_del(module);
113
114         quickpanel_setting_module_icon_view_update(module,
115                         quickpanel_setting_module_icon_state_get(module),
116                         FLAG_VALUE_VOID);
117 }
118
119 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
120 {
121         int ret;
122         int is_on = 0;
123         QP_Module_Setting *module = (QP_Module_Setting *)data;
124         retif(module == NULL, , "Invalid parameter!");
125
126         if (quickpanel_setting_module_is_icon_clickable(module) == 0) {
127                 return;
128         }
129
130         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
131                 ret = bt_adapter_disable();
132                 retif(ret != BT_ERROR_NONE, , "failed to disable BT adapter");
133
134                 is_on = 0;
135         } else {
136                 ret = bt_adapter_enable();
137                 retif(ret != BT_ERROR_NONE, , "failed to enable BT adapter");
138
139                 is_on = 1;
140         }
141
142         if (is_on == 1) {
143                 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
144         } else {
145                 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
146         }
147         quickpanel_setting_module_icon_timer_add(module);
148 }
149
150 static void _bluetooth_status_changed_cb(int result, bt_adapter_state_e adapter_state, void *user_data)
151 {
152         QP_Module_Setting *module = (QP_Module_Setting *)user_data;
153         retif(module == NULL, , "Invalid parameter!");
154
155         INFO("bluetooth state : %d", adapter_state);
156         quickpanel_setting_module_icon_timer_del(module);
157
158         if (result != BT_ERROR_NONE) {
159                 ERR("BT adapter operation is failed");
160                 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
161                 return;
162         }
163
164         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
165 }
166
167 static int _register_module_event_handler(void *data)
168 {
169         int ret = 0;
170
171         ret = bt_initialize();
172         msgif(ret != BT_ERROR_NONE, "bt_initialize failed");
173
174         ret = bt_adapter_set_state_changed_cb(_bluetooth_status_changed_cb, data);
175         msgif(ret != BT_ERROR_NONE, "bt_adapter_set_state_changed_cb failed");
176
177         return QP_OK;
178 }
179
180 static int _unregister_module_event_handler(void *data)
181 {
182         int ret = 0;
183
184         ret = bt_adapter_unset_state_changed_cb();
185         msgif(ret != BT_ERROR_NONE, "bt_adapter_unset_state_changed_cb failed");
186
187         ret = bt_deinitialize();
188         msgif(ret != BT_ERROR_NONE, "bt_deinitialize failed");
189
190         return QP_OK;
191 }
192
193 /****************************************************************************
194  *
195  * Quickpanel Item functions
196  *
197  ****************************************************************************/
198 static int _init(void *data)
199 {
200         int ret = QP_OK;
201
202         ret = _register_module_event_handler(data);
203
204         return ret;
205 }
206
207 static int _fini(void *data)
208 {
209         int ret = QP_OK;
210
211         ret = _unregister_module_event_handler(data);
212
213         return ret;
214 }
215
216 static void _lang_changed(void *data)
217 {
218         QP_Module_Setting *module = (QP_Module_Setting *)data;
219         retif(module == NULL, , "Invalid parameter!");
220
221         quickpanel_setting_module_icon_view_update_text(module);
222 }
223
224 static void _refresh(void *data)
225 {
226         QP_Module_Setting *module = (QP_Module_Setting *)data;
227         retif(module == NULL, , "Invalid parameter!");
228
229         quickpanel_setting_module_icon_view_update_text(module);
230 }
231
232 QP_Module_Setting bluetooth = {
233         .name                           = "bluetooth",
234         .init                           = _init,
235         .fini                           = _fini,
236         .lang_changed           = _lang_changed,
237         .refresh                        = _refresh,
238         .icon_get                       = _icon_get,
239         .label_get                      = _label_get,
240         .view_update            = _view_update,
241         .status_update          = _status_update,
242         .handler_longpress      = _long_press_cb,
243         .handler_press          = _mouse_clicked_cb,
244 };