45a77ae69835c4cf9fcebe17e04a0f0234bc865d
[apps/core/preloaded/quickpanel.git] / daemon / settings / modules / tethering.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
19 #include <vconf.h>
20 #include <tethering.h>
21 #include <glib.h>
22 #include <bundle_internal.h>
23 #include <net_connection.h>
24 #include <syspopup_caller.h>
25 #include "common.h"
26 #include "quickpanel-ui.h"
27 #include "settings.h"
28 #include "setting_utils.h"
29 #include "setting_module_api.h"
30
31 #define MOBILE_AP_SYSPOPUP_NAME         "mobileap-syspopup"
32 #define BUTTON_LABEL                    _("IDS_ST_BUTTON2_WI_FI_NTETHERING")
33 #define BUTTON_ICON_NORMAL              "quick_icon_wifi_tethering.png"
34 #define BUTTON_ICON_HIGHLIGHT           NULL
35 #define BUTTON_ICON_DIM NULL
36 #define PACKAGE_SETTING_MENU            "setting-mobileap-efl"
37
38 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
39
40 static const char *_label_get(void)
41 {
42         return BUTTON_LABEL;
43 }
44
45 static const char *_icon_get(qp_setting_icon_image_type type)
46 {
47         if (type == QP_SETTING_ICON_NORMAL) {
48                 return BUTTON_ICON_NORMAL;
49         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
50                 return BUTTON_ICON_HIGHLIGHT;
51         } else if (type == QP_SETTING_ICON_DIM) {
52 #ifdef BUTTON_ICON_DIM
53                 return BUTTON_ICON_DIM;
54 #endif
55         }
56
57         return NULL;
58 }
59
60 static void _long_press_cb(void *data)
61 {
62 #ifdef PACKAGE_SETTING_MENU
63         quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
64 #endif
65 }
66
67 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
68 {
69         Evas_Object *image = NULL;
70         const char *icon_path = NULL;
71
72         quickpanel_setting_icon_state_set(view, state);
73
74         if (state == ICON_VIEW_STATE_ON) {
75 #ifdef BUTTON_ICON_HIGHLIGHT
76                 icon_path = BUTTON_ICON_HIGHLIGHT;
77 #endif
78         } else if (state == ICON_VIEW_STATE_DIM) {
79 #ifdef BUTTON_ICON_DIM
80                 icon_path = BUTTON_ICON_DIM;
81 #endif
82         } else {
83                 icon_path = BUTTON_ICON_NORMAL;
84         }
85
86         if (icon_path == NULL) {
87                 icon_path = BUTTON_ICON_NORMAL;
88         }
89         image = quickpanel_setting_icon_image_new(view, icon_path);
90         quickpanel_setting_icon_content_set(view, image);
91         quickpanel_setting_icon_text_set(view, BUTTON_LABEL, state);
92 }
93
94 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
95 {
96         retif(module == NULL, , "Invalid parameter!");
97         retif(module->loader->extra_handler_1 == NULL, , "Invalid parameter!");
98
99         quickpanel_setting_module_icon_timer_del(module);
100
101         if (tethering_is_enabled(module->loader->extra_handler_1, TETHERING_TYPE_WIFI)) {
102                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
103         } else {
104                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
105         }
106
107         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
108         quickpanel_setting_module_icon_timer_del(module);
109
110         quickpanel_setting_module_icon_view_update(module,
111                         quickpanel_setting_module_icon_state_get(module),
112                         FLAG_VALUE_VOID);
113 }
114
115 static void _tethering_enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
116 {
117         QP_Module_Setting *module = (QP_Module_Setting *)user_data;
118         retif(module == NULL, , "Invalid parameter!");
119
120         retif(type != TETHERING_TYPE_WIFI, , "Another type of tethering is enabled - type:%d", type);
121
122         if (result != TETHERING_ERROR_NONE) {
123                 if (is_requested == TRUE) {
124                         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
125                         quickpanel_setting_module_icon_timer_del(module);
126                 }
127                 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
128
129                 WARN("Failed to enable tethering - error:%x", result);
130                 return;
131         }
132
133         if (is_requested == TRUE) {
134                 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
135                 quickpanel_setting_module_icon_timer_del(module);
136         }
137         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
138         WARN("WIFI tethering is enabled - type:%d", type);
139
140         return;
141 }
142
143 static void _tethering_disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
144 {
145         QP_Module_Setting *module = (QP_Module_Setting *)user_data;
146         retif(module == NULL, , "Invalid parameter!");
147         retif(module->loader == NULL, , "Invalid parameter!");
148
149         if (result != TETHERING_ERROR_NONE && type == TETHERING_TYPE_WIFI) {
150                 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
151                 quickpanel_setting_module_icon_timer_del(module);
152                 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
153
154                 WARN("Failed to disable tethering - error:%x", result);
155                 return;
156         }
157
158         if (type == TETHERING_TYPE_WIFI) {
159                 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
160                 quickpanel_setting_module_icon_timer_del(module);
161                 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
162                 WARN("WIFI tethering is disabled - cause:%x", cause);
163         } else {
164                 WARN("Ignored tethering event - result:%x type:%d cause:%x", result, type, cause);
165         }
166
167         return;
168 }
169
170 static int _tethering_enabled_set(void *data, Eina_Bool state)
171 {
172         QP_Module_Setting *module = (QP_Module_Setting *)data;
173
174         if (module == NULL || module->loader->extra_handler_1 == NULL) {
175                 ERR("invalid parameter\n");
176                 return QP_FAIL;
177         }
178
179         if (state) {
180                 if (tethering_is_enabled(NULL, TETHERING_TYPE_WIFI) == FALSE) {
181                         // for checking pre-conditions, popup will be provided by mobileap-syspopup
182                         bundle *b = bundle_create();
183                         if (!b) {
184                                 ERR("Failed to create bundle.");
185                                 return QP_FAIL;
186                         }
187                         bundle_add(b, "msg", "Confirm WiFi tethering on");
188                         syspopup_launch(MOBILE_AP_SYSPOPUP_NAME, b);
189                         bundle_free(b);
190                 }
191         } else {
192                 if (tethering_is_enabled(NULL, TETHERING_TYPE_WIFI) == TRUE) {
193                         bundle *b = bundle_create();
194                         if (!b) {
195                                 ERR("Failed to create bundle.");
196                                 return QP_FAIL;
197                         }
198                         bundle_add(b, "msg", "Confirm WiFi tethering off");
199                         syspopup_launch(MOBILE_AP_SYSPOPUP_NAME, b);
200                         bundle_free(b);
201                 }
202         }
203
204         return QP_OK;
205 }
206
207 static void _mouse_clicked_cb(void *data,
208                 Evas_Object *obj, const char *emission, const char *source)
209 {
210         int ret = 0;
211         int is_on = 0;
212         QP_Module_Setting *module = (QP_Module_Setting *)data;
213         retif(module == NULL, , "Invalid parameter!");
214
215         if (quickpanel_setting_module_is_icon_clickable(module) == 0) {
216                 DBG("Icon is not clickable");
217                 return;
218         }
219
220         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
221                 ret = _tethering_enabled_set(module, EINA_TRUE);
222                 if (ret != QP_OK) {
223                         ERR("Failed to enable tethering");
224                         return;
225                 }
226                 is_on = 1;
227         } else {
228                 ret = _tethering_enabled_set(module, EINA_FALSE);
229                 if (ret != QP_OK) {
230                         ERR("Failed to disable tethering");
231                         return;
232                 }
233                 is_on = 0;
234         }
235
236         if (ret == QP_OK) {
237                 if (is_on == 1) {
238                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
239                 } else {
240                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
241                 }
242                 quickpanel_setting_module_icon_timer_add(module);
243         }
244         return;
245 }
246
247 static int _register_module_event_handler(void *data)
248 {
249         tethering_error_e ret = TETHERING_ERROR_NONE;
250         QP_Module_Setting *module = (QP_Module_Setting *)data;
251         retif(module == NULL, ret, "Invalid parameter!");
252
253         ret = tethering_create(&(module->loader->extra_handler_1));
254         msgif(ret != TETHERING_ERROR_NONE, "fail to create tethering handler");
255
256         ret = tethering_set_enabled_cb(module->loader->extra_handler_1,
257                         TETHERING_TYPE_WIFI, _tethering_enabled_cb, data);
258         msgif(ret != TETHERING_ERROR_NONE, "fail to register enabled callback");
259
260         ret = tethering_set_disabled_cb(module->loader->extra_handler_1,
261                         TETHERING_TYPE_WIFI, _tethering_disabled_cb, data);
262         msgif(ret != TETHERING_ERROR_NONE, "fail to register disabled callback");
263
264         return QP_OK;
265 }
266
267 static int _unregister_module_event_handler(void *data)
268 {
269         tethering_error_e ret =TETHERING_ERROR_NONE;
270         QP_Module_Setting *module = (QP_Module_Setting *)data;
271         retif(module == NULL, ret, "Invalid parameter!");
272
273         if (module->loader->extra_handler_1 != NULL) {
274
275                 tethering_destroy(module->loader->extra_handler_1);
276                 module->loader->extra_handler_1 = NULL;
277         }
278
279         return QP_OK;
280 }
281
282 /****************************************************************************
283  *
284  * Quickpanel Item functions
285  *
286  ****************************************************************************/
287 static int _init(void *data)
288 {
289         int ret = QP_OK;
290
291         ret = _register_module_event_handler(data);
292
293         return ret;
294 }
295
296 static int _fini(void *data)
297 {
298         int ret = QP_OK;
299
300         ret = _unregister_module_event_handler(data);
301
302         return ret;
303 }
304
305 static void _lang_changed(void *data)
306 {
307         QP_Module_Setting *module = (QP_Module_Setting *)data;
308         retif(module == NULL, , "Invalid parameter!");
309
310         quickpanel_setting_module_icon_view_update_text(module);
311 }
312
313 static void _refresh(void *data)
314 {
315         QP_Module_Setting *module = (QP_Module_Setting *)data;
316         retif(module == NULL, , "Invalid parameter!");
317
318         quickpanel_setting_module_icon_view_update_text(module);
319 }
320
321 static void _reset_icon(QP_Module_Setting *module)
322 {
323         retif(module == NULL, , "Invalid parameter!");
324
325         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_VALUE_VOID);
326         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
327 }
328
329 static void _handler_on(void *data)
330 {
331         tethering_error_e ret = TETHERING_ERROR_NONE;
332         QP_Module_Setting *module = (QP_Module_Setting *)data;
333         retif(module == NULL, , "Invalid parameter!");
334
335         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
336         quickpanel_setting_module_icon_timer_del(module);
337
338         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
339                 ret = _tethering_enabled_set(module, EINA_TRUE);
340
341                 if (ret == QP_OK) {
342                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
343                         quickpanel_setting_module_icon_timer_add(module);
344                 } else {
345                         ERR("op failed:%d", ret);
346                 }
347         } else {
348                 ERR("the button is already turned on");
349                 _reset_icon(module);
350         }
351 }
352
353 static void _handler_off(void *data)
354 {
355         int ret = TETHERING_ERROR_NONE;
356         QP_Module_Setting *module = (QP_Module_Setting *)data;
357         retif(module == NULL, , "Invalid parameter!");
358
359         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
360         quickpanel_setting_module_icon_timer_del(module);
361
362         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
363                 ret = _tethering_enabled_set(module, EINA_FALSE);
364                 if (ret == QP_OK) {
365                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
366                         quickpanel_setting_module_icon_timer_add(module);
367                 } else {
368                         ERR("op failed:%d", ret);
369                 }
370         } else {
371                 ERR("the button is already turned off");
372                 _reset_icon(module);
373         }
374 }
375
376 static void _handler_progress_on(void *data)
377 {
378         QP_Module_Setting *module = (QP_Module_Setting *)data;
379         retif(module == NULL, , "Invalid parameter!");
380
381         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
382 }
383
384 static void _handler_progress_off(void *data)
385 {
386         QP_Module_Setting *module = (QP_Module_Setting *)data;
387         retif(module == NULL, , "Invalid parameter!");
388
389         _reset_icon(module);
390 }
391
392 static void _handler_progress_reset(void *data)
393 {
394         QP_Module_Setting *module = (QP_Module_Setting *)data;
395         retif(module == NULL, , "Invalid parameter!");
396
397         _reset_icon(module);
398 }
399
400 static int _handler_ipc(const char *command, void *data)
401 {
402         int i = 0;
403         retif(data == NULL, EINA_FALSE, "item data is NULL");
404         retif(command == NULL, EINA_FALSE, "command is NULL");
405
406         static Setting_Activity_Handler __table_handler[] = {
407                 {
408                         .command = "on",
409                         .handler = _handler_on,
410                 },
411                 {
412                         .command = "off",
413                         .handler = _handler_off,
414                 },
415                 {
416                         .command = "progress_on",
417                         .handler = _handler_progress_on,
418                 },
419                 {
420                         .command = "progress_off",
421                         .handler = _handler_progress_off,
422                 },
423                 {
424                         .command = "progress_reset",
425                         .handler = _handler_progress_reset,
426                 },
427                 {
428                         .command = NULL,
429                         .handler = NULL,
430                 },
431         };
432
433         for (i = 0; __table_handler[i].command; i++) {
434                 if (strcasecmp(__table_handler[i].command, command)) {
435                         continue;
436                 }
437
438                 if (__table_handler[i].handler != NULL) {
439                         DBG("process:%s", command);
440                         __table_handler[i].handler(data);
441                 }
442                 break;
443         }
444
445         return EINA_TRUE;
446 }
447
448 QP_Module_Setting tethering = {
449         .name                           = "wifi_hotspot",
450         .init                           = _init,
451         .fini                           = _fini,
452         .lang_changed                   = _lang_changed,
453         .refresh                        = _refresh,
454         .icon_get                       = _icon_get,
455         .label_get                      = _label_get,
456         .view_update        = _view_update,
457         .status_update          = _status_update,
458         .handler_longpress      = _long_press_cb,
459         .handler_ipc        = _handler_ipc,
460         .handler_press          = _mouse_clicked_cb,
461 };