84cb0b196008ae75283ca75238c9073a1f544715
[apps/livebox/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 "setting.h"
35 #include "util.h"
36 #include "debug.h"
37 #include "conf.h"
38 #include "critical_log.h"
39
40 #if defined(HAVE_LIVEBOX)
41 #include <livebox-service.h>
42 #include "client_life.h"
43 #include "slave_life.h"
44 #include "xmonitor.h"
45 #include "package.h"
46 #include "instance.h"
47 #else
48 #define xmonitor_handle_state_changes()
49 #endif
50
51 int errno;
52
53 static struct {
54         int deactivated;
55 } s_info = {
56         .deactivated = 0,
57 };
58
59 static void lcd_state_cb(keynode_t *node, void *user_data)
60 {
61         if (!node) {
62                 return;
63         }
64
65         xmonitor_handle_state_changes();
66 }
67
68 HAPI int setting_is_lcd_off(void)
69 {
70         int state;
71
72         if (vconf_get_int(VCONFKEY_PM_STATE, &state) != 0) {
73                 ErrPrint("Idle lock state is not valid\n");
74                 state = VCONFKEY_PM_STATE_NORMAL; /* UNLOCK */
75         }
76
77         DbgPrint("State: %d, (%d:lcdoff, %d:sleep)\n", state, VCONFKEY_PM_STATE_LCDOFF, VCONFKEY_PM_STATE_SLEEP);
78         return state == VCONFKEY_PM_STATE_LCDOFF || state == VCONFKEY_PM_STATE_SLEEP;
79 }
80
81 static void power_off_cb(keynode_t *node, void *user_data)
82 {
83         int val;
84         CRITICAL_LOG("Terminated(vconf)\n");
85
86         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) != 0) {
87                 ErrPrint("Failed to get power off status (%d)\n", val);
88                 return;
89         }
90
91         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
92                 DbgPrint("Power off requested: Ignored\n");
93         } else {
94                 ErrPrint("Unknown power state: %d\n", val);
95         }
96 }
97
98 static void region_changed_cb(keynode_t *node, void *user_data)
99 {
100         char *region;
101         char *r;
102
103         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
104         if (!region) {
105                 return;
106         }
107
108         setenv("LC_CTYPE", region, 1);
109         setenv("LC_NUMERIC", region, 1);
110         setenv("LC_TIME", region, 1);
111         setenv("LC_COLLATE", region, 1);
112         setenv("LC_MONETARY", region, 1);
113         setenv("LC_PAPER", region, 1);
114         setenv("LC_NAME", region, 1);
115         setenv("LC_ADDRESS", region, 1);
116         setenv("LC_TELEPHONE", region, 1);
117         setenv("LC_MEASUREMENT", region, 1);
118         setenv("LC_IDENTIFICATION", region, 1);
119
120         r = setlocale(LC_ALL, "");
121         if (r == NULL) {
122                 ErrPrint("Failed to change region\n");
123         }
124
125         DbgFree(region);
126 }
127
128 static void lang_changed_cb(keynode_t *node, void *user_data)
129 {
130         char *lang;
131         char *r;
132
133         lang = vconf_get_str(VCONFKEY_LANGSET);
134         if (!lang) {
135                 return;
136         }
137
138         setenv("LANG", lang, 1);
139         setenv("LC_MESSAGES", lang, 1);
140
141         r = setlocale(LC_ALL, "");
142         if (!r) {
143                 ErrPrint("Failed to change locale\n");
144         }
145
146         DbgPrint("Locale: %s\n", setlocale(LC_ALL, NULL));
147         DbgFree(lang);
148 }
149
150 static void low_mem_cb(keynode_t *node, void *user_data)
151 {
152         int val;
153
154         val = vconf_keynode_get_int(node);
155
156         if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)     {
157                 CRITICAL_LOG("Low memory: level %d\n", val);
158                 if (s_info.deactivated == 0) {
159                         s_info.deactivated = 1;
160                         //slave_deactivate_all(0, 1, 0);
161                         malloc_trim(0);
162                         ErrPrint("Fall into the low mem status\n");
163                 }
164         } else {
165                 CRITICAL_LOG("Normal memory: level %d\n", val);
166                 if (s_info.deactivated == 1) {
167                         s_info.deactivated = 0;
168                         //slave_activate_all();
169                         ErrPrint("Recover from the low mem status\n");
170                 }
171         }
172 }
173
174 HAPI int setting_init(void)
175 {
176         int ret;
177
178         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_cb, NULL);
179         if (ret < 0) {
180                 ErrPrint("Failed to add vconf for lock state: %d\n", ret);
181         }
182
183         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb, NULL);
184         if (ret < 0) {
185                 ErrPrint("Failed to add vconf for power state: %d \n", ret);
186         }
187
188         ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_changed_cb, NULL);
189         if (ret < 0) {
190                 ErrPrint("Failed to add vconf for lang change: %d\n", ret);
191         }
192
193         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb, NULL);
194         if (ret < 0) {
195                 ErrPrint("Failed to add vconf for region change: %d\n", ret);
196         }
197
198         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, low_mem_cb, NULL);
199         if (ret < 0) {
200                 ErrPrint("Failed to add vconf for low mem monitor: %d\n", ret);
201         }
202
203         lang_changed_cb(NULL, NULL);
204         region_changed_cb(NULL, NULL);
205         return ret;
206 }
207
208 HAPI int setting_fini(void)
209 {
210         int ret;
211
212         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, low_mem_cb);
213         if (ret < 0) {
214                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
215         }
216
217         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb);
218         if (ret < 0) {
219                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
220         }
221
222         ret = vconf_ignore_key_changed(VCONFKEY_LANGSET, lang_changed_cb);
223         if (ret < 0) {
224                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
225         }
226
227         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE, lcd_state_cb);
228         if (ret < 0) {
229                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
230         }
231
232         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb);
233         if (ret < 0) {
234                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
235         }
236
237         return ret;
238 }
239
240 /* End of a file */