[easytag/wip/musicbrainz-support-merge: 14/75] Implement DiscID Search



commit 8fb5bfd3f73dec5155137c20253b176d7fc47255
Author: Abhinav <abhijangda hotmail com>
Date:   Tue Jun 10 21:35:30 2014 +0530

    Implement DiscID Search
    
    Added support for searching DiscID in mb_search.c. Added dependency for
    libdiscid in configure.ac. Added support for displaying DiscID Search
    results in mbentityview.c. Added DiscID search's callback and
    thread_func in musicbrainz_dialog.c.

 configure.ac             |    5 ++-
 src/mb_search.c          |   86 +++++++++++++++++++++++++++++++++++
 src/mb_search.h          |    3 +
 src/mbentityview.c       |    2 +-
 src/musicbrainz_dialog.c |  113 +++++++++++++++++++++++++++++++++++++++++++---
 5 files changed, 201 insertions(+), 8 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2032a2e..b43cb18 100644
--- a/configure.ac
+++ b/configure.ac
@@ -163,6 +163,9 @@ dnl -------------------------------
 dnl libmusicbrainz5
 MUSICBRAINZ_DEPS="libmusicbrainz5 >= 5.0"
 
+dnl libdiscid
+LIBDISCID_DEPS="libdiscid >= 0.2.2"
+
 dnl Patch from Ben Taylor to fix a Solaris bug (for CDDB)
 AC_CHECK_LIB([socket], [recv])
 
@@ -329,7 +332,7 @@ GIO_DEPS="gio-2.0 >= 2.32.0" dnl For g_file_new_tmp()
 GLIB_DEPRECATION_FLAGS="-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_32 
-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_32"
 GTK_DEPS="gtk+-3.0 >= 3.4.0"
 GTK_DEPRECATION_FLAGS="-DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_3_4 -DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_3_4"
-PKG_CHECK_MODULES([EASYTAG], [$MUSICBRAINZ_DEPS $GIO_DEPS $GTK_DEPS $OPUS_DEPS $OGG_DEPS $SPEEX_DEPS 
$FLAC_DEPS $ID3TAG_DEPS $TAGLIB_DEPS $WAVPACK_DEPS])
+PKG_CHECK_MODULES([EASYTAG], [$LIBDISCID_DEPS $MUSICBRAINZ_DEPS $GIO_DEPS $GTK_DEPS $OPUS_DEPS $OGG_DEPS 
$SPEEX_DEPS $FLAC_DEPS $ID3TAG_DEPS $TAGLIB_DEPS $WAVPACK_DEPS])
 
 dnl Check for winsock
 AC_SEARCH_LIBS([gethostbyname], [nsl socket], [],
diff --git a/src/mb_search.c b/src/mb_search.c
index 9634024..03a5ae1 100644
--- a/src/mb_search.c
+++ b/src/mb_search.c
@@ -565,6 +565,92 @@ et_musicbrainz_search (gchar *string, enum MB_ENTITY_TYPE type, GNode *root,
         }
     }
 
+    else if (type == MB_ENTITY_TYPE_DISCID)
+    {
+        param_names[0] = "toc";
+        param_values [0] = "";
+        metadata = mb5_query_query (query, "discid", string, "", 1, param_names,
+                                    param_values);
+        result = mb5_query_get_lastresult (query);
+
+        if (result == eQuery_Success)
+        {
+            if (metadata)
+            {
+                int i;
+                Mb5ReleaseList list;
+                gchar *message;
+                Mb5Disc disc;
+
+                disc = mb5_metadata_get_disc (metadata);
+                list = mb5_disc_get_releaselist (disc);
+                param_names [0] = "inc";
+                param_values [0] = "artists release-groups";
+                message = g_strdup_printf ("Found %d Album(s)",
+                                           mb5_release_list_size (list));
+                et_show_status_msg_in_idle (message);
+                g_free (message);
+
+                for (i = 0; i < mb5_release_list_size (list); i++)
+                {
+                    Mb5Release release;
+
+                    if (g_cancellable_is_cancelled (cancellable))
+                    {
+                        g_set_error (error, ET_MB5_SEARCH_ERROR,
+                                     ET_MB5_SEARCH_ERROR_CANCELLED,
+                                     "Operation cancelled by user");
+                        mb5_query_delete (query);
+                        mb5_metadata_delete (metadata);
+                        g_assert (error == NULL || *error != NULL);
+                        return FALSE;
+                    }
+
+                    release = mb5_release_list_item (list, i);
+
+                    if (release)
+                    {
+                        Mb5Metadata metadata_release;
+                        gchar buf [NAME_MAX_SIZE];
+                        GNode *node;
+                        EtMbEntity *entity;
+                        int size;
+
+                        size = mb5_release_get_title ((Mb5Release)release, buf,
+                                                      sizeof (buf));
+                        buf [size] = '\0';
+                        message = g_strdup_printf ("Retrieving %s (%d/%d)",
+                                                   buf, i,
+                                                   mb5_release_list_size (list));
+                        et_show_status_msg_in_idle (message);
+                        g_free (message);
+
+                        mb5_release_get_id ((Mb5Release)release,
+                                            buf,
+                                            sizeof (buf));
+                        metadata_release = mb5_query_query (query, "release",
+                                                            buf, "",
+                                                            1, param_names,
+                                                            param_values);
+                        entity = g_malloc (sizeof (EtMbEntity));
+                        entity->entity = mb5_release_clone (mb5_metadata_get_release (metadata_release));
+                        entity->type = MB_ENTITY_TYPE_ALBUM;
+                        entity->is_red_line = FALSE;
+                        node = g_node_new (entity);
+                        g_node_append (root, node);
+                        mb5_metadata_delete (metadata_release);
+                    }
+                }
+            }
+
+            mb5_metadata_delete (metadata);
+        }
+        else
+        {
+            goto err;
+        }
+    }
+
     mb5_query_delete (query);
 
     return TRUE;
diff --git a/src/mb_search.h b/src/mb_search.h
index 42b39cb..a69b30e 100644
--- a/src/mb_search.h
+++ b/src/mb_search.h
@@ -50,6 +50,7 @@ GQuark et_mb5_search_error_quark (void);
  * @ET_MB5_SEARCH_ERROR_REQUEST: Request to MusicBrainz Server cannot be made
  * @ET_MB5_SEARCH_ERROR_RESOURCE_NOT_FOUND: Resource user is trying to search is not found
  * @ET_MB5_SEARCH_ERROR_CANCELLED: Operation was cancelled
+ * @ET_MB5_SEARCH_ERROR_DISCID: Cannot find DiscID
  *
  * Errors while searching MusicBrainz Server.
  */
@@ -62,6 +63,7 @@ typedef enum
     ET_MB5_SEARCH_ERROR_REQUEST,
     ET_MB5_SEARCH_ERROR_RESOURCE_NOT_FOUND,
     ET_MB5_SEARCH_ERROR_CANCELLED,
+    ET_MB5_SEARCH_ERROR_DISCID,
 } EtMB5SearchError;
 
 enum MB_ENTITY_TYPE
@@ -70,6 +72,7 @@ enum MB_ENTITY_TYPE
     MB_ENTITY_TYPE_ALBUM,
     MB_ENTITY_TYPE_TRACK,
     MB_ENTITY_TYPE_COUNT,
+    MB_ENTITY_TYPE_DISCID,
 };
 
 typedef struct
diff --git a/src/mbentityview.c b/src/mbentityview.c
index feda8a5..2fe65f8 100644
--- a/src/mbentityview.c
+++ b/src/mbentityview.c
@@ -416,6 +416,7 @@ add_iter_to_list_store (GtkListStore *list_store, GNode *node)
                 break;
 
             case MB_ENTITY_TYPE_COUNT:
+            case MB_ENTITY_TYPE_DISCID:
                 break;
         }
 
@@ -507,7 +508,6 @@ show_data_in_entity_view (EtMbEntityView *entity_view)
                                             tree_filter_visible_func, priv,
                                             NULL);
     g_free (types);
-
     gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->filter);
     g_object_unref (priv->filter);
     add_iter_to_list_store (GTK_LIST_STORE (priv->list_store),
diff --git a/src/musicbrainz_dialog.c b/src/musicbrainz_dialog.c
index 5ee8a76..253cea2 100644
--- a/src/musicbrainz_dialog.c
+++ b/src/musicbrainz_dialog.c
@@ -23,6 +23,7 @@
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #include <glib/gi18n.h>
+#include <discid/discid.h>
 
 #include "easytag.h"
 #include "log.h"
@@ -433,11 +434,9 @@ selected_find_thread_func (GSimpleAsyncResult *res, GObject *obj,
 
     while (iter)
     {
-        if (g_cancellable_is_cancelled (cancellable))
+        if (!et_musicbrainz_search ((gchar *)iter->data, MB_ENTITY_TYPE_ALBUM,
+                                    mb_tree_root, &error, 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,
                                                   thread_data, error);
@@ -445,9 +444,11 @@ selected_find_thread_func (GSimpleAsyncResult *res, GObject *obj,
             return;
         }
 
-        if (!et_musicbrainz_search ((gchar *)iter->data, MB_ENTITY_TYPE_ALBUM,
-                                    mb_tree_root, &error, cancellable))
+        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,
                                                   thread_data, error);
@@ -553,6 +554,96 @@ bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
                         0, "Starting Selected Files Search");
 }
 
+static void
+discid_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_tree_root);
+        mb_tree_root = g_node_new (NULL);
+        return;
+    }
+
+    et_mb_entity_view_set_tree_root (ET_MB_ENTITY_VIEW (entityView),
+                                     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);
+}
+
+static void
+discid_search_thread_func (GSimpleAsyncResult *res, GObject *obj,
+                           GCancellable *cancellable)
+{
+    GError *error;
+    DiscId *disc;
+    gchar *discid;
+
+    error = NULL;
+    disc = discid_new ();
+    g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res),
+                                               FALSE);
+
+    if (!discid_read_sparse (disc, discid_get_default_device (), 0))
+    {
+        /* Error reading disc */
+        g_set_error (&error, ET_MB5_SEARCH_ERROR,
+                     ET_MB5_SEARCH_ERROR_DISCID,
+                     discid_get_error_msg (disc));
+        g_simple_async_report_gerror_in_idle (NULL,
+                                              mb5_search_error_callback,
+                                              NULL, error);
+        return;
+    }
+
+    discid = discid_get_id (disc);
+
+    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);
+        discid_free (disc);
+        return;
+    }
+
+    if (!et_musicbrainz_search (discid, MB_ENTITY_TYPE_DISCID, mb_tree_root,
+                                &error, cancellable))
+    {
+        g_simple_async_report_gerror_in_idle (NULL,
+                                              mb5_search_error_callback,
+                                              NULL, error);
+        discid_free (disc);
+        return;
+    }
+
+    et_mb_entity_view_set_tree_root (ET_MB_ENTITY_VIEW (entityView),
+                                     mb_tree_root);
+    discid_free (disc);
+    g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res),
+                                               TRUE);
+}
+
+static void
+btn_discid_search (GtkWidget *button, gpointer data)
+{
+    mb5_search_cancellable = g_cancellable_new ();
+    gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, "Starting MusicBrainz Search");
+    async_result = g_simple_async_result_new (NULL, discid_search_callback,
+                                              NULL,
+                                              btn_manual_find_clicked);
+    g_simple_async_result_run_in_thread (async_result,
+                                         discid_search_thread_func, 0,
+                                         mb5_search_cancellable);
+}
+
 /*
  * et_open_musicbrainz_dialog:
  *
@@ -612,10 +703,20 @@ et_open_musicbrainz_dialog ()
     g_signal_connect (gtk_builder_get_object (builder, "btnSelectedFind"),
                       "clicked", G_CALLBACK (bt_selected_find_clicked),
                       NULL);
+    g_signal_connect (gtk_builder_get_object (builder, "btnSelectedStop"),
+                      "clicked", G_CALLBACK (btn_manual_stop_clicked),
+                      NULL);
+    g_signal_connect (gtk_builder_get_object (builder, "btnDiscFind"),
+                      "clicked", G_CALLBACK (btn_discid_search),
+                      NULL);
+    g_signal_connect (gtk_builder_get_object (builder, "btnDiscStop"),
+                      "clicked", G_CALLBACK (btn_manual_stop_clicked),
+                      NULL);
     g_signal_connect_after (gtk_builder_get_object (builder, "entryTreeViewSearch"),
                             "changed",
                             G_CALLBACK (entry_tree_view_search_changed),
                             NULL);
+
     /* Fill Values in cb_manual_search_in */
     cb_manual_search_in = GTK_WIDGET (gtk_builder_get_object (builder,
                                                               "cbManualSearchIn"));


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