Update SMACK, Fix a crash of terminating sequence, etc, ...
[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 <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <locale.h>
26
27 #include <vconf.h>
28 #include <dlog.h>
29
30 #include <Ecore.h>
31 #include <Eina.h>
32
33 #include <livebox-service.h>
34
35 #include "client_life.h"
36 #include "setting.h"
37 #include "util.h"
38 #include "debug.h"
39 #include "slave_life.h"
40 #include "critical_log.h"
41 #include "xmonitor.h"
42 #include "conf.h"
43 #include "package.h"
44 #include "instance.h"
45
46 int errno;
47
48 static void lcd_state_cb(keynode_t *node, void *user_data)
49 {
50         if (!node)
51                 return;
52
53         xmonitor_handle_state_changes();
54 }
55
56 HAPI int setting_is_lcd_off(void)
57 {
58         int state;
59
60         if (vconf_get_int(VCONFKEY_PM_STATE, &state) != 0) {
61                 ErrPrint("Idle lock state is not valid\n");
62                 state = VCONFKEY_PM_STATE_NORMAL; /* UNLOCK */
63         }
64
65         DbgPrint("State: %d, (%d:lcdoff, %d:sleep)\n", state, VCONFKEY_PM_STATE_LCDOFF, VCONFKEY_PM_STATE_SLEEP);
66         return state == VCONFKEY_PM_STATE_LCDOFF || state == VCONFKEY_PM_STATE_SLEEP;
67 }
68
69 static void power_off_cb(keynode_t *node, void *user_data)
70 {
71         int val;
72         CRITICAL_LOG("Terminated(vconf)\n");
73
74         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) != 0) {
75                 ErrPrint("Failed to get power off status (%d)\n", val);
76                 return;
77         }
78
79         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
80                 int fd;
81
82                 fd = creat("/tmp/.stop.provider", 0644);
83                 if (fd < 0 || close(fd) < 0)
84                         ErrPrint("stop.provider [%s]\n", strerror(errno));
85
86                 vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
87                 //exit(0);
88                 ecore_main_loop_quit();
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         setenv("LC_CTYPE", region, 1);
104         setenv("LC_NUMERIC", region, 1);
105         setenv("LC_TIME", region, 1);
106         setenv("LC_COLLATE", region, 1);
107         setenv("LC_MONETARY", region, 1);
108         setenv("LC_PAPER", region, 1);
109         setenv("LC_NAME", region, 1);
110         setenv("LC_ADDRESS", region, 1);
111         setenv("LC_TELEPHONE", region, 1);
112         setenv("LC_MEASUREMENT", region, 1);
113         setenv("LC_IDENTIFICATION", region, 1);
114
115         r = setlocale(LC_ALL, "");
116         if (r == NULL)
117                 ErrPrint("Failed to change region\n");
118
119         DbgFree(region);
120 }
121
122 static void lang_changed_cb(keynode_t *node, void *user_data)
123 {
124         char *lang;
125         char *r;
126
127         lang = vconf_get_str(VCONFKEY_LANGSET);
128         if (!lang)
129                 return;
130
131         setenv("LANG", lang, 1);
132         setenv("LC_MESSAGES", lang, 1);
133
134         r = setlocale(LC_ALL, "");
135         if (!r)
136                 ErrPrint("Failed to change locale\n");
137
138         DbgPrint("Locale: %s\n", setlocale(LC_ALL, NULL));
139         DbgFree(lang);
140 }
141
142 static void ail_info_cb(keynode_t *node, void *user_data)
143 {
144         Eina_List *inst_list;
145         Eina_List *pkg_list;
146         struct inst_info *inst;
147         Eina_List *l;
148         Eina_List *n;
149         Eina_List *j;
150         struct pkg_info *info;
151         char *event;
152         char *appid;
153         char *pkgname;
154         int len;
155         int enabled;
156
157         event = vconf_get_str(VCONFKEY_AIL_INFO_STATE);
158         if (!event)
159                 return;
160
161         len = strlen("update:");
162         if (!strncasecmp(event, "update:", len))
163                 goto out;
164
165         appid = event + len;
166         DbgPrint("AppId: [%s]\n", appid);
167
168         enabled = package_is_enabled(appid);
169
170         DbgPrint("AppId: %s, %d\n", appid, enabled);
171         if (enabled != 0) {
172                 /*
173                  * \note
174                  * reload?
175                  */
176                 goto out;
177         }
178
179         len = strlen(appid);
180
181         pkg_list = (Eina_List *)package_list();
182         EINA_LIST_FOREACH(pkg_list, l, info) {
183                 inst_list = NULL;
184                 pkgname = livebox_service_mainappid(package_name(info));
185                 if (!pkgname)
186                         continue;
187
188                 if (strcmp(appid, pkgname)) {
189                         DbgFree(pkgname);
190                         continue;
191                 }
192                 DbgPrint("Package disabled: %s (%s)\n", pkgname, appid);
193                 DbgFree(pkgname);
194
195                 inst_list = package_instance_list(info);
196                 EINA_LIST_FOREACH_SAFE(inst_list, j, n, inst) {
197                         instance_destroy(inst);
198                 }
199         }
200
201 out:
202         DbgFree(event);
203 }
204
205 HAPI int setting_init(void)
206 {
207         int ret;
208
209         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_cb, NULL);
210         if (ret < 0)
211                 ErrPrint("Failed to add vconf for lock state: %d\n", ret);
212
213         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb, NULL);
214         if (ret < 0)
215                 ErrPrint("Failed to add vconf for power state: %d \n", ret);
216
217         ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_changed_cb, NULL);
218         if (ret < 0)
219                 ErrPrint("Failed to add vconf for lang change: %d\n", ret);
220
221         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb, NULL);
222         if (ret < 0)
223                 ErrPrint("Failed to add vconf for region change: %d\n", ret);
224
225         ret = vconf_notify_key_changed(VCONFKEY_AIL_INFO_STATE, ail_info_cb, NULL);
226         if (ret < 0)
227                 ErrPrint("Failed to add vconf for ail info state: %d\n", ret);
228
229         lang_changed_cb(NULL, NULL);
230         region_changed_cb(NULL, NULL);
231         return ret;
232 }
233
234 HAPI int setting_fini(void)
235 {
236         int ret;
237
238         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb);
239         if (ret < 0)
240                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
241
242         ret = vconf_ignore_key_changed(VCONFKEY_LANGSET, lang_changed_cb);
243         if (ret < 0)
244                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
245
246         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE, lcd_state_cb);
247         if (ret < 0)
248                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
249
250         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb);
251         if (ret < 0)
252                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
253
254         ret = vconf_ignore_key_changed(VCONFKEY_AIL_INFO_STATE, ail_info_cb);
255         if (ret < 0)
256                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
257
258         return ret;
259 }
260
261 /* End of a file */