/* ***********************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 "Update_Callbacks.h"
#include "Update_Interface.h"
#include "Maemo.h"
#include "../Declaration_Constante.h"
#include "../Gconf.h"

GtkWidget *create_window_Update (void)
{
	GSList   *radiobutton_Update_group = NULL;

#ifndef         __MAEMO
	window_Update = gtk_window_new (GTK_WINDOW_TOPLEVEL);
#else
	window_Update = gtk_window_new_maemo ();
#endif

	gtk_window_set_title (GTK_WINDOW (window_Update), "Update failed");
	gtk_window_set_position (GTK_WINDOW (window_Update), GTK_WIN_POS_CENTER);
	window_Update_icon_pixbuf = gdk_pixbuf_new_from_file (PATH_ICONE "/AutoScan.png", NULL);
	if (window_Update_icon_pixbuf) {
		gtk_window_set_icon (GTK_WINDOW (window_Update), window_Update_icon_pixbuf);
		gdk_pixbuf_unref (window_Update_icon_pixbuf);
	}

	vbox_Update_Root = gtk_vbox_new (FALSE, 0);
	gtk_widget_show (vbox_Update_Root);
	gtk_container_add (GTK_CONTAINER (window_Update), vbox_Update_Root);

	vbox_Update_Sup = gtk_vbox_new (FALSE, 6);
	gtk_widget_show (vbox_Update_Sup);
	gtk_box_pack_start (GTK_BOX (vbox_Update_Root), vbox_Update_Sup, TRUE, TRUE, 0);
	gtk_container_set_border_width (GTK_CONTAINER (vbox_Update_Sup), 16);

	vbox_Update = gtk_vbox_new (TRUE, 0);
	gtk_widget_show (vbox_Update);
	gtk_box_pack_start (GTK_BOX (vbox_Update_Sup), vbox_Update, TRUE, TRUE, 0);

	radiobutton_Update = gtk_radio_button_new_with_mnemonic (NULL, "Direct connection to the Internet");
	gtk_widget_show (radiobutton_Update);
	gtk_box_pack_start (GTK_BOX (vbox_Update), radiobutton_Update, FALSE, FALSE, 1);
	gtk_radio_button_set_group (GTK_RADIO_BUTTON (radiobutton_Update), radiobutton_Update_group);
	radiobutton_Update_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radiobutton_Update));
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Update), TRUE);

	radiobutton_Update_Proxy = gtk_radio_button_new_with_mnemonic (NULL, "Manual proxy configuration");
	gtk_widget_show (radiobutton_Update_Proxy);
	gtk_box_pack_start (GTK_BOX (vbox_Update), radiobutton_Update_Proxy, FALSE, FALSE, 0);
	gtk_radio_button_set_group (GTK_RADIO_BUTTON (radiobutton_Update_Proxy), radiobutton_Update_group);
	radiobutton_Update_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radiobutton_Update_Proxy));

	hbox_Update_Http_Proxy = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox_Update_Http_Proxy);
	gtk_box_pack_start (GTK_BOX (vbox_Update), hbox_Update_Http_Proxy, TRUE, TRUE, 0);

	label_Update_Proxy = gtk_label_new ("HTTP Proxy :");
	gtk_widget_show (label_Update_Proxy);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Http_Proxy), label_Update_Proxy, FALSE, FALSE, 10);

	entry_Update_Proxy = gtk_entry_new ();
	gtk_widget_show (entry_Update_Proxy);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Http_Proxy), entry_Update_Proxy, FALSE, TRUE, 0);

	label_Update_Port = gtk_label_new ("Port :");
	gtk_widget_show (label_Update_Port);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Http_Proxy), label_Update_Port, FALSE, FALSE, 10);

	spinbutton_Update_Port_adj = gtk_adjustment_new (8080, 1, 65535, 1, 10, 10);
	spinbutton_Update_Port = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_Update_Port_adj), 1, 0);
	gtk_widget_show (spinbutton_Update_Port);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Http_Proxy), spinbutton_Update_Port, FALSE, TRUE, 0);

	radiobutton_Update_Auto = gtk_radio_button_new_with_mnemonic (NULL, "Automatic proxy configuration");
	gtk_widget_show (radiobutton_Update_Auto);
	gtk_box_pack_start (GTK_BOX (vbox_Update), radiobutton_Update_Auto, FALSE, FALSE, 0);
	gtk_radio_button_set_group (GTK_RADIO_BUTTON (radiobutton_Update_Auto), radiobutton_Update_group);
	radiobutton_Update_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radiobutton_Update_Auto));

	hbox_Update_URL = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox_Update_URL);
	gtk_box_pack_start (GTK_BOX (vbox_Update), hbox_Update_URL, TRUE, TRUE, 0);

	label_Update_URL = gtk_label_new ("URL :");
	gtk_widget_show (label_Update_URL);
	gtk_box_pack_start (GTK_BOX (hbox_Update_URL), label_Update_URL, FALSE, FALSE, 14);

	entry_Update_URL = gtk_entry_new ();
	gtk_widget_show (entry_Update_URL);
	gtk_box_pack_start (GTK_BOX (hbox_Update_URL), entry_Update_URL, FALSE, TRUE, 0);

	checkbutton_Update_Auth = gtk_check_button_new_with_mnemonic ("Authentication");
	gtk_widget_show (checkbutton_Update_Auth);
	gtk_box_pack_start (GTK_BOX (vbox_Update), checkbutton_Update_Auth, FALSE, FALSE, 0);

	hbox_Update_User = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox_Update_User);
	gtk_box_pack_start (GTK_BOX (vbox_Update), hbox_Update_User, TRUE, TRUE, 0);

	label_Update_User = gtk_label_new ("User :");
	gtk_widget_show (label_Update_User);
	gtk_box_pack_start (GTK_BOX (hbox_Update_User), label_Update_User, FALSE, FALSE, 15);

	entry_Update_User = gtk_entry_new ();
	gtk_widget_show (entry_Update_User);
	gtk_box_pack_start (GTK_BOX (hbox_Update_User), entry_Update_User, FALSE, TRUE, 0);

	hbox_Update_Password = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox_Update_Password);
	gtk_box_pack_start (GTK_BOX (vbox_Update), hbox_Update_Password, FALSE, TRUE, 0);

	label_Update_Password = gtk_label_new ("Password : ");
	gtk_widget_show (label_Update_Password);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Password), label_Update_Password, FALSE, FALSE, 0);

	entry_Update_Password = gtk_entry_new ();
	gtk_widget_show (entry_Update_Password);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Password), entry_Update_Password, FALSE, TRUE, 0);
	gtk_entry_set_visibility (GTK_ENTRY (entry_Update_Password), FALSE);

	hbox_Update_Bouton = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox_Update_Bouton);
	gtk_box_pack_start (GTK_BOX (vbox_Update_Root), hbox_Update_Bouton, FALSE, FALSE, 4);

	fixed_Update_Bouton = gtk_fixed_new ();
	gtk_widget_show (fixed_Update_Bouton);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Bouton), fixed_Update_Bouton, TRUE, TRUE, 0);

	button_Update_Save = gtk_button_new_from_stock ("gtk-save");
	gtk_widget_show (button_Update_Save);
	gtk_box_pack_start (GTK_BOX (hbox_Update_Bouton), button_Update_Save, FALSE, FALSE, 37);

	g_signal_connect ((gpointer) radiobutton_Update, "clicked", G_CALLBACK (on_radiobutton_Update_clicked), NULL);
	g_signal_connect ((gpointer) radiobutton_Update_Proxy, "clicked", G_CALLBACK (on_radiobutton_Update_Proxy_clicked), NULL);
	g_signal_connect ((gpointer) radiobutton_Update_Auto, "clicked", G_CALLBACK (on_radiobutton_Update_Auto_clicked), NULL);
	g_signal_connect ((gpointer) checkbutton_Update_Auth, "clicked", G_CALLBACK (on_checkbutton_Update_Auth_clicked), NULL);
	g_signal_connect ((gpointer) button_Update_Save, "clicked", G_CALLBACK (on_button_Update_Save_clicked), NULL);


	if (strcmp (Lecture_Info_Conf_Char ("/system/proxy/mode"), "none") == 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Update), TRUE);
		gtk_widget_set_sensitive (spinbutton_Update_Port, FALSE);
		gtk_widget_set_sensitive (entry_Update_Proxy, FALSE);
		gtk_widget_set_sensitive (entry_Update_URL, FALSE);
	}

	if (strcmp (Lecture_Info_Conf_Char ("/system/proxy/mode"), "manual") == 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Update_Proxy), TRUE);
		gtk_widget_set_sensitive (spinbutton_Update_Port, TRUE);
		gtk_widget_set_sensitive (entry_Update_Proxy, TRUE);
		gtk_widget_set_sensitive (entry_Update_URL, FALSE);
	}
	if (strcmp (Lecture_Info_Conf_Char ("/system/proxy/mode"), "auto") == 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Update_Auto), TRUE);
		gtk_widget_set_sensitive (spinbutton_Update_Port, FALSE);
		gtk_widget_set_sensitive (entry_Update_Proxy, FALSE);
		gtk_widget_set_sensitive (entry_Update_URL, TRUE);
	}

	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton_Update_Auth), Lecture_Info_Conf_Bool ("/system/http_proxy/use_authentication"));
	if (Lecture_Info_Conf_Bool ("/system/http_proxy/use_authentication")) {
		gtk_widget_set_sensitive (entry_Update_User, TRUE);
		gtk_widget_set_sensitive (entry_Update_Password, TRUE);
	}
	else {
		gtk_widget_set_sensitive (entry_Update_User, FALSE);
		gtk_widget_set_sensitive (entry_Update_Password, FALSE);
	}
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Update_Port), (gfloat) Lecture_Info_Conf_Int ("/system/http_proxy/port"));
	gtk_entry_set_text (GTK_ENTRY (entry_Update_Proxy), Lecture_Info_Conf_Char ("/system/http_proxy/host"));
	gtk_entry_set_text (GTK_ENTRY (entry_Update_URL), Lecture_Info_Conf_Char ("/system/proxy/autoconfig_url"));
	gtk_entry_set_text (GTK_ENTRY (entry_Update_User), Lecture_Info_Conf_Char ("/system/http_proxy/authentication_user"));
	gtk_entry_set_text (GTK_ENTRY (entry_Update_Password), Lecture_Info_Conf_Char ("/system/http_proxy/authentication_password"));
	return window_Update;
}
