tizen 2.4 release
[apps/home/quickpanel.git] / daemon / settings / modules / mobile_data.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 <system_settings.h>
23 #include <bundle_internal.h>
24 #include <tzsh.h>
25 #include <tzsh_quickpanel_service.h>
26
27 #include "common_uic.h"
28 #include "common.h"
29 #include "quickpanel-ui.h"
30 #include "settings.h"
31 #include "setting_utils.h"
32 #include "setting_module_api.h"
33 #include "settings_icon_common.h"
34
35 #define BUTTON_LABEL _("IDS_ST_BUTTON2_MOBILE_NDATA")
36 #define BUTTON_ICON_NORMAL "quick_icon_mobile_data.png"
37 #define BUTTON_ICON_HIGHLIGHT NULL
38 #define BUTTON_ICON_DIM NULL
39 #define PACKAGE_SETTING_MENU "setting-network-efl"
40 #define SYSPOPUP_NAME "mode-syspopup"
41
42 #ifndef VCONFKEY_SETAPPL_MOBILE_DATA_ON_REMINDER
43 #define VCONFKEY_SETAPPL_MOBILE_DATA_ON_REMINDER "db/setting/network/mobile_data_on_reminder"
44 #endif
45 #ifndef VCONFKEY_SETAPPL_MOBILE_DATA_OFF_REMINDER
46 #define VCONFKEY_SETAPPL_MOBILE_DATA_OFF_REMINDER "db/setting/network/mobile_data_off_reminder"
47 #endif
48
49 static int _is_simcard_inserted(void);
50 static int _is_in_flightmode(void);
51 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
52
53 static const char *_label_get(void)
54 {
55         return BUTTON_LABEL;
56 }
57
58 static const char *_icon_get(qp_setting_icon_image_type type)
59 {
60         if (type == QP_SETTING_ICON_NORMAL) {
61                 return BUTTON_ICON_NORMAL;
62         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
63                 return BUTTON_ICON_HIGHLIGHT;
64         } else if (type == QP_SETTING_ICON_DIM) {
65 #ifdef BUTTON_ICON_DIM
66                 return BUTTON_ICON_DIM;
67 #endif
68         }
69
70         return NULL;
71 }
72
73 static void _long_press_cb(void *data)
74 {
75 #ifdef PACKAGE_SETTING_MENU
76         struct appdata *ad = quickpanel_get_app_data();
77         retif(ad == NULL, , "invalid data.");
78
79         if (_is_in_flightmode() == 1) {
80                 quickpanel_setting_create_timeout_popup(ad->win, _("IDS_SCP_BODY_UNABLE_TO_CONNECT_TO_MOBILE_NETWORKS_WHILE_FLIGHT_MODE_IS_ENABLED_CONNECT_TO_A_WI_FI_NETWORK_INSTEAD_OR_DISABLE_FLIGHT_MODE_AND_TRY_AGAIN"));
81                 return;
82         }
83
84         if (_is_simcard_inserted() == 0) {
85                 quickpanel_setting_create_timeout_popup(ad->win, _("IDS_ST_BODY_INSERT_SIM_CARD_TO_ACCESS_NETWORK_SERVICES"));
86                 return;
87         }
88
89         quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
90 #endif
91 }
92
93 static int _need_display_popup(int is_on)
94 {
95         int ret = -1;
96         int status = 0;
97
98         if (is_on == 1) {
99                 ret = vconf_get_bool(VCONFKEY_SETAPPL_MOBILE_DATA_ON_REMINDER, &status);
100                 msgif(ret != 0, "failed to get VCONFKEY_SETAPPL_MOBILE_DATA_ON_REMINDER %d %d", ret, is_on);
101                 if (ret == 0 && status == true) {
102                         return 1;
103                 }
104         } else {
105                 ret = vconf_get_bool(VCONFKEY_SETAPPL_MOBILE_DATA_OFF_REMINDER, &status);
106                 msgif(ret != 0, "failed to get VCONFKEY_SETAPPL_MOBILE_DATA_OFF_REMINDER %d %d", ret, is_on);
107                 if (ret == 0 && status == 1) {
108                         return 1;
109                 }
110         }
111
112         return 0;
113 }
114
115 static void _turn_on(int is_on)
116 {
117         int ret = 0;
118
119         if (is_on) {
120                 ret = system_settings_set_value_bool(SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, 1);
121                 msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to set SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED %d %d", ret, is_on);
122         } else {
123                 ret = system_settings_set_value_bool(SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, 0);
124                 msgif(ret != SYSTEM_SETTINGS_ERROR_NONE,"failed to set SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED: %d %d", ret, is_on);
125         }
126 }
127
128 static void _turn_on_with_popup(int is_on)
129 {
130         bundle *b = NULL;
131         b = bundle_create();
132         if (b != NULL) {
133                 if (is_on) {
134                         bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "MODE_SYSTEM_MOBILEDATA_ON");
135                 } else {
136                         bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "MODE_SYSTEM_MOBILEDATA_OFF");
137                 }
138                 syspopup_launch(SYSPOPUP_NAME, b);
139                 bundle_free(b);
140         } else {
141                 ERR("failed to create a bundle");
142         }
143 }
144
145 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
146 {
147         Evas_Object *image = NULL;
148         const char *icon_path = NULL;
149
150         quickpanel_setting_icon_state_set(view, state);
151
152         if (state == ICON_VIEW_STATE_ON) {
153 #ifdef BUTTON_ICON_HIGHLIGHT
154                 icon_path = BUTTON_ICON_HIGHLIGHT;
155 #endif
156         } else if (state == ICON_VIEW_STATE_DIM) {
157 #ifdef BUTTON_ICON_DIM
158                 icon_path = BUTTON_ICON_DIM;
159 #endif
160         } else {
161                 icon_path = BUTTON_ICON_NORMAL;
162         }
163
164         if (icon_path == NULL) {
165                 icon_path = BUTTON_ICON_NORMAL;
166         }
167         image = quickpanel_setting_icon_image_new(view, icon_path);
168         quickpanel_setting_icon_content_set(view, image);
169         quickpanel_setting_icon_text_set(view, BUTTON_LABEL, state);
170 }
171
172 static int _is_simcard_inserted(void)
173 {
174         int ret_1 = QP_FAIL;
175         int ret_2 = QP_FAIL;
176         int sim_status_1 = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
177         int sim_status_2 = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
178
179         ret_1 = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &sim_status_1);
180         msgif(ret_1 != QP_OK, "failed to get the VCONFKEY_TELEPHONY_SIM_SLOT : %d", ret_1);
181
182         ret_2 = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT2, &sim_status_2);
183         msgif(ret_2 != QP_OK, "failed to get the VCONFKEY_TELEPHONY_SIM_SLOT2 : %d", ret_2);
184
185         INFO("MOBILE DATA SIM CARD: %d %d", sim_status_1, sim_status_2);
186
187         if ((ret_1 == QP_OK && sim_status_1 == VCONFKEY_TELEPHONY_SIM_INSERTED) ||
188                         (ret_2 == QP_OK && sim_status_2 == VCONFKEY_TELEPHONY_SIM_INSERTED)) {
189                 return 1;
190         }
191
192         return 0;
193 }
194
195 static int _is_in_flightmode(void)
196 {
197         int ret = QP_FAIL;
198         bool flight_mode = false;
199
200         ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE, &flight_mode);
201         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to get the SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE : %d", ret);
202         if (ret == QP_OK && flight_mode == true) {
203                 return 1;
204         }
205
206         return 0;
207 }
208
209 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
210 {
211         int ret = 0;
212         bool status = false;
213         retif(module == NULL, , "Invalid parameter!");
214
215         quickpanel_setting_module_icon_timer_del(module);
216
217         if (quickpanel_uic_is_emul() == 1) {
218                 status = true;
219         } else if (_is_in_flightmode() == 1) {
220                 status = false;
221         } else if (_is_simcard_inserted() == 0) {
222                 status = false;
223         } else {
224                 ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, &status);
225                 msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "fail to get SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED:%d", ret);
226         }
227
228         if (status == true) {
229                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
230         } else {
231                 quickpanel_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
232         }
233
234         quickpanel_setting_module_icon_view_update(module,
235                         quickpanel_setting_module_icon_state_get(module),
236                         FLAG_VALUE_VOID);
237 }
238
239 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
240 {
241         QP_Module_Setting *module = (QP_Module_Setting *)data;
242         retif(module == NULL, , "Invalid parameter!");
243
244         if (quickpanel_setting_module_is_icon_clickable(module) == 0) {
245                 return;
246         }
247
248         struct appdata *ad = quickpanel_get_app_data();
249         retif(ad == NULL, , "invalid data.");
250
251         if (quickpanel_uic_is_emul() == 1) {
252                 quickpanel_setting_create_timeout_popup(ad->win, _NOT_LOCALIZED("Unsupported."));
253                 return;
254         }
255
256         if (_is_in_flightmode() == 1) {
257                 quickpanel_setting_create_timeout_popup(ad->win, _("IDS_SCP_BODY_UNABLE_TO_CONNECT_TO_MOBILE_NETWORKS_WHILE_FLIGHT_MODE_IS_ENABLED_CONNECT_TO_A_WI_FI_NETWORK_INSTEAD_OR_DISABLE_FLIGHT_MODE_AND_TRY_AGAIN"));
258                 return;
259         }
260
261         if (_is_simcard_inserted() == 0) {
262                 quickpanel_setting_create_timeout_popup(ad->win, _("IDS_ST_BODY_INSERT_SIM_CARD_TO_ACCESS_NETWORK_SERVICES"));
263                 return;
264         }
265
266         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
267                 /* disable */
268                 if (_need_display_popup(0) == 1) {
269                         _turn_on_with_popup(0);
270                 } else {
271                         _turn_on(0);
272                 }
273         } else {
274                 /* enable */
275                 if (_need_display_popup(1) == 1) {
276                         _turn_on_with_popup(1);
277                 } else {
278                         _turn_on(1);
279                 }
280         }
281 }
282
283 static void _mobiledata_vconf_cb(keynode_t *key, void *data)
284 {
285         _status_update(data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
286 }
287
288 static void _mobiledata_setting_cb(system_settings_key_e key, void *data)
289 {
290         _status_update(data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
291 }
292
293 static int _register_module_event_handler(void *data)
294 {
295         int ret = 0;
296
297         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, _mobiledata_setting_cb, data);
298         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to notify key(%s) : %d", SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, ret);
299
300         ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, _mobiledata_vconf_cb, data);
301         msgif(ret != 0, "failed to notify key(%s) : %d", VCONFKEY_TELEPHONY_SIM_SLOT, ret);
302
303         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE, _mobiledata_setting_cb, data);
304         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to notify key(%s) : %d", SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE, ret);
305
306         return QP_OK;
307 }
308
309 static int _unregister_module_event_handler(void *data)
310 {
311         int ret = 0;
312
313         ret = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED);
314         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to ignore key(%s) : %d", SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, ret);
315
316         ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SIM_SLOT, _mobiledata_vconf_cb);
317         msgif(ret != 0, "failed to ignore key(%s) : %d", VCONFKEY_TELEPHONY_SIM_SLOT, ret);
318
319         ret = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE);
320         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "failed to ignore key(%s) : %d", SYSTEM_SETTINGS_KEY_NETWORK_FLIGHT_MODE, ret);
321
322         return QP_OK;
323 }
324
325 /****************************************************************************
326  *
327  * Quickpanel Item functions
328  *
329  ****************************************************************************/
330
331 static int _init(void *data)
332 {
333         int ret = QP_OK;
334
335         ret = _register_module_event_handler(data);
336
337         return ret;
338 }
339
340 static int _fini(void *data)
341 {
342         int ret = QP_OK;
343
344         ret = _unregister_module_event_handler(data);
345
346         return ret;
347 }
348
349 static void _lang_changed(void *data)
350 {
351         QP_Module_Setting *module = (QP_Module_Setting *)data;
352         retif(module == NULL, , "Invalid parameter!");
353
354         quickpanel_setting_module_icon_view_update_text(module);
355 }
356
357 static void _refresh(void *data)
358 {
359         QP_Module_Setting *module = (QP_Module_Setting *)data;
360         retif(module == NULL, , "Invalid parameter!");
361
362         quickpanel_setting_module_icon_view_update_text(module);
363 }
364
365 static void _reset_icon(QP_Module_Setting *module)
366 {
367         retif(module == NULL, , "Invalid parameter!");
368
369         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_VALUE_VOID);
370         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
371 }
372
373 static void _handler_on(void *data)
374 {
375         int ret = 0;
376         QP_Module_Setting *module = (QP_Module_Setting *)data;
377         retif(module == NULL, , "Invalid parameter!");
378
379         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
380         quickpanel_setting_module_icon_timer_del(module);
381
382         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF) {
383                 ret = system_settings_set_value_bool(SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLED, 1);
384
385                 if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
386                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
387                         quickpanel_setting_module_icon_timer_add(module);
388                 } else {
389                         ERR("op failed:%d", ret);
390                 }
391         } else {
392                 ERR("the button is already turned on");
393                 _reset_icon(module);
394         }
395 }
396
397 static void _handler_off(void *data)
398 {
399         int ret = 0;
400         QP_Module_Setting *module = (QP_Module_Setting *)data;
401         retif(module == NULL, , "Invalid parameter!");
402
403         quickpanel_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
404         quickpanel_setting_module_icon_timer_del(module);
405
406         if (quickpanel_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
407                 ret = vconf_set_bool(VCONFKEY_3G_ENABLE, 0);
408
409                 if (ret == 0) {
410                         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
411                         quickpanel_setting_module_icon_timer_add(module);
412                 } else {
413                         ERR("op failed:%d", ret);
414                 }
415         } else {
416                 ERR("the button is already turned off");
417                 _reset_icon(module);
418         }
419 }
420
421 static void _handler_progress_on(void *data)
422 {
423         QP_Module_Setting *module = (QP_Module_Setting *)data;
424         retif(module == NULL, , "Invalid parameter!");
425
426         quickpanel_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_VALUE_VOID);
427 }
428
429 static void _handler_progress_off(void *data)
430 {
431         QP_Module_Setting *module = (QP_Module_Setting *)data;
432         retif(module == NULL, , "Invalid parameter!");
433
434         _reset_icon(module);
435 }
436
437 static int _handler_ipc(const char *command, void *data)
438 {
439         int i = 0;
440         retif(data == NULL, EINA_FALSE, "item data is NULL");
441         retif(command == NULL, EINA_FALSE, "command is NULL");
442
443         static Setting_Activity_Handler __table_handler[] = {
444                 {
445                         .command = "on",
446                         .handler = _handler_on,
447                 },
448                 {
449                         .command = "off",
450                         .handler = _handler_off,
451                 },
452                 {
453                         .command = "progress_on",
454                         .handler = _handler_progress_on,
455                 },
456                 {
457                         .command = "progress_off",
458                         .handler = _handler_progress_off,
459                 },
460                 {
461                         .command = NULL,
462                         .handler = NULL,
463                 },
464         };
465
466         for (i = 0; __table_handler[i].command; i++) {
467                 if (strcasecmp(__table_handler[i].command, command)) {
468                         continue;
469                 }
470
471                 if (__table_handler[i].handler != NULL) {
472                         DBG("process:%s", command);
473                         __table_handler[i].handler(data);
474                 }
475                 break;
476         }
477
478         return EINA_TRUE;
479 }
480
481 QP_Module_Setting mobile_data = {
482         .name                           = "mobile_data",
483         .init                           = _init,
484         .fini                           = _fini,
485         .lang_changed           = _lang_changed,
486         .refresh                        = _refresh,
487         .icon_get                       = _icon_get,
488         .label_get                      = _label_get,
489         .view_update        = _view_update,
490         .status_update          = _status_update,
491         .handler_longpress      = _long_press_cb,
492         .handler_ipc        = _handler_ipc,
493         .handler_press          = _mouse_clicked_cb,
494 };