/* ***********************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 "Root_Callbacks.h"
#include "Root_Interface.h"
#include "Settings_Interface.h"
#include "About_Interface.h"
#include "Recherche_Interface.h"
#include "Open_Save_Interface.h"
#include "Wizard_Interface.h"
#include "../Quit.h"
#include "../Objet.h"
#include "../Objet_Bulle.h"
#include "../Objet_Action.h"
#include "../Settings.h"
#include "../Add_Network_Subnet.h"
#include "../Reseau.h"
#include "../Com/Commun.h"
#include "../Debug.h"
#include "../Modules.h"
#include "../Applet.h"
#include "../Macro.h"

void on_button_Message_Root_clicked(GtkButton  *button, gpointer user_data){
	DEBUG_ACTION_USER();
	Affiche_Page_Html("http://autoscan-network.com/index.php?option=com_content&task=view&id=12&Itemid=27");
}





void on_toolbutton_Help_clicked (GtkToolButton * toolbutton, gpointer user_data)
{	
	DEBUG_ACTION_USER();
	Affiche_Page_Html("http://autoscan-network.com/index.php?option=com_content&task=view&id=17&Itemid=31");
}



gboolean on_window_root_delete_event (GtkWidget * widget, GdkEvent * event, gpointer user_data)
{
	GtkWidget *Win_Quit = NULL;
	
	DEBUG_ACTION_USER();
#ifdef         __EGGTRAYICON_ENABLE
	if (tray_icon_Afficher) {
		gtk_widget_hide (window_root);
		Root_Afficher = FALSE;
	}
	else {
#endif		
		Win_Quit = gtk_message_dialog_new (GTK_WINDOW (window_root), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Are you sure you want to quit?");
		switch (gtk_dialog_run (GTK_DIALOG (Win_Quit))) {
		    case GTK_RESPONSE_YES:
			    Arret_Utilisateur ();
			    break;
		}
		gtk_widget_destroy (Win_Quit);
#ifdef         __EGGTRAYICON_ENABLE
	}
#endif	
	return TRUE;
}


void on_toolbutton_Add_Network_clicked (GtkToolButton * toolbutton, gpointer user_data)
{

		
	DEBUG_ACTION_USER();
	Wizard_Interface_Add_Network_Page1();
	gtk_widget_show (window_wizard);


}


void on_toolbutton_View (GtkToolButton * toolbutton, gpointer user_data)
{
	
	DEBUG_ACTION_USER();
	Switch_Liste_IP();
}


void on_toolbutton_Save_clicked (GtkToolButton * toolbutton, gpointer user_data)
{
	gchar    *Nom_Fenetre = NULL, *Nom_Fichier = NULL;
	
	DEBUG_ACTION_USER();
	create_file_Save ();
	gtk_widget_show (file_Save);
	
	Nom_Fenetre = g_strdup_printf ("Save [%s]", Reseau_Selectionner->Nom_Reseau);
	gtk_window_set_title (GTK_WINDOW (file_Save), Nom_Fenetre);
	MYFREE (Nom_Fenetre);
	Nom_Fichier = g_strdup_printf ("%s.xml", Reseau_Selectionner->Nom_Reseau);
	gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_Save), Nom_Fichier);
	MYFREE (Nom_Fichier);
}


void on_toolbutton_Search_clicked (GtkToolButton * toolbutton, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (window_Recherche==NULL)
		create_window_Recherche ();	
	gtk_widget_show (window_Recherche);
}


void on_toolbutton_Settings_clicked (GtkToolButton * toolbutton, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (dialog_Settings != NULL) {
		Preparation_Interface_Settings ();
#ifdef         __MAEMO
	gtk_widget_set_size_request (dialog_Settings, 800, 640);
#endif
		gtk_widget_show (dialog_Settings);
	}
}


void on_toggletoolbutton_Notification_clicked (GtkToolButton * toolbutton, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (!Modification_Toggle_En_Cours) {
		if (Objet_Selectionner->Action_Notification != NULL) {
			(*Objet_Selectionner->Action_Notification) ((gpointer *) Objet_Selectionner);
		}
		else
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Impossible de lancer Action_Notification");
	}
}


void on_toggletoolbutton_Alert_Intrusion_toggled (GtkToggleToolButton * toggletoolbutton, gpointer user_data)
{
	struct struct_Active_Alerte_Intrus Message_Active;
	struct struct_Desactive_Alerte_Intrus Message_Desactive;
	struct Struct_Liste_Reseau *Reseau = NULL;
	GtkWidget *Win_Demande_User = NULL;
	
	DEBUG_ACTION_USER();
	if (!Modification_Toggle_En_Cours) {
		Reseau = Recupere_Pointeur_Structure_Reseau (ID_Reseau_En_Cours);
		if (gtk_toggle_tool_button_get_active (toggletoolbutton)) {
			debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Intrusion Alert : enable");
			Win_Demande_User = gtk_message_dialog_new (GTK_WINDOW (window_root), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "If you active this function, the new equipments \nfound will be considered as intruders");
			switch (gtk_dialog_run (GTK_DIALOG (Win_Demande_User))) {
			    case GTK_RESPONSE_YES:
				    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);
				    Reseau->Alert_Intrusion = TRUE;
				    break;
			    case GTK_RESPONSE_NO:
				    Modification_Toggle_En_Cours = TRUE;
				    gtk_toggle_tool_button_set_active (toggletoolbutton, FALSE);
				    Modification_Toggle_En_Cours = FALSE;
				    Reseau->Alert_Intrusion = FALSE;
				    break;
			}
			gtk_widget_destroy (Win_Demande_User);
		}
		else {
			bzero (&Message_Desactive, sizeof (struct struct_Desactive_Alerte_Intrus));
			Message_Desactive.ID_Reseau = Reseau->Id_Reseau;
			Communication_Envoie_Donnee (Reseau->Pid_Agent, DESACTIVE_ALERTE_INTRUS, (gpointer *) & Message_Desactive, sizeof (Message_Desactive), Reseau);
			Reseau->Alert_Intrusion = FALSE;
		}
	}
}


void on_toolbutton_About_clicked (GtkToolButton * toolbutton, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (window_About==NULL)
		create_about();
	gtk_widget_show (window_About);
}


void on_toolbutton_Exit_clicked (GtkToolButton * toolbutton, gpointer user_data)
{
	DEBUG_ACTION_USER();
	Arret_Utilisateur ();
}


void on_notebook_reseau_switch_page (GtkNotebook * notebook, GtkNotebookPage * page, guint page_num, gpointer user_data)
{
	DEBUG_ACTION_USER();

	if (Reseau_Selectionner != NULL) {
		Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_INFO);
		Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_INFO_AUTRE);
		Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_INFO_SNMP);
		Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_INFO_INVENTAIRE);
		Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_INFO_HISTORIQUE);
		Efface_Toute_Objet_Liste (Reseau_Selectionner, LISTE_INFO_NESSUS);
		gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook_Info), FALSE);
		gtk_notebook_set_page(GTK_NOTEBOOK (notebook_Info),0);	
		Efface_Info_Utilisation_Disque();
		Initialisation_Aucun_Module ();
	}
	Equipement_Selectionner = NULL;
	Objet_Equipement_Selectionner = NULL;
	Reseau_Selectionner = Recupere_Pointeur_Reseau_Selectionner (gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook_reseau), page_num));
	ID_Reseau_En_Cours = Reseau_Selectionner->Id_Reseau;
	gtk_widget_set_sensitive (combobox_liste_Modules, FALSE);
	gtk_widget_set_sensitive (button_Refresh, FALSE);
	Modification_Toggle_En_Cours = TRUE;
	gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (toggletoolbutton_Alert_Intrusion), Reseau_Selectionner->Alert_Intrusion);
	gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (toggletoolbutton_Notification), FALSE);
	Modification_Toggle_En_Cours = FALSE;

	Reseau_Selectionner->Octet_Envoyer = 0;
	Reseau_Selectionner->Octet_Recu = 0;
	Reseau_Selectionner->Octet_Envoyer_Agent = 0;
	Reseau_Selectionner->Octet_Recu_Agent = 0;
	Affiche_Info_Reseau (Reseau_Selectionner);
	gtk_widget_hide (vbox_Milieu);
}


void on_notebook_Info_switch_page (GtkNotebook * notebook, GtkNotebookPage * page, guint page_num, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (Equipement_Selectionner!=NULL){
		Page_Notebook_Info = page_num;
	
	}
}


void on_togglebutton_Favoris_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (!Modification_Toggle_En_Cours) {
		debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Favoris");
		if (Objet_Selectionner->Action_Favoris != NULL) {
			(*Objet_Selectionner->Action_Favoris) ((gpointer *) Objet_Selectionner);
		}
		else
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Impossible de lancer Favoris");
	}
}


void on_button_Plus_clicked (GtkButton * button, gpointer user_data)
{

}


void on_button_Moins_clicked (GtkButton * button, gpointer user_data)
{

}

void on_button_Erase_clicked (GtkButton * button, gpointer user_data)
{
	struct Struct_Modele_Objet *Objet_Liste = NULL,*Objet_Par_Type = NULL,*Objet_Par_Ip = NULL;
	GtkTreeModel *model = NULL;
		
	DEBUG_ACTION_USER();
	Equipement_Selectionner->Equipement_Erase = TRUE;



	Objet_Liste = Equipement_Selectionner->Objet_Liste_Par_Type;
	Desactive_Rachaichissment_Objet (Objet_Liste, Reseau_Selectionner, FALSE);
	Desactive_Alerte_Objet (Objet_Liste, Reseau_Selectionner, FALSE);
	Desactive_Test_Etat_Objet (Objet_Liste, Reseau_Selectionner, FALSE);

	Recupere_Nouveau_Pointeur_Liste (Objet_Liste, Reseau_Selectionner, Supprime_Dans_Liste_Objets (GList_Liste (Objet_Liste, Reseau_Selectionner), Objet_Liste, TRUE));

	model = gtk_tree_view_get_model (GTK_TREE_VIEW (Widget_Liste (Objet_Liste, Reseau_Selectionner)));
	gtk_tree_store_remove (GTK_TREE_STORE (model), &Objet_Liste->ID_Arbre_Ligne);

	if (Objet_Liste->Objet_Pere != NULL)
		Supprime_Objet_Liste ((struct Struct_Modele_Objet *) Objet_Liste->Objet_Pere, Reseau_Selectionner);
	MYFREE (Objet_Liste);
	

	Objet_Liste = Equipement_Selectionner->Objet_Liste_Par_Ip;
	Desactive_Rachaichissment_Objet (Objet_Liste, Reseau_Selectionner, FALSE);
	Desactive_Alerte_Objet (Objet_Liste, Reseau_Selectionner, FALSE);
	Desactive_Test_Etat_Objet (Objet_Liste, Reseau_Selectionner, FALSE);

	Recupere_Nouveau_Pointeur_Liste (Objet_Liste, Reseau_Selectionner, Supprime_Dans_Liste_Objets (GList_Liste (Objet_Liste, Reseau_Selectionner), Objet_Liste, TRUE));

	model = gtk_tree_view_get_model (GTK_TREE_VIEW (Widget_Liste (Objet_Liste, Reseau_Selectionner)));
	gtk_tree_store_remove (GTK_TREE_STORE (model), &Objet_Liste->ID_Arbre_Ligne);
	MYFREE (Objet_Liste);
		

	Efface_Toutes_Info_Equipement();
		

	Reseau_Selectionner->Nb_Equipement_Erase++;
	Affiche_Info_Reseau (Reseau_Selectionner);
}


void on_button_Rafraichir_clicked (GtkButton * button, gpointer user_data)
{
	DEBUG_ACTION_USER();
	if (Objet_Selectionner->Action_Rafraichissement != NULL) {
		gtk_widget_set_sensitive (button_Refresh, FALSE);
		(*Objet_Selectionner->Action_Rafraichissement) ((gpointer *) Objet_Selectionner);
	}
	else
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Impossible de lancer Rafraichissement");
}


void on_button_Alerte_clicked (GtkButton * button, gpointer user_data)
{

}


void on_button_Warning_clicked (GtkButton * button, gpointer user_data)
{

}
