/* ***********************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 "Modules.h"
#include "Objet_Liste.h"
#include "Objet_Icone.h"
#include "Reseau.h"
#include "Objet.h"
#include "Interface/Nessus_Interface.h"
#include "Interface/WakeUp_Interface.h"
#include "Interface/Samba_Connect_Interface.h"
#include "Interface/NetSend_Interface.h"
#include "Interface/Snmp_Interface.h"
#include "Interface/Nessus_Message_Interface.h"
#include "Interface/Script_Interface.h"
#include "Interface/Root_Interface.h"
#include "Interface/Console.h"
#include "Interface/Vnc_Interface.h"
#include "Vnc.h"
#include "Equipement_Port.h"
#include "Settings_Port.h"
#include "Module_Samba.h"
#include "Debug.h"
#include "Animation.h"
#include "Macro.h"


void Initialisation_Aucun_Module (void)
{
	GtkWidget *Liste_Vide = NULL;

	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Initialisation_Aucun_Module");
	Destruction_Modules ();
	scrolledwindow_Vide = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow_Vide);
	gtk_container_add (GTK_CONTAINER (viewport_Modules), scrolledwindow_Vide);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow_Vide), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	Liste_Vide = Create_Liste (FALSE, FALSE, FALSE,FALSE, FALSE);
	gtk_widget_show (Liste_Vide);
	gtk_container_add (GTK_CONTAINER (scrolledwindow_Vide), Liste_Vide);
	Modules_Actuel = AUCUN_MODULES_SELECTIONNER;
}



void Initialisation_WakeUp (void)
{
	Destruction_Modules ();
	create_win_WakeUp();
	Modules_Actuel = MODULES_WAKEUP;
}

void Initialisation_Liste_ports (void)
{
	struct Struct_Port *Port = NULL;
	struct Struct_Modele_Objet *Objet = NULL;
	gchar    *Nom_Port = NULL,*Info_Port = NULL;
	gint      i, Nb_Objet_Liste;

	if (Reseau_Selectionner == NULL)
		return;
	Destruction_Modules ();
	Modules_Actuel = MODULES_LISTE_PORTS;

	treeview_Modules = Create_Icone (FALSE, TRUE, FALSE);

	Nb_Objet_Liste = g_list_length (Equipement_Selectionner->GList_Port);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Port = g_list_nth_data (Equipement_Selectionner->GList_Port, i);
		if ((Nom_Port = Nom_Du_Port (Port->Num_Port)) != NULL) {
			Info_Port = g_strdup_printf ("%s\n -%d- ",Nom_Port,Port->Num_Port);
			Objet = Ajouter_Nouvelle_Objet (PORTS, Info_Port, (gpointer *) Port, NULL, PERE_RACINE, NULL, PERE_RACINE, PERE_RACINE, PERE_RACINE, PERE_RACINE, ID_Reseau_En_Cours, LISTE_MODULE);
			MYFREE(Info_Port);
			if (Port->Etat_Port == FERMER) {
				Affiche_Icone_Defaut_Griser_Objet_liste (Objet, Reseau_Selectionner);
				Change_Couleur_Gris_Texte_liste (Objet, Reseau_Selectionner);
			}
			Objet->Notification_En_Cours = Port->Notification_En_Cours;
			if (Port->Alert_notification)
				Active_Test_Etat_Objet (Objet, Reseau_Selectionner);
		}
	
		
	}
}


void Initialisation_Partage_Fichier_Smb (void)
{
#ifdef         __SAMBA_ENABLE
	Destruction_Modules ();
	Modules_Actuel = MODULES_PARTAGE_FICHIER_SMB;
	create_win_Samba_Connect ();
	if (Equipement_Selectionner->User_Smb != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Samba_Connect_User), Equipement_Selectionner->User_Smb);
	if (Equipement_Selectionner->Password_Smb != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Samba_Connect_Password), Equipement_Selectionner->Password_Smb);
	gtk_entry_set_text (GTK_ENTRY (entry_Samba_Connect_Domaine), Equipement_Selectionner->Smb.DomainName);
#endif
}


void Initialisation_Partage_Fichier_Smb_Browser (void)
{
#ifdef         __SAMBA_ENABLE
	Destruction_Modules ();
	Modules_Actuel = MODULES_PARTAGE_FICHIER_SMB_BROWSER;
	scrolledwindow_Liste = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow_Liste);
	gtk_container_add (GTK_CONTAINER (viewport_Modules), scrolledwindow_Liste);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow_Liste), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	treeview_Modules = Create_Liste (TRUE, TRUE, FALSE, TRUE, TRUE);
	gtk_widget_show (treeview_Modules);
	gtk_container_add (GTK_CONTAINER (scrolledwindow_Liste), treeview_Modules);
#endif
}


void Initialisation_Nessus (void)
{
#ifdef         __NESSUS_ENABLE
	Destruction_Modules ();
	create_win_Nessus_Connect ();
	if (Reseau_Selectionner->User_Nessus != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Nessus_Connect_User), Reseau_Selectionner->User_Nessus);
	if (Reseau_Selectionner->Password_Nessus != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Nessus_Connect_Password), Reseau_Selectionner->Password_Nessus);
	if (Reseau_Selectionner->IP_Nessus != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Nessus_Connect_Domaine), Reseau_Selectionner->IP_Nessus);
	Modules_Actuel = MODULES_NESSUS;
#endif
}

void Initialisation_Vnc (void)
{
#ifdef   __VCN_ENABLE
	Destruction_Modules ();
	create_win_Vnc_Connect ();
/*	if (Reseau_Selectionner->User_Nessus != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Nessus_Connect_User), Reseau_Selectionner->User_Nessus);
	if (Reseau_Selectionner->Password_Nessus != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Nessus_Connect_Password), Reseau_Selectionner->Password_Nessus);
	if (Reseau_Selectionner->IP_Nessus != NULL)
		gtk_entry_set_text (GTK_ENTRY (entry_Nessus_Connect_Domaine), Reseau_Selectionner->IP_Nessus);*/
	Modules_Actuel = MODULES_VNC;
#endif
}


void Initialisation_Vnc_Connect (void)
{
#ifdef   __VCN_ENABLE
	Destruction_Modules ();
	Vnc_Console ();
	Modules_Actuel = MODULES_VNC_CONNECT;
#endif
}


void Initialisation_Telnet (void)
{
#ifdef         __CONSOLE_ENABLE
	Destruction_Modules ();
	Widget_Telnet = Creation_Console ("telnet");
	Modules_Actuel = MODULES_TELNET;
#endif
}


void Initialisation_SSH (void)
{
#ifdef         __CONSOLE_ENABLE
	Destruction_Modules ();
	Widget_Telnet = Creation_Console ("ssh");
	Modules_Actuel = MODULES_SSH;
#endif
}


void Initialisation_NetSend (void)
{
#ifdef         __SAMBA_ENABLE
	Destruction_Modules ();
	create_win_NetSend ();
	Modules_Actuel = MODULES_NET_SEND;
#endif
}

void Initialisation_Nessus_Message (void)
{
#ifdef         __NESSUS_ENABLE
	if (Modules_Actuel != MODULES_NESSUS_MESSAGE) {
		gtk_widget_set_sensitive (combobox_liste_Modules, FALSE);
		gtk_widget_set_sensitive (button_Refresh, FALSE);
		Destruction_Modules ();
		create_win_Nessus ();
		Modules_Actuel = MODULES_NESSUS_MESSAGE;
	}
#endif
}


void Initialisation_Script (void)
{
#ifdef         __SCRIPT_ENABLE
	Destruction_Modules ();
	create_win_Script ();
	Modules_Actuel = MODULES_SCRIPTS;
#endif
}


void Initialisation_Snmp (void)
{
	if (Modules_Actuel != MODULES_SNMP) {
		Destruction_Modules ();
		create_win_Snmp ();
		gtk_widget_set_sensitive (combobox_liste_Modules, FALSE);
		Modules_Actuel = MODULES_SNMP;
	}
}

void Destruction_Modules (void)
{
	switch (Modules_Actuel) {
	    case AUCUN_MODULES_SELECTIONNER:
			if (scrolledwindow_Vide==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Vide==NULL");
		    gtk_widget_destroy (scrolledwindow_Vide);
		    break;
	    case MODULES_LISTE_PORTS:
			if (scrolledwindow_Icone_Liste==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Icone_Liste==NULL");
		    Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_MODULE);
		    gtk_widget_destroy (scrolledwindow_Icone_Liste);
		    break;
	    case MODULES_PARTAGE_FICHIER_SMB:
		#ifdef         __SAMBA_ENABLE
			if (scrolledwindow_Samba_Connect==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Samba_Connect==NULL");
		    gtk_widget_destroy (scrolledwindow_Samba_Connect);
		#endif
		    break;
	    case MODULES_PARTAGE_FICHIER_SMB_BROWSER:
		#ifdef         __SAMBA_ENABLE
			if (scrolledwindow_Liste==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Liste==NULL");
		    Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_MODULE);
		    gtk_widget_destroy (scrolledwindow_Liste);
		#endif
		    break;
	    case MODULES_NESSUS:
		#ifdef         __NESSUS_ENABLE
			if (scrolledwindow_Nessus_Connect==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Nessus_Connect==NULL");
		    gtk_widget_destroy (scrolledwindow_Nessus_Connect);
		    break;
		#endif
		case MODULES_VNC:
		#ifdef  __VCN_ENABLE
			if (scrolledwindow_Vnc_Connect==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Vnc_Connect==NULL");
		    gtk_widget_destroy (scrolledwindow_Vnc_Connect);
		    break;
		#endif		
		case MODULES_VNC_CONNECT:
		#ifdef  __VCN_ENABLE
			if (scrolledwindow_Vnc==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Vnc==NULL");
		    gtk_widget_destroy (scrolledwindow_Vnc);
		    break;
		#endif			
	    case MODULES_TELNET:
		#ifdef         __CONSOLE_ENABLE
			if (Widget_Telnet==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Widget_Telnet==NULL");
		    gtk_widget_destroy (Widget_Telnet);
		#endif
		    break;
	    case MODULES_SSH:
		#ifdef         __CONSOLE_ENABLE
			if (Widget_Telnet==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Widget_Telnet==NULL");
		    gtk_widget_destroy (Widget_Telnet);
		#endif
		    break;
	    case MODULES_SCRIPTS:
		#ifdef         __SCRIPT_ENABLE
			if (scrolledwindow_Script==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Script==NULL");
		    gtk_widget_destroy (scrolledwindow_Script);
		#endif
		    break;
	    case MODULES_NET_SEND:
		#ifdef         __SAMBA_ENABLE
			if (scrolledwindow_NetSend==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_NetSend==NULL");
		    gtk_widget_destroy (scrolledwindow_NetSend);
		#endif
		    break;
	    case MODULES_NESSUS_MESSAGE:
		#ifdef         __NESSUS_ENABLE
			if (scrolledwindow_Nessus==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Nessus==NULL");
		    gtk_widget_destroy (scrolledwindow_Nessus);
		    gtk_widget_set_sensitive (combobox_liste_Modules, TRUE);
		    gtk_widget_set_sensitive (button_Refresh, TRUE);
		#endif
		    break;
	    case MODULES_SNMP:
		    gtk_widget_set_sensitive (combobox_liste_Modules, TRUE);
			if (scrolledwindow_Snmp==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_Snmp==NULL");
		    gtk_widget_destroy (scrolledwindow_Snmp);
		    break;
		case MODULES_WAKEUP:
			if (scrolledwindow_WakeUp==NULL)	
				debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "scrolledwindow_WakeUp==NULL");
			gtk_widget_destroy (scrolledwindow_WakeUp);
			break;
	    default:
		    debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Erreur : Destruction_Modules modules %d Inconnu", Modules_Actuel);
	}

}


void Initialisation_Modules (void)
{
	GtkWidget *Liste_Vide = NULL;

	scrolledwindow_Vide = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow_Vide);
	gtk_container_add (GTK_CONTAINER (viewport_Modules), scrolledwindow_Vide);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow_Vide), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	Liste_Vide = Create_Liste (FALSE, FALSE, FALSE, FALSE, FALSE);
	gtk_widget_show (Liste_Vide);
	gtk_container_add (GTK_CONTAINER (scrolledwindow_Vide), Liste_Vide);
	Modules_Actuel = AUCUN_MODULES_SELECTIONNER;
}
