2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
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.
18 #include <sys/types.h>
26 #include "cam_error.h"
28 #include "cam_debug.h"
29 #include "cam_config.h"
30 static gchar **g_group_name = NULL; /* config information group name for save */
31 static GKeyFile *g_key_file = NULL;
32 static gboolean disable_set_mode = FALSE; /* if disable_set_mode is true unable to set setmode use for scene mode and ncr case ... */
33 gboolean cam_config_set_group_name(CamConfigType config_type,
34 const gchar *set_group_name)
36 g_return_val_if_fail(set_group_name, FALSE);
37 g_return_val_if_fail(g_group_name, FALSE);/*fix crash*/
38 if (g_group_name[config_type]) {
39 g_free(g_group_name[config_type]);
40 g_group_name[config_type] = NULL;
42 g_group_name[config_type] = g_strdup(set_group_name);
46 gboolean cam_config_init(GError **error)
49 debug_fenter(LOG_CONFIG);
51 g_group_name = g_new0(gchar *, CAM_CONFIG_MAX);
52 cam_config_set_group_name(CAM_CONFIG_TYPE_COMMON, "common");
53 cam_config_set_group_name(CAM_CONFIG_TYPE_SHORTCUTS, "shortcuts");
56 warn_msg(LOG_UI, "already initialized.");
59 g_key_file = g_key_file_new();
60 if (!g_key_file_load_from_file
61 (g_key_file, CONFIG_PATH, G_KEY_FILE_NONE, &err)) {
63 warn_msg(LOG_UI, "config file not exists. %s",
77 void cam_config_finalize(void)
83 for (i = 0; i < CAM_CONFIG_MAX; i++) {
84 if (g_group_name[i]) {
85 g_free(g_group_name[i]);
86 g_group_name[i] = NULL;
93 g_key_file_free(g_key_file);
99 void cam_config_save(void)
101 debug_fenter(LOG_UI);
102 if (g_key_file != NULL) {
106 buf = g_key_file_to_data(g_key_file, &len, &err);
109 debug_msg(LOG_UI, "%s", err->message);
114 FILE *fp = fopen(CONFIG_PATH, "w");
117 ((const void *)buf, len, 1,
119 /**fwrite return count(unsigned int) if write correct.
120 the-return-value is always >=0*/
121 critical_msg(LOG_CONFIG,
124 cam_debug(LOG_CONFIG,
129 critical_msg(LOG_CONFIG,
134 g_file_set_contents(CONFIG_PATH,
135 (const gchar *)buf, len,
138 critical_msg(LOG_CONFIG, "%s",
154 void cam_config_set_control(gboolean enable)
156 cam_debug(LOG_UI, "%d ", enable);
157 disable_set_mode = !enable;
160 void cam_config_set_int(const gchar *key, int nval)
162 g_return_if_fail(g_key_file);
163 g_return_if_fail(g_group_name);
164 g_return_if_fail(key);
165 if (disable_set_mode) {
166 /* cam_warning(LOG_UI," disable_set_mode is true "); */
169 cam_config_set_int_by_type(CAM_CONFIG_TYPE_PREVIEW, key, nval);
173 void cam_config_set_string(const gchar *key, const gchar *strval)
175 g_return_if_fail(g_key_file);
176 g_return_if_fail(g_group_name);
177 g_return_if_fail(key);
178 if (disable_set_mode) {
179 cam_warning(LOG_UI, " disable_set_mode is true ");
182 cam_config_set_string_by_type(CAM_CONFIG_TYPE_PREVIEW, key, strval);
186 void cam_config_set_boolean(const gchar *key, gboolean bval)
188 g_return_if_fail(g_key_file);
189 g_return_if_fail(g_group_name);
190 g_return_if_fail(key);
191 if (disable_set_mode) {
192 cam_warning(LOG_UI, " disable_set_mode is true ");
196 debug_msg(LOG_UI, "%s", bval ? "TRUE" : "FALSE");
199 cam_config_set_boolean_by_type(CAM_CONFIG_TYPE_PREVIEW, key, bval);
203 int cam_config_get_int(const gchar *key, int default_value)
205 g_return_val_if_fail(g_key_file, -1);
206 g_return_val_if_fail(g_group_name, -1);
207 g_return_val_if_fail(key, -1);
208 return cam_config_get_int_by_type(CAM_CONFIG_TYPE_PREVIEW, key,
212 gchar *cam_config_get_string(const gchar *key, const gchar *default_value)
214 g_return_val_if_fail(g_key_file, NULL);
215 g_return_val_if_fail(g_group_name, NULL);
216 g_return_val_if_fail(key, NULL);
217 return cam_config_get_string_by_type(CAM_CONFIG_TYPE_PREVIEW, key,
221 gboolean cam_config_get_boolean(const gchar *key, gboolean default_value)
223 g_return_val_if_fail(g_key_file, FALSE);
224 g_return_val_if_fail(g_group_name, FALSE);
225 g_return_val_if_fail(key, FALSE);
226 return cam_config_get_boolean_by_type(CAM_CONFIG_TYPE_PREVIEW, key,
230 void cam_config_set_int_by_type(CamConfigType config_type, const gchar *key,
233 g_return_if_fail(g_key_file);
234 g_return_if_fail(g_group_name);
235 g_return_if_fail(g_group_name[config_type]);
236 g_return_if_fail(key);
237 if (disable_set_mode) {
238 /* cam_warning(LOG_UI," disable_set_mode is true "); */
242 debug_msg(LOG_UI, "%s,%s,%d", g_group_name[config_type], key, nval);
245 g_key_file_set_integer(g_key_file, g_group_name[config_type], key,
249 void cam_config_set_string_by_type(CamConfigType config_type, const gchar *key,
252 g_return_if_fail(g_key_file);
253 g_return_if_fail(g_group_name);
254 g_return_if_fail(g_group_name[config_type]);
255 g_return_if_fail(key);
256 if (disable_set_mode) {
257 cam_warning(LOG_UI, " disable_set_mode is true ");
261 debug_msg(LOG_UI, "%s", strval);
264 g_key_file_set_string(g_key_file, g_group_name[config_type], key,
268 void cam_config_set_boolean_by_type(CamConfigType config_type, const gchar *key,
271 g_return_if_fail(g_key_file);
272 g_return_if_fail(g_group_name);
273 g_return_if_fail(g_group_name[config_type]);
274 g_return_if_fail(key);
275 if (disable_set_mode) {
276 cam_warning(LOG_UI, " disable_set_mode is true ");
280 debug_msg(LOG_UI, "%s", bval ? "TRUE" : "FALSE");
283 g_key_file_set_boolean(g_key_file, g_group_name[config_type], key,
287 int cam_config_get_int_by_type(CamConfigType config_type, const gchar *key,
290 g_return_val_if_fail(g_key_file, -1);
291 g_return_val_if_fail(g_group_name, -1);
292 g_return_val_if_fail(g_group_name[config_type], -1);
293 g_return_val_if_fail(key, -1);
294 GError *error = NULL;
296 g_key_file_get_integer(g_key_file, g_group_name[config_type], key,
301 if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
302 critical_msg("error:%s", error->message);
304 cam_config_set_int_by_type(config_type, key, default_value);
307 DEBUG_TRACE("-------- key[%s], value[%d]", key, default_value);
308 return default_value;
310 DEBUG_TRACE("-------- key[%s], value[%d]", key, nval);
315 gchar *cam_config_get_string_by_type(CamConfigType config_type,
317 const gchar *default_value)
319 g_return_val_if_fail(g_key_file, NULL);
320 g_return_val_if_fail(g_group_name, NULL);
321 g_return_val_if_fail(g_group_name[config_type], NULL);
322 g_return_val_if_fail(key, NULL);
323 GError *error = NULL;
324 const gchar *strval =
325 g_key_file_get_string(g_key_file, g_group_name[config_type], key,
328 if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
329 critical_msg(LOG_CONFIG, "error:%s", error->message);
330 cam_config_set_string_by_type(config_type, key, default_value);
333 return default_value ? g_strdup(default_value) : NULL;
335 return strval ? g_strdup(strval) : NULL;
339 gboolean cam_config_get_boolean_by_type(CamConfigType config_type,
341 gboolean default_value)
343 g_return_val_if_fail(g_key_file, FALSE);
344 g_return_val_if_fail(g_group_name, FALSE);
345 g_return_val_if_fail(g_group_name[config_type], FALSE);
346 g_return_val_if_fail(key, FALSE);
347 GError *error = NULL;
349 g_key_file_get_boolean(g_key_file, g_group_name[config_type], key,
352 if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
353 critical_msg(LOG_CONFIG, "error:%s", error->message);
354 cam_config_set_boolean_by_type(config_type, key, default_value);
357 return default_value;