/* ***********************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 "Objet.h"
#include "Objet_Liste.h"
#include "Objet_Bulle.h"
#include "Interface/Root_Interface.h"
#include "Interface/Copie_Fichier_Interface.h"
#include "main.h"
#include "Modules.h"
#include "Module_Samba.h"
#include "Debug.h"
#include "Gconf.h"
#include "Macro.h"


void Detruire_Liste (GtkWidget * Liste_A_Detruire)
{
	GtkTreeModel *model = NULL;

	
	model = gtk_tree_view_get_model (GTK_TREE_VIEW (Liste_A_Detruire));
	gtk_tree_store_clear (GTK_TREE_STORE (model));
	gtk_widget_destroy (Liste_A_Detruire);
}


void source_drag_data_get (GtkWidget * widget_source, GdkDragContext * context, GtkSelectionData * selection_data, guint info, guint time, gpointer data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	GList    *Liste_Selection = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeSelection *selection = NULL;
	gchar    *donnee = NULL, *Chaine_Total = NULL, *Chaine_Temp = NULL;
	GtkTreePath *path = NULL;
	GtkTreeIter iter;
	gint      Nb_Selection, i, Taille_Total_Chaine;

	model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget_source));
	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget_source));
	Liste_Selection = gtk_tree_selection_get_selected_rows (selection, &model);
	Nb_Selection = g_list_length (Liste_Selection);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(%d) icone selectionne", Nb_Selection);
	Taille_Total_Chaine = 1;
	MALLOC (Chaine_Total,sizeof (gchar) * Taille_Total_Chaine);
	strcpy (Chaine_Total, "");
	for (i = 0; i < Nb_Selection; i++) {
		path = g_list_nth_data (Liste_Selection, Nb_Selection - i - 1);
		if (gtk_tree_model_get_iter (model, &iter, path)) {
			gtk_tree_model_get (model, &iter, COL_TEXT_LIST, &donnee, POINTER_LIST, &Objet, -1);
			switch (Objet->ID_Modele_Objet) {
			    case FICHIER_PARTAGER:
				    Taille_Total_Chaine = Taille_Total_Chaine + strlen (Objet->Chaine_Attacher) + 1;
				    REALLOC (Chaine_Total,Taille_Total_Chaine);
					strcat (Chaine_Total, Objet->Chaine_Attacher);
				    strcat (Chaine_Total, "\n");
				    break;
			    case PARTAGE_REPERTOIRE:
				    Taille_Total_Chaine = Taille_Total_Chaine + strlen (Equipement_Selectionner->Partage_En_Cours) + strlen (donnee) + 1;
				    REALLOC (Chaine_Total,Taille_Total_Chaine);
					strcat (Chaine_Total, Equipement_Selectionner->Partage_En_Cours);
				    strcat (Chaine_Total, donnee);
				    strcat (Chaine_Total, "\n");
				    break;
			    case LISTE_MODELE_EQUIPEMENT:
				    Taille_Total_Chaine = Taille_Total_Chaine + strlen (donnee);
				    REALLOC (Chaine_Total,Taille_Total_Chaine);
					strcat (Chaine_Total, donnee);
				    break;
			    case INFO_ADRESSE_IP:
			    case INFO_ADRESSE_MAC:
			    case INFO_FOURNISSEUR:
			    case INFO_OS:
			    case INFO_NOM_HOTE:
			    case INFO_ALIAS:
			    case INFO_NOM_DOMAINE:
			    case INFO_NOM_UTILISATEUR:
			    case INFO_CONTACT_ADMIN:
			    case INFO_INVENTAIRE_LOCALISATION:
				    debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Info : [%s]", donnee);
				    Chaine_Temp = index (donnee, ':') + 2;
				    Taille_Total_Chaine = Taille_Total_Chaine + strlen (Chaine_Temp);
				    REALLOC ( Chaine_Total,Taille_Total_Chaine);
					strcat (Chaine_Total, Chaine_Temp);
				    break;
			}
			MYFREE (donnee);
		}
		else {
		
			return;
		}
	}
	gtk_selection_data_set (selection_data, selection_data->target, 8, Chaine_Total, strlen (Chaine_Total));
	g_list_free (Liste_Selection);
}


void on_iconlist_drag_data_received (GtkWidget * widget, GdkDragContext * drag_context, gint x, gint y, GtkSelectionData * data, guint info, guint time, gpointer user_data)
{
	gchar    *donnee = NULL, *Position_Retour_Chariot = NULL;

	if ((data->length >= 0) && (data->format == 8)) {
		#ifdef         __SAMBA_ENABLE	
		donnee = data->data;
		switch (drag_context->action) {
		    case GDK_ACTION_COPY:
		    case GDK_ACTION_MOVE:
			    if (strncmp (Equipement_Selectionner->Partage_En_Cours, donnee, strlen (Equipement_Selectionner->Partage_En_Cours)) != 0) {
					if (window_Copie_Fichiers==NULL) 
							create_window_Copie_Fichiers ();
				    gtk_widget_show (window_Copie_Fichiers);
				    if (treeview_Modules == widget) {
					    if (Modules_Actuel == MODULES_PARTAGE_FICHIER_SMB_BROWSER) {
						    Annulation_Copie_Fichier = FALSE;
						    if (index (donnee, '\n') != NULL) {
							   
							    while ((!Annulation_Copie_Fichier) && ((Position_Retour_Chariot = index (donnee, '\n')) != NULL)) {
								    if (Position_Retour_Chariot != NULL)
									    donnee[Position_Retour_Chariot - donnee] = 0;
								    Copie_Fichier_Samba (donnee, Equipement_Selectionner->Partage_En_Cours);
								    donnee = Position_Retour_Chariot + 1;
							    }
						    }
						    else {
							   
							    Copie_Fichier_Samba (donnee, Equipement_Selectionner->Partage_En_Cours);
						    }
					    }
				    }
			    }
			   
			
			    break;
		   
			   
		}
		gtk_widget_hide (window_Copie_Fichiers);
		gtk_drag_finish (drag_context, TRUE, FALSE, time);
		#endif	
		return;
	}
	gtk_drag_finish (drag_context, FALSE, FALSE, time);
}


void on_iconlist_drag_begin (GtkWidget * widget, GdkDragContext * drag_context, gpointer user_data)
{
}


void on_iconlist_drag_end (GtkWidget * widget, GdkDragContext * drag_context, gpointer user_data)
{
}

gboolean Souris_Sort_Liste (GtkWidget * widget, GdkEventCrossing * event, gpointer user_data)
{


	if (gdk_window_at_pointer (NULL, NULL) == NULL)
		Cacher_Fenetre_Bulle ();
	return FALSE;
}


gboolean Selectionne (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	GtkTreeModel *model = NULL;
	GtkTreePath *path = NULL;
	GtkTreeIter iter;
	gint      x, y;

	if (event->type == GDK_BUTTON_PRESS) {
		GdkEventButton *bev = (GdkEventButton *) event;

		model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
		gdk_window_get_pointer (event->window, &x, &y, NULL);
		if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget), x, y, &path, NULL, NULL, NULL)) {
			return FALSE;
		}
		gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
		gtk_tree_model_get (model, &iter, POINTER_LIST, &Objet, -1);
		Objet_Selectionner = Objet;
		Modifie_Etat_Bouton (Objet);
		switch (bev->button) {
		    case 1:
			    if (Objet->Win_Bulle != NULL)
				    Fige_La_Bulle ();
			    if (Objet->Action_Selection != NULL)
				    (*Objet->Action_Selection) ((gpointer *) Objet);
			    break;
		    case 2:
			   
			   
			    break;
		    case 3:
			    if (Objet->Action_Clique_Droit != NULL)
				    (*Objet->Action_Clique_Droit) ((gpointer *) Objet, bev);
			    break;
		   
			   
		}
	}
	return FALSE;
}


void Double_Clic (GtkTreeView * tree_view, GtkTreePath * path, GtkTreeViewColumn * column, gpointer data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;

	model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));

	if (gtk_tree_model_get_iter (model, &iter, path))
		gtk_tree_model_get (model, &iter, POINTER_LIST, &Objet, -1);
	else {
	
		return;
	}
	if (Objet->Action_Double_Clique != NULL) {
	
		(*Objet->Action_Double_Clique) ((gpointer *) Objet);
	}

	
}


void Edit_Texte (GtkCellRendererText * cell, const gchar * path_new, const gchar * new_text, gpointer data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	struct Struct_Liste_Reseau *Reseau = NULL;
	GtkTreePath *path = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;

	Reseau = Recupere_Pointeur_Structure_Reseau (ID_Reseau_En_Cours);
	path = gtk_tree_path_new_from_string (path_new);
	switch (Reseau->ID_Derniere_Liste_Selectionner) {
	    case LISTE_MACHINE_PAR_TYPE:
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (Reseau->Liste_Equipement_Type));
		    break;
		case LISTE_MACHINE_PAR_IP:
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (Reseau->Liste_Equipement_IP));
		    break;
	    case LISTE_INFO:
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Info));
		    break;
		case LISTE_INFO_AUTRE:
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Info_Autre));
		    break;
		case LISTE_INFO_SNMP:	
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Info_Snmp));
		    break;	
		case LISTE_INFO_INVENTAIRE:
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Info_Inventaire));
		    break;
		case LISTE_INFO_HISTORIQUE:
			model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Info_Historique));
		    break;
		case LISTE_INFO_NESSUS:
			model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Info_Nessus));
		    break;		
	    case LISTE_MODULE:
		    model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview_Modules));
		    break;
	    default:
		   
		    return;
	}
	debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Edit_Texte_Liste_Machine : %s", gtk_tree_path_to_string (path));
	if (gtk_tree_model_get_iter (model, &iter, path))
		gtk_tree_model_get (model, &iter, POINTER_LIST, &Objet, -1);
	else {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "La Donnée n'existe plus");
		return;
	}
	if (Objet->Modification != NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Lance fonction Edit [%s]", new_text);
		(*Objet->Modification) ((gpointer *) Objet, (gchar *) new_text);
	}
	else {
		if (Objet->Modification_menu != NULL) {
		
			(*Objet->Modification_menu) ((gpointer *) Objet, (gchar *) new_text);
		
			MYFREE (Objet->Texte_Defaut);
			Objet->Texte_Defaut = g_strdup (new_text);
		
			gtk_tree_store_set (GTK_TREE_STORE (model), &Objet->ID_Arbre_Ligne, COL_ICON_LIST, Objet->icone, COL_TEXT_LIST, new_text, COLUMN_EDITABLE_LIST, FALSE, -1);
		}
		else
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Pas d'action");
	}
}


gint sort_func (GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data)
{
	gchar    *name_a = NULL, *name_b = NULL;
	gboolean  is_dir_a, is_dir_b;
	int       ret;

	gtk_tree_model_get (model, a, COL_IS_DIRECTORY_LIST, &is_dir_a, COL_TEXT_LIST, &name_a, -1);
	gtk_tree_model_get (model, b, COL_IS_DIRECTORY_LIST, &is_dir_b, COL_TEXT_LIST, &name_b, -1);

	if (!is_dir_a && is_dir_b)
		ret = 1;
	else if (is_dir_a && !is_dir_b)
		ret = -1;
	else {
		ret = g_utf8_collate (name_a, name_b);
	}
	MYFREE (name_a);
	MYFREE (name_b);
	return ret;
}


gboolean on_treeview_Reseau_1_focus_out_event (GtkWidget * widget, GdkEventFocus * event, gpointer user_data)
{
	return FALSE;
}


GtkWidget *Create_Liste (gboolean TRIER, gboolean Selection_Multiple, gboolean Selection_None, gboolean Drag_Drop_Send, gboolean Drag_Drop_Recev)
{
	GtkTreeModel *model = NULL;
	GtkTreeViewColumn *col = NULL;
	GtkCellRenderer *image = NULL, *texte = NULL;
	GtkWidget *view = NULL;
	GtkTreeSelection *selection = NULL;
	GtkTreeStore *store = NULL;

	store = gtk_tree_store_new (NUM_COLS_LIST, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER, GDK_TYPE_COLOR, G_TYPE_INT, G_TYPE_BOOLEAN,GDK_TYPE_COLOR);
	if (TRIER) {
		gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store), sort_func, NULL, NULL);
		gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
	}
	model = GTK_TREE_MODEL (store);
	view = gtk_tree_view_new_with_model (model);
	gtk_widget_show (view);
	g_object_unref (model);	
	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), Lecture_Gconf_Boolean (BOOL_TREE_VIEW_SET_RULES_HINT));

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
	if (Selection_Multiple)
		gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
	if (Selection_None)
		gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);	
	gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (view), Lecture_Gconf_Boolean (BOOL_TREE_VIEW_SET_HOVER_EXPAND));
	col = gtk_tree_view_column_new ();
	image = gtk_cell_renderer_pixbuf_new ();
	gtk_tree_view_column_pack_start (col, image, FALSE);
	gtk_tree_view_column_set_attributes (col, image, "pixbuf", COL_ICON_LIST, NULL);


	texte = gtk_cell_renderer_text_new ();
	g_signal_connect (texte, "edited", G_CALLBACK (Edit_Texte), model);

	gtk_tree_view_column_pack_start (col, texte, FALSE);
	gtk_tree_view_column_set_attributes (col, texte, "text", COL_TEXT_LIST, "editable", COLUMN_EDITABLE_LIST, "foreground-gdk", FOREGROUND_TEXT_LIST, "weight", BOLD_TEXT_LIST, "background-gdk", BACKGROUND_TEXT_LIST, NULL);
	gtk_tree_view_set_search_column (GTK_TREE_VIEW (view),COL_TEXT_LIST);

	gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
	enum {
		TARGET_STRING,
		TARGET_URL
	};
	static GtkTargetEntry targetentries[] = {
		{"text/plain", 0, TARGET_STRING}
	};
	static GtkTargetEntry target_table[] = {
		{"STRING", 0, TARGET_STRING},
		{"text/plain", 0, TARGET_STRING},
		{"text/uri-list", 0, TARGET_URL}
	};
	static guint n_targets = sizeof (target_table) / sizeof (target_table[0]);

	if (Drag_Drop_Send) {
	
		gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (view), GDK_BUTTON1_MASK | GDK_BUTTON2_MASK, target_table, n_targets, GDK_ACTION_COPY);
		g_signal_connect ((gpointer) view, "drag_begin", G_CALLBACK (on_iconlist_drag_begin), NULL);
		g_signal_connect ((gpointer) view, "drag_data_get", G_CALLBACK (source_drag_data_get), NULL);
		g_signal_connect ((gpointer) view, "drag_end", G_CALLBACK (on_iconlist_drag_end), NULL);
	}
	if (Drag_Drop_Recev) {
	
		gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW (view), targetentries, 1, GDK_ACTION_COPY);
		g_signal_connect ((gpointer) view, "drag_data_received", G_CALLBACK (on_iconlist_drag_data_received), NULL);
	}
	g_signal_connect ((gpointer) view, "row_activated", G_CALLBACK (Double_Clic), NULL);
	g_signal_connect ((gpointer) view, "focus_out_event", G_CALLBACK (on_treeview_Reseau_1_focus_out_event), NULL);
	g_signal_connect ((gpointer) view, "motion_notify_event", G_CALLBACK (show_win_bulle), NULL);
	g_signal_connect ((gpointer) view, "button-press-event", G_CALLBACK (Selectionne), view);

	return view;
}



GtkWidget *Create_Liste_Barre ()
{
	GtkTreeModel *model = NULL;
	GtkTreeViewColumn *col = NULL;
	GtkCellRenderer *image = NULL, *progress = NULL,*texte = NULL;;
	GtkWidget *view = NULL;
	GtkTreeSelection *selection = NULL;
	GtkTreeStore *store = NULL;
	
	store = gtk_tree_store_new (NUM_COLS_LIST_BARRE, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT);
	model = GTK_TREE_MODEL (store);
	view = gtk_tree_view_new_with_model (model);
	gtk_widget_show (view);
	g_object_unref (model);	
	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);

	col = gtk_tree_view_column_new ();
	image = gtk_cell_renderer_pixbuf_new ();
	gtk_tree_view_column_pack_start (col, image, FALSE);
	gtk_tree_view_column_set_attributes (col, image, "pixbuf", COL_ICON_LIST_BARRE, NULL);
	gtk_tree_view_column_set_sizing(col,GTK_TREE_VIEW_COLUMN_FIXED);
	gtk_tree_view_column_set_fixed_width(col,40);
	gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);

	col = gtk_tree_view_column_new ();
	texte = gtk_cell_renderer_text_new ();
	gtk_tree_view_column_pack_start (col, texte, FALSE);
	gtk_tree_view_column_set_attributes (col, texte, "text", COLUMN_TEXT_LIST_BARRE, NULL);
	gtk_tree_view_column_set_sizing(col,GTK_TREE_VIEW_COLUMN_FIXED);
	gtk_tree_view_column_set_fixed_width(col,120);
	gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);

	col = gtk_tree_view_column_new ();
	progress = gtk_cell_renderer_progress_new ();
	gtk_tree_view_column_pack_start (col, progress, TRUE);
	gtk_tree_view_column_set_attributes (col, progress, "value", PROGRESSION_LIST_BARRE, NULL);
	gtk_tree_view_column_set_sizing(col,GTK_TREE_VIEW_COLUMN_FIXED);
	gtk_tree_view_column_set_fixed_width(col,400);
	gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
	gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
	return view;
}







gint sort_func_Equipement_Par_Type (GtkTreeModel * model, GtkTreeIter * a_iter, GtkTreeIter * b_iter, gpointer user_data)
{
	struct Struct_Modele_Objet *Objet = NULL;
	gchar    *name_a = NULL, *name_b = NULL;
	gboolean  is_dir_a, is_dir_b;
	int       ret, a, b, c, d;

	gtk_tree_model_get (model, a_iter, COL_IS_DIRECTORY_LIST, &is_dir_a, COL_TEXT_LIST, &name_a, POINTER_LIST, &Objet, -1);
	gtk_tree_model_get (model, b_iter, COL_IS_DIRECTORY_LIST, &is_dir_b, COL_TEXT_LIST, &name_b, -1);
	
	if (!is_dir_a && is_dir_b)
		ret = 1;
	else if (is_dir_a && !is_dir_b)
		ret = -1;
	else {
		ret = g_utf8_collate (name_a, name_b);
	}
	MYFREE (name_a);
	MYFREE (name_b);
	return ret;
}

gint sort_func_Equipement_Par_IP (GtkTreeModel * model, GtkTreeIter * a_iter, GtkTreeIter * b_iter, gpointer user_data)
{
	gchar    *name_a = NULL, *name_b = NULL;
	int       ret, a, b, c, d;

	gtk_tree_model_get (model, a_iter, COL_TEXT_LIST, &name_a, -1);
	gtk_tree_model_get (model, b_iter, COL_TEXT_LIST, &name_b, -1);
	if (name_a[2]==':'){
	
		if (name_b[2]==':')
			ret = 0;
		else
			ret = -1;
		MYFREE (name_a);
		MYFREE (name_b);
		return ret;
	}	
	if (name_b[2]==':'){
	
		if (name_a[2]==':')
			ret = 0;
		else
			ret = 1;
		MYFREE (name_a);
		MYFREE (name_b);
		return ret;
	}	
	if (sscanf (name_a, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
	
		MYFREE (name_a);
		MYFREE (name_b);
		return -1;
	}
	MYFREE (name_a);
	name_a = g_strdup_printf ("%03d.%03d.%03d.%03d", a, b, c, d);
	if (sscanf (name_b, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
	
		return 1;
	}
	MYFREE (name_b);
	name_b = g_strdup_printf ("%03d.%03d.%03d.%03d", a, b, c, d);
	ret = g_utf8_collate (name_a, name_b);
	MYFREE (name_a);
	MYFREE (name_b);
	return ret;
}

GtkWidget *Create_Liste_Equipement (gboolean Par_Ip)
{
	GtkTreeModel *model = NULL;
	GtkTreeViewColumn *col = NULL;
	GtkCellRenderer *image = NULL, *texte = NULL;
	GtkWidget *view = NULL;
	GtkTreeSelection *selection = NULL;
	GtkTreeStore *store = NULL;

	if (Par_Ip)
		store = gtk_tree_store_new (NUM_COLS_EQUIPEMENT_PAR_IP, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER, GDK_TYPE_COLOR, G_TYPE_INT, G_TYPE_BOOLEAN, GDK_TYPE_COLOR, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
	else
		store = gtk_tree_store_new (NUM_COLS_EQUIPEMENT_PAR_TYPE, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER, GDK_TYPE_COLOR, G_TYPE_INT, G_TYPE_BOOLEAN, GDK_TYPE_COLOR);

	if (!Par_Ip) {
		gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store), sort_func_Equipement_Par_Type, NULL, NULL);
		gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
	}
	model = GTK_TREE_MODEL (store);
	view = gtk_tree_view_new_with_model (model);
	gtk_widget_show (view);
	g_object_unref (model);	
	if (Par_Ip)
		gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), TRUE);
	else
		gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);

	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), Lecture_Gconf_Boolean (BOOL_TREE_VIEW_SET_RULES_HINT));

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));

	gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (view), Lecture_Gconf_Boolean (BOOL_TREE_VIEW_SET_HOVER_EXPAND));
	col = gtk_tree_view_column_new ();
	image = gtk_cell_renderer_pixbuf_new ();
	gtk_tree_view_column_pack_start (col, image, FALSE);
	gtk_tree_view_column_set_attributes (col, image, "pixbuf", COL_ICON_LIST, NULL);
	texte = gtk_cell_renderer_text_new ();
	g_signal_connect (texte, "edited", G_CALLBACK (Edit_Texte), model);
	gtk_tree_view_column_pack_start (col, texte, FALSE);
	gtk_tree_view_column_set_attributes (col, texte, "text", COL_TEXT_LIST, "editable", COLUMN_EDITABLE_LIST, "foreground-gdk", FOREGROUND_TEXT_LIST, "weight", BOLD_TEXT_LIST,"background-gdk", BACKGROUND_TEXT_LIST, NULL);
	gtk_tree_view_column_set_title (col, "Ip");
	gtk_tree_view_column_set_sort_column_id (col, COL_TEXT_LIST);
	gtk_tree_view_column_set_resizable(col,TRUE);  
	gtk_tree_view_column_set_reorderable(col,TRUE);  

	gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);

	
	if (Par_Ip){
		gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model), COL_TEXT_LIST, sort_func_Equipement_Par_IP, NULL, NULL);
		gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(model), COL_TEXT_LIST, GTK_SORT_ASCENDING);
	}


	/*image = gtk_cell_renderer_pixbuf_new ();
	col = gtk_tree_view_column_new ();
	gtk_tree_view_column_pack_start (col, image, FALSE);
	gtk_tree_view_column_set_attributes (col, image, "pixbuf", MODULE_ACTIF, NULL);
	gtk_tree_view_column_set_title (col, "Module");
	gtk_tree_view_column_set_resizable(col,TRUE);  
	gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);*/
	
	if (Par_Ip) {
		texte = gtk_cell_renderer_text_new ();
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_pack_start (col, texte, FALSE);
		gtk_tree_view_column_set_attributes (col, texte, "text", HOSTNAME, NULL);
		gtk_tree_view_column_set_title (col, "HostName");
		gtk_tree_view_column_set_sort_column_id (col, HOSTNAME);
		gtk_tree_view_column_set_resizable(col,TRUE);  
		gtk_tree_view_column_set_reorderable(col,TRUE);  
		gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
		gtk_tree_view_set_search_column (GTK_TREE_VIEW (view),HOSTNAME); 
	
		
		texte = gtk_cell_renderer_text_new ();
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_pack_start (col, texte, FALSE);
		gtk_tree_view_column_set_attributes (col, texte, "text", USER, NULL);
		gtk_tree_view_column_set_title (col, "USER");
		gtk_tree_view_column_set_sort_column_id (col, USER);
		gtk_tree_view_column_set_resizable(col,TRUE);  
		gtk_tree_view_column_set_reorderable(col,TRUE);  
		gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
	
		
		texte = gtk_cell_renderer_text_new ();
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_pack_start (col, texte, FALSE);
		gtk_tree_view_column_set_attributes (col, texte, "text", NOTIFICATION, NULL);
		gtk_tree_view_column_set_title (col, "Notification");
		gtk_tree_view_column_set_sort_column_id (col, NOTIFICATION);
		gtk_tree_view_column_set_resizable(col,TRUE);  
		gtk_tree_view_column_set_reorderable(col,TRUE);  
		gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);

		texte = gtk_cell_renderer_text_new ();
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_pack_start (col, texte, FALSE);
		gtk_tree_view_column_set_attributes (col, texte, "text", OS, NULL);
		gtk_tree_view_column_set_title (col, "Os");
		gtk_tree_view_column_set_sort_column_id (col, OS);
		gtk_tree_view_column_set_resizable(col,TRUE);  
		gtk_tree_view_column_set_reorderable(col,TRUE);  
		gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
	}
	
	enum {
		TARGET_STRING,
		TARGET_URL
	};

	static GtkTargetEntry target_table[] = {
		{"STRING", 0, TARGET_STRING},
		{"text/plain", 0, TARGET_STRING},
		{"text/uri-list", 0, TARGET_URL}
	};
	static guint n_targets = sizeof (target_table) / sizeof (target_table[0]);


	gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (view), GDK_BUTTON1_MASK | GDK_BUTTON2_MASK, target_table, n_targets, GDK_ACTION_COPY);
	g_signal_connect ((gpointer) view, "drag_begin", G_CALLBACK (on_iconlist_drag_begin), NULL);
	g_signal_connect ((gpointer) view, "drag_data_get", G_CALLBACK (source_drag_data_get), NULL);
	g_signal_connect ((gpointer) view, "drag_end", G_CALLBACK (on_iconlist_drag_end), NULL);


	g_signal_connect ((gpointer) view, "row_activated", G_CALLBACK (Double_Clic), NULL);
	g_signal_connect ((gpointer) view, "focus_out_event", G_CALLBACK (on_treeview_Reseau_1_focus_out_event), NULL);
	g_signal_connect ((gpointer) view, "motion_notify_event", G_CALLBACK (show_win_bulle), NULL);
	g_signal_connect ((gpointer) view, "button-press-event", G_CALLBACK (Selectionne), view);
		

	return view;
}
