Logging the critical message to a file.
[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 "client_life.h"
34 #include "setting.h"
35 #include "util.h"
36 #include "debug.h"
37 #include "slave_life.h"
38 #include "critical_log.h"
39 #include "xmonitor.h"
40 #include "conf.h"
41
42 int errno;
43
44 static void lcd_state_cb(keynode_t *node, void *user_data)
45 {
46         if (!node)
47                 return;
48
49         xmonitor_handle_state_changes();
50 }
51
52 HAPI int setting_is_lcd_off(void)
53 {
54         int state;
55
56         if (vconf_get_int(VCONFKEY_PM_STATE, &state) != 0) {
57                 ErrPrint("Idle lock state is not valid\n");
58                 state = VCONFKEY_PM_STATE_NORMAL; /* UNLOCK */
59         }
60
61         DbgPrint("State: %d, (%d:lcdoff, %d:sleep)\n", state, VCONFKEY_PM_STATE_LCDOFF, VCONFKEY_PM_STATE_SLEEP);
62         return state == VCONFKEY_PM_STATE_LCDOFF || state == VCONFKEY_PM_STATE_SLEEP;
63 }
64
65 static void power_off_cb(keynode_t *node, void *user_data)
66 {
67         int val;
68         CRITICAL_LOG("Terminated(vconf)\n");
69
70         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) != 0) {
71                 ErrPrint("Failed to get power off status (%d)\n", val);
72                 return;
73         }
74
75         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
76                 int fd;
77
78                 fd = creat("/tmp/.stop.provider", 0644);
79                 if (fd < 0 || close(fd) < 0)
80                         ErrPrint("stop.provider [%s]\n", strerror(errno));
81
82                 vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
83                 exit(0);
84                 //ecore_main_loop_quit();
85         } else {
86                 ErrPrint("Unknown power state: %d\n", val);
87         }
88 }
89
90 static void region_changed_cb(keynode_t *node, void *user_data)
91 {
92         char *region;
93         char *r;
94
95         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
96         if (!region)
97                 return;
98
99         setenv("LC_CTYPE", region, 1);
100         setenv("LC_NUMERIC", region, 1);
101         setenv("LC_TIME", region, 1);
102         setenv("LC_COLLATE", region, 1);
103         setenv("LC_MONETARY", region, 1);
104         setenv("LC_PAPER", region, 1);
105         setenv("LC_NAME", region, 1);
106         setenv("LC_ADDRESS", region, 1);
107         setenv("LC_TELEPHONE", region, 1);
108         setenv("LC_MEASUREMENT", region, 1);
109         setenv("LC_IDENTIFICATION", region, 1);
110
111         r = setlocale(LC_ALL, "");
112         if (r == NULL)
113                 ErrPrint("Failed to change region\n");
114
115         free(region);
116 }
117
118 static void lang_changed_cb(keynode_t *node, void *user_data)
119 {
120         char *lang;
121         char *r;
122
123         lang = vconf_get_str(VCONFKEY_LANGSET);
124         if (!lang)
125                 return;
126
127         setenv("LANG", lang, 1);
128         setenv("LC_MESSAGES", lang, 1);
129
130         r = setlocale(LC_ALL, "");
131         if (!r)
132                 ErrPrint("Failed to change locale\n");
133
134         DbgPrint("Locale: %s\n", setlocale(LC_ALL, NULL));
135         free(lang);
136 }
137
138 HAPI int setting_init(void)
139 {
140         int ret;
141
142         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_cb, NULL);
143         if (ret < 0)
144                 ErrPrint("Failed to add vconf for lock state: %d\n", ret);
145
146         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb, NULL);
147         if (ret < 0)
148                 ErrPrint("Failed to add vconf for power state: %d \n", ret);
149
150         ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_changed_cb, NULL);
151         if (ret < 0)
152                 ErrPrint("Failed to add vconf for lang change: %d\n", ret);
153
154         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb, NULL);
155         if (ret < 0)
156                 ErrPrint("Failed to add vconf for region change: %d\n", ret);
157
158         lang_changed_cb(NULL, NULL);
159         region_changed_cb(NULL, NULL);
160         return ret;
161 }
162
163 HAPI int setting_fini(void)
164 {
165         int ret;
166
167         ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb);
168         if (ret < 0)
169                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
170
171         ret = vconf_ignore_key_changed(VCONFKEY_LANGSET, lang_changed_cb);
172         if (ret < 0)
173                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
174
175         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE, lcd_state_cb);
176         if (ret < 0)
177                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
178
179         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb);
180         if (ret < 0)
181                 ErrPrint("Failed to ignore vconf key (%d)\n", ret);
182
183         return ret;
184 }
185
186 /* End of a file */