2 * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <tethering.h>
22 #include <bundle_internal.h>
23 #include <net_connection.h>
24 #include <syspopup_caller.h>
26 #include "quickpanel-ui.h"
28 #include "setting_utils.h"
29 #include "setting_module_api.h"
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"
38 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
40 static const char *_label_get(void)
45 static const char *_icon_get(qp_setting_icon_image_type type)
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;
60 static void _long_press_cb(void *data)
62 #ifdef PACKAGE_SETTING_MENU
63 quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
67 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
69 Evas_Object *image = NULL;
70 const char *icon_path = NULL;
72 quickpanel_setting_icon_state_set(view, state);
74 if (state == ICON_VIEW_STATE_ON) {
75 #ifdef BUTTON_ICON_HIGHLIGHT
76 icon_path = BUTTON_ICON_HIGHLIGHT;
78 } else if (state == ICON_VIEW_STATE_DIM) {
79 #ifdef BUTTON_ICON_DIM
80 icon_path = BUTTON_ICON_DIM;
83 icon_path = BUTTON_ICON_NORMAL;
86 if (icon_path == NULL) {
87 icon_path = BUTTON_ICON_NORMAL;
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);
94 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
96 retif(module == NULL, , "Invalid parameter!");
97 retif(module->loader->extra_handler_1 == NULL, , "Invalid parameter!");
99 quickpanel_setting_module_icon_timer_del(module);
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);
104 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
107 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
108 quickpanel_setting_module_icon_timer_del(module);
110 quickpanel_setting_module_icon_view_update(module,
111 quickpanel_setting_module_icon_state_get(module),
115 static void _tethering_enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
117 QP_Module_Setting *module = (QP_Module_Setting *)user_data;
118 retif(module == NULL, , "Invalid parameter!");
120 retif(type != TETHERING_TYPE_WIFI, , "Another type of tethering is enabled - type:%d", type);
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);
127 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
129 WARN("Failed to enable tethering - error:%x", result);
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);
137 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
138 WARN("WIFI tethering is enabled - type:%d", type);
143 static void _tethering_disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
145 QP_Module_Setting *module = (QP_Module_Setting *)user_data;
146 retif(module == NULL, , "Invalid parameter!");
147 retif(module->loader == NULL, , "Invalid parameter!");
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);
154 WARN("Failed to disable tethering - error:%x", result);
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);
164 WARN("Ignored tethering event - result:%x type:%d cause:%x", result, type, cause);
170 static int _tethering_enabled_set(void *data, Eina_Bool state)
172 QP_Module_Setting *module = (QP_Module_Setting *)data;
174 if (module == NULL || module->loader->extra_handler_1 == NULL) {
175 ERR("invalid parameter\n");
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();
184 ERR("Failed to create bundle.");
187 bundle_add(b, "msg", "Confirm WiFi tethering on");
188 syspopup_launch(MOBILE_AP_SYSPOPUP_NAME, b);
192 if (tethering_is_enabled(NULL, TETHERING_TYPE_WIFI) == TRUE) {
193 bundle *b = bundle_create();
195 ERR("Failed to create bundle.");
198 bundle_add(b, "msg", "Confirm WiFi tethering off");
199 syspopup_launch(MOBILE_AP_SYSPOPUP_NAME, b);
207 static void _mouse_clicked_cb(void *data,
208 Evas_Object *obj, const char *emission, const char *source)
212 QP_Module_Setting *module = (QP_Module_Setting *)data;
213 retif(module == NULL, , "Invalid parameter!");
215 if (quickpanel_setting_module_is_icon_clickable(module) == 0) {
216 DBG("Icon is not clickable");
220 if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
221 ret = _tethering_enabled_set(module, EINA_TRUE);
223 ERR("Failed to enable tethering");
228 ret = _tethering_enabled_set(module, EINA_FALSE);
230 ERR("Failed to disable tethering");
238 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
240 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
242 quickpanel_setting_module_icon_timer_add(module);
247 static int _register_module_event_handler(void *data)
249 tethering_error_e ret = TETHERING_ERROR_NONE;
250 QP_Module_Setting *module = (QP_Module_Setting *)data;
251 retif(module == NULL, ret, "Invalid parameter!");
253 ret = tethering_create(&(module->loader->extra_handler_1));
254 msgif(ret != TETHERING_ERROR_NONE, "fail to create tethering handler");
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");
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");
267 static int _unregister_module_event_handler(void *data)
269 tethering_error_e ret =TETHERING_ERROR_NONE;
270 QP_Module_Setting *module = (QP_Module_Setting *)data;
271 retif(module == NULL, ret, "Invalid parameter!");
273 if (module->loader->extra_handler_1 != NULL) {
275 tethering_destroy(module->loader->extra_handler_1);
276 module->loader->extra_handler_1 = NULL;
282 /****************************************************************************
284 * Quickpanel Item functions
286 ****************************************************************************/
287 static int _init(void *data)
291 ret = _register_module_event_handler(data);
296 static int _fini(void *data)
300 ret = _unregister_module_event_handler(data);
305 static void _lang_changed(void *data)
307 QP_Module_Setting *module = (QP_Module_Setting *)data;
308 retif(module == NULL, , "Invalid parameter!");
310 quickpanel_setting_module_icon_view_update_text(module);
313 static void _refresh(void *data)
315 QP_Module_Setting *module = (QP_Module_Setting *)data;
316 retif(module == NULL, , "Invalid parameter!");
318 quickpanel_setting_module_icon_view_update_text(module);
321 static void _reset_icon(QP_Module_Setting *module)
323 retif(module == NULL, , "Invalid parameter!");
325 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_VALUE_VOID);
326 _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
329 static void _handler_on(void *data)
331 tethering_error_e ret = TETHERING_ERROR_NONE;
332 QP_Module_Setting *module = (QP_Module_Setting *)data;
333 retif(module == NULL, , "Invalid parameter!");
335 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
336 quickpanel_setting_module_icon_timer_del(module);
338 if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
339 ret = _tethering_enabled_set(module, EINA_TRUE);
342 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
343 quickpanel_setting_module_icon_timer_add(module);
345 ERR("op failed:%d", ret);
348 ERR("the button is already turned on");
353 static void _handler_off(void *data)
355 int ret = TETHERING_ERROR_NONE;
356 QP_Module_Setting *module = (QP_Module_Setting *)data;
357 retif(module == NULL, , "Invalid parameter!");
359 quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
360 quickpanel_setting_module_icon_timer_del(module);
362 if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
363 ret = _tethering_enabled_set(module, EINA_FALSE);
365 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
366 quickpanel_setting_module_icon_timer_add(module);
368 ERR("op failed:%d", ret);
371 ERR("the button is already turned off");
376 static void _handler_progress_on(void *data)
378 QP_Module_Setting *module = (QP_Module_Setting *)data;
379 retif(module == NULL, , "Invalid parameter!");
381 quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
384 static void _handler_progress_off(void *data)
386 QP_Module_Setting *module = (QP_Module_Setting *)data;
387 retif(module == NULL, , "Invalid parameter!");
392 static void _handler_progress_reset(void *data)
394 QP_Module_Setting *module = (QP_Module_Setting *)data;
395 retif(module == NULL, , "Invalid parameter!");
400 static int _handler_ipc(const char *command, void *data)
403 retif(data == NULL, EINA_FALSE, "item data is NULL");
404 retif(command == NULL, EINA_FALSE, "command is NULL");
406 static Setting_Activity_Handler __table_handler[] = {
409 .handler = _handler_on,
413 .handler = _handler_off,
416 .command = "progress_on",
417 .handler = _handler_progress_on,
420 .command = "progress_off",
421 .handler = _handler_progress_off,
424 .command = "progress_reset",
425 .handler = _handler_progress_reset,
433 for (i = 0; __table_handler[i].command; i++) {
434 if (strcasecmp(__table_handler[i].command, command)) {
438 if (__table_handler[i].handler != NULL) {
439 DBG("process:%s", command);
440 __table_handler[i].handler(data);
448 QP_Module_Setting tethering = {
449 .name = "wifi_hotspot",
452 .lang_changed = _lang_changed,
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,