---
src/include/internal_libreport.h | 11 ++++
src/lib/Makefile.am | 1 +
src/lib/user_settings.c | 122 ++++++++++++++++++++++++++++++++++++++
3 files changed, 134 insertions(+), 0 deletions(-)
create mode 100644 src/lib/user_settings.c
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
index e754871..4b5cd7a 100644
--- a/src/include/internal_libreport.h
+++ b/src/include/internal_libreport.h
@@ -576,6 +576,17 @@ void parse_release_for_rhts(const char *pRelease, char **product,
char **version
#define load_conf_file libreport_load_conf_file
bool load_conf_file(const char *pPath, map_string_h *settings, bool
skipKeysWithoutValue);
+#define save_conf_file libreport_save_conf_file
+bool save_conf_file(const char *path, map_string_h *settings);
+#define save_user_settings libreport_save_user_settings
+bool save_user_settings();
+#define load_user_settings libreport_load_user_settings
+bool load_user_settings(const char *application_name);
+#define set_user_setting libreport_set_user_setting
+void set_user_setting(const char *name, const char *value);
+#define get_user_setting libreport_get_user_setting
+const char *get_user_setting(const char *name);
+
/* Connect to abrtd over unix domain socket, issue DELETE command */
int delete_dump_dir_possibly_using_abrtd(const char *dump_dir_name);
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 624c3f6..cc8b71e 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -46,6 +46,7 @@ libreport_la_SOURCES = \
event_config.c \
report.c \
make_dir_recursive.c \
+ user_settings.c \
client.c
libreport_la_CPPFLAGS = \
-Wall -Wwrite-strings -Werror \
diff --git a/src/lib/user_settings.c b/src/lib/user_settings.c
new file mode 100644
index 0000000..df7fe03
--- /dev/null
+++ b/src/lib/user_settings.c
@@ -0,0 +1,122 @@
+/*
+ Copyright (C) 2011 RedHat inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#include "internal_libreport.h"
+
+static GHashTable *user_settings;
+static char *conf_path;
+
+static bool create_parentdir(char *path)
+{
+ bool ret;
+ char *c;
+
+ /* dirname() */
+ for (c = path + strlen(path); c > path && *c != '/'; c--)
+ ;
+ if (*c != '/')
+ return false;
+ *c = '\0';
+
+ ret = make_dir_recursive(path, 0755);
+ *c = '/'; /* restore path back */
+
+ return ret;
+}
+
+/* Returns false if write failed */
+bool save_conf_file(const char *path, map_string_h *settings)
+{
+ bool ret;
+ FILE *out;
+ char *temp_path, *name, *value;
+ GHashTableIter iter;
+
+ ret = false, out = NULL;
+
+ if (!(temp_path = xasprintf("%s.tmp", path)) ||
+ !create_parentdir(temp_path) ||
+ !(out = fopen(temp_path, "w")))
+ goto cleanup;
+
+ g_hash_table_iter_init(&iter, settings);
+ while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
+ fprintf(out, "%s = \"%s\"\n", name, value);
+
+ fclose(out);
+ out = NULL;
+
+ if (!rename(temp_path, path))
+ goto cleanup;
+
+ ret = true; /* success */
+
+cleanup:
+ if (out)
+ fclose(out);
+ free(temp_path);
+
+ return ret;
+}
+
+static char *get_conf_path(const char *name)
+{
+ char *HOME = getenv("HOME"), *s, *conf;
+
+ s = xasprintf("%s/%s", ".abrt/settings", name);
+ conf = concat_path_file(HOME, s);
+ free(s);
+ return conf;
+}
+
+bool save_user_settings()
+{
+ if (!conf_path || !user_settings)
+ return true;
+
+ return save_conf_file(conf_path, user_settings);
+}
+
+bool load_user_settings(const char *application_name)
+{
+ if (conf_path)
+ free(conf_path);
+ conf_path = get_conf_path(application_name);
+
+ if (user_settings)
+ g_hash_table_destroy(user_settings);
+ user_settings = g_hash_table_new_full(
+ /*hash_func*/ g_str_hash,
+ /*key_equal_func:*/ g_str_equal,
+ /*key_destroy_func:*/ free,
+ /*value_destroy_func:*/ free);
+
+ return load_conf_file(conf_path, user_settings, false);
+}
+
+void set_user_setting(const char *name, const char *value)
+{
+ if (value)
+ g_hash_table_replace(user_settings, xstrdup(name), xstrdup(value));
+ else
+ g_hash_table_remove(user_settings, name);
+}
+
+const char *get_user_setting(const char *name)
+{
+ return g_hash_table_lookup(user_settings, name);
+}
--
1.7.6.4