Sync with the latest code of phone profile
[platform/framework/web/data-provider-master.git] / src / setting.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <stdio.h>
18 #include <malloc.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <locale.h>
27
28 #include <vconf.h>
29 #include <dlog.h>
30
31 #include <Ecore.h>
32 #include <Eina.h>
33
34 #include <livebox-service.h>
35
36 #include "client_life.h"
37 #include "setting.h"
38 #include "util.h"
39 #include "debug.h"
40 #include "slave_life.h"
41 #include "critical_log.h"
42 #include "xmonitor.h"
43 #include "conf.h"
44 #include "package.h"
45 #include "instance.h"
46
47 int errno;
48
49 static struct {
50         int deactivated;
51 } s_info = {
52         .deactivated = 0,
53 };
54
55 static void lcd_state_cb(keynode_t *node, void *user_data)
56 {
57         if (!node) {
58                 return;
59         }
60
61         xmonitor_handle_state_changes();
62 }
63
64 HAPI int setting_is_lcd_off(void)
65 {
66         int state;
67
68         if (vconf_get_int(VCONFKEY_PM_STATE, &state) != 0) {
69                 ErrPrint("Idle lock state is not valid\n");
70                 state = VCONFKEY_PM_STATE_NORMAL; /* UNLOCK */
71         }
72
73         DbgPrint("State: %d, (%d:lcdoff, %d:sleep)\n", state, VCONFKEY_PM_STATE_LCDOFF, VCONFKEY_PM_STATE_SLEEP);
74         return state == VCONFKEY_PM_STATE_LCDOFF || state == VCONFKEY_PM_STATE_SLEEP;
75 }
76
77 static void power_off_cb(keynode_t *node, void *user_data)
78 {
79         int val;
80         CRITICAL_LOG("Terminated(vconf)\n");
81
82         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) != 0) {
83                 ErrPrint("Failed to get power off status (%d)\n", val);
84                 return;
85         }
86
87         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
88                 DbgPrint("Power off requested: Ignored\n");
89         } else {
90                 ErrPrint("Unknown power state: %d\n", val);
91         }
92 }
93
94 static void region_changed_cb(keynode_t *node, void *user_data)
95 {
96         char *region;
97         char *r;
98
99         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
100         if (!region) {
101                 return;
102         }
103
104         setenv("LC_CTYPE", region, 1);
105         setenv("LC_NUMERIC", region, 1);
106         setenv("LC_TIME", region, 1);
107         setenv("LC_COLLATE", region, 1);
108         setenv("LC_MONETARY", region, 1);
109         setenv("LC_PAPER", region, 1);
110         setenv("LC_NAME", region, 1);
111         setenv("LC_ADDRESS", region, 1);
112         setenv("LC_TELEPHONE", region, 1);
113         setenv("LC_MEASUREMENT", region, 1);
114         setenv("LC_IDENTIFICATION", region, 1);
115
116         r = setlocale(LC_ALL, "");
117         if (r == NULL) {
118                 ErrPrint("Failed to change region\n");
119         }
120
121         DbgFree(region);
122 }
123
124 static void lang_changed_cb(keynode_t *node, void *user_data)
125 {
126         char *lang;
127         char *r;
128
129         lang = vconf_get_str(VCONFKEY_LANGSET);
130         if (!lang) {
131                 return;
132         }
133
134         setenv("LANG", lang, 1);
135         setenv("LC_MESSAGES", lang, 1);
136
137         r = setlocale(LC_ALL, "");
138         if (!r) {
139                 ErrPrint("Failed to change locale\n");
140         }
141
142         DbgPrint("Locale: %s\n", setlocale(LC_ALL, NULL));
143         DbgFree(lang);
144 }
145
146 static void ail_info_cb(keynode_t *node, void *user_data)
147 {
148         Eina_List *inst_list;
149         Eina_List *pkg_list;
150         struct inst_info *inst;
151         Eina_List *l;
152         Eina_List *n;
153         Eina_List *j;
154         struct pkg_info *info;
155         char *event;
156         char *appid;
157         char *pkgname;
158         int len;
159         int enabled;
160
161         event = vconf_get_str(VCONFKEY_AIL_INFO_STATE);
162         if (!event) {
163                 return;
164         }
165
166         len = strlen("update:");
167         if (!strncasecmp(event, "update:", len)) {
168                 goto out;
169         }
170
171         appid = event + len;
172         DbgPrint("AppId: [%s]\n", appid);
173
174         enabled = package_is_enabled(appid);
175
176         DbgPrint("AppId: %s, %d\n", appid, enabled);
177         if (enabled != 0) {
178                 /*
179                  * \note
180                  * reload?
181                  */
182                 goto out;
183         }
184
185         len = strlen(appid);
186
187         pkg_list = (Eina_List *)package_list();
188         EINA_LIST_FOREACH(pkg_list, l, info) {
189                 inst_list = NULL;
190                 pkgname = livebox_service_mainappid(package_name(info));
191                 if (!pkgname) {
192                         /*!
193                          * Even if we failed to get package name,
194                          * Try to use the ordinal package name first.
195                          */
196                         pkgname = strdup(package_name(info));
197                         DbgPrint("Try to use the pkgname: %s\n", pkgname);
198                         if (!pkgname) {
199                                 continue;
200                         }
201                 }
202
203                 if (strcmp(appid, pkgname)) {
204                         DbgFree(pkgname);
205                         continue;
206                 }
207                 DbgPrint("Package disabled: %s (%s)\n", pkgname, appid);
208                 DbgFree(pkgname);
209
210                 inst_list = package_instance_list(info);
211                 EINA_LIST_FOREACH_SAFE(inst_list, j, n, inst) {
212                         instance_destroy(inst, INSTANCE_DESTROY_DEFAULT);
213                 }
214         }
215
216 out:
217         DbgFree(event);
218 }
219
220 static void low_mem_cb(keynode_t *node, void *user_data)
221 {
222         int val;
223
224         val = vconf_keynode_get_int(node);
225
226         if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)     {
227                 CRITICAL_LOG("Low memory: level %d\n", val);
228                 if (s_info.deactivated == 0) {
229                         s_info.deactivated = 1;
230                         //slave_deactivate_all(0, 1);
231                         malloc_trim(0);
232                         ErrPrint("Fall into the low mem status\n");
233                 }
234         } else {
235                 CRITICAL_LOG("Normal memory: level %d\n", val);
236                 if (s_info.deactivated == 1) {
237                         s_info.deactivated = 0;
238                         //slave_activate_all();
239                         ErrPrint("Recover from the low mem status\n");
240                 }
241         }
242 }
243
244 HAPI int setting_init(void)
245 {
246         int ret;
247
248         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_cb, NULL);
249         if (ret < 0) {
250                 ErrPrint("Failed to add vconf for lock state: %d\n", ret);
251         }
252
253         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb, NULL);
254         if (ret < 0) {
255                 ErrPrint("Failed to add vconf for power state: %d \n", ret);
256         }
257
258         ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_changed_cb, NULL);
259         if (ret < 0) {
260                 ErrPrint("Failed to add vconf for lang change: %d\n", ret);
261         }
262
263         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb, NULL);
264         if (ret < 0) {
265                 ErrPrint("Failed to add vconf for region change: %d\n", ret);
266         }
267
268         ret = vconf_notify_key_changed(VCONFKEY_AIL_INFO_STATE, ail_info_cb, NULL);
269         if (ret < 0) {
270                 ErrPrint("Failed to add vconf for ail info state: %d\n", ret);
271         }
272
273         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, low_mem_cb, NULL);
274         if (ret < 0) {
275                 ErrPrint("Failed to add vconf for low mem monitor: %d\n", ret);
276         }
277
278         lang_changed_cb(NULL, NULL);
279         region_changed_cb(NULL, NULL);
280         return ret;
281 }
282
283 HAPI int setting_fini(void)
284 {
285         int ret;
286
287         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, low_mem_cb);
288         if (ret < 0) {
289                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
290         }
291
292         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb);
293         if (ret < 0) {
294                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
295         }
296
297         ret = vconf_ignore_key_changed(VCONFKEY_LANGSET, lang_changed_cb);
298         if (ret < 0) {
299                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
300         }
301
302         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE, lcd_state_cb);
303         if (ret < 0) {
304                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
305         }
306
307         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb);
308         if (ret < 0) {
309                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
310         }
311
312         ret = vconf_ignore_key_changed(VCONFKEY_AIL_INFO_STATE, ail_info_cb);
313         if (ret < 0) {
314                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
315         }
316
317         return ret;
318 }
319
320 /* End of a file */