/* ***********************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 "Update.h"
#include "config.h"
#include "Log.h"
#include "Thread.h"
#include "Debug.h"
#include "Animation.h"
#include "Gconf.h"
#include "main.h"
#include "Interface/Demarrage_Interface.h"
#include "Interface/Maemo.h"
#include "Declaration_Constante.h"
#include "Macro.h"


  
gchar    *Download_Http (gchar * uri)
{
	GnomeVFSHandle *handle = NULL;
	GnomeVFSResult result;
	gchar     buffer[BUF_SIZE_UPDATE];
	gchar    *Texte = NULL;
	GnomeVFSFileSize bytes_read;
	gint      Taille_Texte;

	ERROR_if (uri == NULL);
	debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "uri : %s", uri);
	Taille_Texte = 1;
	MALLOC (Texte,sizeof (gchar));
	strcpy (Texte, "");
	ERROR_if (Texte == NULL);
	result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
	debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "result : %d", result);
	if (result != GNOME_VFS_OK) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Unable to open URI, aborting\n");
		gnome_vfs_close (handle);
		return NULL;
	}
	while (result == GNOME_VFS_OK) {
		result = gnome_vfs_read (handle, buffer, BUF_SIZE_UPDATE, &bytes_read);
		buffer[bytes_read] = 0;
		if (bytes_read != 0) {
			Taille_Texte = Taille_Texte + bytes_read;
			REALLOC (Texte, (sizeof (gchar) * (Taille_Texte + 1)));
			strcat (Texte, buffer);
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(%d )%s", (int) bytes_read, buffer);
		}
	}
	gnome_vfs_close (handle);
	return Texte;
}

  
gpointer Thread_Test_Version (void)
{
	gchar    *Version = NULL, *Requete = NULL;

	Create_Log(CATEG_THREAD_TEST_VERSION,NULL,NULL,TRUE);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Thread_Test_Version");
	Requete = g_strdup_printf ("http://autoscan-network.com/check_version/check_version.php?OS=" SYSTEM "&CURRENT_VERSION=" VERSION);
	if ((Version = Download_Http (Requete)) == NULL) {
		MYFREE (Requete);
		Stop_Thread (0);
	}
	MYFREE (Requete);

	if (strstr(Version,VERSION)==NULL){
		Nouvelle_Version_Telechargable = TRUE;
	}
	MYFREE (Version);
	Stop_Thread (0);
}


gpointer Thread_Update_Fingerprinting (void)
{
	gchar    *Num_Version = NULL, *Fingerprinting = NULL, *Destination = NULL, *Deb_Version = NULL, *Requete = NULL;
	gint      Version;
	FILE     *fdestination = NULL;

	Create_Log(CATEG_THREAD_UPDATE_FINGERPRINTING,NULL,NULL,TRUE);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Update thread");
	Requete = g_strdup_printf ("http://autoscan-network.com/FingerPrinting/Version.php?MD5=%s", MD5_SOURCE);

	Etat = UPDATE_THREAD_OK;
	if ((Num_Version = Download_Http (Requete)) == NULL) {
		MYFREE (Requete);
		Stop_Thread (0);
	}
	MYFREE (Requete);

	Etat = UPDATE_VERSION_OK;

	Version = 0;
	Deb_Version = strchr (Num_Version, ']');
	Deb_Version[0] = 0;
	Deb_Version = strchr (Num_Version, '[') + 1;


	Version = atoi (Deb_Version);
	MYFREE (Num_Version);


	if (Lecture_Gconf_Int (INT_VERSION_SIGNATURE) != Version) {
		Etat = UPDATE_DOWNLOAD;
		if ((Fingerprinting = Download_Http ("http://autoscan-network.com/FingerPrinting/Finger_Printing_2007.xml")) == NULL) {
			Etat = UPDATE_FIN;
			Stop_Thread (0);
		}
	
		Destination = g_strdup_printf ("%s/.autoscan-network/config/Finger_Printing.xml", Info_User->pw_dir);
		fdestination = fopen (Destination, "w");
		if (fdestination == NULL) {
			debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Creating file: %s: %s", Destination, strerror (errno));
			Stop_Thread (0);
		}
		fputs (Fingerprinting, fdestination);
		fclose (fdestination);
		MYFREE (Fingerprinting);
	
		Enregistrement_Gconf_Entier (INT_VERSION_SIGNATURE, Version);
	}
	Etat = UPDATE_FIN;
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Fin Thread Update   (%d)", Lecture_Gconf_Int (INT_VERSION_SIGNATURE));
	Stop_Thread (0);
}



void Initialisation_Update (void)
{
	gint      Timeout;
	gchar    *Text = NULL;

	Etat = UPDATE_DEBUT;
	demmarrage_Thread (Thread_Update_Fingerprinting, NULL);

	while (Etat == UPDATE_DEBUT) {
		refresh ();
		sleep (1);
	}
	Timeout = 15;
	while ((Timeout != 0) && (Etat == UPDATE_THREAD_OK)) {
		Text = g_strdup_printf ("Updating (Fingerprinting)   please wait (%d)", Timeout);
#ifndef         __MAEMO
		gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), Text);
#else
		gtk_progress_bar_set_text_maemo (banner_maemo_Demarrage, Text);
#endif
		refresh ();
		MYFREE (Text);
		sleep (1);
		Timeout--;
	}
	if (Etat == UPDATE_THREAD_OK) {
#ifndef         __MAEMO
		gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), "Updating (Fingerprinting)   FAILED");
#else
		gtk_progress_bar_set_text_maemo (banner_maemo_Demarrage, "Updating (Fingerprinting)   FAILED");
#endif
		refresh ();
		sleep (2);
		return;
	}

	while (Etat == UPDATE_VERSION_OK) {
		sleep (1);
	}
#ifndef         __MAEMO
	gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progressbar_Demarrage), 0.8);
#else
	gtk_progress_bar_set_fraction_maemo (banner_maemo_Demarrage, 0.8);
#endif
	if (Etat == UPDATE_DOWNLOAD) {
		Timeout = 15;
		while ((Timeout != 0) && (Etat == UPDATE_DOWNLOAD)) {
			Text = g_strdup_printf ("Download (Fingerprinting)   please wait (%d)", Timeout);
#ifndef         __MAEMO
			gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), Text);
#else
			gtk_progress_bar_set_text_maemo (banner_maemo_Demarrage, Text);
#endif
			refresh ();
			MYFREE (Text);
			sleep (1);
			Timeout--;
		}

	}
#ifndef         __MAEMO
	gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), "Updating (Fingerprinting)   OK");
#else
	gtk_progress_bar_set_text_maemo (banner_maemo_Demarrage, "Updating (Fingerprinting)   OK");
#endif
	refresh ();
}
