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