e9c8e38f48d7b1e5613c3e423e521a9dd442f3d7
[platform/core/api/runtime-info.git] / src / runtime_info_vconf.c
1 /*
2  * Copyright (c) 2011 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <vconf.h>
22 #include <dlog.h>
23
24 #include <runtime_info.h>
25 #include <runtime_info_private.h>
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
32
33 #define CHECK_VCONF_RESULT(ret) \
34         do { \
35                 if (ret < 0) { \
36                         ret = vconf_get_ext_errno(); \
37                         if (ret == VCONF_ERROR_FILE_NO_ENT) \
38                                 ret = RUNTIME_INFO_ERROR_NOT_SUPPORTED; \
39                         else \
40                                 ret = RUNTIME_INFO_ERROR_IO_ERROR; \
41                 } else { \
42                         ret = RUNTIME_INFO_ERROR_NONE; \
43                 } \
44         } while (0);
45
46 int runtime_info_vconf_get_value_int(const char *vconf_key, int *value)
47 {
48         int ret = vconf_get_int(vconf_key, value);
49         CHECK_VCONF_RESULT(ret);
50         return ret;
51 }
52
53 int runtime_info_vconf_get_value_bool(const char *vconf_key, int *value)
54 {
55         int ret = vconf_get_bool(vconf_key, value);
56         CHECK_VCONF_RESULT(ret);
57         return ret;
58 }
59
60 int runtime_info_vconf_get_value_double(const char *vconf_key, double *value)
61 {
62         int ret = vconf_get_dbl(vconf_key, value);
63         CHECK_VCONF_RESULT(ret);
64         return ret;
65 }
66
67 int runtime_info_vconf_get_value_string(const char *vconf_key, char **value)
68 {
69         char *str_value = NULL;
70         int ret;
71
72         str_value = vconf_get_str(vconf_key);
73         if (!str_value) {
74                 ret = -1;
75                 CHECK_VCONF_RESULT(ret);
76                 return ret;
77         }
78
79         *value = str_value;
80         return RUNTIME_INFO_ERROR_NONE;
81 }
82
83 typedef void (*runtime_info_vconf_event_cb)(keynode_t *node, void *event_data);
84
85 static void runtime_info_vconf_event_cb0(keynode_t *node, void *event_data)
86 {
87         if (node != NULL)
88                 runtime_info_updated((runtime_info_key_e)event_data);
89 }
90
91 static void runtime_info_vconf_event_cb1(keynode_t *node, void *event_data)
92 {
93         if (node != NULL)
94                 runtime_info_updated((runtime_info_key_e)event_data);
95 }
96
97 static void runtime_info_vconf_event_cb2(keynode_t *node, void *event_data)
98 {
99         if (node != NULL)
100                 runtime_info_updated((runtime_info_key_e)event_data);
101 }
102
103 static void runtime_info_vconf_event_cb3(keynode_t *node, void *event_data)
104 {
105         if (node != NULL)
106                 runtime_info_updated((runtime_info_key_e)event_data);
107 }
108
109 static void runtime_info_vconf_event_cb4(keynode_t *node, void *event_data)
110 {
111         if (node != NULL)
112                 runtime_info_updated((runtime_info_key_e)event_data);
113 }
114
115 static runtime_info_vconf_event_cb runtime_info_vconf_get_event_cb_slot(int slot)
116 {
117         switch (slot) {
118         case 0:
119                 return runtime_info_vconf_event_cb0;
120
121         case 1:
122                 return runtime_info_vconf_event_cb1;
123
124         case 2:
125                 return runtime_info_vconf_event_cb2;
126
127         case 3:
128                 return runtime_info_vconf_event_cb3;
129
130         case 4:
131                 return runtime_info_vconf_event_cb4;
132
133         default:
134                 return NULL;
135         }
136 }
137
138 int runtime_info_vconf_set_event_cb(const char *vconf_key, runtime_info_key_e runtime_info_key, int slot)
139 {
140         runtime_info_vconf_event_cb vconf_event_cb;
141         int ret;
142
143         vconf_event_cb = runtime_info_vconf_get_event_cb_slot(slot);
144
145         if (vconf_event_cb == NULL)
146                 return RUNTIME_INFO_ERROR_IO_ERROR;
147
148         ret = vconf_notify_key_changed(vconf_key, vconf_event_cb, (void *)runtime_info_key);
149         CHECK_VCONF_RESULT(ret);
150         return ret;
151 }
152
153 void runtime_info_vconf_unset_event_cb(const char *vconf_key, int slot)
154 {
155         runtime_info_vconf_event_cb vconf_event_cb;
156
157         vconf_event_cb = runtime_info_vconf_get_event_cb_slot(slot);
158
159         if (vconf_event_cb != NULL)
160                 vconf_ignore_key_changed(vconf_key, vconf_event_cb);
161 }