Re: [Easytag-mailing] [PATCH] generate cddb query from song lengths



Hi Justus,

Before going on vacation, I've started to develop the same thing (on the gtk-1.2 version), but not terminated. Now I'm back and I'll check the patches supplied in the earlier mails to implement in my current sources (gtk2)

Thanks to alls,
Jerome



justus schwartz wrote:
hi!

is there any interest in a function, that takes the currently marked files and
generates a cddb query from them? (certainly only works for complete albums)
i did a quick implementation (by just copy pasting from the
Cddb_Search_Album_List_From_String function). i reused the Cddb_Generate_Cddb_Id
function wich was present, but haven't seen, where it is used.

i've added a button to the cddb search window, but i'm not at all a gui
programmer :) (the label is somewhat misleading, as i take all currently
displayed files if none are selected)

the patch is attached (against easytag-0.31, there is no cvs, is there?) so test
for yourselve, if you think it's useful.
ciao
  -justus




------------------------------------------------------------------------

--- easytag-0.31/src/cddb.c	2004-03-22 23:12:28 +0100
+++ /home/justus/sources/easytag-0.31/src/cddb.c	2004-08-12 22:04:17 +0200
@@ -57,10 +57,11 @@ GList *CddbAlbumList = NULL;
 GtkWidget *CddbSearchStringEntry = NULL;
 GtkWidget *CddbAlbumCList = NULL;
 GtkWidget *CddbTrackCList = NULL;
 GtkWidget *CddbApplyButton = NULL;
 GtkWidget *CddbSearchButton = NULL;
+GtkWidget *CddbSearchMarkedButton = NULL;
 GtkWidget *CddbSearchStringInResultEntry;
 GtkWidget *CddbStatusBar;
 guint      CddbStatusBarContext;
GtkWidget *CddbStopSearchButton;
@@ -90,10 +91,11 @@ void Cddb_Close_Connection (gint socket_
 gint Cddb_Read_Line        (gint socket_id, gchar *cddb_out);
 gint Cddb_Read_Http_Header (gint socket_id, gchar *cddb_out);
 gint Cddb_Read_Cddb_Header (gint socket_id, gchar *cddb_out);
gboolean Cddb_Search_Album_List_From_String (void);
+gboolean Cddb_Search_Album_From_Marked_Files (void);
 gboolean Cddb_Get_Album_Tracks_List_CB     (GtkCList *clist, gint row, gint column);
 gboolean Cddb_Get_Album_Tracks_List        (GtkCList *clist, gint row, gint column);
void Cddb_Load_Album_List (gboolean only_red_lines);
 void     Cddb_Load_Track_Album_List        (GList *track_list);
@@ -241,10 +243,20 @@ void Open_Cddb_Window (void)
     gtk_widget_grab_default(CddbSearchButton);
     gtk_signal_connect(GTK_OBJECT(CddbSearchButton),"clicked",(GtkSignalFunc)Cddb_Search_Album_List_From_String,NULL);
     gtk_signal_connect_object(GTK_OBJECT(CddbSearchButton),"clicked",(GtkSignalFunc)Add_To_Combo_Box_History,GTK_OBJECT(CddbSearchStringEntry));
     gtk_signal_connect(GTK_OBJECT(GTK_COMBO(CddbSearchStringEntry)->entry),"changed",(GtkSignalFunc)Cddb_Set_Search_Button_Sensivity,NULL);
+
+    // Button to generate/run the search directly from the file lengths
+    CddbSearchMarkedButton = gtk_button_new_with_label("Search currently marked");
+
+    gtk_box_pack_start(GTK_BOX(hbox),CddbSearchMarkedButton,FALSE,FALSE,0);
+    GTK_WIDGET_SET_FLAGS(CddbSearchMarkedButton,GTK_CAN_DEFAULT);
+    gtk_widget_grab_default(CddbSearchMarkedButton);
+    gtk_signal_connect(GTK_OBJECT(CddbSearchMarkedButton),"clicked",(GtkSignalFunc)Cddb_Search_Album_From_Marked_Files,NULL);
+
+
     // Button to quit
     Button = Create_Button_With_Pixmap(BUTTON_CLOSE);
     gtk_box_pack_end(GTK_BOX(hbox),Button,FALSE,FALSE,0);
     GTK_WIDGET_SET_FLAGS(Button,GTK_CAN_DEFAULT);
     gtk_signal_connect(GTK_OBJECT(Button),"clicked",(GtkSignalFunc)Cddb_Destroy_Window,NULL);
@@ -1628,10 +1642,166 @@ gboolean Cddb_Search_Album_List_From_Str
     Cddb_Load_Album_List(FALSE);
return TRUE;
 }
+gboolean Cddb_Search_Album_From_Marked_Files (void)
+{
+  GList *selected_etfilelist;
+  guint selected_etfilelist_length;
+  gchar* cddb_discid;
+  gint   bytes_written;
+  gint   bytes_read;
+  gulong bytes_read_total = 0;
+  gint   socket_id;
+  gchar *cddb_in;    // For the request to send
+  gchar *cddb_out;   // Answer received
+  gchar *cddb_out_tmp;
+  gchar *msg;
+  gchar* tmp;
+  guint total_frames = 150; // first offset is (almost) always 150
+  guint disc_length = 0;
+
+
+
+  // Connection to the server
+  if ( (socket_id=Cddb_Open_Connection(CDDB_USE_PROXY?CDDB_PROXY_NAME:"www.freedb.org",
+                                       CDDB_USE_PROXY?CDDB_PROXY_PORT:80)) <= 0 )
+  {
+    return FALSE;
+  }
+
+
+  if ( (selected_etfilelist = ET_File_List_Get_Selection()) == NULL)
+    selected_etfilelist = g_list_first(ETCore->ETFileDisplayedList);
+  selected_etfilelist_length = g_list_length(selected_etfilelist);
+
+  if (selected_etfilelist_length == 0)
+    return TRUE;
+
+  cddb_discid = Cddb_Generate_Cddb_Id (selected_etfilelist);
+  cddb_in = g_strdup_printf("GET http://%s%s?cmd=cddb+query+%s+%d";,
+                            CDDB_SERVER_NAME,CDDB_SERVER_CGI_PATH,
+                            cddb_discid,
+                            selected_etfilelist_length);
+  g_free (cddb_discid);
+
+
+  while(selected_etfilelist_length--)
+  {
+    ET_File   *etfile   = (ET_File *)selected_etfilelist->data;
+    tmp = cddb_in;
+    cddb_in = g_strdup_printf ("%s+%d", cddb_in, total_frames);
+    total_frames += etfile->ETFileInfo->duration * 75;
+    disc_length += etfile->ETFileInfo->duration;
+    g_free (tmp);
+    selected_etfilelist = selected_etfilelist->next;
+  }
+
+
+  tmp = cddb_in;
+  cddb_in = g_strdup_printf ("%s+%d"
+                             "&hello=noname+localhost+%s+%s"
+                             "&proto=1 HTTP/1.1\r\n"
+                             "Host: %s:%d\r\nConnection: close\r\n\r\n",
+                             tmp, disc_length,
+                             APPNAME,VERSION,
+                             CDDB_SERVER_NAME,CDDB_SERVER_PORT
+                             );
+  g_free (tmp);
+
+  printf ("%s\n", cddb_in);
+
+  gtk_statusbar_push(GTK_STATUSBAR(CddbStatusBar),CddbStatusBarContext,_("Sending request ..."));
+  while (gtk_events_pending()) gtk_main_iteration();
+  if ( (bytes_written=send(socket_id,cddb_in,strlen(cddb_in)+1,0)) < 0)
+  {
+    g_print(_("Can't send the request (%s)!\n"),g_strerror(errno));
+    Cddb_Close_Connection(socket_id);
+    g_free(cddb_in);
+    return FALSE;
+  }
+  g_free(cddb_in);
+
+  // Delete previous album list
+  gtk_clist_clear(GTK_CLIST(CddbAlbumCList));
+  gtk_clist_clear(GTK_CLIST(CddbTrackCList));
+  Cddb_Free_Album_List();
+  gtk_widget_set_sensitive(GTK_WIDGET(CddbStopSearchButton),TRUE);
+
+
+  /*
+   * Read the answer
+   */
+  gtk_statusbar_push(GTK_STATUSBAR(CddbStatusBar),CddbStatusBarContext,_("Receiving data ..."));
+  while (gtk_events_pending()) gtk_main_iteration();
+  cddb_out = g_malloc0(CDDB_ANSWER_LINE_SIZE+1);
+  // Parse server answer : Check returned code in the first line
+  if ( !cddb_out || (bytes_read=Cddb_Read_Http_Header(socket_id,cddb_out)) <= 0 )
+  {
+    msg = g_strdup_printf(_("The server returned a wrong answer! (%s)"),cddb_out);
+    gtk_statusbar_push(GTK_STATUSBAR(CddbStatusBar),CddbStatusBarContext,msg);
+    g_print("%s\n",msg);
+    g_free(msg);
+    return FALSE;
+  }
+  bytes_read_total = bytes_read;
+
+  while ( !CddbStopSearch && (bytes_read=Cddb_Read_Line(socket_id,cddb_out)) > 0 )
+  {
+    bytes_read_total += bytes_read;
+    msg = g_strdup_printf(_("Receiving data (%s) ..."),Convert_Size_1(bytes_read_total));
+    gtk_statusbar_push(GTK_STATUSBAR(CddbStatusBar),CddbStatusBarContext,msg);
+    while (gtk_events_pending()) gtk_main_iteration();
+    g_free(msg);
+
+    cddb_out_tmp = cddb_out;
+    //g_print("%s\n", bytes_read, cddb_out);
+
+    if ( cddb_out != NULL && strstr(cddb_out_tmp,"/") != NULL)
+    {
+      gchar *ptr;
+      CddbAlbum *cddbalbum;
+
+      cddbalbum = g_malloc0(sizeof(CddbAlbum));
+
+      // Get album category
+      ptr = strstr (cddb_out_tmp, " ");
+      *ptr = 0;
+      cddbalbum->category = g_strdup(cddb_out_tmp);
+      *ptr = ' ';
+      cddb_out_tmp = ptr + 1;
+
+
+      // Get album ID
+      ptr = strstr (cddb_out_tmp, " ");
+      *ptr = 0;
+      cddbalbum->id = g_strdup(cddb_out_tmp);
+      *ptr = ' ';
+      cddb_out_tmp = ptr + 1;
+
+      // Get album and artist names.
+      cddbalbum->artist_album = g_strdup(cddb_out_tmp);
+
+      CddbAlbumList = g_list_append(CddbAlbumList,cddbalbum);
+    }
+  }
+  // Close connection
+  Cddb_Close_Connection(socket_id);
+
+  msg = g_strdup_printf(_("Found %d matching album(s)"),g_list_length(CddbAlbumList));
+  gtk_statusbar_push(GTK_STATUSBAR(CddbStatusBar),CddbStatusBarContext,msg);
+  g_free(msg);
+  // Initialize the button
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(CddbDisplayRedLinesButton),FALSE);
+
+  // Load the albums found in the list
+  Cddb_Load_Album_List(FALSE);
+
+  return TRUE;
+}
+
/*
  * Callback when selecting a row in the Album CList.
  * We get the list of tracks of the selected album
  */
@@ -2092,8 +2262,8 @@ gchar *Cddb_Generate_Cddb_Id (GList *etf
numtracks++;
         list = list->next;
     }
- return g_strdup_printf("%08x",(((totalid % 0xFF) << 24) | (totalsecs << 8) | numtracks));
+    return g_strdup_printf("%08x",(guint)(((totalid % 0xFF) << 24) | (totalsecs << 8) | numtracks));
}


------------------------------------------------------------------------



--
EasyTAG - Tag editor for MP3 and Ogg Vorbis files
http://easytag.sourceforge.net
--
Jerome COUDERC <j couderc ifrance com>





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]