tizen 2.4 release
[apps/home/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
26 #include "common.h"
27 #include "quickpanel-ui.h"
28 #include "settings.h"
29 #include "setting_utils.h"
30 #include "setting_module_api.h"
31 #include "settings_icon_common.h"
32
33 #define BUTTON_LABEL _("IDS_ST_BUTTON2_BLUETOOTH_ABB")
34 #define BUTTON_ICON_NORMAL "quick_icon_bluetooth.png"
35 #define BUTTON_ICON_HIGHLIGHT NULL
36 #define BUTTON_ICON_DIM NULL
37 #define PACKAGE_SETTING_MENU "ug-bluetooth-efl-single"
38
39 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
40
41 static const char *_label_get(void)
42 {
43         return BUTTON_LABEL;
44 }
45
46 static const char *_icon_get(qp_setting_icon_image_type type)
47 {
48         if (type == QP_SETTING_ICON_NORMAL) {
49                 return BUTTON_ICON_NORMAL;
50         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
51                 return BUTTON_ICON_HIGHLIGHT;
52         } else if (type == QP_SETTING_ICON_DIM) {
53 #ifdef BUTTON_ICON_DIM
54                 return BUTTON_ICON_DIM;
55 #endif
56         }
57
58         return NULL;
59 }
60
61 static void _long_press_cb(void *data)
62 {
63 #ifdef PACKAGE_SETTING_MENU
64         quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
65 #endif
66 }
67
68 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
69 {
70         Evas_Object *image = NULL;
71         const char *icon_path = NULL;
72
73         quickpanel_setting_icon_state_set(view, state);
74
75         if (state == ICON_VIEW_STATE_ON) {
76 #ifdef BUTTON_ICON_HIGHLIGHT
77                 icon_path = BUTTON_ICON_HIGHLIGHT;
78 #endif
79         } else if (state == ICON_VIEW_STATE_DIM) {
80 #ifdef BUTTON_ICON_DIM
81                 icon_path = BUTTON_ICON_DIM;
82 #endif
83         } else {
84                 icon_path = BUTTON_ICON_NORMAL;
85         }
86
87         if (icon_path == NULL) {
88                 icon_path = BUTTON_ICON_NORMAL;
89         }
90         image = quickpanel_setting_icon_image_new(view, icon_path);
91         quickpanel_setting_icon_content_set(view, image);
92         quickpanel_setting_icon_text_set(view, BUTTON_LABEL, state);
93 }
94
95 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
96 {
97         int ret = 0;
98         bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
99         retif(module == NULL, , "Invalid parameter!");
100
101         ret = bt_adapter_get_state(&adapter_state);
102         retif(ret != BT_ERROR_NONE, , "bt_adapter_get_state failed");
103
104         if (adapter_state == BT_ADAPTER_ENABLED) {
105                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
106         } else {
107                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
108         }
109
110         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
111         quickpanel_setting_module_icon_timer_del(module);
112
113         quickpanel_setting_module_icon_view_update(module,
114                         quickpanel_setting_module_icon_state_get(module),
115                         FLAG_VALUE_VOID);
116 }
117
118 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
119 {
120         int ret;
121         int is_on = 0;
122         QP_Module_Setting *module = (QP_Module_Setting *)data;
123         retif(module == NULL, , "Invalid parameter!");
124
125         if (quickpanel_setting_module_is_icon_clickable(module) == 0) {
126                 return;
127         }
128
129         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
130                 ret = bt_adapter_disable();
131                 retif(ret != BT_ERROR_NONE, , "failed to disable BT adapter");
132
133                 is_on = 0;
134         } else {
135                 ret = bt_adapter_enable();
136                 retif(ret != BT_ERROR_NONE, , "failed to enable BT adapter");
137
138                 is_on = 1;
139         }
140
141         if (is_on == 1) {
142                 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
143         } else {
144                 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
145         }
146         quickpanel_setting_module_icon_timer_add(module);
147 }
148
149 static void _bluetooth_status_changed_cb(int result, bt_adapter_state_e adapter_state, void *user_data)
150 {
151         QP_Module_Setting *module = (QP_Module_Setting *)user_data;
152         retif(module == NULL, , "Invalid parameter!");
153
154         INFO("bluetooth state : %d", adapter_state);
155         quickpanel_setting_module_icon_timer_del(module);
156
157         if (result != BT_ERROR_NONE) {
158                 ERR("BT adapter operation is failed");
159                 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
160                 return;
161         }
162
163         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
164 }
165
166 static int _register_module_event_handler(void *data)
167 {
168         int ret = 0;
169
170         ret = bt_initialize();
171         msgif(ret != BT_ERROR_NONE, "bt_initialize failed");
172
173         ret = bt_adapter_set_state_changed_cb(_bluetooth_status_changed_cb, data);
174         msgif(ret != BT_ERROR_NONE, "bt_adapter_set_state_changed_cb failed");
175
176         return QP_OK;
177 }
178
179 static int _unregister_module_event_handler(void *data)
180 {
181         int ret = 0;
182
183         ret = bt_adapter_unset_state_changed_cb();
184         msgif(ret != BT_ERROR_NONE, "bt_adapter_unset_state_changed_cb failed");
185
186         ret = bt_deinitialize();
187         msgif(ret != BT_ERROR_NONE, "bt_deinitialize failed");
188
189         return QP_OK;
190 }
191
192 /****************************************************************************
193  *
194  * Quickpanel Item functions
195  *
196  ****************************************************************************/
197 static int _init(void *data)
198 {
199         int ret = QP_OK;
200
201         ret = _register_module_event_handler(data);
202
203         return ret;
204 }
205
206 static int _fini(void *data)
207 {
208         int ret = QP_OK;
209
210         ret = _unregister_module_event_handler(data);
211
212         return ret;
213 }
214
215 static void _lang_changed(void *data)
216 {
217         QP_Module_Setting *module = (QP_Module_Setting *)data;
218         retif(module == NULL, , "Invalid parameter!");
219
220         quickpanel_setting_module_icon_view_update_text(module);
221 }
222
223 static void _refresh(void *data)
224 {
225         QP_Module_Setting *module = (QP_Module_Setting *)data;
226         retif(module == NULL, , "Invalid parameter!");
227
228         quickpanel_setting_module_icon_view_update_text(module);
229 }
230
231 QP_Module_Setting bluetooth = {
232         .name                           = "bluetooth",
233         .init                           = _init,
234         .fini                           = _fini,
235         .lang_changed           = _lang_changed,
236         .refresh                        = _refresh,
237         .icon_get                       = _icon_get,
238         .label_get                      = _label_get,
239         .view_update            = _view_update,
240         .status_update          = _status_update,
241         .handler_longpress      = _long_press_cb,
242         .handler_press          = _mouse_clicked_cb,
243 };