/* ***********************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.
 **************************************************************************************/
#ifdef         __NESSUS_ENABLE

#include "Equipement_Nessus.h"
#include "Debug.h"
#include "Objet.h"
#include "Interface/Nessus_Message_Interface.h"
#include "Macro.h"


void Afficher_Texte_Nessus (struct Struct_Equipement *Equipement, gchar * Info, gint Niveau_Nessus)
{
	struct Struct_Nessus *Nessus = NULL;
	GtkTextBuffer *buffer = NULL;
	GtkTextIter iter;
	GtkTextIter start;
	GtkTextIter end;
	gint      Nb_Objet_Liste, i, j;
	gboolean  Premier = TRUE;
	gchar    *Nessus_Texte = NULL;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview_Nessus));
	gtk_text_buffer_get_start_iter (buffer, &start);
	gtk_text_buffer_get_end_iter (buffer, &end);
	gtk_text_buffer_delete (buffer, &start, &end);
	gtk_text_buffer_get_end_iter (buffer, &iter);

	Nb_Objet_Liste = g_list_length (Equipement->GList_Nessus);

	for (i = 0; i < Nb_Objet_Liste; i++) {
		Nessus = g_list_nth_data (Equipement->GList_Nessus, i);

		if ((Nessus->Niveau_Nessus == Niveau_Nessus) && (strcmp (Nessus->Info, Info) == 0)) {
			if (!Premier)
				gtk_text_buffer_insert (buffer, &iter, "\n\n====================================================\n\n", -1);
		
			Nessus_Texte = g_strdup (Nessus->Texte);
			for (j = 0; j < (strlen (Nessus_Texte) - 1); j++) {
				if ((Nessus_Texte[j] == 92) && (Nessus_Texte[j + 1] == 'n')) {
					Nessus_Texte[j] = 13;
					Nessus_Texte[j + 1] = 10;
				}
			}
			gtk_text_buffer_insert (buffer, &iter, (const gchar *) Nessus_Texte, -1);
			MYFREE (Nessus_Texte);
			Premier = FALSE;
		}
	}
}



void Ajouter_Texte_Nessus (struct Struct_Equipement *Equipement, gchar * Texte)
{
	struct Struct_Nessus *Nessus = Equipement->Nessus_En_Cours;

	if (Nessus->Texte == NULL) {
	
		Nessus->Texte = g_strdup (Texte);
	}
	else {
	
		REALLOC (Nessus->Texte, sizeof (gchar) * (strlen (Nessus->Texte) + strlen (Texte) + 1));
		strcat (Nessus->Texte, Texte);
	}
}


struct Struct_Nessus *Ajouter_Info_Nessus (struct Struct_Equipement *Equipement, gchar * Info, gint Niveau_Nessus)
{
	struct Struct_Nessus *Nessus = NULL;

	ERROR_if (Equipement == NULL);
	ERROR_if (Info == NULL);
	MALLOC (Nessus, sizeof (struct Struct_Nessus));
	Nessus->Niveau_Nessus = Niveau_Nessus;
	Nessus->Info = g_strdup (Info);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Ajout Nessus : %s", Nessus->Info);
	G_LIST_APPEND (Equipement->GList_Nessus, Nessus);
	Equipement->Nessus_En_Cours = Nessus;
	return Nessus;
}


#endif
