7f670d417bfba9927de26088b0d6bdc9cd79e181
[apps/home/quickpanel.git] / daemon / settings / modules / gps.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 <app_control.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_LOCATION_ABB")
29 #define BUTTON_ICON_NORMAL "quick_icon_location.png"
30 #define BUTTON_ICON_HIGHLIGHT NULL
31 #define BUTTON_ICON_DIM NULL
32 #define PACKAGE_SETTING_MENU "org.tizen.setting-location"
33 #define OPERATION_SETTING_MENU "http://tizen.org/appcontrol/operation/configure/location"
34 #define PACKAGE_SYSPOPUP "gps-syspopup"
35
36 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
37
38 static const char *_label_get(void) {
39         return BUTTON_LABEL;
40 }
41
42 static const char *_icon_get(qp_setting_icon_image_type type) {
43         if (type == QP_SETTING_ICON_NORMAL) {
44                 return BUTTON_ICON_NORMAL;
45         } else if (type == QP_SETTING_ICON_NORMAL) {
46                 return BUTTON_ICON_HIGHLIGHT;
47         } else if (type == QP_SETTING_ICON_DIM) {
48 #ifdef BUTTON_ICON_DIM
49                 return BUTTON_ICON_DIM;
50 #endif
51         }
52
53         return NULL;
54 }
55
56 static void _long_press_cb(void *data) {
57 #ifdef PACKAGE_SETTING_MENU
58         // Because operation is not DEFAULT, this function can not be called. Too many changes to add operation param.
59         // qp_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
60
61         app_control_h service;
62         int ret = 0;
63
64         ret = app_control_create(&service);
65         if (ret != 0)
66         {
67                 ERR("Failed to create app control");
68                 return;
69         }
70
71         app_control_set_app_id(service, PACKAGE_SETTING_MENU);
72         app_control_set_operation(service, OPERATION_SETTING_MENU);
73
74         ret = app_control_send_launch_request(service, NULL, NULL);
75         if (ret != 0)
76         {
77                 ERR("Failed to launch[%d]", ret);
78         }
79
80         app_control_destroy(service);
81
82 #endif
83 }
84
85 static void _syspopup_launch(int is_on)
86 {
87         syspopup_launch(PACKAGE_SYSPOPUP, NULL);
88 }
89
90 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2) {
91         Evas_Object *image = NULL;
92         const char *icon_path = NULL;
93
94         qp_setting_icon_state_set(view, state);
95
96         if (state == ICON_VIEW_STATE_ON) {
97 #ifdef BUTTON_ICON_HIGHLIGHT
98                 icon_path = BUTTON_ICON_HIGHLIGHT;
99 #endif
100         } else if (state == ICON_VIEW_STATE_DIM) {
101 #ifdef BUTTON_ICON_DIM
102                 icon_path = BUTTON_ICON_DIM;
103 #endif
104         } else {
105                 icon_path = BUTTON_ICON_NORMAL;
106         }
107
108         if (icon_path == NULL) {
109                 icon_path = BUTTON_ICON_NORMAL;
110         }
111         image = qp_setting_icon_image_new(view, icon_path);
112         qp_setting_icon_content_set(view, image);
113         qp_setting_icon_text_set(view, BUTTON_LABEL, state);
114 }
115
116 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2) {
117         int ret = 0;
118         int status = 0;
119         retif(module == NULL, , "Invalid parameter!");
120
121         qp_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
122         qp_setting_module_icon_timer_del(module);
123
124         ret = vconf_get_int(VCONFKEY_LOCATION_USE_MY_LOCATION, &status);
125         msgif(ret != 0, "fail to get VCONFKEY_LOCATION_USE_MY_LOCATION:%d", ret);
126
127         if (status == 1) {
128                 qp_setting_module_icon_state_set(module, ICON_VIEW_STATE_ON);
129         } else {
130                 qp_setting_module_icon_state_set(module, ICON_VIEW_STATE_OFF);
131         }
132
133         qp_setting_module_icon_view_update(module,
134                         qp_setting_module_icon_state_get(module),
135                         FLAG_VALUE_VOID);
136 }
137
138 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
139 {
140         int ret = 0;
141         int enable = 0;
142         QP_Module_Setting *module = (QP_Module_Setting *) data;
143         retif(module == NULL, , "Invalid parameter!");
144
145         if (qp_setting_module_is_icon_clickable(module) == 0)
146         {
147                 return;
148         }
149
150         if (qp_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF)
151         {
152                 _syspopup_launch(qp_setting_module_icon_state_get(module));
153         }
154         else
155         {
156                 // Use my location off
157                 ret = vconf_get_int(VCONFKEY_LOCATION_USE_MY_LOCATION, &enable);
158                 if (ret == 0)
159                 {
160                         if (enable == 1)
161                         {
162                                 vconf_set_int(VCONFKEY_LOCATION_USE_MY_LOCATION, 0);
163                         }
164                 }
165                 else
166                 {
167                         ERR("Failed to get Use my location[%d]", ret);
168                 }
169                 // GPS off
170                 ret = vconf_get_int(VCONFKEY_LOCATION_ENABLED, &enable);
171                 if (ret == 0)
172                 {
173                         if (enable == 1)
174                         {
175                                 vconf_set_int(VCONFKEY_LOCATION_ENABLED, 0);
176                         }
177                 }
178                 else
179                 {
180                         ERR("Failed to get GPS[%d]", ret);
181                 }
182                 // Wireless networks off
183                 ret = vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &enable);
184                 if (ret == 0)
185                 {
186                         if (enable == 1)
187                         {
188                                 vconf_set_int(VCONFKEY_LOCATION_NETWORK_ENABLED, 0);
189                         }
190                 }
191                 else
192                 {
193                         ERR("Failed to get GPS[%d]", ret);
194                 }
195         }
196 }
197
198 static void _gps_vconf_cb(keynode_t *node,
199                 void *data) {
200         _status_update(data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
201 }
202
203 static int _register_module_event_handler(void *data) {
204         int ret = 0;
205
206         ret = vconf_notify_key_changed(VCONFKEY_LOCATION_USE_MY_LOCATION,
207                         _gps_vconf_cb, data);
208         msgif(ret != 0, "failed to notify key(VCONFKEY_LOCATION_USE_MY_LOCATION) : %d", ret);
209
210         return QP_OK;
211 }
212
213 static int _unregister_module_event_handler(void *data) {
214         int ret = 0;
215
216         ret = vconf_ignore_key_changed(VCONFKEY_LOCATION_USE_MY_LOCATION,
217                         _gps_vconf_cb);
218         msgif(ret != 0, "failed to ignore key(VCONFKEY_LOCATION_USE_MY_LOCATION) : %d", ret);
219
220         return QP_OK;
221 }
222
223 /****************************************************************************
224  *
225  * Quickpanel Item functions
226  *
227  ****************************************************************************/
228 static int _init(void *data) {
229         int ret = QP_OK;
230
231         ret = _register_module_event_handler(data);
232
233         return ret;
234 }
235
236 static int _fini(void *data) {
237         int ret = QP_OK;
238
239         ret = _unregister_module_event_handler(data);
240
241         return ret;
242 }
243
244 static void _lang_changed(void *data) {
245         QP_Module_Setting *module = (QP_Module_Setting *)data;
246         retif(module == NULL, , "Invalid parameter!");
247
248         qp_setting_module_icon_view_update_text(module);
249 }
250
251 static void _refresh(void *data) {
252         QP_Module_Setting *module = (QP_Module_Setting *)data;
253         retif(module == NULL, , "Invalid parameter!");
254
255         qp_setting_module_icon_view_update_text(module);
256 }
257
258 static void _reset_icon(QP_Module_Setting *module) {
259         retif(module == NULL, , "Invalid parameter!");
260
261         qp_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_VALUE_VOID);
262         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
263 }
264
265 static void _handler_on(void *data) {
266         QP_Module_Setting *module = (QP_Module_Setting *)data;
267         retif(module == NULL, , "Invalid parameter!");
268
269         qp_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
270         qp_setting_module_icon_timer_del(module);
271
272         if (qp_setting_module_icon_state_get(module) == ICON_VIEW_STATE_OFF)
273         {
274                 qp_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_ON);
275                 qp_setting_module_icon_timer_add(module);
276         }
277         else
278         {
279                 ERR("the button is already turned on");
280                 _reset_icon(module);
281         }
282 }
283
284 static void _handler_off(void *data) {
285         QP_Module_Setting *module = (QP_Module_Setting *)data;
286         retif(module == NULL, , "Invalid parameter!");
287
288         qp_setting_module_progress_mode_set(module, FLAG_DISABLE, FLAG_TURN_OFF);
289         qp_setting_module_icon_timer_del(module);
290
291         if (qp_setting_module_icon_state_get(module) == ICON_VIEW_STATE_ON)
292         {
293                 qp_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_TURN_OFF);
294                 qp_setting_module_icon_timer_add(module);
295         }
296         else
297         {
298                 ERR("the button is already turned off");
299                 _reset_icon(module);
300         }
301 }
302
303 static void _handler_progress_on(void *data) {
304         QP_Module_Setting *module = (QP_Module_Setting *)data;
305         retif(module == NULL, , "Invalid parameter!");
306
307         qp_setting_module_progress_mode_set(module, FLAG_ENABLE, FLAG_VALUE_VOID);
308 }
309
310 static void _handler_progress_off(void *data) {
311         QP_Module_Setting *module = (QP_Module_Setting *)data;
312         retif(module == NULL, , "Invalid parameter!");
313
314         _reset_icon(module);
315 }
316
317 static int _handler_ipc(const char *command, void *data) {
318         int i = 0;
319         retif(data == NULL, EINA_FALSE, "item data is NULL");
320         retif(command == NULL, EINA_FALSE, "command is NULL");
321
322         static Setting_Activity_Handler __table_handler[] = {
323                 {
324                         .command = "on",
325                         .handler = _handler_on,
326                 },
327                 {
328                         .command = "off",
329                         .handler = _handler_off,
330                 },
331                 {
332                         .command = "progress_on",
333                         .handler = _handler_progress_on,
334                 },
335                 {
336                         .command = "progress_off",
337                         .handler = _handler_progress_off,
338                 },
339                 {
340                         .command = NULL,
341                         .handler = NULL,
342                 },
343         };
344
345         for (i = 0; __table_handler[i].command; i++) {
346                 if (strcasecmp(__table_handler[i].command, command)) {
347                         continue;
348                 }
349
350                 if (__table_handler[i].handler != NULL) {
351                         DBG("process:%s", command);
352                         __table_handler[i].handler(data);
353                 }
354                 break;
355         }
356
357         return EINA_TRUE;
358 }
359
360 QP_Module_Setting gps = {
361         .name                           = "gps",
362         .init                           = _init,
363         .fini                           = _fini,
364         .lang_changed           = _lang_changed,
365         .refresh                        = _refresh,
366         .icon_get                       = _icon_get,
367         .label_get                      = _label_get,
368         .view_update        = _view_update,
369         .status_update          = _status_update,
370         .handler_longpress              = _long_press_cb,
371         .handler_ipc        = _handler_ipc,
372         .handler_press          = _mouse_clicked_cb,
373 };