/*
 * This file is part of libgaminggear.
 *
 * libgaminggear 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.
 *
 * libgaminggear 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 libgaminggear. If not, see <http://www.gnu.org/licenses/>.
 */

#include "gaminggear_key_file.h"
#include "gaminggear_helper.h"
#include "i18n-lib.h"
#include <string.h>

struct _GaminggearKeyFile {
	GKeyFile *key_file;
	gboolean modified;
	gchar *filename;
};

static GaminggearKeyFile *gaminggear_key_file_new(void) {
	GaminggearKeyFile *key_file;

	key_file = (GaminggearKeyFile *)g_malloc0(sizeof(GaminggearKeyFile));

	key_file->key_file = g_key_file_new();
	key_file->modified = FALSE;

	return key_file;
}

GaminggearKeyFile *gaminggear_key_file_load(gchar const *filename) {
	GaminggearKeyFile *key_file;
	GError *error = NULL;

	key_file = gaminggear_key_file_new();
	key_file->filename = g_strdup(filename);

	if (!g_key_file_load_from_file(key_file->key_file, key_file->filename, G_KEY_FILE_NONE, &error))
		g_clear_error(&error);

	return key_file;
}

gboolean gaminggear_key_file_save(GaminggearKeyFile *key_file, GError **error) {
	gchar *data;
	gsize length;
	gboolean retval;
	gchar *dir;

	if (key_file->modified == FALSE)
		return TRUE;

	dir = g_path_get_dirname(key_file->filename);
	retval = gaminggear_create_dir_if_needed(dir, error);
	g_free(dir);
	if (!retval)
		return FALSE;

	data = g_key_file_to_data(key_file->key_file, &length, error);
	retval = g_file_set_contents(key_file->filename, data, length, error);
	g_free(data);
	if (retval)
		key_file->modified = FALSE;

	return retval;
}

gboolean gaminggear_key_file_save_as(GaminggearKeyFile *key_file, gchar const *filename, GError **error) {
	g_free(key_file->filename);
	key_file->filename = g_strdup(filename);
	key_file->modified = TRUE;
	return gaminggear_key_file_save(key_file, error);
}

void gaminggear_key_file_free(GaminggearKeyFile *key_file) {
	g_key_file_free(key_file->key_file);
	g_free(key_file->filename);
	g_free(key_file);
}

static gchar *gaminggear_key_file_get_string(GaminggearKeyFile *key_file, char const *group, gchar const *key, GError **error) {
	return g_key_file_get_string(key_file->key_file, group, key, error);
}

gchar *gaminggear_key_file_get_string_with_default(GaminggearKeyFile *key_file, char const *group, gchar const *key, gchar const *defval) {
	gchar *result;
	GError *error = NULL;

	result = gaminggear_key_file_get_string(key_file, group, key, &error);
	if (!result) {
		g_clear_error(&error);
		return g_strdup(defval);
	}

	return result;
}

void gaminggear_key_file_set_string(GaminggearKeyFile *key_file, char const *group, gchar const *key, gchar const *new_value) {
	gchar *orig;
	GError *error = NULL;

	orig = gaminggear_key_file_get_string(key_file, group, key, &error);
	if (error || strcmp(orig, new_value)) {
		g_key_file_set_string(key_file->key_file, group, key, new_value);
		key_file->modified = TRUE;
	}
	g_free(orig);
}

static gint gaminggear_key_file_get_integer(GaminggearKeyFile *key_file, char const *group, gchar const *key, GError **error) {
	return g_key_file_get_integer(key_file->key_file, group, key, error);
}

gint gaminggear_key_file_get_integer_with_default(GaminggearKeyFile *key_file, char const *group, gchar const *key, gint defval) {
	gint retval;
	GError *error = NULL;

	retval = gaminggear_key_file_get_integer(key_file, group, key, &error);
	if (error) {
		g_clear_error(&error);
		return defval;
	}

	return retval;
}

void gaminggear_key_file_set_integer(GaminggearKeyFile *key_file, char const *group, gchar const *key, gint new_value) {
	gint orig;
	GError *error = NULL;

	orig = gaminggear_key_file_get_integer(key_file, group, key, &error);
	if (error || (orig != new_value)) {
		g_clear_error(&error);
		g_key_file_set_integer(key_file->key_file, group, key, new_value);
		key_file->modified = TRUE;
	}
}

static gdouble gaminggear_key_file_get_double(GaminggearKeyFile *key_file, char const *group, gchar const *key, GError **error) {
	return g_key_file_get_double(key_file->key_file, group, key, error);
}

gdouble gaminggear_key_file_get_double_with_default(GaminggearKeyFile *key_file, char const *group, gchar const *key, gdouble defval) {
	gint retval;
	GError *error = NULL;

	retval = gaminggear_key_file_get_double(key_file, group, key, &error);
	if (error) {
		g_clear_error(&error);
		return defval;
	}

	return retval;
}

void gaminggear_key_file_set_double(GaminggearKeyFile *key_file, char const *group, gchar const *key, gdouble new_value) {
	gdouble orig;
	GError *error = NULL;

	orig = gaminggear_key_file_get_double(key_file, group, key, &error);
	if (error || (orig != new_value)) {
		g_clear_error(&error);
		g_key_file_set_double(key_file->key_file, group, key, new_value);
		key_file->modified = TRUE;
	}
}

static gboolean gaminggear_key_file_get_boolean(GaminggearKeyFile *key_file, char const *group, gchar const *key, GError **error) {
	return g_key_file_get_boolean(key_file->key_file, group, key, error);
}

gboolean gaminggear_key_file_get_boolean_with_default(GaminggearKeyFile *key_file, char const *group, gchar const *key, gboolean defval) {
	gboolean result;
	GError *error = NULL;

	result = gaminggear_key_file_get_boolean(key_file, group, key, &error);
	if (error) {
		g_clear_error(&error);
		return defval;
	}
	return result;
}

void gaminggear_key_file_set_boolean(GaminggearKeyFile *key_file, char const *group, gchar const *key, gboolean new_value) {
	gboolean orig;
	GError *error = NULL;

	orig = gaminggear_key_file_get_boolean(key_file, group, key, &error);
	if (error || orig != new_value) {
		g_clear_error(&error);
		g_key_file_set_boolean(key_file->key_file, group, key, new_value);
		key_file->modified = TRUE;
	}
}
