/* ***********************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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "Wizard_Callbacks_Add_Network.h"
#include "Wizard_Interface_Add_Network.h"
#include "Wizard_Interface.h"
#include "Wizard_Callbacks.h"
#include "Open_Save_Interface.h"
#include "Open_Save_Callbacks.h"
#include "../Declaration_Constante.h"
#include "../Com/Initialisation_Com.h"
#include "../Com/Communication.h"
#include "../Com/Commun.h"
#include "../Gconf.h"
#include "../Debug.h"
#include "../Restoration_Agent.h"
#include "Root_Interface.h"
#include "Macro.h"


void Initialisation_Variable_Wizard_Add_Network ()
{
	Wizard_Name_Network = g_strdup (Lecture_Gconf_Char (CHAR_WIZARD_NAME_NETWORK));
	Wizard_Chemin_Icon_Nework = g_strdup (Lecture_Gconf_Char (CHAR_WIZARD_ICON_NETWORK));
	Wizard_Snmp_Community_Name = g_strdup ("public");
	Wizard_Ip_Dynamic = Lecture_Gconf_Boolean (BOOL_IP_DYNAMIC);
	Wizard_Restauration_Fichier_Xml = FALSE;
	Wizard_Chemin_Fichier_Xml = g_strdup (Lecture_Gconf_Char (CHAR_WIZARD_RESTORE_FOLDER));
	Wizard_Localhost = TRUE;
	Wizard_Ip_Address = g_strdup (Lecture_Gconf_Char (CHAR_WIZARD_CONNECT_IP_ADDRESS));
	Wizard_Port = Lecture_Gconf_Int (INT_WIZARD_CONNECT_PORT);
	Wizard_Interface_Network = g_strdup ("");
	Wizard_Interface_IP_Network = g_strdup ("");
}

void Wizard_Bouton_Annuler_Add_Network_Standard (gpointer * pointer)
{
	DEBUG_ACTION_USER ();
	gtk_widget_hide (window_wizard);
	Pid_Agent_Initialisation = -1;
	Connecter_Agent = FALSE;
}

void Wizard_Bouton_Add_Network_Page1 (gpointer * pointer)
{
	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Network_Page1 ();
}

void Wizard_Bouton_Add_Network_Page2 (gpointer * pointer)
{
	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Network_Page2 ();
}

void Wizard_Bouton_Post_Add_Network_Page3 (gpointer * pointer)
{
	DEBUG_ACTION_USER ();


	MYFREE (Wizard_Name_Network);
	Wizard_Name_Network = g_strdup ((gchar *) gtk_entry_get_text (GTK_ENTRY (entry_Network_New_Name)));
	Enregistrement_Gconf_Char (CHAR_WIZARD_NAME_NETWORK, Wizard_Name_Network);

	if (Recuperation_Chaine_Attacher_Combo (combobox_Network_New_Name)==NULL) {
		Post_Boolean = FALSE;
		return;
	}
	MYFREE (Wizard_Chemin_Icon_Nework);
	Wizard_Chemin_Icon_Nework = g_strdup (Recuperation_Chaine_Attacher_Combo (combobox_Network_New_Name));
	Enregistrement_Gconf_Char (CHAR_WIZARD_ICON_NETWORK, Wizard_Chemin_Icon_Nework);

	Wizard_Ip_Dynamic = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton_Ip_Dynamic));
	Enregistrement_Gconf_Boolean (BOOL_IP_DYNAMIC, Wizard_Ip_Dynamic);

	MYFREE (Wizard_Chemin_Fichier_Xml);
	Wizard_Chemin_Fichier_Xml = g_strdup ((gchar *) gtk_entry_get_text (GTK_ENTRY (entry_Network_Restore)));
	Enregistrement_Gconf_Char (CHAR_WIZARD_RESTORE_FOLDER, Wizard_Chemin_Fichier_Xml);

	Wizard_Restauration_Fichier_Xml = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radiobutton_Network_Restore));
	if (!Wizard_Restauration_Fichier_Xml) {
	
	
		if (strlen (Wizard_Name_Network) == 0) {
			Post_Boolean = FALSE;
			return;
		}
	
		if (Recherche_Si_Reseau_Existe_Deja (Wizard_Name_Network)) {
			debug_printf (INTERFACE, D_INFO_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s] : already exist", Wizard_Name_Network);
			Post_Boolean = FALSE;
			return;
		}
	}
	else {
	
	
		if (strlen (Wizard_Chemin_Fichier_Xml) == 0) {
			Post_Boolean = FALSE;
			return;
		}
	
		if (!Lecture_Fichier_Restoration_Agent (Wizard_Chemin_Fichier_Xml)) {
			Post_Boolean = FALSE;
			return;
		}
	
		if (Recherche_Si_Reseau_Existe_Deja (Nom_Reseau_Fichier_Xml)) {
			debug_printf (INTERFACE, D_INFO_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "[%s] : already exist", Nom_Reseau_Fichier_Xml);
			Post_Boolean = FALSE;
			return;
		}
	}
	Enregistrement_Donnee_Xml_Subnet ();
	gtk_expander_set_expanded (GTK_EXPANDER (expander_Network_Private_Subnet), FALSE);
	Post_Boolean = TRUE;
	Wizard_Bouton_Avancer_Post = NULL;
}

void Wizard_Bouton_Add_Network_Page3 (gpointer * pointer)
{
	Wizard_Bouton_Avancer = NULL;
	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Network_Page3 ();
	if (Wizard_Restauration_Fichier_Xml) {
	
		if (strcmp (Dns_Fichier_Xml, "127.0.0.1") == 0) {
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Localisation_Localhost), TRUE);
			gtk_widget_set_sensitive (entry_Localisation_Connect_Ip_Address, FALSE);
			gtk_widget_set_sensitive (spinbutton_Localisation_Connect_Port, FALSE);
		}
		else {
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Localisation_Connect), TRUE);
			gtk_widget_set_sensitive (entry_Localisation_Connect_Ip_Address, TRUE);
			gtk_widget_set_sensitive (spinbutton_Localisation_Connect_Port, TRUE);
			gtk_entry_set_text (GTK_ENTRY (entry_Localisation_Connect_Ip_Address), Dns_Fichier_Xml);
			gtk_spin_button_set_value (spinbutton_Localisation_Connect_Port, Port_Fichier_Xml);
		}
	}
	else {
		if (Wizard_Localhost)
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Localisation_Localhost), TRUE);
		else
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiobutton_Localisation_Connect), TRUE);
		gtk_entry_set_text (GTK_ENTRY (entry_Localisation_Connect_Ip_Address), Wizard_Ip_Address);
		gtk_spin_button_set_value (spinbutton_Localisation_Connect_Port, Wizard_Port);
	}
}

void Wizard_Bouton_Post_Add_Network_Page4 (gpointer * pointer)
{
	Wizard_Bouton_Avancer_Post = NULL;
	DEBUG_ACTION_USER ();


	if (Wizard_Ip_Address!=NULL)
		MYFREE (Wizard_Ip_Address);
	Wizard_Ip_Address = g_strdup ((gchar *) gtk_entry_get_text (GTK_ENTRY (entry_Localisation_Connect_Ip_Address)));
	Enregistrement_Gconf_Char (CHAR_WIZARD_CONNECT_IP_ADDRESS, Wizard_Ip_Address);

	Wizard_Port = (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton_Localisation_Connect_Port));
	Enregistrement_Gconf_Entier (INT_WIZARD_CONNECT_PORT, Wizard_Port);

	Wizard_Localhost = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radiobutton_Localisation_Localhost));

	if ((!Wizard_Localhost) && (strlen (Wizard_Ip_Address) == 0)) {
		Post_Boolean = FALSE;
		 Wizard_Bouton_Avancer_Post = Wizard_Bouton_Post_Add_Network_Page4;
		return;
	}

	Agent_Initialiser = FALSE;
	AutoRestore = FALSE;

	if (!Wizard_Localhost)
		Socket_Agent_Init = Initialisation_TCP_Agent (Wizard_Ip_Address, Wizard_Port);
	else
		Socket_Agent_Init = Initialisation_TCP_Agent ("127.0.0.1", 4555);

	if (Socket_Agent_Init == -1) {
		Post_Boolean = FALSE;
		 Wizard_Bouton_Avancer_Post = Wizard_Bouton_Post_Add_Network_Page4;
		return;
	}
	Post_Boolean = TRUE;
}


void Wizard_Bouton_Add_Network_Page4 (gpointer * pointer)
{
	Wizard_Bouton_Avancer = NULL;
	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Network_Page4 ();
}

void Wizard_Bouton_Post_Add_Network_Page5 (gpointer * pointer)
{
	Wizard_Bouton_Avancer_Post = NULL;
	DEBUG_ACTION_USER ();
	MYFREE (Wizard_Interface_Network);
	Wizard_Interface_Network = g_strdup (Recuperation_Chaine_Attacher_Combo (combobox_Select_Interface));
	MYFREE (Wizard_Interface_IP_Network);
	Wizard_Interface_IP_Network = g_strdup (Recuperation_Chaine2_Attacher_Combo (combobox_Select_Interface));
}

void Wizard_Bouton_Add_Network_Page5 (gpointer * pointer)
{
#define TEXTE "Confirm the following information: \n\nName Network : %s\nInterface : %s\n"
	gchar    *text, *Interface_IP, *Index_Fin_Nom_Reseau;

	Wizard_Bouton_Avancer = NULL;
	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Network_Page5 ();
	if (Wizard_Restauration_Fichier_Xml) {
		if ((Index_Fin_Nom_Reseau = index (Nom_Reseau_Restauration, '-')) != NULL)
			Nom_Reseau_Restauration[Index_Fin_Nom_Reseau - Nom_Reseau_Restauration] = '\0';
	
		while (Nom_Reseau_Restauration[strlen (Nom_Reseau_Restauration) - 1] == 0x20) {
			Nom_Reseau_Restauration[strlen (Nom_Reseau_Restauration) - 1] = 0;
		}
		text = g_strdup_printf (TEXTE, Nom_Reseau_Restauration, Wizard_Interface_Network);
	}
	else
		text = g_strdup_printf (TEXTE, Wizard_Name_Network, Wizard_Interface_Network);

	gtk_label_set_text (label_wizard_Final_Add_Network, text);
	MYFREE (text);
}

void Wizard_Bouton_Add_Network_Fin (gpointer * pointer)
{
	struct Struct_Liste_Reseau *Reseau = NULL;
	struct struct_Active_Alerte_Intrus Message_Active;

	Wizard_Bouton_Avancer = NULL;
	DEBUG_ACTION_USER ();
	if (Wizard_Restauration_Fichier_Xml) {
		Reseau = Creation_Nouveau_Reseau (g_strdup (Nom_Reseau_Restauration), g_strdup (Wizard_Interface_Network), g_strdup (Wizard_Interface_IP_Network), g_strdup (Communaute_Snmp_Restauration), g_strdup (Icon_Restauration), Socket_Agent_Init, g_strdup (Dns_Fichier_Xml), Port_Fichier_Xml);
		if (Reseau != NULL) {
			Copie_Subnet (Reseau, Subnet_Restauration);
			Reseau->Alert_Intrusion = Intrusion_Alert_Restauration;
		}
	}
	else {
		if (!Wizard_Localhost)
			Reseau = Creation_Nouveau_Reseau (g_strdup (Wizard_Name_Network), g_strdup (Wizard_Interface_Network), g_strdup (Wizard_Interface_IP_Network), g_strdup (Wizard_Snmp_Community_Name), g_strdup (Wizard_Chemin_Icon_Nework), Socket_Agent_Init, g_strdup (Wizard_Ip_Address), Wizard_Port);
		else
			Reseau = Creation_Nouveau_Reseau (g_strdup (Wizard_Name_Network), g_strdup (Wizard_Interface_Network), g_strdup (Wizard_Interface_IP_Network), g_strdup (Wizard_Snmp_Community_Name), g_strdup (Wizard_Chemin_Icon_Nework), Socket_Agent_Init, g_strdup ("127.0.0.1"), 4555);
		if (Reseau != NULL)
			Copie_Subnet (Reseau, GList_Subnet);
	}
	gtk_widget_hide (window_wizard);

	if ((Reseau != NULL) && (Wizard_Restauration_Fichier_Xml))
		Restoration_Interface (Wizard_Chemin_Fichier_Xml, Reseau->Id_Reseau);
	Pid_Agent_Initialisation = -1;
	if (Reseau != NULL) {
		gtk_widget_set_sensitive (toolbutton_View, TRUE);
		gtk_widget_set_sensitive (toolbutton_Save, TRUE);
		gtk_widget_set_sensitive (toolbutton_Search, TRUE);
		gtk_widget_set_sensitive (toggletoolbutton_Alert_Intrusion, TRUE);
		if ((Reseau_Selectionner == Reseau) && (Intrusion_Alert_Restauration)) {
		
			Modification_Toggle_En_Cours = TRUE;
			gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (toggletoolbutton_Alert_Intrusion), Reseau_Selectionner->Alert_Intrusion);
			Modification_Toggle_En_Cours = FALSE;
			bzero (&Message_Active, sizeof (struct struct_Active_Alerte_Intrus));
			Message_Active.ID_Reseau = Reseau->Id_Reseau;
			Communication_Envoie_Donnee (Reseau->Pid_Agent, ACTIVE_ALERTE_INTRUS, (gpointer *) & Message_Active, sizeof (Message_Active), Reseau);
		}
	}
	Connecter_Agent = FALSE;
}

void Modifie_Class_A ()
{
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_A, TRUE);
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_B, FALSE);
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_C, FALSE);
}

void Modifie_Class_B ()
{
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_A, TRUE);
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_B, TRUE);
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_C, FALSE);
}

void Modifie_Class_C ()
{
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_A, TRUE);
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_B, TRUE);
	gtk_widget_set_sensitive (spinbutton_Network_Private_Subnet_C, TRUE);
}

void Subnet_changed (GtkComboBox * combobox, gpointer user_data)
{
	if (!Liste_Subnet_Modification_Interface) {
		gtk_widget_set_sensitive (button_Network_Private_Subnet_OK, TRUE);
		gtk_widget_set_sensitive (button_Network_Private_Subnet_Cancel, TRUE);
		gtk_widget_set_sensitive (button_Network_Private_Subnet_Delete, FALSE);
		gtk_widget_set_sensitive (button_Network_Private_Subnet_Add, FALSE);
		Subnet_Modification = Subnet_Selectionner;
	}
}

void on_button_Network_Private_Subnet_Add_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Network_Private_Subnet), 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_A), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_B), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_C), (gfloat) 0);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_OK, TRUE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Cancel, TRUE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Delete, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Add, FALSE);
	gtk_widget_set_sensitive (combobox_Network_Private_Subnet, TRUE);
	Subnet_Modification = NULL;
	Subnet_Selectionner = NULL;
}


void on_button_Network_Private_Subnet_Delete_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	Supprime_Subnet_liste (Subnet_Selectionner);
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Network_Private_Subnet), -1);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_A), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_B), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_C), (gfloat) 0);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_OK, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Cancel, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Delete, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Add, TRUE);
	Subnet_Sensible (FALSE);
	Subnet_Modification = NULL;
	Subnet_Selectionner = NULL;
}

void on_button_Network_Private_Subnet_OK_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	Modifie_Ajoute_Nouveau_Subnet ();
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Network_Private_Subnet), -1);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_A), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_B), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_C), (gfloat) 0);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_OK, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Cancel, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Delete, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Add, TRUE);
	Subnet_Sensible (FALSE);
	Subnet_Modification = NULL;
	Subnet_Selectionner = NULL;
}

void on_button_Network_Private_Subnet_Cancel_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Network_Private_Subnet), -1);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_A), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_B), (gfloat) 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton_Network_Private_Subnet_C), (gfloat) 0);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_OK, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Cancel, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Delete, FALSE);
	gtk_widget_set_sensitive (button_Network_Private_Subnet_Add, TRUE);
	Subnet_Sensible (FALSE);
	Subnet_Modification = NULL;
	Subnet_Selectionner = NULL;
}

void Wizard_Add_Network_New_Network_sensitive (gboolean bool)
{
	gtk_widget_set_sensitive (entry_Network_Restore, !bool);
	gtk_widget_set_sensitive (button_Network_Restore_Ouvrir, !bool);
	gtk_widget_set_sensitive (entry_Network_New_Name, bool);
	gtk_widget_set_sensitive (combobox_Network_New_Name, bool);
	gtk_widget_set_sensitive (expander_Network_Private_Subnet, bool);
	gtk_widget_set_sensitive (entry_Network_SNMP, bool);
	gtk_expander_set_expanded (GTK_EXPANDER (expander_Network_Private_Subnet), FALSE);
}

void on_radiobutton_Network_New_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	Wizard_Add_Network_New_Network_sensitive (TRUE);
}

void on_radiobutton_Select_Network_Restore_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	Wizard_Add_Network_New_Network_sensitive (FALSE);
}

void on_radiobutton__Select_Network_Local_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	gtk_widget_set_sensitive (entry_Localisation_Connect_Ip_Address, FALSE);
	gtk_widget_set_sensitive (spinbutton_Localisation_Connect_Port, FALSE);
}

void on_radiobutton_Select_Network_Connect_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER ();
	gtk_widget_set_sensitive (entry_Localisation_Connect_Ip_Address, TRUE);
	gtk_widget_set_sensitive (spinbutton_Localisation_Connect_Port, TRUE);

}

void on_button_Network_Open_File_Xml_Restore (GtkButton * button, gpointer user_data)
{
	create_file_Open ();
	gtk_widget_show (file_Open);
	entry_Open_File = entry_Network_Restore;
	if (strlen (gtk_entry_get_text (GTK_ENTRY (entry_Network_Restore))) != 0) {
		gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (file_Open), gtk_entry_get_text (GTK_ENTRY (entry_Network_Restore)));
	}
}
