cfac0ad8fa995bc1372fb661333203e42af37f2d
[apps/home/quickpanel.git] / daemon / settings / modules / ultra_power_saving.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 <app.h>
20 #include <vconf.h>
21 #include <syspopup_caller.h>
22 #include "common.h"
23 #include "quickpanel-ui.h"
24 #include "settings.h"
25 #include "setting_utils.h"
26 #include "setting_module_api.h"
27
28 #define BUTTON_LABEL _("IDS_QP_BUTTON2_U_POWER_NSAVING_ABB")
29 #define BUTTON_ICON_NORMAL "quick_icon_ultra_power_saving.png"
30 #define BUTTON_ICON_HIGHLIGHT NULL
31 #define BUTTON_ICON_DIM NULL
32 #define PACKAGE_SETTING_MENU "setting-powersaving-efl"
33 #define SYSPOPUP_NAME "mode-syspopup"
34
35 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
36
37 static const char *_label_get(void) {
38         return BUTTON_LABEL;
39 }
40
41 static const char *_icon_get(qp_setting_icon_image_type type) {
42         if (type == QP_SETTING_ICON_NORMAL) {
43                 return BUTTON_ICON_NORMAL;
44         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
45                 return BUTTON_ICON_HIGHLIGHT;
46         } else if (type == QP_SETTING_ICON_DIM) {
47 #ifdef BUTTON_ICON_DIM
48                 return BUTTON_ICON_DIM;
49 #endif
50         }
51
52         return NULL;
53 }
54
55 static void _long_press_cb(void *data) {
56 #ifdef PACKAGE_SETTING_MENU
57         bundle *kb = bundle_create();
58         if (kb != NULL) {
59                 bundle_add(kb, "power_saving", "upsm");
60                 qp_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, kb);
61                 bundle_free(kb);
62         } else {
63                 ERR("failed to create the bunlde");
64         }
65 #endif
66 }
67
68 static int _is_need_to_show_disclaimer(void) {
69         int ret = 0, status = 0;
70
71         ret = vconf_get_bool(VCONFKEY_SETAPPL_UPSM_DO_NOT_SHOW_DISCLAIMER, &status);
72         if (ret == 0 && status) {
73                 return 0;
74         }
75
76         return 1;
77 }
78
79 static void _turn_on(int is_on)
80 {
81         bundle *b = NULL;
82
83         if (_is_need_to_show_disclaimer() == 1)
84         {
85                 b = bundle_create();
86                 if (b != NULL)
87                 {
88                         bundle_add(b, "viewtype", "disc");
89
90                         qp_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, b);
91                         bundle_free(b);
92                 }
93         }
94         else
95         {
96                 b = bundle_create();
97                 if (b != NULL)
98                 {
99                         if (is_on)
100                         {
101                                 bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "POPUP_EMERGENCY_PSMODE_SETTING");
102                         }
103                         else
104                         {
105                                 bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "POPUP_NORMAL_PSMODE");
106                         }
107                         syspopup_launch(SYSPOPUP_NAME, b);
108                         bundle_free(b);
109                 }
110                 else
111                 {
112                         ERR("failed to create a bundle");
113                 }
114         }
115 }
116
117 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2) {
118         Evas_Object *image = NULL;
119         const char *icon_path = NULL;
120
121         qp_setting_icon_state_set(view, state);
122
123         if (state == ICON_VIEW_STATE_ON) {
124 #ifdef BUTTON_ICON_HIGHLIGHT
125                 icon_path = BUTTON_ICON_HIGHLIGHT;
126 #endif
127         } else if (state == ICON_VIEW_STATE_DIM) {
128 #ifdef BUTTON_ICON_DIM
129                 icon_path = BUTTON_ICON_DIM;
130 #endif
131         } else {
132                 icon_path = BUTTON_ICON_NORMAL;
133         }
134
135         if (icon_path == NULL) {
136                 icon_path = BUTTON_ICON_NORMAL;
137         }
138         image = qp_setting_icon_image_new(view, icon_path);
139         qp_setting_icon_content_set(view, image);
140         qp_setting_icon_text_set(view, BUTTON_LABEL, state);
141 }
142
143 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2) {
144         int ret = 0;
145         int status = 0;
146         retif(module == NULL, , "Invalid parameter!");
147
148         ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &status);
149         msgif(ret != 0, "fail to get VCONFKEY_SETAPPL_PSMODE:%d", ret);
150
151         if (status == SETTING_PSMODE_EMERGENCY) {
152                 qp_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
153         } else {
154                 qp_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
155         }
156
157         qp_setting_module_icon_view_update(module,
158                         qp_setting_module_icon_state_get(module),
159                         FLAG_VALUE_VOID);
160 }
161
162 static void _mouse_clicked_cb(void *data,
163                 Evas_Object *obj, const char *emission, const char *source) {
164         QP_Module_Setting *module = (QP_Module_Setting *)data;
165         retif(module == NULL, , "Invalid parameter!");
166
167         if (qp_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON) {
168                 _turn_on(0);
169         } else {
170                 _turn_on(1);
171         }
172 }
173
174 static void _powersave_vconf_cb(keynode_t *node, void *data) {
175         _status_update(data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
176 }
177
178 static int _register_module_event_handler(void *data) {
179         int ret = 0;
180
181         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
182                         _powersave_vconf_cb, data);
183         msgif(ret != 0, "failed to notify key(VCONFKEY_SETAPPL_PSMODE) : %d", ret);
184
185         return QP_OK;
186 }
187
188 static int _unregister_module_event_handler(void *data) {
189         int ret = 0;
190
191         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
192                         _powersave_vconf_cb);
193         msgif(ret != 0, "failed to ignore key(VCONFKEY_SETAPPL_PSMODE) : %d", ret);
194
195         return QP_OK;
196 }
197
198 /****************************************************************************
199  *
200  * Quickpanel Item functions
201  *
202  ****************************************************************************/
203 static int _init(void *data) {
204         int ret = QP_OK;
205
206         ret = _register_module_event_handler(data);
207
208         return ret;
209 }
210
211 static int _fini(void *data) {
212         int ret = QP_OK;
213
214         ret = _unregister_module_event_handler(data);
215
216         return ret;
217 }
218
219 static void _lang_changed(void *data) {
220         QP_Module_Setting *module = (QP_Module_Setting *)data;
221         retif(module == NULL, , "Invalid parameter!");
222
223         qp_setting_module_icon_view_update_text(module);
224 }
225
226 static void _refresh(void *data) {
227         QP_Module_Setting *module = (QP_Module_Setting *)data;
228         retif(module == NULL, , "Invalid parameter!");
229
230         qp_setting_module_icon_view_update_text(module);
231 }
232
233 QP_Module_Setting u_power_saving = {
234         .name                           = "u_power_saving",
235         .init                           = _init,
236         .fini                           = _fini,
237         .lang_changed           = _lang_changed,
238         .refresh                        = _refresh,
239         .icon_get                       = _icon_get,
240         .label_get                      = _label_get,
241         .view_update        = _view_update,
242         .status_update          = _status_update,
243         .handler_longpress              = _long_press_cb,
244         .handler_press          = _mouse_clicked_cb,
245 };