/* ***********************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 "Settings.h"
#include "Gconf.h"
#include "Debug.h"
#include "Interface/Settings_Interface.h"
#include "Macro.h"

void Affiche_Champ_Connection_Internet (void)
{
	if (strcmp (Lecture_Info_Conf_Char ("/system/proxy/mode"), "none") == 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_General_Internet_Direct), TRUE);
		gtk_widget_set_sensitive (spinbutton_General_Internet_Port, FALSE);
		gtk_widget_set_sensitive (entry_General_Internet_Http_Proxy, FALSE);
		gtk_widget_set_sensitive (entry_General_Internet_Url, FALSE);
	}
	if (strcmp (Lecture_Info_Conf_Char ("/system/proxy/mode"), "manual") == 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_General_Internet_Http_Proxy), TRUE);
		gtk_widget_set_sensitive (spinbutton_General_Internet_Port, TRUE);
		gtk_widget_set_sensitive (entry_General_Internet_Http_Proxy, TRUE);
		gtk_widget_set_sensitive (entry_General_Internet_Url, FALSE);
	}
	if (strcmp (Lecture_Info_Conf_Char ("/system/proxy/mode"), "auto") == 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_General_Internet_Url), TRUE);
		gtk_widget_set_sensitive (spinbutton_General_Internet_Port, FALSE);
		gtk_widget_set_sensitive (entry_General_Internet_Http_Proxy, FALSE);
		gtk_widget_set_sensitive (entry_General_Internet_Url, TRUE);
	}
	gtk_widget_set_sensitive (entry_General_Internet_Authentication_Password, FALSE);
	gtk_widget_set_sensitive (entry_General_Internet_Authentication_User, FALSE);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton_General_Internet_Authentication), Lecture_Info_Conf_Bool ("/system/http_proxy/use_authentication"));
	if (Lecture_Info_Conf_Bool ("/system/http_proxy/use_authentication")) {
		gtk_widget_set_sensitive (entry_General_Internet_Authentication_User, TRUE);
		gtk_widget_set_sensitive (entry_General_Internet_Authentication_Password, TRUE);
	}
	else {
		gtk_widget_set_sensitive (entry_General_Internet_Authentication_User, FALSE);
		gtk_widget_set_sensitive (entry_General_Internet_Authentication_Password, FALSE);
	}
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_General_Internet_Port), (gfloat) Lecture_Info_Conf_Int ("/system/http_proxy/port"));
	gtk_entry_set_text (GTK_ENTRY (entry_General_Internet_Http_Proxy), Lecture_Info_Conf_Char ("/system/http_proxy/host"));
	gtk_entry_set_text (GTK_ENTRY (entry_General_Internet_Url), Lecture_Info_Conf_Char ("/system/proxy/autoconfig_url"));
	gtk_entry_set_text (GTK_ENTRY (entry_General_Internet_Authentication_User), Lecture_Info_Conf_Char ("/system/http_proxy/authentication_user"));
	gtk_entry_set_text (GTK_ENTRY (entry_General_Internet_Authentication_Password), Lecture_Info_Conf_Char ("/system/http_proxy/authentication_password"));
}


void Enregistre_Champ_Connection_Internet (void)
{
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radiobutton_General_Internet_Direct))) {
		Enregistrement_Info_Conf_Char ("/system/proxy/mode", "none");
		Enregistrement_Info_Conf_Bool ("/system/http_proxy/use_http_proxy", FALSE);
	}
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radiobutton_General_Internet_Http_Proxy))) {
		Enregistrement_Info_Conf_Char ("/system/proxy/mode", "manual");
		Enregistrement_Info_Conf_Bool ("/system/http_proxy/use_http_proxy", TRUE);
	}
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radiobutton_General_Internet_Url))) {
		Enregistrement_Info_Conf_Char ("/system/proxy/mode", "auto");
		Enregistrement_Info_Conf_Bool ("/system/http_proxy/use_http_proxy", TRUE);
	}
	Enregistrement_Info_Conf_Char ("/system/http_proxy/host", (gchar *) gtk_entry_get_text (GTK_ENTRY (entry_General_Internet_Http_Proxy)));
	Enregistrement_Info_Conf_Char ("/system/proxy/autoconfig_url", (gchar *) gtk_entry_get_text (GTK_ENTRY (entry_General_Internet_Url)));
	Enregistrement_Info_Conf_Char ("/system/http_proxy/authentication_user", (gchar *) gtk_entry_get_text (GTK_ENTRY (entry_General_Internet_Authentication_User)));
	Enregistrement_Info_Conf_Char ("/system/http_proxy/authentication_password", (gchar *) gtk_entry_get_text (GTK_ENTRY (entry_General_Internet_Authentication_Password)));
	Enregistrement_Info_Conf_Int ("/system/http_proxy/port", (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton_General_Internet_Port)));
	Enregistrement_Info_Conf_Bool ("/system/http_proxy/use_authentication", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_General_Internet_Authentication)));
}


void Enregistre_Parametre_General (void)
{
#ifdef         __EGGTRAYICON_ENABLE
	Enregistrement_Gconf_Boolean (BOOL_MINIMIZED_TRAY_ICON, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_General_Interface_TrayIcon)));
	Enregistrement_Gconf_Boolean (BOOL_START_MINIMIZED, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_General_Interface_Minimized)));
#endif
	
	Enregistrement_Gconf_Char (CHAR_SERVEUR_SMTP, (gchar *) gtk_entry_get_text (GTK_ENTRY (entry_General_SMTP)));
#ifdef  __SOUND_ENABLE
	Enregistrement_Info_Conf_Int ("/apps/autoscan-network/preference/global/audio_device", gtk_combo_box_get_active (GTK_COMBO_BOX (combobox_General_Audio_Device)));
#endif
}


void Afficher_Info_Entree_Texte(GtkWidget *entry,gchar *donnee){
	if (donnee != NULL)
	    gtk_entry_set_text (GTK_ENTRY (entry), donnee);
	else
		gtk_entry_set_text (GTK_ENTRY (entry), "");	
}


gchar *Lire_Info_Cle_Xml_Chaine (xmlNodePtr xmlnode,gchar *cle){
	gchar     *Info_Cle = NULL;
	
	Info_Cle = xmlGetProp (xmlnode, cle);
	if ((Info_Cle == NULL) || (strlen(Info_Cle)==0)){
		MYFREE(Info_Cle);
		return NULL;
	}
	return Info_Cle;
}


void free_gchar_if_no_null (gchar * chaine)
{
	if (chaine != NULL)
		MYFREE (chaine);
	chaine = NULL;
}

void Preparation_Interface_Settings (void)
{
	Affiche_Champ_Connection_Internet ();
}


void Initialisation_Settings (void)
{

	MALLOC ( Settings,sizeof (struct Struct_Settings));
}
