tizen 2.4 release
[apps/home/quickpanel.git] / daemon / settings / modules / flightmode.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 <vconf.h>
21 #include <syspopup_caller.h>
22 #include <tapi_common.h>
23 #include <ITapiSim.h>
24 #include <ITapiModem.h>
25 #include <TapiUtility.h>
26 #include <system_settings.h>
27 #include <bundle_internal.h>
28 #include <tzsh.h>
29 #include <tzsh_quickpanel_service.h>
30
31 #include "common.h"
32 #include "quickpanel-ui.h"
33 #include "settings.h"
34 #include "setting_utils.h"
35 #include "setting_module_api.h"
36 #include "settings_icon_common.h"
37
38 #define BUTTON_LABEL _("IDS_ST_BUTTON2_FLIGHT_NMODE")
39 #define BUTTON_ICON_NORMAL "quick_icon_flightmode.png"
40 #define BUTTON_ICON_HIGHLIGHT NULL
41 #define BUTTON_ICON_DIM NULL
42 #define PACKAGE_SETTING_MENU "setting-flightmode-efl"
43 #define SYSPOPUP_NAME "mode-syspopup"
44
45 static Eina_Bool fly_icon_is_locked = EINA_FALSE;
46 static Ecore_Timer *timer = NULL;
47
48 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
49
50 static const char *_label_get(void)
51 {
52         return BUTTON_LABEL;
53 }
54
55 static const char *_icon_get(qp_setting_icon_image_type type)
56 {
57         if (type == QP_SETTING_ICON_NORMAL) {
58                 return BUTTON_ICON_NORMAL;
59         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
60                 return BUTTON_ICON_HIGHLIGHT;
61         } else if (type == QP_SETTING_ICON_DIM) {
62 #ifdef BUTTON_ICON_DIM
63                 return BUTTON_ICON_DIM;
64 #endif
65         }
66
67         return NULL;
68 }
69
70 static Eina_Bool _unlock_fly_icon(void *data)
71 {
72         fly_icon_is_locked = EINA_FALSE;
73         ecore_timer_del(timer);
74         timer = NULL;
75
76         return ECORE_CALLBACK_CANCEL;
77 }
78
79 static void _long_press_cb(void *data)
80 {
81 #ifdef PACKAGE_SETTING_MENU
82         if (fly_icon_is_locked == EINA_TRUE) {
83                 LOGD("Fly icon is locked");
84                 return;
85         }
86         quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
87 #endif
88 }
89
90 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
91 {
92         Evas_Object *image = NULL;
93         const char *icon_path = NULL;
94
95         quickpanel_setting_icon_state_set(view, state);
96
97         if (state == ICON_VIEW_STATE_ON) {
98 #ifdef BUTTON_ICON_HIGHLIGHT
99                 icon_path = BUTTON_ICON_HIGHLIGHT;
100 #endif
101         } else if (state == ICON_VIEW_STATE_DIM) {
102 #ifdef BUTTON_ICON_DIM
103                 icon_path = BUTTON_ICON_DIM;
104 #endif
105         } else {
106                 icon_path = BUTTON_ICON_NORMAL;
107         }
108
109         if (icon_path == NULL) {
110                 icon_path = BUTTON_ICON_NORMAL;
111         }
112         image = quickpanel_setting_icon_image_new(view, icon_path);
113         quickpanel_setting_icon_content_set(view, image);
114         quickpanel_setting_icon_text_set(view, BUTTON_LABEL, state);
115 }
116
117 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
118 {
119         LOGD("");
120         int ret = 0;
121         bool status = false;
122         retif(module == NULL, , "Invalid parameter!");
123
124         ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE, &status);
125         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "fail to get VCONFKEY_TELEPHONY_FLIGHT_MODE:%d", ret);
126
127         if (status == true) {
128                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
129         } else {
130                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
131         }
132
133         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
134         quickpanel_setting_module_icon_timer_del(module);
135
136         quickpanel_setting_module_icon_view_update(module, quickpanel_setting_module_icon_state_get(module), FLAG_VALUE_VOID);
137 }
138
139 static void _tapi_flight_mode_cb(TapiHandle *handle, int result, void *data, void *user_data)
140 {
141         ERR("flight mode result:%d", result);
142         _status_update(user_data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
143 }
144
145 static int _tapi_flight_mode_set(int on, void *data)
146 {
147         LOGD("");
148         int ret = QP_OK;
149         int ret_t = TAPI_API_SUCCESS;
150         TapiHandle *tapi_handle = NULL;
151         QP_Module_Setting *module = (QP_Module_Setting *)data;
152         retif(module == NULL, QP_FAIL, "Invalid parameter!");
153
154         tapi_handle = tel_init(NULL);
155         retif(tapi_handle == NULL, QP_FAIL, "failed to initialized tapi handler");
156
157         if (on == 1) {
158                 ret_t = tel_set_flight_mode(tapi_handle,
159                                 TAPI_POWER_FLIGHT_MODE_ENTER, _tapi_flight_mode_cb, data);
160                 if (ret_t != TAPI_API_SUCCESS) {
161                         ret = QP_FAIL;
162                         ERR("tel_set_flight_mode enter error:%d", ret_t);
163                 }
164         } else {
165                 ret_t = tel_set_flight_mode(tapi_handle,
166                                 TAPI_POWER_FLIGHT_MODE_LEAVE, _tapi_flight_mode_cb, data);
167                 if (ret_t != TAPI_API_SUCCESS) {
168                         ret = QP_FAIL;
169                         ERR("tel_set_flight_mode leave error:%d", ret_t);
170                 }
171         }
172
173         if ((ret_t = tel_deinit(tapi_handle)) != TAPI_API_SUCCESS) {
174                 ERR("failed to deinitialized tapi handler:%d", ret_t);
175         }
176
177         return ret;
178 }
179
180 static void _turn_on(int is_on)
181 {
182         LOGD("");
183         bundle *b = NULL;
184         b = bundle_create();
185         if (b != NULL) {
186                 if (is_on) {
187                         bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "MODE_SYSTEM_FLIGHTMODE_ON");
188                 } else {
189                         bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "MODE_SYSTEM_FLIGHTMODE_OFF");
190                 }
191                 syspopup_launch(SYSPOPUP_NAME, b);
192                 bundle_free(b);
193         } else {
194                 ERR("failed to create a bundle");
195         }
196
197         timer = ecore_timer_add(1.0, _unlock_fly_icon, NULL);
198 }
199
200 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
201 {
202         QP_Module_Setting *module = (QP_Module_Setting *)data;
203         LOGD("");
204         retif(module == NULL, , "Invalid parameter!");
205
206         if (fly_icon_is_locked == EINA_TRUE) {
207                 LOGD("Fly icon is locked");
208                 return;
209         }
210
211         if (quickpanel_setting_module_is_icon_clickable(module) == 0) {
212                 LOGD("Fly icon is not clickable");
213                 return;
214         }
215
216         fly_icon_is_locked = EINA_TRUE;
217
218         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
219                 _turn_on(1);
220         } else {
221                 _turn_on(0);
222         }
223 }
224
225 static void _tapi_flight_mode_vconf_cb(system_settings_key_e key, void *data)
226 {
227         _status_update(data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
228 }
229
230 static int _register_module_event_handler(void *data)
231 {
232         int ret = 0;
233
234         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE, _tapi_flight_mode_vconf_cb, data);
235         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to notify key(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE) : %d", ret);
236
237         return QP_OK;
238 }
239
240 static int _unregister_module_event_handler(void *data)
241 {
242         int ret = 0;
243
244         ret = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE);
245         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to ignore key(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE) : %d", ret);
246
247         return QP_OK;
248 }
249
250 /****************************************************************************
251  *
252  * Quickpanel Item functions
253  *
254  ****************************************************************************/
255
256 static int _init(void *data)
257 {
258         int ret = QP_OK;
259
260         ret = _register_module_event_handler(data);
261
262         return ret;
263 }
264
265 static int _fini(void *data)
266 {
267         int ret = QP_OK;
268
269         ret = _unregister_module_event_handler(data);
270
271         return ret;
272 }
273
274 static void _lang_changed(void *data)
275 {
276         QP_Module_Setting *module = (QP_Module_Setting *)data;
277         retif(module == NULL, , "Invalid parameter!");
278
279         quickpanel_setting_module_icon_view_update_text(module);
280 }
281
282 static void _refresh(void *data)
283 {
284         QP_Module_Setting *module = (QP_Module_Setting *)data;
285         retif(module == NULL, , "Invalid parameter!");
286
287         quickpanel_setting_module_icon_view_update_text(module);
288 }
289
290 static void _reset_icon(QP_Module_Setting *module)
291 {
292         retif(module == NULL, , "Invalid parameter!");
293
294         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_VALUE_VOID);
295         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
296 }
297
298 static void _handler_on(void *data)
299 {
300         int ret = 0;
301         QP_Module_Setting *module = (QP_Module_Setting *)data;
302         retif(module == NULL, , "Invalid parameter!");
303
304         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
305         quickpanel_setting_module_icon_timer_del(module);
306
307         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
308                 ret = _tapi_flight_mode_set(1, module);
309
310                 if (ret == QP_OK) {
311                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
312                         quickpanel_setting_module_icon_timer_add(module);
313                 } else {
314                         ERR("op failed:%d", ret);
315                 }
316         } else {
317                 ERR("the button is already turned on");
318                 _reset_icon(module);
319         }
320 }
321
322 static void _handler_off(void *data)
323 {
324         int ret = 0;
325         QP_Module_Setting *module = (QP_Module_Setting *)data;
326         retif(module == NULL, , "Invalid parameter!");
327
328         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
329         quickpanel_setting_module_icon_timer_del(module);
330
331         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
332                 ret = _tapi_flight_mode_set(0, module);
333
334                 if (ret == QP_OK) {
335                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
336                         quickpanel_setting_module_icon_timer_add(module);
337                 } else {
338                         ERR("op failed:%d", ret);
339                 }
340         } else {
341                 ERR("the button is already turned off");
342                 _reset_icon(module);
343         }
344 }
345
346 static void _handler_progress_on(void *data)
347 {
348         QP_Module_Setting *module = (QP_Module_Setting *)data;
349         retif(module == NULL, , "Invalid parameter!");
350
351         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_VALUE_VOID);
352 }
353
354 static void _handler_progress_off(void *data)
355 {
356         QP_Module_Setting *module = (QP_Module_Setting *)data;
357         retif(module == NULL, , "Invalid parameter!");
358
359         _reset_icon(module);
360 }
361
362 static int _handler_ipc(const char *command, void *data)
363 {
364         int i = 0;
365         retif(data == NULL, EINA_FALSE, "item data is NULL");
366         retif(command == NULL, EINA_FALSE, "command is NULL");
367
368         static Setting_Activity_Handler __table_handler[] = {
369                 {
370                         .command = "on",
371                         .handler = _handler_on,
372                 },
373                 {
374                         .command = "off",
375                         .handler = _handler_off,
376                 },
377                 {
378                         .command = "progress_on",
379                         .handler = _handler_progress_on,
380                 },
381                 {
382                         .command = "progress_off",
383                         .handler = _handler_progress_off,
384                 },
385                 {
386                         .command = NULL,
387                         .handler = NULL,
388                 },
389         };
390
391         for (i = 0; __table_handler[i].command; i++) {
392                 if (strcmp(__table_handler[i].command, command)) {
393                         continue;
394                 }
395
396                 if (__table_handler[i].handler != NULL) {
397                         DBG("process:%s", command);
398                         __table_handler[i].handler(data);
399                 }
400                 break;
401         }
402
403         return EINA_TRUE;
404 }
405
406 QP_Module_Setting flightmode = {
407         .name                           = "flightmode",
408         .init                           = _init,
409         .fini                           = _fini,
410         .lang_changed           = _lang_changed,
411         .refresh                        = _refresh,
412         .icon_get                       = _icon_get,
413         .label_get                      = _label_get,
414         .view_update        = _view_update,
415         .status_update          = _status_update,
416         .handler_longpress      = _long_press_cb,
417         .handler_ipc        = _handler_ipc,
418         .handler_press          = _mouse_clicked_cb,
419 };