diff options
Diffstat (limited to 'src/cam_config.c')
-rw-r--r-- | src/cam_config.c | 361 |
1 files changed, 361 insertions, 0 deletions
diff --git a/src/cam_config.c b/src/cam_config.c new file mode 100644 index 0000000..02fc71b --- /dev/null +++ b/src/cam_config.c @@ -0,0 +1,361 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + +/* +#define DEBUG_CONFIG +#include "cam.h" +#include "cam_error.h" +*/ +#include "cam_debug.h" +#include "cam_config.h" +static gchar **g_group_name = NULL; /* config information group name for save */ +static GKeyFile *g_key_file = NULL; +static gboolean disable_set_mode = FALSE; /* if disable_set_mode is true unable to set setmode use for scene mode and ncr case ... */ +gboolean cam_config_set_group_name(CamConfigType config_type, + const gchar *set_group_name) +{ + g_return_val_if_fail(set_group_name, FALSE); + g_return_val_if_fail(g_group_name, FALSE);/*fix crash*/ + if (g_group_name[config_type]) { + g_free(g_group_name[config_type]); + g_group_name[config_type] = NULL; + } + g_group_name[config_type] = g_strdup(set_group_name); + return TRUE; +} + +gboolean cam_config_init(GError **error) +{ + GError *err = NULL; + debug_fenter(LOG_CONFIG); + if (!g_group_name) { + g_group_name = g_new0(gchar *, CAM_CONFIG_MAX); + cam_config_set_group_name(CAM_CONFIG_TYPE_COMMON, "common"); + cam_config_set_group_name(CAM_CONFIG_TYPE_SHORTCUTS, "shortcuts"); + } + if (g_key_file) { + warn_msg(LOG_UI, "already initialized."); + return TRUE; + } + g_key_file = g_key_file_new(); + if (!g_key_file_load_from_file + (g_key_file, CONFIG_PATH, G_KEY_FILE_NONE, &err)) { + if (err != NULL) { + warn_msg(LOG_UI, "config file not exists. %s", + err->message); + g_error_free(err); + err = NULL; + } + } + if (err != NULL) { + g_error_free(err); + err = NULL; + } + debug_fleave(LOG_UI); + return TRUE; +} + +void cam_config_finalize(void) +{ + debug_fenter(LOG_UI); + cam_config_save(); + if (g_group_name) { + int i; + for (i = 0; i < CAM_CONFIG_MAX; i++) { + if (g_group_name[i]) { + g_free(g_group_name[i]); + g_group_name[i] = NULL; + } + } + g_free(g_group_name); + g_group_name = NULL; + } + if (g_key_file) { + g_key_file_free(g_key_file); + g_key_file = NULL; + } + debug_fleave(LOG_UI); +} + +void cam_config_save(void) +{ + debug_fenter(LOG_UI); + if (g_key_file != NULL) { + GError *err = NULL; + gchar *buf = NULL; + gsize len = 0; + buf = g_key_file_to_data(g_key_file, &len, &err); + if (buf) { + if (err) { + debug_msg(LOG_UI, "%s", err->message); + g_error_free(err); + err = NULL; + } else { +#if 1 + FILE *fp = fopen(CONFIG_PATH, "w"); + if (fp != NULL) { + if (fwrite + ((const void *)buf, len, 1, + fp) != 1) { + /**fwrite return count(unsigned int) if write correct. + the-return-value is always >=0*/ + critical_msg(LOG_CONFIG, + "fwrite failed"); + } else { + cam_debug(LOG_CONFIG, + "save success"); + } + fclose(fp); + } else { + critical_msg(LOG_CONFIG, + "fopen failed"); + } + +#else + g_file_set_contents(CONFIG_PATH, + (const gchar *)buf, len, + &err); + if (err) { + critical_msg(LOG_CONFIG, "%s", + err->message); + g_error_free(err); + err = NULL; + } +#endif + } + g_free(buf); + } + if (err != NULL) { + g_error_free(err); + err = NULL; + } + } +} + +void cam_config_set_control(gboolean enable) +{ + cam_debug(LOG_UI, "%d ", enable); + disable_set_mode = !enable; +} + +void cam_config_set_int(const gchar *key, int nval) +{ + g_return_if_fail(g_key_file); + g_return_if_fail(g_group_name); + g_return_if_fail(key); + if (disable_set_mode) { + /* cam_warning(LOG_UI," disable_set_mode is true "); */ + return; + } + cam_config_set_int_by_type(CAM_CONFIG_TYPE_PREVIEW, key, nval); + return; +} + +void cam_config_set_string(const gchar *key, const gchar *strval) +{ + g_return_if_fail(g_key_file); + g_return_if_fail(g_group_name); + g_return_if_fail(key); + if (disable_set_mode) { + cam_warning(LOG_UI, " disable_set_mode is true "); + return; + } + cam_config_set_string_by_type(CAM_CONFIG_TYPE_PREVIEW, key, strval); + return; +} + +void cam_config_set_boolean(const gchar *key, gboolean bval) +{ + g_return_if_fail(g_key_file); + g_return_if_fail(g_group_name); + g_return_if_fail(key); + if (disable_set_mode) { + cam_warning(LOG_UI, " disable_set_mode is true "); + return; + } +#ifdef DEBUG_CONFIG + debug_msg(LOG_UI, "%s", bval ? "TRUE" : "FALSE"); + +#endif /*
*/ + cam_config_set_boolean_by_type(CAM_CONFIG_TYPE_PREVIEW, key, bval); + return; +} + +int cam_config_get_int(const gchar *key, int default_value) +{ + g_return_val_if_fail(g_key_file, -1); + g_return_val_if_fail(g_group_name, -1); + g_return_val_if_fail(key, -1); + return cam_config_get_int_by_type(CAM_CONFIG_TYPE_PREVIEW, key, + default_value); +} + +gchar *cam_config_get_string(const gchar *key, const gchar *default_value) +{ + g_return_val_if_fail(g_key_file, NULL); + g_return_val_if_fail(g_group_name, NULL); + g_return_val_if_fail(key, NULL); + return cam_config_get_string_by_type(CAM_CONFIG_TYPE_PREVIEW, key, + default_value); +} + +gboolean cam_config_get_boolean(const gchar *key, gboolean default_value) +{ + g_return_val_if_fail(g_key_file, FALSE); + g_return_val_if_fail(g_group_name, FALSE); + g_return_val_if_fail(key, FALSE); + return cam_config_get_boolean_by_type(CAM_CONFIG_TYPE_PREVIEW, key, + default_value); +} + +void cam_config_set_int_by_type(CamConfigType config_type, const gchar *key, + int nval) +{ + g_return_if_fail(g_key_file); + g_return_if_fail(g_group_name); + g_return_if_fail(g_group_name[config_type]); + g_return_if_fail(key); + if (disable_set_mode) { + /* cam_warning(LOG_UI," disable_set_mode is true "); */ + return; + } +#ifdef DEBUG_CONFIG + debug_msg(LOG_UI, "%s,%s,%d", g_group_name[config_type], key, nval); + +#endif /*
*/ + g_key_file_set_integer(g_key_file, g_group_name[config_type], key, + nval); +} + +void cam_config_set_string_by_type(CamConfigType config_type, const gchar *key, + const gchar *strval) +{ + g_return_if_fail(g_key_file); + g_return_if_fail(g_group_name); + g_return_if_fail(g_group_name[config_type]); + g_return_if_fail(key); + if (disable_set_mode) { + cam_warning(LOG_UI, " disable_set_mode is true "); + return; + } +#ifdef DEBUG_CONFIG + debug_msg(LOG_UI, "%s", strval); + +#endif /*
*/ + g_key_file_set_string(g_key_file, g_group_name[config_type], key, + strval); +} + +void cam_config_set_boolean_by_type(CamConfigType config_type, const gchar *key, + gboolean bval) +{ + g_return_if_fail(g_key_file); + g_return_if_fail(g_group_name); + g_return_if_fail(g_group_name[config_type]); + g_return_if_fail(key); + if (disable_set_mode) { + cam_warning(LOG_UI, " disable_set_mode is true "); + return; + } +#ifdef DEBUG_CONFIG + debug_msg(LOG_UI, "%s", bval ? "TRUE" : "FALSE"); + +#endif /*
*/ + g_key_file_set_boolean(g_key_file, g_group_name[config_type], key, + bval); +} + +int cam_config_get_int_by_type(CamConfigType config_type, const gchar *key, + int default_value) +{ + g_return_val_if_fail(g_key_file, -1); + g_return_val_if_fail(g_group_name, -1); + g_return_val_if_fail(g_group_name[config_type], -1); + g_return_val_if_fail(key, -1); + GError *error = NULL; + gint nval = + g_key_file_get_integer(g_key_file, g_group_name[config_type], key, + &error); + if (error) { + +/* + if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) + critical_msg("error:%s", error->message); +*/ + cam_config_set_int_by_type(config_type, key, default_value); + g_error_free(error); + error = NULL; + DEBUG_TRACE("-------- key[%s], value[%d]", key, default_value); + return default_value; + } else { + DEBUG_TRACE("-------- key[%s], value[%d]", key, nval); + return nval; + } +} + +gchar *cam_config_get_string_by_type(CamConfigType config_type, + const gchar *key, + const gchar *default_value) +{ + g_return_val_if_fail(g_key_file, NULL); + g_return_val_if_fail(g_group_name, NULL); + g_return_val_if_fail(g_group_name[config_type], NULL); + g_return_val_if_fail(key, NULL); + GError *error = NULL; + const gchar *strval = + g_key_file_get_string(g_key_file, g_group_name[config_type], key, + &error); + if (error) { + if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) + critical_msg(LOG_CONFIG, "error:%s", error->message); + cam_config_set_string_by_type(config_type, key, default_value); + g_error_free(error); + error = NULL; + return default_value ? g_strdup(default_value) : NULL; + } else { + return strval ? g_strdup(strval) : NULL; + } +} + +gboolean cam_config_get_boolean_by_type(CamConfigType config_type, + const gchar *key, + gboolean default_value) +{ + g_return_val_if_fail(g_key_file, FALSE); + g_return_val_if_fail(g_group_name, FALSE); + g_return_val_if_fail(g_group_name[config_type], FALSE); + g_return_val_if_fail(key, FALSE); + GError *error = NULL; + gboolean bval = + g_key_file_get_boolean(g_key_file, g_group_name[config_type], key, + &error); + if (error) { + if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) + critical_msg(LOG_CONFIG, "error:%s", error->message); + cam_config_set_boolean_by_type(config_type, key, default_value); + g_error_free(error); + error = NULL; + return default_value; + } else { + return bval; + } +} |