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



hi!

Daniel Drake <dan reactivated net> writes:


> If we can get the code simple/clean enough then it should be up for consideration.

ok, i've addressed the problems you mentioned below, i just copied most of the
code from other functions, therefore missed the deleting stuff. i suppose
gettext is for i18n? i'm not so sure about the position of the button and the
text on it at all, therefore i haven't changed that yet. 

i also found a minor bug in line 2159 of cddb.c

instead of 
    file_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(BrowserList));
    file_selectedcount = gtk_tree_selection_count_selected_rows(selection);
it should be
    file_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(BrowserList));
    file_selectedcount = gtk_tree_selection_count_selected_rows(file_selection);

else one can crash easytag by selecting all files in the cddb view and
unselecting all files in the browser view and then clicking apply in the cddb
window.


>> has anybody implemented (or is implementing) the parsing of cddb records of
>> albums with various artists. quote from freedb.org faq:
>> 
>>>   6. When submitting a sampler or compilation, you should include the
>>>   track-artist in the track-name, using the syntax "artist / track-title" and
>>>   set the CD-artist to "Various"
>> it would be nice to tag mp3s from such records with easytag.
>
> This is also a good idea, fancy writing a patch? :)

i'll think about it, but unfortunately people submitting cddb data often don't
stick to the "rules" :(, therefore i'm not sure, if it is worth implementing
this. (retagged today a lot of my mp3s, and saw a lot of strange cddb records)

--- easytag-0.31_gtk2.4_pre1/src/cddb.c	2004-07-16 00:07:21 +0200
+++ easytag-0.31_gtk2.4_pre1-mod/src/cddb.c	2004-08-20 19:44:41 +0200
@@ -91,10 +91,11 @@ GtkWidget *CddbAlbumListView = NULL;
 GtkListStore *CddbAlbumListModel = NULL;
 GtkWidget *CddbTrackListView = NULL;
 GtkListStore *CddbTrackListModel = NULL;
 GtkWidget *CddbApplyButton = NULL;
 GtkWidget *CddbSearchButton = NULL;
+GtkWidget *CddbSearchSelectedButton = NULL;
 GtkWidget *CddbStatusBar;
 guint      CddbStatusBarContext;
 
 GtkWidget *CddbStopSearchButton;
 GtkWidget *CddbSearchStringInResultNextButton;
@@ -122,10 +123,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_Selected_Files (void);
 gboolean Cddb_Get_Album_Tracks_List_CB(GtkTreeSelection *selection, gpointer data);
 gboolean Cddb_Get_Album_Tracks_List(GtkTreeSelection *selection);
 
 void     Cddb_Load_Album_List              (gboolean only_red_lines);
 void     Cddb_Load_Track_Album_List        (GList *track_list);
@@ -254,10 +256,17 @@ void Open_Cddb_Window (void)
     GTK_WIDGET_SET_FLAGS(CddbSearchButton,GTK_CAN_DEFAULT);
     gtk_widget_grab_default(CddbSearchButton);
     g_signal_connect(G_OBJECT(CddbSearchButton),"clicked", G_CALLBACK(Cddb_Search_Album_List_From_String),NULL);
     g_signal_connect(G_OBJECT(GTK_ENTRY(GTK_BIN(CddbSearchStringCombo)->child)),"changed", G_CALLBACK(Cddb_Set_Search_Button_Sensivity),NULL);
 
+    // Button to generate/run the search directly from the file lengths
+    CddbSearchSelectedButton = gtk_button_new_with_label("Search currently marked");
+    gtk_box_pack_start(GTK_BOX(hbox),CddbSearchSelectedButton,FALSE,FALSE,0);
+    GTK_WIDGET_SET_FLAGS(CddbSearchSelectedButton,GTK_CAN_DEFAULT);
+    gtk_widget_grab_default(CddbSearchSelectedButton);
+    g_signal_connect(GTK_OBJECT(CddbSearchSelectedButton),"clicked",G_CALLBACK (Cddb_Search_Album_From_Selected_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);
     g_signal_connect(G_OBJECT(Button),"clicked", G_CALLBACK(Cddb_Destroy_Window),NULL);
@@ -1785,10 +1794,265 @@ gboolean Cddb_Search_Album_List_From_Str
     Cddb_Load_Album_List(FALSE);
 
     return TRUE;
 }
 
+gboolean Cddb_Search_Album_From_Selected_Files (void)
+{
+
+  gint   socket_id;
+  gint   bytes_written;
+  gint   bytes_read;
+  gulong bytes_read_total = 0;
+
+  gchar *cddb_in;               /* For the request to send */
+  gchar *cddb_out;              /* Answer received */
+  gchar *cddb_out_tmp;
+  gchar *msg;
+  gchar* tmp;
+  gchar* query_string;
+  gchar* cddb_discid;
+
+  guint total_frames = 150;     /* first offset is (almost) always 150 */
+  guint disc_length = 0;
+
+  GtkTreeSelection *file_selection = NULL;
+  guint file_selectedcount;
+  GtkTreeIter  currentIter;
+  guint total_id;
+  guint num_tracks;
+
+  gpointer iterptr;
+
+  GtkListStore *fileListModel;
+  GtkTreeIter *fileIter;
+  GList *file_iterlist = NULL;
+
+  // 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;
+  }
+
+  fileListModel =
+    GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(BrowserList)));
+  file_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(BrowserList));
+  file_selectedcount = gtk_tree_selection_count_selected_rows(file_selection);
+
+  if (file_selectedcount > 0)
+  {
+    GList* file_selectedrows =
+      gtk_tree_selection_get_selected_rows(file_selection, NULL);
+
+
+    while (file_selectedrows) {
+      iterptr = g_malloc0(sizeof(GtkTreeIter));
+      gtk_tree_model_get_iter(GTK_TREE_MODEL(fileListModel),
+                              (GtkTreeIter*) iterptr,
+                              (GtkTreePath*) file_selectedrows->data);
+      file_iterlist = g_list_append(file_iterlist, iterptr);
+
+      if (!file_selectedrows->next) break;
+      file_selectedrows = file_selectedrows->next;
+    }
+    g_list_foreach(file_selectedrows, (GFunc)gtk_tree_path_free, NULL);
+    g_list_free(file_selectedrows);
+
+  } else /* No rows selected, use the whole list */
+  {
+    gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fileListModel), &currentIter);
+
+    do
+    {
+      iterptr = g_memdup(&currentIter, sizeof(GtkTreeIter));
+      file_iterlist = g_list_append(file_iterlist, iterptr);
+    } while (gtk_tree_model_iter_next(
+                 GTK_TREE_MODEL(fileListModel), &currentIter));
+
+    file_selectedcount =
+      gtk_tree_model_iter_n_children(GTK_TREE_MODEL(fileListModel), NULL);
+  }
+
+
+  if (file_selectedcount == 0)
+    return TRUE;
+
+
+  // generate query string and compute discid
+  total_id = 0;
+  num_tracks = file_selectedcount;
+  query_string = g_strdup ("");
+  while (file_iterlist)
+  {
+    gulong secs = 0;
+    fileIter = (GtkTreeIter*) file_iterlist->data;
+    ET_File   *etfile   = Browser_List_Get_ETFile_From_Iter(fileIter);
+    tmp = query_string;
+    query_string = g_strdup_printf ("%s+%d", query_string, total_frames);
+    g_free (tmp);
+    secs = etfile->ETFileInfo->duration;
+    total_frames +=  secs * 75;
+    disc_length += secs;
+    while (secs > 0) {
+      total_id = total_id + (secs % 10);
+      secs = secs / 10;
+    }
+    if (!file_iterlist->next) break;
+
+    file_iterlist = file_iterlist->next;
+  }
+
+  g_list_foreach(file_iterlist, (GFunc)g_free, NULL);
+  g_list_free(file_iterlist);
+
+
+  cddb_discid =
+    g_strdup_printf("%08x",(guint)(((total_id % 0xFF) << 24) |
+                                   (disc_length << 8) | num_tracks));
+
+  cddb_in = g_strdup_printf("GET http://%s%s?cmd=cddb+query+%s+%d+%s";,
+                            CDDB_SERVER_NAME,CDDB_SERVER_CGI_PATH,
+                            cddb_discid,
+                            num_tracks, query_string);
+  g_free (cddb_discid);
+  g_free (query_string);
+
+  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_list_store_clear(CddbAlbumListModel);
+  gtk_list_store_clear(CddbTrackListModel);
+  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);
+  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;
+
+    if ( cddb_out != NULL && strstr(cddb_out_tmp,"/") != NULL)
+    {
+      gchar* ptr;
+      gchar* copy;
+      gchar* valid;
+
+      CddbAlbum *cddbalbum;
+
+      cddbalbum = g_malloc0(sizeof(CddbAlbum));
+
+      // Get album category
+      ptr = strstr (cddb_out_tmp, " ");
+      *ptr = 0;
+      copy = g_strdup(cddb_out_tmp);
+
+      if(g_utf8_validate(copy, -1, NULL)) {
+        valid = copy;
+      } else {
+        valid = convert_string(copy, "iso-8859-1", "utf-8");
+        g_free(copy);
+      }
+
+      cddbalbum->category = valid;
+      *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.
+      copy = g_strdup(cddb_out_tmp);
+
+      if(g_utf8_validate(copy, -1, NULL)) {
+        valid = copy;
+      } else {
+        valid = convert_string(copy, "iso-8859-1", "utf-8");
+        g_free(copy);
+      }
+
+      cddbalbum->artist_album = valid;
+
+      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 List.
  * We get the list of tracks of the selected album
  */
@@ -2154,11 +2418,11 @@ gboolean Cddb_Set_Track_Infos_To_File_Li
         CddbTrackList_Line_Selected = FALSE;
         rows_to_loop = list_length;
     }
 
     file_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(BrowserList));
-    file_selectedcount = gtk_tree_selection_count_selected_rows(selection);
+    file_selectedcount = gtk_tree_selection_count_selected_rows(file_selection);
 
     if (file_selectedcount > 0)
     {
         /* Rows are selected in the file list, apply tags to them only */
         file_selectedrows = gtk_tree_selection_get_selected_rows(file_selection, NULL);
ciao
  -justus



-- 
int m,o,O=0;float l,I,_;main(){for(;1840-O;putchar((++O>907&&936>O?61-m:o)
["\rX-#!*X'bc)jrs)inG}sufpodt'''trstrM"]^7))for(o=I=l=0;79-(m=O%80)&&_*l+I*
I<6&&26-++o;I=2*l*I+O/80*.09-1,l=_)_=l*l-I*I-2+m/27.;}


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