tizen 2.4 release
[apps/home/lockscreen.git] / src / property.c
1 /*
2  * Copyright (c) 2009-2014 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 #include <vconf.h>
18
19 #include "lockscreen.h"
20 #include "log.h"
21 #include "property.h"
22 #include "default_lock.h"
23
24 static struct _s_info {
25         bool is_sound_lock;
26         bool is_sound_touch;
27         bool is_rotation_enabled;
28 } s_info = {
29         .is_sound_lock = false,
30         .is_sound_touch = false,
31         .is_rotation_enabled = false,
32 };
33
34 bool lock_property_sound_lock_get(void)
35 {
36         return s_info.is_sound_lock;
37 }
38
39 bool lock_property_sound_touch_get(void)
40 {
41         return s_info.is_sound_touch;
42 }
43
44 bool lock_property_rotation_enabled_get(void)
45 {
46         return s_info.is_rotation_enabled;
47 }
48
49 lock_error_e lock_property_get_string(property_type_e type, void *key, char **str)
50 {
51         int ret = 0;
52
53         switch(type) {
54         case PROPERTY_TYPE_SYSTEM_SETTINGS :
55                 ret = system_settings_get_value_string((int)key, &(*str));
56                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
57                         ret = -1;
58                 }
59                 _D("SYSTEM SETTINGS : key(%d), val(%s)", (int)key, *str);
60                 break;
61         case PROPERTY_TYPE_SYSTEM_INFO :
62                 ret = system_info_get_platform_string((char *)key, &(*str));
63                 if (SYSTEM_INFO_ERROR_NONE != ret) {
64                         ret = -1;
65                 }
66                 break;
67                 _D("SYSTEM INFO : key(%s), val(%s)", (char *)key, *str);
68         case PROPERTY_TYPE_RUNTIME_INFO :
69                 ret = runtime_info_get_value_string((int)key, &(*str));
70                 if (RUNTIME_INFO_ERROR_NONE != ret) {
71                         ret = -1;
72                 }
73                 _D("RUNTIME INFO : key(%d), val(%s)", (int)key, *str);
74                 break;
75         case PROPERTY_TYPE_VCONFKEY :
76                 *str = vconf_get_str((char *)key);
77                 if (!(*str)) {
78                         ret = -1;
79                 }
80                 _D("vconfkey : key(%s), val(%s)", (char *)key, *str);
81                 break;
82         default :
83                 _E("Failed to get property. type error(%d)", type);
84                 return LOCK_ERROR_FAIL;
85         }
86
87         if (ret == -1) {
88                 _E("Failed to get property : type(%d), ret(%d)", type, ret);
89                 return LOCK_ERROR_FAIL;
90         }
91
92         return LOCK_ERROR_OK;
93 }
94
95 lock_error_e lock_property_get_bool(property_type_e type, void *key, void *val)
96 {
97         int ret = 0;
98
99         switch(type) {
100         case PROPERTY_TYPE_SYSTEM_SETTINGS :
101                 ret = system_settings_get_value_bool((int)key, val);
102                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
103                         ret = -1;
104                 }
105                 _D("SYSTEM SETTINGS : key(%d), val(%d)", (int)key, *((bool *)val));
106                 break;
107         case PROPERTY_TYPE_SYSTEM_INFO :
108                 ret = system_info_get_platform_bool((char *)key, val);
109                 if (SYSTEM_INFO_ERROR_NONE != ret) {
110                         ret = -1;
111                 }
112                 _D("SYSTEM INFO : key(%s), val(%d)", (char *)key, *((bool *)val));
113                 break;
114         case PROPERTY_TYPE_RUNTIME_INFO :
115                 ret = runtime_info_get_value_bool((int)key, val);
116                 if (RUNTIME_INFO_ERROR_NONE != ret) {
117                         ret = -1;
118                 }
119                 _D("RUNTIME INFO : key(%d), val(%d)", (int)key, *((bool *)val));
120                 break;
121         case PROPERTY_TYPE_VCONFKEY :
122                 ret = vconf_get_bool((char *)key, val);
123                 if (ret < 0) {
124                         ret = -1;
125                 }
126                 _D("vconfkey : key(%s), val(%d)", (char *)key, *((int *)val));
127                 break;
128         default :
129                 _E("Failed to get property. type error(%d)", type);
130                 return LOCK_ERROR_FAIL;
131         }
132
133         if (ret == -1) {
134                 _E("Failed to get property : type(%d), ret(%d)", type, ret);
135                 return LOCK_ERROR_FAIL;
136         }
137
138         return LOCK_ERROR_OK;
139 }
140
141 lock_error_e lock_property_get_int(property_type_e type, void *key, int *val)
142 {
143         int ret = 0;
144
145         switch(type) {
146         case PROPERTY_TYPE_SYSTEM_SETTINGS :
147                 ret = system_settings_get_value_int((int)key, val);
148                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
149                         ret = -1;
150                 }
151                 _D("SYSTEM SETTINGS : key(%d), val(%d)", (int)key, *val);
152                 break;
153         case PROPERTY_TYPE_SYSTEM_INFO :
154                 ret = system_info_get_platform_int((char *)key, val);
155                 if (SYSTEM_INFO_ERROR_NONE != ret) {
156                         ret = -1;
157                 }
158                 _D("SYSTEM INFO : key(%s), val(%d)", (char *)key, *val);
159                 break;
160         case PROPERTY_TYPE_RUNTIME_INFO :
161                 ret = runtime_info_get_value_int((int)key, &(*val));
162                 if (RUNTIME_INFO_ERROR_NONE != ret) {
163                         ret = -1;
164                 }
165                 _D("RUNTIME INFO : key(%d), val(%d)", (int)key, *val);
166                 break;
167         case PROPERTY_TYPE_VCONFKEY :
168                 ret = vconf_get_int((char *)key, &(*val));
169                 if (ret < 0) {
170                         ret = -1;
171                 }
172                 _D("vconfkey : key(%s), val(%d)", (char *)key, *val);
173                 break;
174         default :
175                 _E("Failed to get property. type error(%d)", type);
176                 return LOCK_ERROR_FAIL;
177         }
178
179         if (ret == -1) {
180                 _E("Failed to get property : type(%d), ret(%d)", type, ret);
181                 return LOCK_ERROR_FAIL;
182         }
183
184         return LOCK_ERROR_OK;
185 }
186
187 static void _feedback_state_changed_cb(system_settings_key_e key, void *user_data)
188 {
189         int ret = 0;
190         bool val = false;
191
192         ret = lock_property_get_bool(PROPERTY_TYPE_SYSTEM_SETTINGS, (void *)key, &val);
193         if (ret != LOCK_ERROR_OK) {
194                 _E("Failed to get system settings");
195                 return;
196         }
197
198         switch(key) {
199         case SYSTEM_SETTINGS_KEY_SOUND_LOCK :
200                 _D("lock sound : %d", val);
201                 s_info.is_sound_lock = val;
202                 break;
203         case SYSTEM_SETTINGS_KEY_SOUND_TOUCH :
204                 _D("touch sound : %d", val);
205                 s_info.is_sound_touch = val;
206                 break;
207         default :
208                 break;
209         }
210 }
211
212 void lock_property_register(void *data)
213 {
214         int ret = 0;
215         bool val = false;
216
217         ret = lock_property_get_bool(PROPERTY_TYPE_SYSTEM_SETTINGS, (void *)SYSTEM_SETTINGS_KEY_SOUND_LOCK, &val);
218         if (ret != LOCK_ERROR_OK) {
219                 _E("Failed to get system settings : %d", SYSTEM_SETTINGS_KEY_SOUND_LOCK);
220                 val = false;
221         }
222         s_info.is_sound_lock = val;
223
224         ret = lock_property_get_bool(PROPERTY_TYPE_SYSTEM_SETTINGS, (void *)SYSTEM_SETTINGS_KEY_SOUND_TOUCH, &val);
225         if (ret != LOCK_ERROR_OK) {
226                 _E("Failed to get system settings : %d", SYSTEM_SETTINGS_KEY_SOUND_TOUCH);
227                 val = false;
228         }
229         s_info.is_sound_touch = val;
230
231         ret = lock_property_get_bool(PROPERTY_TYPE_SYSTEM_SETTINGS, (void *)SYSTEM_SETTINGS_KEY_DISPLAY_SCREEN_ROTATION_AUTO, &val);
232         if (ret != LOCK_ERROR_OK) {
233                 _E("Failed to get system settings : %d", SYSTEM_SETTINGS_KEY_DISPLAY_SCREEN_ROTATION_AUTO);
234                 val = false;
235         }
236         s_info.is_rotation_enabled = val;
237
238         _D("sound_lock(%d), sound_touch(%d), rotation(%d)", s_info.is_sound_lock, s_info.is_sound_touch, s_info.is_rotation_enabled);
239
240         /* Register changed callback */
241         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_SOUND_LOCK, _feedback_state_changed_cb, NULL);
242         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
243                 _E("Failed to register sound feedback callback");
244         }
245
246         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_SOUND_TOUCH, _feedback_state_changed_cb, NULL);
247         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
248                 _E("Failed to register touch feedback callback");
249         }
250 }
251
252 void lock_property_unregister(void)
253 {
254         _D("unregister property cb");
255         system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_SOUND_LOCK);
256         system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_SOUND_TOUCH);
257 }