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