/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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.
 *
 *
 *                      ********************
 *
 *
 *  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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#include <gtk/gtk.h>
#include "Gconf.h"
#include "Quit.h"
#include "Debug.h"
#include "Declaration_Constante.h"

struct Struct_Gconf_Variable {
	int       index;
	gchar    *path_gconf;
	gchar    *defaut_chaine;
	int       defaut_entier;
	gboolean  defaut_boolean;
} Gconf_Variable[] = {
	{
	BOOL_FIRST_START, "/apps/autoscan-network/preference/global/first_start", NULL, 0, TRUE}, {
	INT_DEBUG, "/apps/autoscan-network/preference/global/debug", NULL, D_INFO, FALSE}, {
	BOOL_START_MINIMIZED, "/apps/autoscan-network/preference/global/start_minimized", NULL, 0, FALSE}, {
	BOOL_MINIMIZED_TRAY_ICON, "/apps/autoscan-network/preference/global/show_tray_icon", NULL, 0, FALSE}, {
	BOOL_LOAD_DEFAULT_NETWORK, "/apps/autoscan-network/preference/global/load_default_network", NULL, 0, FALSE}, {
	CHAR_LOAD_DEFAULT_NETWORK_FILE, "/apps/autoscan-network/preference/global/load_default_network_file", "", 0, FALSE}, {
	BOOL_AUTO_SAVE_NETWORK, "/apps/autoscan-network/preference/global/auto_save_network", NULL, 0, FALSE}, {
	CHAR_AUTO_SAVE_NETWORK_FILE, "/apps/autoscan-network/preference/global/auto_save_network_file", "", 0, FALSE}, {
	CHAR_SERVEUR_SMTP, "/apps/autoscan-network/preference/global/server_SMTP", "smtp.server.fr", 0, FALSE}, {
	INT_AUDIO_DEVICE, "/apps/autoscan-network/preference/global/audio_device", NULL, 0, FALSE}, {
	INT_VERSION_SIGNATURE, "/apps/autoscan-network/preference/global/version_fingerprinting", NULL, 0, FALSE}, {
	CHAR_CHEMIN_SON_ALERT, "/apps/autoscan-network/preference/global/path_sound_alert", "/usr/share/sounds/autoscan-network/redalert.ogg", 0, FALSE}, {
	INT_ROOT_POSITION_X, "/apps/autoscan-network/window/position_X", NULL, 0, FALSE}, {
	INT_ROOT_POSITION_Y, "/apps/autoscan-network/window/position_Y", NULL, 0, FALSE}, {
	INT_ROOT_SIZE_X, "/apps/autoscan-network/window/size_X", NULL, 570, FALSE}, {
	INT_ROOT_SIZE_Y, "/apps/autoscan-network/window/size_Y", NULL, 450, FALSE}, {
	INT_SETTINGS_SIZE_X, "/apps/autoscan-network/window/setting_size_X", NULL, 668, FALSE}, {
	INT_SETTINGS_SIZE_Y, "/apps/autoscan-network/window/setting_size_Y", NULL, 382, FALSE}, {
	INT_ICONLIST_ICON_WIDTH, "/apps/autoscan-network/window/iconlist_icon_width", NULL, 70, FALSE}, {
	INT_V_PANED_POSITION, "/apps/autoscan-network/window/v_paned_position", NULL, 150, FALSE}, {
	INT_H_PANED_POSITION, "/apps/autoscan-network/window/h_paned_position", NULL, 195, FALSE}, {
	BOOL_TREE_VIEW_SET_HOVER_EXPAND, "/apps/autoscan-network/window/tree_view_set_hover_expand", NULL, 0, FALSE}, {
	BOOL_TREE_VIEW_SET_RULES_HINT, "/apps/autoscan-network/window/tree_view_set_rules_hint", NULL, 0, FALSE}, {
	INT_THREAD_MAX, "/apps/autoscan-network/preference/network/thread_max", NULL, 50, FALSE}, {
	BOOL_NORMAL_MODE, "/apps/autoscan-network/preference/network/normal_mode", NULL, 0, TRUE}, {
	BOOL_IP_DYNAMIC, "/apps/autoscan-network/preference/network/ip_dynamic", NULL, 0, TRUE}, {	
	CHAR_WIZARD_NAME_NETWORK, "/apps/autoscan-network/wizard/name", "Local network", -1, FALSE}, {
	CHAR_WIZARD_ICON_NETWORK, "/apps/autoscan-network/wizard/icon", PATH_ICONE "Network/home.png", -1, FALSE}, {
	INT_WIZARD_SELECT_NETWORK, "/apps/autoscan-network/wizard/select_network", NULL, 0, FALSE}, {
	CHAR_WIZARD_RESTORE_FOLDER, "/apps/autoscan-network/wizard/restore_folder", "", -1, FALSE}, {
	CHAR_WIZARD_CONNECT_IP_ADDRESS, "/apps/autoscan-network/wizard/connect_ip_address", "", -1, FALSE}, {
	INT_WIZARD_CONNECT_PORT, "/apps/autoscan-network/wizard/connect_port", NULL, 4555, FALSE}, {
	CHAR_WIZARD_CONNECT_USER, "/apps/autoscan-network/wizard/connect_user", "User", -1, FALSE}, {
	CHAR_WIZARD_CONNECT_PASSWORD, "/apps/autoscan-network/wizard/connect_password", "AutoScan", -1, FALSE}, {
	CHAR_EXTENSION_SXC, "/apps/autoscan-network/extension/sxc", NULL, 0, FALSE}, {
	CHAR_EXTENSION_SXD, "/apps/autoscan-network/extension/sxd", NULL, 0, FALSE}, {
	CHAR_EXTENSION_SXI, "/apps/autoscan-network/extension/sxi", NULL, 0, FALSE}, {
	CHAR_EXTENSION_SXW, "/apps/autoscan-network/extension/sxw", NULL, 0, FALSE}, {
	CHAR_EXTENSION_PNG, "/apps/autoscan-network/extension/png", NULL, 0, FALSE}, {
	CHAR_EXTENSION_JPG, "/apps/autoscan-network/extension/jgp", NULL, 0, FALSE}, {
	CHAR_EXTENSION_XPM, "/apps/autoscan-network/extension/xpm", NULL, 0, FALSE}, {
	CHAR_EXTENSION_RPM, "/apps/autoscan-network/extension/rpm", NULL, 0, FALSE}, {
	CHAR_EXTENSION_DEB, "/apps/autoscan-network/extension/deb", NULL, 0, FALSE}, {
	CHAR_EXTENSION_MP3, "/apps/autoscan-network/extension/mp3", NULL, 0, FALSE}, {
	CHAR_EXTENSION_OGG, "/apps/autoscan-network/extension/ogg", NULL, 0, FALSE}, {
	CHAR_EXTENSION_WAV, "/apps/autoscan-network/extension/wav", NULL, 0, FALSE}, {
	CHAR_EXTENSION_HTML, "/apps/autoscan-network/extension/html", NULL, 0, FALSE}, {
	CHAR_EXTENSION_PDF, "/apps/autoscan-network/extension/pdf", NULL, 0, FALSE}, {
	CHAR_EXTENSION_README, "/apps/autoscan-network/extension/readme", NULL, 0, FALSE}, {
	CHAR_EXTENSION_CONF, "/apps/autoscan-network/extension/conf", NULL, 0, FALSE}, {
	CHAR_EXTENSION_C, "/apps/autoscan-network/extension/c", NULL, 0, FALSE}, {
	CHAR_EXTENSION_H, "/apps/autoscan-network/extension/h", NULL, 0, FALSE}, {
	CHAR_EXTENSION_TAR, "/apps/autoscan-network/extension/tar", NULL, 0, FALSE}, {
	CHAR_EXTENSION_BZ2, "/apps/autoscan-network/extension/bz2", NULL, 0, FALSE}, {
	CHAR_EXTENSION_GZ, "/apps/autoscan-network/extension/gz", NULL, 0, FALSE}, {
	CHAR_EXTENSION_TXT, "/apps/autoscan-network/extension/txt", NULL, 0, FALSE}, {
	CHAR_EXTENSION_AVI, "/apps/autoscan-network/extension/avi", NULL, 0, FALSE}, {
	CHAR_EXTENSION_DOC, "/apps/autoscan-network/extension/doc", NULL, 0, FALSE}, {
	CHAR_EXTENSION_OGM, "/apps/autoscan-network/extension/ogm", NULL, 0, FALSE}
};


gboolean gconf_error (GError ** err)
{
	if (*err != NULL)
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "gconf_error : [%s]", (*err)->message);
	return FALSE;
}


char     *Lecture_Gconf_Char (int i)
{
	GError   *err = NULL;
	GConfValue *Gconf_value = NULL;
	gchar    *valeur = NULL;

	if (i == Gconf_Variable[i].index) {
		Gconf_value = gconf_client_get (client_gconf, Gconf_Variable[i].path_gconf, &err);
		if (Gconf_value == NULL) {
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s] not found", Gconf_Variable[i].path_gconf);
			return Gconf_Variable[i].defaut_chaine;
		}
		valeur = (char *) gconf_value_get_string (Gconf_value);
		if (strlen(valeur)==0)
			return Gconf_Variable[i].defaut_chaine;
		return valeur;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%d]", i);
	return NULL;
}


gboolean Lecture_Gconf_Boolean (int i)
{
	GError   *err = NULL;
	GConfValue *Gconf_value = NULL;
	gboolean  valeur;

	if (i == Gconf_Variable[i].index) {
		Gconf_value = gconf_client_get (client_gconf, Gconf_Variable[i].path_gconf, &err);
		if (Gconf_value == NULL) {
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s] not found", Gconf_Variable[i].path_gconf);
			return Gconf_Variable[i].defaut_boolean;
		}
		valeur = gconf_value_get_bool (Gconf_value);
		return valeur;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%d]", i);
	return FALSE;
}


int Lecture_Gconf_Int (int i)
{
	GError   *err = NULL;
	GConfValue *Gconf_value = NULL;
	int       valeur;

	if (i == Gconf_Variable[i].index) {
		Gconf_value = gconf_client_get (client_gconf, Gconf_Variable[i].path_gconf, &err);
		if (Gconf_value == NULL) {
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s] not found", Gconf_Variable[i].path_gconf);
			return Gconf_Variable[i].defaut_entier;
		}
		valeur = gconf_value_get_int (Gconf_value);
		return valeur;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%d]", i);
	return 0;
}


void Enregistrement_Gconf_Char (int i, gchar * valeur)
{
	ERROR_if (valeur == NULL);
	if (i == Gconf_Variable[i].index) {
		gconf_client_set_string (client_gconf, Gconf_Variable[i].path_gconf, valeur, NULL);
		Gconf_Variable[i].defaut_chaine = valeur;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%d]", i);
}


void Enregistrement_Gconf_Boolean (int i, gboolean valeur)
{
	if (i == Gconf_Variable[i].index) {
		gconf_client_set_bool (client_gconf, Gconf_Variable[i].path_gconf, valeur, NULL);
		Gconf_Variable[i].defaut_boolean = valeur;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%d]", i);
}


void Enregistrement_Gconf_Entier (int i, int valeur)
{
	if (i == Gconf_Variable[i].index) {
		gconf_client_set_int (client_gconf, Gconf_Variable[i].path_gconf, valeur, NULL);
		Gconf_Variable[i].defaut_entier = valeur;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%d]", i);
}

char     *Lecture_Info_Conf_Char (char *path)
{
	GError   *err = NULL;
	gchar    *Donnee = NULL;

	ERROR_if (path == NULL);
	Donnee = gconf_client_get_string (client_gconf, path, &err);
	if (!gconf_error (&err) && (Donnee != NULL)) {
		return Donnee;
	}
	else
	
		return g_strdup("");
}


gboolean Lecture_Info_Conf_Bool (char *path)
{
	GError   *err = NULL;
	gboolean  Donnee;

	ERROR_if (path == NULL);
	Donnee = gconf_client_get_bool (client_gconf, path, &err);
	if (!gconf_error (&err)) {
		return Donnee;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s]", path);
	return FALSE;
}


int Lecture_Info_Conf_Int (char *path)
{
	GError   *err = NULL;
	int       Donnee;

	ERROR_if (path == NULL);
	Donnee = gconf_client_get_int (client_gconf, path, &err);
	if (!gconf_error (&err)) {
		return Donnee;
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s]", path);
	return -1;
}


void Enregistrement_Info_Conf_Char (gchar * path, gchar * Donnee)
{
	GError   *err = NULL;
	gboolean  retour;

	ERROR_if (path == NULL);
	ERROR_if (Donnee == NULL);
	retour = gconf_client_set_string (client_gconf, path, Donnee, &err);
	if (!retour || !gconf_error (&err)) {
	
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[I] Erreur (Enregistrement_Info_Conf_Char) : [%s]", path);
}


void Enregistrement_Info_Conf_Bool (char *path, gboolean Donnee)
{
	GError   *err = NULL;
	gboolean  retour;

	ERROR_if (path == NULL);
	retour = gconf_client_set_bool (client_gconf, path, Donnee, &err);
	if (!retour || !gconf_error (&err)) {
	
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[I] Erreur (Enregistrement_Info_Conf_Bool) : [%s]", path);
}


void Enregistrement_Info_Conf_Int (char *path, int Donnee)
{
	GError   *err = NULL;
	gboolean  retour;

	ERROR_if (path == NULL);
	retour = gconf_client_set_int (client_gconf, path, Donnee, &err);
	if (!retour || !gconf_error (&err)) {
	
	}
	else
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[I] Erreur (Enregistrement_Info_Conf_Int) : [%s]", path);
}
