/* ***********************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 <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <signal.h>
#include "Quit.h"
#include "Module_Samba.h"
#include "Declaration_Constante.h"
#include "Interface/Root_Interface.h"
#include "Interface/Settings_Interface.h"
#include "Interface/Arret_Interface.h"
#include "Applet.h"
#include "Sound.h"
#include "Debug.h"
#include "Gconf.h"
#include "main.h"
#include "Update.h"
#include "Sound.h"
#include "Smtp.h"
#include "Contribution.h"
#include "Log.h"
#include "BackTrace.h"
#include "Com/Commun.h"
#include "Semaphore.h"
#include "Macro.h"

#ifdef __LOG_ENABLE
extern struct Struct_Categorie Liste_Categorie[];
#endif

void Enregistre_Position_Fenetre (void)
{
	gint      hauteur, largeur, x, y;

	Enregistrement_Gconf_Entier (INT_ROOT_POSITION_X, 30);
	Enregistrement_Gconf_Entier (INT_ROOT_POSITION_Y, 30);
	gtk_window_get_size ((GtkWindow *) window_root, &largeur, &hauteur);
	Enregistrement_Gconf_Entier (INT_ROOT_SIZE_X, largeur);
	Enregistrement_Gconf_Entier (INT_ROOT_SIZE_Y, hauteur);
	gtk_window_get_size ((GtkWindow *) dialog_Settings, &largeur, &hauteur);
	Enregistrement_Gconf_Entier (INT_SETTINGS_SIZE_X, largeur);
	Enregistrement_Gconf_Entier (INT_SETTINGS_SIZE_Y, hauteur);
	Enregistrement_Gconf_Entier (INT_V_PANED_POSITION, gtk_paned_get_position (GTK_PANED (vpaned_root)));
	Enregistrement_Gconf_Entier (INT_H_PANED_POSITION, gtk_paned_get_position (GTK_PANED (hpaned_root)));
}


void Control_C (int sig)
{
	printf ("(Control_C)\n");
	Arret_Utilisateur ();
}

void Arret_Immediat(){
	printf("Arret_Immediat : FAILED INTERFACE\n");
	exit(-1);
}


void segfault_handler (int sig)
{
#ifdef __LOG_ENABLE
	struct struct_Cle_Privee *pCle = NULL;
#endif
	GtkWidget *Win_Demande_User = NULL;
	gchar *Message_Erreur = NULL;
		

	signal (SIGSEGV, Arret_Immediat);

#ifdef __BACKTRACE_ENABLE
	print_trace();
	display_backtrace(NULL,NULL,0);
#endif

#ifdef __LOG_ENABLE
	if ((pCle = pthread_getspecific (Cle_privee_thread)) == NULL)
		pCle = Donnee_Cle_Main;
	printf ("Segfault : Invalid memory reference (%s)\n", Liste_Categorie[pCle->Categorie].Info);

	Log (pCle, "---------- Segfault ----------\n");
#endif
#ifdef __BACKTRACE_ENABLE
	display_backtrace (pCle,NULL,0);
#endif

	gtk_widget_hide_all (window_root);
#ifdef __LOG_ENABLE
	Message_Erreur = g_strdup_printf ("Segfault : Invalid memory reference (%s)", Liste_Categorie[pCle->Categorie].Info);
#else
	Message_Erreur = g_strdup("Segfault : Invalid memory reference");	
#endif
	Win_Demande_User = gtk_message_dialog_new (GTK_WINDOW (window_root), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, Message_Erreur);
	gtk_dialog_run (GTK_DIALOG (Win_Demande_User));
	gtk_widget_hide(Win_Demande_User);
	MYFREE(Message_Erreur);

#ifdef __LOG_ENABLE
	Envoie_Email_Segfault(pCle);
#endif

	Exit();
}


void Initialisation_signaux (void)
{
	struct sigaction    sa;
 	sigset_t            sigmsk;

 
	sigfillset( &sigmsk );
    sigdelset( &sigmsk, SIGUSR1 );
	sigdelset( &sigmsk, SIGUSR2 );
    sigdelset( &sigmsk, SIGHUP );
    sigdelset( &sigmsk, SIGWINCH );
    sigdelset( &sigmsk, SIGINT );
    sigdelset( &sigmsk, SIGSEGV );
    sigdelset( &sigmsk, SIGILL );
    sigdelset( &sigmsk, SIGFPE );
    pthread_sigmask( SIG_SETMASK, &sigmsk, NULL );

#ifdef         __LOG_ENABLE
 
    sa.sa_sigaction = Pause_Thread;
    sigemptyset( &sa.sa_mask );
    sa.sa_flags = SA_RESTART;
    sigaction( SIGUSR1, &sa, NULL );
#endif 
	
	signal (SIGINT, Control_C);
	signal (SIGTERM, Arret_Utilisateur);
	signal (SIGHUP, Arret_Utilisateur);
	signal (SIGSEGV, segfault_handler);
}


void Arret_Utilisateur (void)
{
	gtk_widget_hide_all (window_root);
	Enregistre_Position_Fenetre ();
	Arret_Demande();
}

void Arret_Erreur (void)	
{

	Arret_Demande();
}


void Exit(void){

#ifdef __BACKTRACE_ENABLE
	display_backtrace (Donnee_Cle_Main,NULL,0);
#endif
#ifdef __SAMBA_ENABLE
	Demonte_Tout_Partage ();
#endif
	Arreter_Tous_Les_Agents ();
	Close_Log_Main();	
	Ckeck_Fermeture_Log(); 

	printf ("Stopping %s (http://autoscan-network.com)\n", VERSION_AUTOSCAN);
	gtk_main_quit ();
	exit(EXIT_SUCCESS);	
}



void Arret_Demande(void){
#ifdef         __LOG_ENABLE
	if(!pthread_equal( pthread_self(), mainThreadId )){
		pthread_kill(mainThreadId,SIGINT);
		printf("Stopping ...\n");
		sleep(1);  
	
	
	
		printf("__1\n");
		sleep(1); 
		printf("__2\n");
		sleep(1); 
		printf("__3\n");
		sleep(1); 
		Exit();
	}
	signal (SIGSEGV, Arret_Immediat);

	printf("******** Stopping Thread  *************\n");
	envoie_Signal_All_Thread(SIGUSR1);
	while (Nb_Thead!=0)
		sleep(1);
	printf("***************************************\n");
#endif
	gtk_widget_hide_all (window_root);
#ifdef __EGGTRAYICON_ENABLE
	destruction_applet_icone ();
#endif	
	Exit();
}
