/* ***********************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 <stdio.h>
#include <dirent.h>
#include "Root_Interface.h"
#include "Combo.h"
#include "../Objet.h"
#include "Settings_Interface.h"
#include "Recherche_Interface.h"
#include "../Image.h"
#include "../Declaration_Constante.h"
#include "Modules.h"
#include "Script_Interface.h"
#include "Wizard_Interface_Add_Network.h"
#include "Wizard_Callbacks_Add_Network.h"
#include "../Debug.h"
#include "../Settings_Script.h"
#include "../Settings.h"
#include "../Gconf.h"
#include "../Equipement_Port.h"
#include "../Settings_Port.h"
#include "../Macro.h"

extern struct Struct_Modele_Objet Liste_Modele_Objet[];

gchar    *Recuperation_Texte_Combo (GtkWidget * combobox)
{
	gchar    *texte = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;

	ERROR_if (combobox == NULL);
	if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &iter)) {
		model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
		gtk_tree_model_get (model, &iter, COMBO_TEXT, &texte, -1);
		return texte;
	}
	return NULL;
}



gchar    *Recuperation_Chaine_Attacher_Combo (GtkWidget * combobox)
{
	gchar    *chaine = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;

	ERROR_if (combobox == NULL);
	if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &iter)) {
		model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
		gtk_tree_model_get (model, &iter, COMBO_POINTER_CHAINE, &chaine, -1);
		return chaine;
	}
	return NULL;
}

gchar    *Recuperation_Chaine2_Attacher_Combo (GtkWidget * combobox)
{
	gchar    *chaine = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;

	ERROR_if (combobox == NULL);
	if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &iter)) {
		model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
		gtk_tree_model_get (model, &iter, COMBO_POINTER_CHAINE2, &chaine, -1);
		return chaine;
	}
	return NULL;
}


void Active_Combo_Correspondant(GtkWidget * Combo,gchar *Texte){
	GtkTreeModel *model = NULL;
  	gboolean valid;
  	gint Num_Ligne = 0;
	GtkTreeIter iter;


	model = gtk_combo_box_get_model (GTK_COMBO_BOX (Combo));

  	valid = gtk_tree_model_get_iter_first (model, &iter);

  	while (valid){
      		gchar *data;
		gtk_tree_model_get (model, &iter, COMBO_TEXT, &data, -1);
		if (strstr(Texte,data)!=NULL){
			MYFREE (data);
			gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), Num_Ligne);
			return;
		}
		MYFREE (data);
		Num_Ligne ++;
		valid = gtk_tree_model_iter_next (model, &iter);
    	}
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), -1);
}


void Active_Combo_Correspondant_Chaine_Attache(GtkWidget * Combo,gchar *Texte){
	GtkTreeModel *model = NULL;
  	gboolean valid;
  	gint Num_Ligne = 0;
	GtkTreeIter iter;


	model = gtk_combo_box_get_model (GTK_COMBO_BOX (Combo));

  	valid = gtk_tree_model_get_iter_first (model, &iter);

  	while (valid){
      		gchar *data;
		gtk_tree_model_get (model, &iter, COMBO_POINTER_CHAINE, &data, -1);
		if (strstr(Texte,data)!=NULL){
			MYFREE (data);
			gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), Num_Ligne);
			return;
		}
		MYFREE (data);
		Num_Ligne ++;
		valid = gtk_tree_model_iter_next (model, &iter);
    	}
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), -1);
}


void Affiche_Liste_Combo_Liste_Fichier_Image(GtkWidget * Combo){
	GtkTreeModel *model = NULL;
  	gboolean valid;
  	gint Num_Ligne = 0;
	GtkTreeIter iter;

	model = gtk_combo_box_get_model (GTK_COMBO_BOX (Combo));

  	valid = gtk_tree_model_get_iter_first (model, &iter);

  	while (valid){
      		gchar *data;
		gtk_tree_model_get (model, &iter, COMBO_TEXT, &data, -1);
	
		MYFREE (data);
		Num_Ligne ++;
		valid = gtk_tree_model_iter_next (model, &iter);
    	}
}


void Efface_Combo (GtkWidget * combobox)
{
	GtkTreeModel *model = NULL;


	ERROR_if (combobox == NULL);
	model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
	gtk_tree_store_clear (GTK_TREE_STORE (model));
}

void on_combobox_liste_changed (GtkComboBox * combobox, gpointer user_data)
{
	gchar    *texte = NULL, *Chaine = NULL;
	GtkTreeModel *model = NULL;
	void      (*Fonction) ();
	GtkTreeIter iter;

	ERROR_if (combobox == NULL);
	if (gtk_combo_box_get_active_iter (combobox, &iter)) {
		model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
		gtk_tree_model_get (model, &iter, COMBO_TEXT, &texte, COMBO_POINTER, &Fonction, COMBO_POINTER_CHAINE, &Chaine, -1);
		if (Fonction != NULL) {
			(Fonction) ();
		}
		MYFREE (texte);
	}
}


void Ajoute_Entree_Combo (GtkWidget * combobox, gchar * path_icone, gchar * texte, gboolean sensible, gpointer * Pointeur_Fonction, gchar * Chaine, gchar * Chaine2, GtkTreeIter * iter, GtkTreeIter * iter_parent)
{
	GdkPixbuf *pixbuf = NULL;
	GtkTreeModel *model = NULL;

	ERROR_if (combobox == NULL);
	ERROR_if (texte == NULL);
	model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
	gtk_tree_store_append (GTK_TREE_STORE (model), iter, iter_parent);
	if (path_icone != NULL) {
		pixbuf = Chargement_Image (path_icone, COULEUR);
		gtk_tree_store_set (GTK_TREE_STORE (model), iter, COMBO_ICON, pixbuf, COMBO_TEXT, texte, COMBO_SENSIBLE, sensible, COMBO_POINTER, Pointeur_Fonction, COMBO_POINTER_CHAINE, Chaine, COMBO_POINTER_CHAINE2, Chaine2, -1);
	}
	else
		gtk_tree_store_set (GTK_TREE_STORE (model), iter, COMBO_TEXT, texte, COMBO_SENSIBLE, sensible, COMBO_POINTER, Pointeur_Fonction, COMBO_POINTER_CHAINE, Chaine, COMBO_POINTER_CHAINE2, Chaine2, -1);
}


void creation_combo_milieu (void)
{
	GtkTreeIter iter;

	if (Equipement_Selectionner->Equipement_Allumer){
		Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Port_Ouvert.png", "Open TCP Ports", TRUE, (gpointer) & Initialisation_Liste_ports, NULL, NULL, &iter, NULL);
#ifdef         __SAMBA_ENABLE
		if (Recherche_Port_Samba_Ouvert_Sur_Equipement_Selectionner ())
			Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Partage_Fichiers.png", "Share", TRUE, (gpointer) & Initialisation_Partage_Fichier_Smb, NULL, NULL, &iter, NULL);
#endif
#ifdef         __NESSUS_ENABLE
		Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Nessus.png", "Nessus", TRUE, (gpointer) & Initialisation_Nessus, NULL, NULL, &iter, NULL);
#endif
#ifdef         __CONSOLE_ENABLE
		if (Recherche_Port_Telnet_Ouvert_Sur_Equipement_Selectionner ())
			Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Telnet.png", "Telnet", TRUE, (gpointer) & Initialisation_Telnet, NULL, NULL, &iter, NULL);
		if (Recherche_Port_SSH_Ouvert_Sur_Equipement_Selectionner ())
			Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Ssh.png", "SSH", TRUE, (gpointer) & Initialisation_SSH, NULL, NULL, &iter, NULL);
#endif
		
#ifdef   __VCN_ENABLE
		if (Cherche_Si_Port_Ouvert (Equipement_Selectionner, 5900))
			Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "vnc.png", "VNC", TRUE, (gpointer) & Initialisation_Vnc, NULL, NULL, &iter, NULL);
#endif		
		
#ifdef         __SCRIPT_ENABLE
		Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Port_Ouvert.png", "Script", TRUE, (gpointer) & Initialisation_Script, NULL, NULL, &iter, NULL);
#endif
#ifdef         __SAMBA_ENABLE
		if (Recherche_Port_Samba_Ouvert_Sur_Equipement_Selectionner ())
			Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "Email.png", "Net Send", TRUE, (gpointer) & Initialisation_NetSend, NULL, NULL, &iter, NULL);
#endif
	
	}
	else
		Ajoute_Entree_Combo (combobox_liste_Modules, PATH_ICONE "wakeup.png", "WakeUp", TRUE, (gpointer) & Initialisation_WakeUp, NULL, NULL, &iter, NULL);

	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_liste_Modules), 0);
}


void creation_combo_script (void)
{
#ifdef         __SCRIPT_ENABLE	
	struct Struct_Liste_Script *Script = NULL;
	gchar    *command = NULL;
	GtkTreeIter iter;
	guint     Nb_Objet_Liste, i;

	Nb_Objet_Liste = g_list_length (Settings->GList_Script);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Script = g_list_nth_data (Settings->GList_Script, i);
		command = g_strdup_printf ("%s %s", Script->Path, Script->Param);
		if ((Script->Icon != NULL) && (strlen (Script->Icon) != 0))
			Ajoute_Entree_Combo (combobox_Script, Script->Icon, Script->Name, TRUE, NULL, command, NULL, &iter, NULL);
		else
			Ajoute_Entree_Combo (combobox_Script, PATH_ICONE "run.png", Script->Name, TRUE, NULL, command, NULL, &iter, NULL);
	}
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Script), 0);
#endif
}


void creation_combo_liste_port (void)
{
	struct Struct_Liste_Port *Port = NULL;
	GtkTreeIter iter;
	guint     Nb_Objet_Liste, i;

	Nb_Objet_Liste = g_list_length (Settings->GList_Port);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Port = g_list_nth_data (Settings->GList_Port, i);
		if (Port->Port_Pere == NULL)
			Ajoute_Entree_Combo (combobox_Recherche_Port, NULL, Port->Service, TRUE, NULL, NULL, NULL, &iter, NULL);
	}
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Recherche_Port), 0);
}


void creation_combo_type_port (void)
{
	GtkTreeIter iter;

	Ajoute_Entree_Combo (combobox_Liste_Ports_Info_Type, NULL, "Server", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_Liste_Ports_Info_Type, NULL, "Other", TRUE, NULL, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Liste_Ports_Info_Type), 0);
}


void creation_combo_Audio_Device (void)
{
#ifdef  __SOUND_ENABLE

	GtkTreeIter iter;

	Ajoute_Entree_Combo (combobox_General_Audio_Device, NULL, "arts", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_General_Audio_Device, NULL, "alsa", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_General_Audio_Device, NULL, "alsa09", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_General_Audio_Device, NULL, "esd", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_General_Audio_Device, NULL, "irix", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_General_Audio_Device, NULL, "oss", TRUE, NULL, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_General_Audio_Device), Lecture_Gconf_Int (INT_AUDIO_DEVICE));
#endif
}


void creation_combo_type (GtkWidget * Combo)
{
	GtkTreeIter iter;
	int       i;

	ERROR_if (Combo == NULL);
	for (i = 0; i <= NB_TYPE_EQUIPEMENT; i++)
		Ajoute_Entree_Combo (Combo, Liste_Modele_Objet[i].Chemin_Icone, Liste_Modele_Objet[i].Texte_Defaut, TRUE, NULL, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), 0);
}


void creation_combo_etat_port (GtkWidget * Combo)
{
	GtkTreeIter iter;

	Ajoute_Entree_Combo (Combo, NULL, "Closed", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "Indifferent", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "Can be opened", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "Must be opened", TRUE, NULL, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), 0);
}

void creation_combo_etat_snmp (GtkWidget * Combo)
{
	GtkTreeIter iter;

	Ajoute_Entree_Combo (Combo, NULL, "Indifferent", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "Can be opened", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "Must be opened", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "NULL", TRUE, NULL, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), 0);
}

void creation_combo_etat_GW_RT (GtkWidget * Combo)
{
	GtkTreeIter iter;

	Ajoute_Entree_Combo (Combo, NULL, "Indifferent", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "Yes", TRUE, NULL, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (Combo, NULL, "No", TRUE, NULL, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), 0);
}

void creation_combo_class (void)
{
	GtkTreeIter iter;

	Ajoute_Entree_Combo (combobox_Network_Private_Subnet, NULL, "255.0.0.0", TRUE, (gpointer) & Modifie_Class_A, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_Network_Private_Subnet, NULL, "255.255.0.0", TRUE, (gpointer) & Modifie_Class_B, NULL, NULL, &iter, NULL);
	Ajoute_Entree_Combo (combobox_Network_Private_Subnet, NULL, "255.255.255.0", TRUE, (gpointer) & Modifie_Class_C, NULL, NULL, &iter, NULL);
	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox_Network_Private_Subnet), -1);
}


void Ajoute_Interface_Combo (gchar * Interface, gchar * Ip)
{
	GtkTreeIter iter;
	gchar    *Interface_Reseau, *Ip_Reseau, *temp;

	ERROR_if (Interface == NULL);
	ERROR_if (Ip == NULL);
	Interface_Reseau = g_strdup (Interface);
	Ip_Reseau = g_strdup (Ip);
	temp = g_strdup_printf ("%s [%s]", Interface, Ip);
	Ajoute_Entree_Combo (combobox_Select_Interface, PATH_ICONE "hwinfo.png", temp, TRUE, NULL, Interface_Reseau, Ip_Reseau, &iter, NULL);
	MYFREE (temp);
}


void Creation_Combo_Liste_Fichier_Image (GtkWidget * Combo, gchar * Chemin, gboolean Affiche_Nom)
{
	GtkTreeIter iter;
	DIR* dir;
	struct dirent* entree = NULL;
	gchar    *temp = NULL;


	ERROR_if (Chemin == NULL);
	dir = opendir(Chemin);
	ERROR_if (dir == NULL);
	while ((entree = readdir(dir))!= NULL){
		if ((strcmp (entree->d_name, ".") != 0) && (strcmp (entree->d_name, ".."))) {
			temp = g_strdup_printf ("%s%s", Chemin, entree->d_name);
		
			if (Affiche_Nom)
				Ajoute_Entree_Combo (Combo, temp, entree->d_name, TRUE, NULL, temp , NULL, &iter, NULL);
			else
				Ajoute_Entree_Combo (Combo, temp, "", TRUE, NULL, temp , NULL, &iter, NULL);
			MYFREE (temp);
		}
	}
	closedir(dir);
	gtk_combo_box_set_active (GTK_COMBO_BOX (Combo), 0);
}



GtkWidget *creation_combo (gboolean Detachable)
{
	GtkCellRenderer *renderer = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeStore *store = NULL;
	GtkWidget *Combo = NULL;

	store = gtk_tree_store_new (COMBO_NUM_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING);
	model = GTK_TREE_MODEL (store);
	Combo = gtk_combo_box_new_with_model (model);
	g_object_unref (model);
	gtk_widget_show (Combo);
	if (Detachable)
		gtk_combo_box_set_add_tearoffs (GTK_COMBO_BOX (Combo), TRUE);
	renderer = gtk_cell_renderer_pixbuf_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (Combo), renderer, FALSE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (Combo), renderer, "pixbuf", COMBO_ICON, "sensitive", COMBO_SENSIBLE, NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (Combo), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (Combo), renderer, "text", COMBO_TEXT, "sensitive", COMBO_SENSIBLE, NULL);
	g_signal_connect ((gpointer) Combo, "changed", G_CALLBACK (on_combobox_liste_changed), NULL);
	return Combo;
}
