/* ***********************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 <libgnomevfs/gnome-vfs.h>


#include "Interface/Root_Interface.h"
#include "Interface/Bulle_Port_Interface.h"
#include "Interface/About_Interface.h"
#include "Interface/Settings_Interface.h"
#include "Interface/Menu_Samba_Interface.h"
#include "Interface/Copie_Fichier_Interface.h"
#include "Interface/Recherche_Interface.h"
#include "Interface/Open_Save_Interface.h"
#include "Interface/Alert_Interface.h"
#include "Interface/Bulle_Network_Interface.h"
#include "Interface/Erreur_Interface.h"
#include "Interface/Debug_Interface.h"
#include "Interface/Demarrage_Interface.h"
#include "Interface/Wizard_Interface.h"
#include "Interface/Wizard_Callbacks_Add_Network.h"
#include "Interface/Wizard_Callbacks_Add_FingerPrinting.h"
#include "Reseau.h"
#include "Objet.h"
#include "Animation.h"
#include "Objet_Bulle.h"
#include "Modules.h"
#include "Gconf.h"
#include "Settings.h"
#include "Settings_Signature.h"
#include "Settings_Port.h"
#include "Settings_Script.h"
#include "Debug.h"
#include "Com/Communication.h"
#include "Add_Network_Subnet.h"
#include "Fournisseur.h"
#include "Equipement.h"
#include "main.h"
#include "Style.h"
#include "Declaration_Constante.h"
#include "Conf.h"
#include "Thread.h"
#include "Sound.h"
#include "Applet.h"
#include "Settings_Restore.h"
#include "Contribution.h"
#include "Quit.h"
#include "Restoration_Agent.h"
#include "Update.h"
#include "BackTrace.h"
#include "Semaphore.h"
#include "Objet_Liste.h"
#include "Com/Initialisation_Com.h"
#include "Com/Commun.h"
#include "Interface/Wizard_Interface_Add_FingerPrinting.h"
#include "Interface/Maemo.h"
#include "Macro.h"

extern struct Struct_Modele_Objet Liste_Modele_Objet[];


int main (int argc, char *argv[])
{
	int       ptimer_Reception_Donnee = 0, ptimer_Rachaichir_Info = 0;
	gchar    *Chemin = NULL, *CharTemp = NULL;

	printf ("Starting %s (http://autoscan-network.com)\n", VERSION_AUTOSCAN);
	 

	
  	if (sizeof(int)    != 4) { printf("[WARNING] This platform's integers are not 32 bits.");}
  	if (sizeof(double) != 8) { printf("[WARNING] This platform's floats are not 64 bits.");}
  	if (sizeof(time_t) != 4) { printf("[WARNING] This platform's time_t is not 32 bits.");}
	
	window_root = NULL;
	Info_Message_Critique = NULL;
	Connecter_Agent = FALSE; 
#ifdef __BACKTRACE_ENABLE
	binary_name = g_strdup(argv[0]);
#endif
	GList_Liste_Thread = NULL;
	Id_Thread = 0;
	Nb_Thead = 0;
	mainThreadId = pthread_self ();
	Initialisation_Semaphore ();
	

	Info_User = getpwuid (getuid ());

#ifdef __LOG_ENABLE
	Initialisation_Repertoire_Log();
	Log_Equipement = NULL;
	Initialisation_Log();
#endif	
	

	Initialisation_signaux ();
	
	
	Pid_AutoScan = getpid ();
	gtk_init (&argc, &argv);
	client_gconf = gconf_client_get_default ();

	if (uname (&Uname) != -1)
		Info_Arch = g_strdup_printf ("%s\nOS : [%s]\nRELEASE : [%s]\nVERSION : [%s]\nARCH : [%s]\n", version_compiled, Uname.sysname, Uname.release, Uname.version, Uname.machine);
	else
		Info_Arch = "none";

	Initialisation_Debug ();
	Initialisation_Style ();
#ifdef         __MAEMO
	Init_Maemo(); 
#endif

	
#ifndef         __MAEMO
	create_win_Demarrage ();
#else
	create_win_Demarrage_banner_maemo();
#endif
	



	Change_Etat_Barre_Demarrage("Loading (Configuration)",0.1);

#ifndef __CONFIG_STATIC
	Copie_Fichier_Configuration ();
#endif


	Change_Etat_Barre_Demarrage("Loading (gnome vfs)",0.1);
	gnome_vfs_init ();


	Change_Etat_Barre_Demarrage("Loading Interface (Root)",0.1);
	create_window_root ();
	gtk_window_set_policy (GTK_WINDOW (window_root), TRUE, TRUE, FALSE);
	gtk_widget_realize (window_root);


	Change_Etat_Barre_Demarrage("Loading (debug)",0.2);
	Win_Debug = NULL;
	window_Erreur = NULL;


	Change_Etat_Barre_Demarrage("Loading (Variables)",0.2);
	Initialisation_Variable_Reseau ();
	Initialisation_Settings ();
	Initialisation_Subnet ();
	Initialisation_Equipement ();
	Initialisation_Variable_Restoration_Agent ();

	

	Initialisation_Module_Samba ();
#ifdef         __SAMBA_ENABLE
	Init_Liste_Partage_Monter ();
#endif


	Change_Etat_Barre_Demarrage("Loading (Ports)",0.2);
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Port.xml", Info_User->pw_dir);
	Lit_Xml_Port_Fichier (Chemin);
	MYFREE (Chemin);
	Change_Etat_Barre_Demarrage("Loading (Subnet)",0.3);
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Subnet.xml", Info_User->pw_dir);
	Lit_Xml_Subnet_Fichier (Chemin);
	MYFREE (Chemin);
#ifdef         __SCRIPT_ENABLE
	Change_Etat_Barre_Demarrage("Loading (Script)",0.4);
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Script.xml", Info_User->pw_dir);
	Lit_Xml_Script_Fichier (Chemin);
	MYFREE (Chemin);
#endif
	Change_Etat_Barre_Demarrage("Loading (Restore)",0.4);
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Restore.xml", Info_User->pw_dir);
	Lit_Xml_Restore_Fichier (Chemin);
	MYFREE (Chemin);
	Change_Etat_Barre_Demarrage("Loading (Mac)",0.5);
	Creer_Liste_Fournisseur();
	




	Change_Etat_Barre_Demarrage("Loading (Fingerprinting)",0.7);
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/" FILE_XML_FINGER_PRINTING, Info_User->pw_dir);
	Lit_Xml_Signature_Fichier (Chemin);
	MYFREE (Chemin);
	CharTemp = g_strdup_printf ("Loading (%d Fingerprintings)", Nb_Signature);
	Change_Etat_Barre_Demarrage(CharTemp,0.7);
	MYFREE (CharTemp);


	Charge_Rachaichissement_Icone_Liste ();


#ifdef	__SOUND_ENABLE	
	Change_Etat_Barre_Demarrage("Loading (sound system)",0.7);
	Initialisation_Sound_Alert ();
#endif


	Initialisation_Modules ();
	

	create_window_wizard ();
	Initialisation_Variable_Wizard_Add_Network();
    Initialisation_Variable_Wizard_Add_FingerPrinting();
	
	

#ifdef         __EGGTRAYICON_ENABLE	
	Change_Etat_Barre_Demarrage("Loading Interface (Tray Icon)",0.7);
	creation_applet_icone ();
#endif	
	


	Change_Etat_Barre_Demarrage("Loading Interface (Settings)",0.9);
	create_dialog_Settings ();
	Afficher_Liste_Signature ();
	Afficher_Liste_Port ();
#ifdef         __SCRIPT_ENABLE	
	Afficher_Liste_Script ();
#endif
	Afficher_Liste_Restore ();
	

	window_About = NULL;
	win_Alert = NULL;
	window_Recherche = NULL;
#ifdef         __SAMBA_ENABLE

	window_Copie_Fichiers = NULL;
#endif

	

#ifdef         __BULLE_NETWORK_ENABLE
	create_Bulle_Reseau ();
#endif
#ifdef         __BULLE_PORT_ENABLE
	win_raccourci = create_Bulle_Port ();
	gtk_widget_realize (win_raccourci);
	Liste_Modele_Objet[PORTS].Win_Bulle = win_raccourci;
#endif



	Change_Etat_Barre_Demarrage("Init Communication",1);
	Communication_Initialisation ();
	ptimer_Reception_Donnee = gtk_timeout_add (200, Communication_Reception_Donnee_Boucle, NULL);
	ptimer_Rachaichir_Info = gtk_timeout_add (1000, Rachaichi_Info_Transfert_Entre_Agent, NULL);
	


	Change_Etat_Barre_Demarrage("Restore",1);
	Restoration_Liste_Fichier ();
	

	Change_Etat_Barre_Demarrage("Init (timers)",1);
	ptimer_Rachaichissement_Liste = gtk_timeout_add (500, timer_Rachaichissement_Liste, NULL);
	Etat_Rafraichissement = 0;
	ptimer_Alerte_Liste = gtk_timeout_add (500, timer_Alerte_Liste, NULL);
	ptimer_Test_Etat_Liste = gtk_timeout_add (500, timer_Test_Etat_Liste, NULL);


	Nouvelle_Version_Telechargable = FALSE;
	demmarrage_Thread (Thread_Test_Version, NULL);


#ifndef         __MAEMO
	gtk_widget_hide (win_Demarrage);
	gtk_widget_destroy(win_Demarrage);
#else
	gtk_widget_destroy(banner_maemo_Demarrage);
#endif


#ifdef         __EGGTRAYICON_ENABLE	
	if (!(Lecture_Gconf_Boolean (BOOL_MINIMIZED_TRAY_ICON) && (Lecture_Gconf_Boolean (BOOL_START_MINIMIZED)))) {
#endif
		gtk_widget_show (window_root);
		if (g_list_length (Settings->GList_Restore) == 0){
			Wizard_Interface_Add_Network_Page1();
			gtk_widget_show (window_wizard);
		}
#ifdef         __EGGTRAYICON_ENABLE
	}
#endif
	
#ifdef  __CHECK_STRUCT_ENABLE
	if (sizeof(struct struct_ALL)!=TAILLE_ALL_STRUCTURE)
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Size structures FAILED (%d!=%d)",TAILLE_ALL_STRUCTURE,sizeof(struct struct_ALL));
#endif					

	gtk_main ();
	return 0;
}
