2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
24 #include <app_common.h>
26 #define CONFIG_FILENAME "config.ini"
28 static GKeyFile *gk = NULL;
30 static char *_config_file_path_get()
33 char *data = app_get_data_path();
38 if (asprintf(&ret, "%s%s", data, CONFIG_FILENAME) == -1) {
51 _D("config_init already called.");
55 gk = g_key_file_new();
57 _E("g_key_file_new failed.");
61 char *path = _config_file_path_get();
63 if (!g_key_file_load_from_file(gk, path, G_KEY_FILE_NONE, &error)) {
64 if (error->code != G_FILE_ERROR_NOENT) {
65 _E("g_key_file_load_from_file failed. %s", error->message);
84 char *path = _config_file_path_get();
86 if (!g_key_file_save_to_file(gk, path, &error)) {
87 _E("g_key_file_save_to_file failed: %s", error->message);
96 void config_shutdown()
98 if (gk) g_key_file_free(gk);
102 int config_get_string(const char *group, const char *key, char **out)
104 GError *error = NULL;
112 value = g_key_file_get_string(gk, group, key, &error);
114 _E("g_key_file_get_string failed: %s", error->message);
122 void config_set_string(const char *group, const char *key, const char *value)
128 g_key_file_set_string(gk, group, key, value);
131 int config_get_int(const char *group, const char *key, int *out)
133 GError *error = NULL;
141 value = g_key_file_get_integer(gk, group, key, &error);
143 _E("g_key_file_get_integer failed: %s", error->message);
151 void config_set_int(const char *group, const char *key, int value)
157 g_key_file_set_integer(gk, group, key, value);
160 int config_get_double(const char *group, const char *key, double *out)
162 GError *error = NULL;
170 value = g_key_file_get_double(gk, group, key, &error);
172 _E("g_key_file_get_double failed: %s", error->message);
180 void config_set_double(const char *group, const char *key, double value)
186 g_key_file_set_double(gk, group, key, value);
189 int config_get_bool(const char *group, const char *key, bool *out)
191 GError *error = NULL;
199 value = g_key_file_get_boolean(gk, group, key, &error);
201 _E("g_key_file_get_boolean failed: %s", error->message);
209 void config_set_bool(const char *group, const char *key, bool value)
215 g_key_file_set_boolean(gk, group, key, value);
218 int config_remove_key(const char *group, const char *key)
224 return g_key_file_remove_key(gk, group, key, NULL) ? 0 : 1;
227 int config_remove_group(const char *group)
232 return g_key_file_remove_group(gk, group, NULL) ? 0 : 1;
235 bool config_get_string_or_set_default(char *group, char *key, char *default_value, char **value)
237 bool modified = false;
239 if (config_get_string(group, key, value) != 0) {
240 config_set_string(group, key, default_value);
242 *value = strdup(default_value);
250 bool config_get_int_with_default(char *group, char *key, int default_value, int *value)
252 bool modified = false;
254 if (config_get_int(group, key, value) != 0) {
255 config_set_int(group, key, default_value);
257 *value = default_value;
264 bool config_get_rgb_with_default(char *group, char *key,
265 int default_r, int default_g, int default_b,
266 int *red, int *green, int *blue)
268 bool modified = false;
271 if (config_get_int(group, key, &color) != 0) {
273 color = (default_r & 0xFF) << 16;
274 color += (default_g & 0xFF) << 8;
275 color += (default_b & 0xFF) << 0;
277 config_set_int(group, key, color);
282 *red = (color >> 16) & 0xFF;
283 *green = (color >> 8) & 0xFF;
284 *blue = (color >> 0) & 0xFF;