[easytag/wip/musicbrainz-support] Implemented first level of Automatic Search



commit 0588a82c09c4981cbe3ca8b292c4de5aad0e6160
Author: Abhinav <abhijangda hotmail com>
Date:   Fri Jul 11 21:56:45 2014 +0530

    Implemented first level of Automatic Search

 data/musicbrainz_dialog.ui |   23 ++++++++--
 src/mb_search.c            |   54 ++++++++++++++++++++++++
 src/mb_search.h            |    1 +
 src/mbentityview.c         |   36 ++++++++++++++--
 src/mbentityview.h         |    8 ++++
 src/musicbrainz_dialog.c   |   97 ++++++++++++++++++++++++++++++++++++++------
 6 files changed, 197 insertions(+), 22 deletions(-)
---
diff --git a/data/musicbrainz_dialog.ui b/data/musicbrainz_dialog.ui
index df69716..8ba20ca 100755
--- a/data/musicbrainz_dialog.ui
+++ b/data/musicbrainz_dialog.ui
@@ -1,11 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
-  <object class="GtkImage" id="img_unselect_all">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="stock">easytag-unselect-all</property>
-  </object>
   <object class="GtkImage" id="findimage">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
@@ -21,6 +16,11 @@
     <property name="can_focus">False</property>
     <property name="stock">easytag-red-lines</property>
   </object>
+  <object class="GtkImage" id="img_unselect_all">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="stock">easytag-unselect-all</property>
+  </object>
   <object class="GtkDialog" id="mbDialog">
     <property name="can_focus">False</property>
     <property name="border_width">5</property>
@@ -151,6 +151,19 @@
                       </packing>
                     </child>
                     <child>
+                      <object class="GtkButton" id="btnAutomaticSearch">
+                        <property name="label" translatable="yes">Automatic Search</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="receives_default">True</property>
+                      </object>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">True</property>
+                        <property name="position">1</property>
+                      </packing>
+                    </child>
+                    <child>
                       <object class="GtkButton" id="btnDiscFind">
                         <property name="label" translatable="yes">DiscID Search</property>
                         <property name="visible">True</property>
diff --git a/src/mb_search.c b/src/mb_search.c
index c1f463f..502e05a 100755
--- a/src/mb_search.c
+++ b/src/mb_search.c
@@ -721,6 +721,60 @@ et_musicbrainz_search (gchar *string, MbEntityKind type, GNode *root,
             goto err;
         }
     }
+    else if (type == MB_ENTITY_KIND_FREEDBID)
+    {
+        param_values [0] = g_strconcat ("discid:", string, NULL);
+        metadata = mb5_query_query (query, "freedb", "", "", 2, param_names,
+                                    param_values);
+        result = mb5_query_get_lastresult (query);
+        g_free (param_values [0]);
+
+        if (result == eQuery_Success)
+        {
+            if (metadata)
+            {
+                int i;
+                Mb5FreeDBDiscList list;
+                gchar *message;
+
+                list = mb5_metadata_get_freedbdisclist (metadata);
+                message = g_strdup_printf (_("Found %d Result(s)"),
+                                           mb5_freedbdisc_list_size (list));
+#ifndef TEST
+                //et_show_status_msg_in_idle (message);
+#endif
+                g_free (message);
+
+                for (i = 0; i < mb5_freedbdisc_list_size (list); i++)
+                {
+                    Mb5FreeDBDisc freedbdisc;
+
+                    CHECK_CANCELLED(cancellable);
+                    freedbdisc = mb5_freedbdisc_list_item (list, i);
+
+                    if (freedbdisc)
+                    {
+                        gchar buf [NAME_MAX_SIZE];
+                        GNode *node;
+                        EtMbEntity *entity;
+                        int size;
+
+                        size = mb5_freedbdisc_get_title (freedbdisc,
+                                                         buf, sizeof (buf));
+                        buf [size] = '\0';
+                        entity = g_malloc (sizeof (EtMbEntity));
+                        entity->entity = mb5_freedbdisc_clone (freedbdisc);
+                        entity->type = MB_ENTITY_KIND_FREEDBID;
+                        entity->is_red_line = FALSE;
+                        node = g_node_new (entity);
+                        g_node_append (root, node);
+                    }
+                }
+            }
+
+            mb5_metadata_delete (metadata);
+        }
+    }
 
     mb5_query_delete (query);
     CHECK_CANCELLED(cancellable);
diff --git a/src/mb_search.h b/src/mb_search.h
index 4155a4b..181fa68 100755
--- a/src/mb_search.h
+++ b/src/mb_search.h
@@ -76,6 +76,7 @@ typedef enum _MbEntityKind
     MB_ENTITY_KIND_ARTIST = 0,
     MB_ENTITY_KIND_ALBUM,
     MB_ENTITY_KIND_TRACK,
+    MB_ENTITY_KIND_FREEDBID,
     MB_ENTITY_KIND_COUNT,
     MB_ENTITY_KIND_DISCID,
 } MbEntityKind;
diff --git a/src/mbentityview.c b/src/mbentityview.c
index 173ff60..73e97b5 100755
--- a/src/mbentityview.c
+++ b/src/mbentityview.c
@@ -41,10 +41,11 @@ G_DEFINE_TYPE (EtMbEntityView, et_mb_entity_view, GTK_TYPE_BOX)
  * Declaration *
  ***************/
 
-char *columns [MB_ENTITY_KIND_COUNT][8] = {
+char *columns [MB_ENTITY_KIND_COUNT][10] = {
     {"Name", "Gender", "Type"},
     {"Name", "Artist", "Type"},
     {"Name", "Album", "Artist", "Time"},
+    {"FreeDB ID", "Title", "Artist"}
     };
 
 /*
@@ -473,6 +474,27 @@ add_iter_to_list_store (GtkListStore *list_store, GNode *node)
                 break;
             }
 
+            case MB_ENTITY_KIND_FREEDBID:
+            {
+                gchar freedbid [NAME_MAX_SIZE];
+                gchar title [NAME_MAX_SIZE];
+                gchar artist [NAME_MAX_SIZE];
+
+                mb5_freedbdisc_get_artist ((Mb5FreeDBDisc)entity,
+                                           artist, sizeof (artist));
+                mb5_freedbdisc_get_title ((Mb5FreeDBDisc)entity,
+                                          title, sizeof (title));
+                mb5_freedbdisc_get_id ((Mb5FreeDBDisc)entity,
+                                       freedbid, sizeof (freedbid));
+                gtk_list_store_insert_with_values (list_store, &iter, -1,
+                                                   MB_FREEDBID_COLUMNS_ID,
+                                                   freedbid,
+                                                   MB_FREEDBID_COLUMNS_NAME,
+                                                   title,
+                                                   MB_FREEDBID_COLUMNS_ARTIST,
+                                                   artist, -1);
+            }
+
             case MB_ENTITY_KIND_COUNT:
             case MB_ENTITY_KIND_DISCID:
                 break;
@@ -542,6 +564,10 @@ show_data_in_entity_view (EtMbEntityView *entity_view)
             total_cols = MB_TRACK_COLUMNS_N;
             break;
 
+        case MB_ENTITY_KIND_FREEDBID:
+            total_cols = MB_FREEDBID_COLUMNS_N;
+            break;
+
         default:
             total_cols = 0;
     }
@@ -845,7 +871,7 @@ search_in_levels (EtMbEntityView *entity_view, GNode *child,
 
     if (((EtMbEntity *)child->data)->type ==
         MB_ENTITY_KIND_TRACK)
-    {printf ("sdsd\n");
+    {
         return;
     }
 
@@ -969,6 +995,10 @@ et_mb_entity_view_set_tree_root (EtMbEntityView *entity_view, GNode *treeRoot)
                 gtk_button_set_label (GTK_BUTTON (btn), _("Tracks"));
                 break;
 
+            case MB_ENTITY_KIND_FREEDBID:
+                gtk_button_set_label (GTK_BUTTON (btn), _("FreeDB Disc"));
+                break;
+
             default:
                 break;
         }
@@ -1191,11 +1221,9 @@ void
 et_mb_entity_view_refresh_current_level (EtMbEntityView *entity_view)
 {
     EtMbEntityViewPrivate *priv;
-    EtMbEntity *et_entity;
     GNode *child;
 
     priv = ET_MB_ENTITY_VIEW_GET_PRIVATE (entity_view);
-    et_entity = priv->mb_tree_current_node->data;
 
     /* Delete Current Data */
     et_mb_entity_view_clear_all (entity_view);
diff --git a/src/mbentityview.h b/src/mbentityview.h
index 7a90c94..0e72b54 100755
--- a/src/mbentityview.h
+++ b/src/mbentityview.h
@@ -98,6 +98,14 @@ enum MB_TRACK_COLUMNS
     MB_TRACK_COLUMNS_N
 };
 
+enum MB_FREEDBID_COLUMNS
+{
+    MB_FREEDBID_COLUMNS_ID,
+    MB_FREEDBID_COLUMNS_NAME,
+    MB_FREEDBID_COLUMNS_ARTIST,
+    MB_FREEDBID_COLUMNS_N
+};
+
 /**************
  * Prototypes *
  **************/
diff --git a/src/musicbrainz_dialog.c b/src/musicbrainz_dialog.c
index 4470f05..9353bc9 100755
--- a/src/musicbrainz_dialog.c
+++ b/src/musicbrainz_dialog.c
@@ -26,6 +26,7 @@
 #include <gdk/gdkkeysyms.h>
 #include <glib/gi18n.h>
 #include <discid/discid.h>
+#include <openssl/ssl.h>
 
 #include "gtk2_compat.h"
 #include "easytag.h"
@@ -838,7 +839,8 @@ discid_search_thread_func (GSimpleAsyncResult *res, GObject *obj,
         return;
     }
 
-    if (!et_musicbrainz_search (discid, MB_ENTITY_KIND_DISCID, mb_dialog_priv->mb_tree_root,
+    if (!et_musicbrainz_search (discid, MB_ENTITY_KIND_DISCID,
+                                mb_dialog_priv->mb_tree_root,
                                 &error, cancellable))
     {
         g_simple_async_report_gerror_in_idle (NULL,
@@ -848,8 +850,6 @@ discid_search_thread_func (GSimpleAsyncResult *res, GObject *obj,
         return;
     }
 
-    et_mb_entity_view_set_tree_root (ET_MB_ENTITY_VIEW (entityView),
-                                     mb_dialog_priv->mb_tree_root);
     discid_free (disc);
     g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res),
                                                TRUE);
@@ -877,7 +877,70 @@ btn_close_clicked (GtkWidget *button, gpointer data)
     gtk_dialog_response (GTK_DIALOG (mbDialog), GTK_RESPONSE_DELETE_EVENT);
 }
 
-#if 0
+static void
+freedbid_search_callback (GObject *source, GAsyncResult *res,
+                          gpointer user_data)
+{
+    if (!g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res)))
+    {
+        g_object_unref (res);
+        free_mb_tree (&mb_dialog_priv->mb_tree_root);
+        mb_dialog_priv->mb_tree_root = g_node_new (NULL);
+        g_free (user_data);
+        return;
+    }
+
+    et_mb_entity_view_set_tree_root (ET_MB_ENTITY_VIEW (entityView),
+                                     mb_dialog_priv->mb_tree_root);
+    gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, _("Searching Completed"));
+    g_object_unref (res);
+    g_free (user_data);
+    et_music_brainz_dialog_stop_set_sensitive (FALSE);
+
+    if (exit_on_complete)
+    {
+        btn_close_clicked (NULL, NULL);
+    }
+}
+
+static void
+freedbid_search_thread_func (GSimpleAsyncResult *res, GObject *obj,
+                             GCancellable *cancellable)
+{
+    GError *error;
+    gchar *freedbid;
+
+    error = NULL;
+    g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res),
+                                               FALSE);
+    freedbid = g_async_result_get_user_data (G_ASYNC_RESULT (res));
+
+    if (g_cancellable_is_cancelled (cancellable))
+    {
+        g_set_error (&error, ET_MB5_SEARCH_ERROR,
+                     ET_MB5_SEARCH_ERROR_CANCELLED,
+                     _("Operation cancelled by user"));
+        g_simple_async_report_gerror_in_idle (NULL,
+                                              mb5_search_error_callback,
+                                              NULL, error);
+        return;
+    }
+
+    if (!et_musicbrainz_search (freedbid, MB_ENTITY_KIND_FREEDBID,
+                                mb_dialog_priv->mb_tree_root,
+                                &error, cancellable))
+    {
+        g_simple_async_report_gerror_in_idle (NULL,
+                                              mb5_search_error_callback,
+                                              NULL, error);
+        return;
+    }
+
+    g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res),
+                                               TRUE);
+}
+
 static void
 btn_automatic_search_clicked (GtkWidget *button, gpointer data)
 {
@@ -886,13 +949,10 @@ btn_automatic_search_clicked (GtkWidget *button, gpointer data)
     int count;
     GList *iter_list;
     GList *l;
-    GHashTable *hash_table;
-    SelectedFindThreadData *thread_data;
     int total_id;
     int num_tracks;
     guint total_frames = 150;
     guint disc_length  = 2;
-    gchar *query_string;
     gchar *cddb_discid;
 
     iter_list = NULL;
@@ -992,10 +1052,21 @@ btn_automatic_search_clicked (GtkWidget *button, gpointer data)
         }
     }
 
-    cddb_discid = g_strdup_printf("%08x",(guint)(((total_id % 0xFF) << 24) |
-                                         (disc_length << 8) | num_tracks));
+    cddb_discid = g_strdup_printf ("%08x", (guint)(((total_id % 0xFF) << 24) |
+                                   (disc_length << 8) | num_tracks));
+
+    mb5_search_cancellable = g_cancellable_new ();
+    gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, _("Starting MusicBrainz Search"));
+    mb_dialog_priv->async_result = g_simple_async_result_new (NULL,
+                                                              freedbid_search_callback,
+                                                              cddb_discid,
+                                                              btn_automatic_search_clicked);
+    g_simple_async_result_run_in_thread (mb_dialog_priv->async_result,
+                                         freedbid_search_thread_func, 0,
+                                         mb5_search_cancellable);
+    et_music_brainz_dialog_stop_set_sensitive (TRUE);
 }
-#endif
 
 void
 et_music_brainz_dialog_stop_set_sensitive (gboolean sensitive)
@@ -1427,9 +1498,9 @@ et_open_musicbrainz_dialog ()
                       "clicked", G_CALLBACK (btn_apply_changes_clicked),
                       NULL);
  
-    //g_signal_connect (gtk_builder_get_object (builder, "btnAutomaticSearch"),
-    //                  "clicked", G_CALLBACK (btn_automatic_search_clicked),
-    //                  NULL);
+    g_signal_connect (gtk_builder_get_object (builder, "btnAutomaticSearch"),
+                      "clicked", G_CALLBACK (btn_automatic_search_clicked),
+                      NULL);
     g_signal_connect_after (gtk_builder_get_object (builder, "entryTreeViewSearch"),
                             "changed",
                             G_CALLBACK (entry_tree_view_search_changed),


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