replace value to macro for HTTP OK
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-config.c
1 /*
2  * Copyright (c) 2018 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 <glib.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <tzplatform_config.h>
21
22 #include "ttd-config.h"
23 #include "ttd-cmd.h"
24 #include "ttd-log.h"
25
26 #define CONF_FILE_NAME "ttd.conf"
27 #define CONF_PATH tzplatform_mkpath3(TZ_SYS_ETC, "ttd", "ttd.conf")
28
29 static GKeyFile *gkf = NULL;
30
31 int ttd_config_init(void)
32 {
33         GKeyFile *tmp_gkf = NULL;
34         tmp_gkf = g_key_file_new();
35         retv_if(!tmp_gkf, -1);
36
37         if (!g_key_file_load_from_file(tmp_gkf, CONF_PATH, G_KEY_FILE_NONE, NULL)) {
38                 _E("Failed to load conf file");
39                 g_key_file_free(tmp_gkf);
40                 return -1;
41         }
42         gkf = tmp_gkf;
43         return 0;
44 }
45
46 void ttd_config_fini(void)
47 {
48         if (gkf)
49                 g_key_file_free(gkf);
50
51         gkf = NULL;
52 }
53
54 int ttd_config_read_integer(const char *group, const char *key, int *value)
55 {
56         GError *error = NULL;
57         int ret = 0;
58
59         retv_if(!group, -1);
60         retv_if(!key, -1);
61         retv_if(!value, -1);
62
63         if (!gkf) {
64                 if (ttd_config_init() < 0) {
65                         _E("Failed to initialize configure");
66                         return -1;
67                 }
68         }
69
70         ret = g_key_file_get_integer(gkf, group, key, &error);
71         if (error) {
72                 _E("Failed to get integer[%s]", error->message);
73                 g_error_free(error);
74                 return -1;
75         }
76
77         *value = ret;
78
79         return 0;
80 }
81
82 int ttd_config_read_boolean(const char *group, const char *key, bool *value)
83 {
84         GError *error = NULL;
85         gboolean ret = FALSE;
86
87         retv_if(!group, -1);
88         retv_if(!key, -1);
89         retv_if(!value, -1);
90
91         if (!gkf) {
92                 if (ttd_config_init() < 0) {
93                         _E("Failed to initialize configure");
94                         return -1;
95                 }
96         }
97
98         ret = g_key_file_get_boolean(gkf, group, key, &error);
99         if (error) {
100                 _E("Failed to get boolean[%s]", error->message);
101                 g_error_free(error);
102                 return -1;
103         }
104
105         *value = ret;
106
107         return 0;
108 }
109
110 int ttd_config_read_string(const char *group, const char *key, char **value)
111 {
112         GError *error = NULL;
113         char *ret = NULL;
114
115         retv_if(!group, -1);
116         retv_if(!key, -1);
117         retv_if(!value, -1);
118
119         if (!gkf) {
120                 if (ttd_config_init() < 0) {
121                         _E("Failed to initialize configure");
122                         return -1;
123                 }
124         }
125
126         ret = g_key_file_get_string(gkf, group, key, &error);
127         if (error) {
128                 _E("Failed to get string[%s]", error->message);
129                 g_error_free(error);
130                 return -1;
131         }
132
133         *value = ret;
134
135         return 0;
136 }
137
138 int ttd_config_write_integer(const char *group, const char *key, int value)
139 {
140         retv_if(!group, -1);
141         retv_if(!key, -1);
142
143         if (!gkf) {
144                 if (ttd_config_init() < 0) {
145                         _E("Failed to initialize configure");
146                         return -1;
147                 }
148         }
149
150         g_key_file_set_integer(gkf, group, key, value);
151
152         return 0;
153 }
154
155 int ttd_config_write_boolean(const char *group, const char *key, bool value)
156 {
157         retv_if(!group, -1);
158         retv_if(!key, -1);
159
160         if (!gkf) {
161                 if (ttd_config_init() < 0) {
162                         _E("Failed to initialize configure");
163                         return -1;
164                 }
165         }
166
167         g_key_file_set_boolean(gkf, group, key, value);
168
169         return 0;
170 }
171
172 int ttd_config_write_string(const char *group, const char *key, const char *value)
173 {
174         retv_if(!group, -1);
175         retv_if(!key, -1);
176         retv_if(!value, -1);
177
178         if (!gkf) {
179                 if (ttd_config_init() < 0) {
180                         _E("Failed to initialize configure");
181                         return -1;
182                 }
183         }
184
185         g_key_file_set_string(gkf, group, key, value);
186
187         return 0;
188 }
189
190 int ttd_config_remove_group(const char *group)
191 {
192         gboolean ret = FALSE;
193         GError *error = NULL;
194
195         retv_if(!group, -1);
196
197         if (!gkf) {
198                 if (ttd_config_init() < 0) {
199                         _E("Failed to initialize configure");
200                         return -1;
201                 }
202         }
203
204         ret = g_key_file_remove_group(gkf, group, &error);
205         if (!ret) {
206                 _E("Failed to remove group[%s]--caused by[%s]", group, error->message);
207                 g_error_free(error);
208                 return -1;
209         }
210
211         return 0;
212 }
213
214 int ttd_config_remove_key(const char *group, const char *key)
215 {
216         gboolean ret = FALSE;
217         GError *error = NULL;
218
219         retv_if(!group, -1);
220         retv_if(!key, -1);
221
222         if (!gkf) {
223                 if (ttd_config_init() < 0) {
224                         _E("Failed to initialize configure");
225                         return -1;
226                 }
227         }
228
229         ret = g_key_file_remove_key(gkf, group, key, &error);
230         if (!ret) {
231                 _E("Failed to remove group[%s]--caused by[%s]", group, error->message);
232                 g_error_free(error);
233                 return -1;
234         }
235
236         return 0;
237 }