Logo Search packages:      
Sourcecode: xcfa version File versions

poche_web.c

 /*
 * file      : poche_web.c
 * project   : xcfa
 * with      : Gtk-2
 *
 * copyright : (C) 2003,2004,2005,2006,2007,2008,2009 by Claude Bulin
 *
 * xcfa - GTK+ implementation of the GNU shell command
 * GNU General Public License
 *
 * 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, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * OLD ADRESS:
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * NEW ADRESS:
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "support.h"
#include "global.h"
#include "info_song.h"
#include "win_pulse.h"
#include "win_info.h"
#include "config_user.h"

#include "poche.h"
#include "secu.h"
#include "get.h"

/*
*---------------------------------------------------------------------------
* VARIABLES
*---------------------------------------------------------------------------
*/

#define BUFFER_SIZE 1024

gchar *nameindexhtml = NULL;

typedef struct {
      gboolean  EndThread;
      gboolean  EndTimeout;
      gchar    *name;
      guint     timeout;
      pthread_t nmr_tid;
} POCHEWEB_EVENT;

POCHEWEB_EVENT pocheweb_event;

guint pocheweb_tt_glist = 0;

/*
*---------------------------------------------------------------------------
* FONCTIONS
*---------------------------------------------------------------------------
*/

/* Recuperation sur le web du fichier image et sauvegarde
*/
gchar *pocheweb_get_on_web_file_img (gchar *filename)
{
      /*FILE     *index;*/
      gchar    *name_file = NULL;
      /*gchar    *ptr_name = NULL;*/
      gint      num = 0;
      gchar    *LineCommand = NULL;


      /*PRINT_FUNC_LF();*/

      /* Si le nom existe deja, en chercher un nouveau */
      /*
      if ( (ptr_name = strrchr (filename, '/')) == NULL) return (NULL);
      ptr_name ++;
      name_file = g_strdup_printf ("%s/%s", poche.Path_pochette, ptr_name);
      if (infosong_file_is_reg (name_file) == TRUE) {
            g_print ("EXISTE DEJA: %s\n", name_file);
            g_free(name_file);
            name_file = NULL;

            num = 1;
            while (TRUE) {
                  name_file = g_strdup_printf ("%s/%03d.jpg", poche.Path_pochette, num);
                  if (infosong_file_is_reg (name_file) == FALSE) {
                        g_print ("NOUVEAU NOM: %s\n", name_file);
                        break;
                  }
                  num ++;
                  g_free (name_file);
                  name_file = NULL;
            }
      }
      */
      num = 1;
      while (TRUE) {
            name_file = g_strdup_printf ("%s/%03d.jpg", poche.Path_pochette, num);
            if (infosong_file_is_reg (name_file) == FALSE) {
                  /*g_print ("NOUVEAU NOM: %s\n", name_file);*/
                  break;
            }
            num ++;
            g_free (name_file);
            name_file = NULL;
      }
      
      LineCommand = g_strdup_printf ("wget --user-agent=\"Mozilla 22.0\" -O %s %s", name_file, filename);
g_print("LineCommand=%s\n", LineCommand);
      system (LineCommand);
      g_free (LineCommand);
      LineCommand = NULL;

      if (infosong_file_is_reg (name_file) == FALSE) {
            g_free(name_file);
            name_file = NULL;
      }
      if (infosong_file_is (FILE_IS_IMG, name_file) != FILE_IS_IMG) {
            infosong_delete_file (name_file);
            g_free (name_file);
            name_file = NULL;
      }

      return ((gchar *)name_file);
}

/* Le fichier NAME_INDEX_HTML est analyse:
*  Tous les noms de fichiers seront passes en parametre a: pocheget_get_file (char *filename, int cpt)
*  pour recuperation et seront sauvegardes
*/
void pocheweb_analyze_file_html (void)
{
      size_t   cpt = 1;
      size_t   i;
      size_t   lenfile = 0;
      gchar   *buffer = NULL;
      gchar   *result = g_malloc0 (sizeof (char) * (BUFFER_SIZE + 10));
      FILE    *fp;
      gchar   *ptr = NULL;
      gchar   *ptr1 = NULL;
      gchar   *ptr_name = NULL;
      gint     TypeCode = 0;
      
      /*PRINT_FUNC_LF();*/

      nameindexhtml = g_strdup_printf ("%s/index.html", poche.Path_pochette);

      lenfile = infosong_get_size_file (nameindexhtml);
      if (lenfile <= 100) {
            g_print ("ERREUR TAILLE DE %s = %d\n", nameindexhtml, (gint)lenfile);
            g_free (nameindexhtml);
            nameindexhtml = NULL;
            return;
      }
      buffer = g_malloc0 (sizeof (char) * (lenfile + 1024));

      fp = fopen (nameindexhtml, "r");
      fread (buffer, 1, lenfile, fp);
      fclose (fp);

      ptr = buffer;
      while (*ptr) ptr ++;

      if (strstr (buffer, "/images?q=tbn:")) {
            TypeCode = 0;
            g_print ("\n");
            PRINT_FUNC_LF();
            g_print ("\tRecup des infos web via: \"/images?q=tbn:\"\n");
            g_print ("\n");
      } else if (strstr (buffer, "M:\",\"http")) {
            TypeCode = 1;
            g_print ("\n");
            PRINT_FUNC_LF();
            g_print ("\tRecup des infos web via: \"M:\",\"http\"\n");
            g_print ("\n");
      } else {
            g_free (buffer);
            buffer = NULL;
            g_free (result);
            result = NULL;
            infosong_delete_file (nameindexhtml);
            g_free (nameindexhtml);
            nameindexhtml = NULL;
            
            return;
      }
      
      ptr = buffer;
      while ((ptr - buffer) < lenfile) {
            /*if ((ptr = strstr (ptr + 1, "/images?q=tbn:"))) {*/
            /*if ((ptr = strstr (ptr + 1, "M:\",\"http"))) {*/
            
            if ((ptr = strstr (ptr + 1, TypeCode == 0 ? "/images?q=tbn:" : "M:\",\"http"))) {
            
                  if ((ptr = strstr (ptr, "http:"))) {
                        for (i=0; i < BUFFER_SIZE; i++) result [ i ] = '\0';
                        ptr1 = ptr;
                        
                        if (TypeCode == 0) {
                              while (*ptr1 != ' ') ptr1 ++;
                        }
                        else if (TypeCode == 1) {
                              while (*ptr1 != '\"') ptr1 ++;
                        }
                        
                        strncpy (result, ptr, ptr1 - ptr);
                        /*
                        g_print ("%s\n", result);
                        g_print ("\tPOS: %05d\n", ptr - buffer);
                        g_print ("\tCPT: %02d \n", cpt);
                        */
                        if ((ptr_name = pocheweb_get_on_web_file_img (result))) {
                              if (pocheweb_event.name) {
                                    g_free (pocheweb_event.name);
                                    pocheweb_event.name = NULL;
                              }
                              pocheweb_event.name = g_strdup (ptr_name);
                              pochedir_make_glist (ptr_name);
                        }

                        if (winpulse_event_set_close ()) {
                              g_print ("DEMANDE D'ANNULATION DEPUIS: ");
                              PRINT_FUNC_LF();
                              break;
                        }

                        cpt ++;
                  }
            }
            if ((ptr - buffer) >= lenfile -100) break;
      }
      g_free (buffer);
      buffer = NULL;
      g_free (result);
      result = NULL;
      infosong_delete_file (nameindexhtml);
      g_free (nameindexhtml);
      nameindexhtml = NULL;
}

/* Recuperation du fichier *.html et sauvegarde
*/
gboolean pocheweb_recup_file_html (gchar *cd_name)
{
      /*FILE      *index;*/
      gchar     *name = NULL;
      gchar     *ptrname = NULL;
      gboolean   bool_ret = FALSE;
      gchar     *PtrNew = NULL;
      gchar     *Ptr = NULL;
      gchar     *LineCommand = NULL;

      /*PRINT_FUNC_LF();*/

      /* Construction du path */
      Ptr = PtrNew = g_strdup_printf ("http://images.google.com/images?q=%s", cd_name);
      /* Chaine de transformation du nom */
      ptrname = name = g_strnfill (1024, '\0');
/*
http://images.google.com/images?q=Dire   Straits   Brothers   In   Arms+Dire   Straits   Brothers   in   arms
http://images.google.com/images?q=Dire%20Straits%20Brothers%20In%20Arms+Dire%20Straits%20Brothers%20in%20arms
*/

      while (*Ptr) {
            if (*Ptr == ' ') {
                  *ptrname ++ = '%';
                  *ptrname ++ = '2';
                  *ptrname ++ = '0';
                  Ptr ++;
            }
            else {
                  *ptrname ++ = *Ptr ++;
            }
      }

      g_free (PtrNew);
      PtrNew = Ptr = NULL;

/*g_print ("\n---------NAME = %s\n\n", name);*/

      nameindexhtml = g_strdup_printf ("%s/index.html", poche.Path_pochette);
      LineCommand = g_strdup_printf ("wget --user-agent=\"Mozilla 22.0\" -O %s %s", nameindexhtml, name);
g_print("LineCommand=%s\n", LineCommand);
      system (LineCommand);
      g_free (LineCommand);
      LineCommand = NULL;

      bool_ret = (infosong_file_is_reg (nameindexhtml) && infosong_get_size_file (nameindexhtml) > 0);

      g_free (name);
      name = NULL;

      return (bool_ret);
}

static void pocheweb_thread (void *arg)
{
      gchar *ptr_cdname = NULL;

      /*PRINT_FUNC_LF();*/

      ptr_cdname = pocheevnt_get_comboboxentry_cdaudio ();
      /*g_print ("PTR_CDNAME = %s\n", ptr_cdname);*/
      if (pocheweb_recup_file_html (ptr_cdname) == TRUE) {
            pocheweb_analyze_file_html ();
      }

      pocheweb_event.EndTimeout = TRUE;
      pocheweb_event.EndThread = TRUE;

      secu_thread_sub ();
      pthread_exit(0);
}

gboolean pocheweb_bool_file_add (void)
{
      return (pocheweb_tt_glist < g_list_length (poche.glist) ? TRUE : FALSE);
}

static gint pocheweb_timeout (gpointer data)
{
      if (pocheweb_event.name) {
            winpulse_set_labelfile (pocheweb_event.name);
            g_free (pocheweb_event.name);
            pocheweb_event.name = NULL;
      }
      else if (pocheweb_event.EndThread == TRUE) {
            pocheevnt_table_add_images ();
            pocheevnt_set_flag_buttons_recto ();
            pocheevnt_set_flag_buttons_verso ();
            gtk_timeout_remove (pocheweb_event.timeout);
            winpulse_close ();
            if (pocheweb_bool_file_add () == FALSE) {
                  wininfo_create (
                        _("Aucun fichier trouve !"),
                        _("    Pour resoudre ce probleme:"),
                          "\n",
                          "    -----------------------------------\n",
                        _("    Modifiez la chaine de recherche et"),
                          "            \n",
                        _("    recommencez."),
                          "     \n\n",
                          "");
            }
      }
      return (TRUE);
}

void  pocheweb_get (void)
{
      /*PRINT_FUNC_LF();*/

      pocheweb_tt_glist = g_list_length (poche.glist);
      
      winpulse_create ();
      winpulse_set_init_pulse ();
      winpulse_set_title (_("Chargement des images"));
      winpulse_set_labeltitle (_("Chargement des images"));
      pocheweb_event.name = g_strdup (_("[Recherche des fichiers images]"));
      winpulse_set_labelfile (pocheweb_event.name);

      pocheweb_event.name = NULL;
      pocheweb_event.EndThread = FALSE;
      pocheweb_event.EndTimeout = FALSE;
      pocheweb_event.timeout = gtk_timeout_add (10, pocheweb_timeout, 0);
      secu_thread_add ();
      pthread_create (&pocheweb_event.nmr_tid, NULL ,(void *)pocheweb_thread, (void *)NULL);
}









Generated by  Doxygen 1.6.0   Back to index