[goobox] use libcoverart to fetch CD covers, if available



commit 00daf660d83f8dd932379e765fa24a4e9c709c58
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Thu Jun 7 19:42:35 2012 +0200

    use libcoverart to fetch CD covers, if available

 configure.ac            |   26 +++++-
 src/Makefile.am         |    2 +
 src/album-info.c        |    8 ++
 src/dlg-cover-chooser.c |  231 +++++++++++++++++++++++++++++++++++++++++++++--
 src/dlg-cover-chooser.h |   27 ++++--
 src/goo-window.c        |    6 +-
 src/gth-window.c        |    6 +-
 7 files changed, 282 insertions(+), 24 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index fa09cb8..f5d0689 100644
--- a/configure.ac
+++ b/configure.ac
@@ -20,6 +20,7 @@ GTK_REQUIRED=3.4.0
 GSTREAMER_REQUIRED=0.10.12
 LIBNOTIFY_REQUIRED=0.4.3
 LIBMUSICBRAINZ4_REQUIRED=4.0.0
+LIBCOVERART_REQUIRED=1.0.0beta1
 
 dnl ===========================================================================
 
@@ -88,9 +89,31 @@ AC_SUBST(M_LIBS)
 
 dnl ===========================================================================
 dnl
-dnl libnotify
+dnl libcoverart
 dnl
+AC_ARG_ENABLE(libcoverart,[AC_HELP_STRING([--enable-libcoverart],[enable use of libcoverart to fetch CD covers [default=yes]])],, [enable_libcoverart="yes"])
+
+LIBCOVERART_LIBS=""
+LIBCOVERART_CFLAGS=""
+if test x"$enable_libcoverart" = xyes; then
+   AC_MSG_CHECKING(for libcoverart support)
+
+   if pkg-config --atleast-version=$LIBCOVERART_REQUIRED libcoverart; then
+      LIBCOVERART_LIBS=`pkg-config --libs libcoverart`
+      LIBCOVERART_CFLAGS=`pkg-config --cflags libcoverart`
+      AC_DEFINE(HAVE_LIBCOVERART, 1, [Have libcoverart])
+   else
+      enable_libcoverart=no
+   fi
+fi
+AC_MSG_RESULT($enable_libcoverart)
+AC_SUBST(LIBCOVERART_LIBS)
+AC_SUBST(LIBCOVERART_CFLAGS)
 
+dnl ===========================================================================
+dnl
+dnl libnotify
+dnl
 AC_ARG_ENABLE(notification,[AC_HELP_STRING([--enable-notification],[enable current track notification if libnotify is available [default=yes]])],, [enable_notification="yes"])
 AM_CONDITIONAL(ENABLE_NOTIFICATION, test "x$enable_notification" = xyes)
 
@@ -208,4 +231,5 @@ Configuration:
 	Run in place         : ${enable_run_in_place}
 	Enable notification  : ${enable_notification}
 	Enable media keys    : ${enable_media_keys}
+	Use libcoverart      : ${enable_libcoverart}
 "
diff --git a/src/Makefile.am b/src/Makefile.am
index 0f61475..d2bb267 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -18,6 +18,7 @@ INCLUDES =						\
         -DGOO_LIBDIR=\"$(libdir)\"  	         	\
 	-DGOO_UIDIR=\""$(uidir)"\"     			\
 	$(GOO_CFLAGS)					\
+	$(LIBCOVERART_CFLAGS)				\
 	$(LIBNOTIFY_CFLAGS)
 
 BUILT_SOURCES =			\
@@ -101,6 +102,7 @@ goobox_LDADD = 				\
 	$(top_builddir)/copy-n-paste/libeggsmclient.la  \
 	$(GOO_LIBS) 			\
 	$(M_LIBS)			\
+	$(LIBCOVERART_LIBS)		\
 	$(LIBNOTIFY_LIBS)		\
 	$(SYSTEM_LIBS)
 
diff --git a/src/album-info.c b/src/album-info.c
index 0bd20b9..46a04e6 100644
--- a/src/album-info.c
+++ b/src/album-info.c
@@ -325,6 +325,12 @@ album_info_load_from_cache (AlbumInfo  *album,
 	}
 	g_free (path);
 
+	s = g_key_file_get_string (f, "Album", "ID", NULL);
+	if (s != NULL) {
+		album_info_set_id (album, s);
+		g_free (s);
+	}
+
 	s = g_key_file_get_string (f, "Album", "Title", NULL);
 	if (s != NULL) {
 		album_info_set_title (album, s);
@@ -398,6 +404,8 @@ album_info_save_to_cache (AlbumInfo  *album,
 
 	f = g_key_file_new ();
 
+	if (album->id != NULL)
+		g_key_file_set_string (f, "Album", "ID", album->id);
 	if (album->title != NULL)
 		g_key_file_set_string (f, "Album", "Title", album->title);
 	if (album->artist != NULL)
diff --git a/src/dlg-cover-chooser.c b/src/dlg-cover-chooser.c
index b25f18b..d043ed7 100644
--- a/src/dlg-cover-chooser.c
+++ b/src/dlg-cover-chooser.c
@@ -24,6 +24,10 @@
 #include <config.h>
 #include <string.h>
 #include <gtk/gtk.h>
+#if HAVE_LIBCOVERART
+#include <coverart/caa_c.h>
+#endif
+#include "dlg-cover-chooser.h"
 #include "gio-utils.h"
 #include "glib-utils.h"
 #include "gtk-utils.h"
@@ -571,15 +575,20 @@ fetch_data_free (FetchData *data)
 
 
 static void
-fetch_image_data_ready_cb (void     *buffer,
-			   gsize     count,
-			   GError   *error,
-			   gpointer  user_data)
+image_data_ready_for_query_cb (void     *buffer,
+			       gsize     count,
+			       GError   *error,
+			       gpointer  user_data)
 {
 	FetchData *data = user_data;
 
-	if (error == NULL)
+	if ((error == NULL) && (count > 0))
 		goo_window_set_cover_image_from_data (data->window, buffer, count);
+	else
+		fetch_cover_image_from_album_info (data->window,
+						   goo_window_get_album (data->window),
+						   FETCH_COVER_STAGE_AFTER_WEB_SEARCH);
+
 	fetch_data_free (data);
 }
 
@@ -606,7 +615,7 @@ query_ready_cb (void     *buffer,
 		g_load_file_async (file,
 				   G_PRIORITY_DEFAULT,
 				   NULL,
-				   fetch_image_data_ready_cb,
+				   image_data_ready_for_query_cb,
 				   data);
 
 		g_object_unref (file);
@@ -643,6 +652,28 @@ fetch_cover_image_from_name (GooWindow  *window,
 }
 
 
+/* -- fetch_cover_image_from_asin -- */
+
+
+static void
+image_data_ready_for_asin_cb (void     *buffer,
+			      gsize     count,
+			      GError   *error,
+			      gpointer  user_data)
+{
+	FetchData *data = user_data;
+
+	if ((error == NULL) && (count > 0))
+		goo_window_set_cover_image_from_data (data->window, buffer, count);
+	else
+		fetch_cover_image_from_album_info (data->window,
+						   goo_window_get_album (data->window),
+						   FETCH_COVER_STAGE_AFTER_ASIN);
+
+	fetch_data_free (data);
+}
+
+
 void
 fetch_cover_image_from_asin (GooWindow  *window,
 		             const char *asin)
@@ -658,9 +689,195 @@ fetch_cover_image_from_asin (GooWindow  *window,
 	g_load_file_async (file,
 			   G_PRIORITY_DEFAULT,
 			   NULL,
-			   fetch_image_data_ready_cb,
+			   image_data_ready_for_asin_cb,
 			   data);
 
 	g_object_unref (file);
 	g_free (url);
 }
+
+
+/* -- fetch_cover_image_from_album_info -- */
+
+
+#if HAVE_LIBCOVERART
+
+
+typedef struct {
+	GooWindow *window;
+	AlbumInfo *album;
+	guchar    *buffer;
+	gsize      size;
+} GetCoverArtData;
+
+
+static void
+get_cover_art_data_free (GetCoverArtData *data)
+{
+	g_object_unref (data->window);
+	album_info_unref (data->album);
+	g_free (data->buffer);
+	g_free (data);
+}
+
+
+static void
+metadata_get_coverart_thread (GSimpleAsyncResult *result,
+			      GObject            *object,
+			      GCancellable       *cancellable)
+{
+	GetCoverArtData *data;
+	CaaCoverArt      cover_art;
+	CaaImageData     image_data;
+
+	data = g_simple_async_result_get_op_res_gpointer (result);
+
+	cover_art = caa_coverart_new (PACKAGE_NAME "-" PACKAGE_VERSION);
+	image_data = caa_coverart_fetch_front (cover_art, data->album->id);
+	if (image_data != NULL) {
+		data->size = caa_imagedata_size (image_data);
+		data->buffer = g_new (guchar, data->size);
+		memcpy (data->buffer, caa_imagedata_data (image_data), data->size);
+	}
+
+	caa_coverart_delete (cover_art);
+}
+
+
+static void
+metadata_get_coverart (GooWindow           *window,
+		       AlbumInfo           *album,
+		       GCancellable        *cancellable,
+		       GAsyncReadyCallback  callback,
+		       gpointer             user_data)
+{
+	GSimpleAsyncResult *result;
+	GetCoverArtData    *data;
+
+	result = g_simple_async_result_new (NULL,
+	                                    callback,
+	                                    user_data,
+	                                    metadata_get_coverart);
+
+	data = g_new0 (GetCoverArtData, 1);
+	data->window = g_object_ref (window);
+	data->album = album_info_ref (album);
+	data->buffer = NULL;
+	data->size = 0;
+
+	g_simple_async_result_set_op_res_gpointer (result,
+						   data,
+                                                   (GDestroyNotify) get_cover_art_data_free);
+
+	g_simple_async_result_run_in_thread (result,
+					     metadata_get_coverart_thread,
+					     G_PRIORITY_DEFAULT,
+					     cancellable);
+
+	g_object_unref (result);
+}
+
+
+static gboolean
+metadata_get_coverart_finish (GAsyncResult  *result,
+			      guchar       **buffer,
+			      gsize         *count)
+{
+	GetCoverArtData *data;
+
+	g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL, metadata_get_coverart), FALSE);
+
+        data = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
+        if (data->size == 0)
+        	return FALSE;
+
+        *buffer = data->buffer;
+        *count = data->size;
+
+        return TRUE;
+}
+
+
+typedef struct {
+	GooWindow *window;
+	AlbumInfo *album;
+} CoverArtData;
+
+
+static void
+cover_art_data_free (CoverArtData *data)
+{
+	g_object_unref (data->window);
+	album_info_unref (data->album);
+	g_free (data);
+}
+
+
+static void
+metadata_get_coverart_cb (GObject      *source_object,
+			  GAsyncResult *result,
+			  gpointer      user_data)
+{
+	CoverArtData *data = user_data;
+	guchar       *buffer;
+	gsize         size;
+
+	if (metadata_get_coverart_finish (result, &buffer, &size))
+		goo_window_set_cover_image_from_data (data->window,
+						      buffer,
+						      size);
+	else
+		fetch_cover_image_from_album_info (data->window,
+						   data->album,
+						   FETCH_COVER_STAGE_AFTER_LIBCOVERART);
+
+	cover_art_data_free (data);
+}
+
+
+#endif
+
+
+void
+fetch_cover_image_from_album_info (GooWindow       *window,
+				   AlbumInfo       *album,
+				   FetchCoverStage  after_stage)
+{
+	if ((FETCH_COVER_STAGE_AFTER_ASIN > after_stage)
+	    && (album != NULL)
+	    && (album->asin != NULL))
+	{
+		fetch_cover_image_from_asin (window, album->asin);
+		return;
+	}
+
+#if HAVE_LIBCOVERART
+
+	if ((FETCH_COVER_STAGE_AFTER_LIBCOVERART > after_stage)
+	    && (album != NULL)
+	    && (album->id != NULL))
+	{
+		CoverArtData *data;
+
+		data = g_new0 (CoverArtData, 1);
+		data->window = g_object_ref (window);
+		data->album = album_info_ref (album);
+		metadata_get_coverart (data->window,
+				       data->album,
+				       NULL,
+				       metadata_get_coverart_cb,
+				       data);
+
+		return;
+	}
+
+#endif
+
+	if ((FETCH_COVER_STAGE_AFTER_WEB_SEARCH > after_stage)
+	    && (album != NULL)
+	    && (album->title != NULL)
+	    && (album->artist != NULL))
+	{
+		fetch_cover_image_from_name (window, album->title, album->artist);
+	}
+}
diff --git a/src/dlg-cover-chooser.h b/src/dlg-cover-chooser.h
index 92ec08d..1bb5eeb 100644
--- a/src/dlg-cover-chooser.h
+++ b/src/dlg-cover-chooser.h
@@ -24,14 +24,25 @@
 #define DLG_COVER_CHOOSER_H
 
 #include "goo-window.h"
+#include "album-info.h"
 
-void   dlg_cover_chooser            (GooWindow  *window,
-				     const char *album,
-				     const char *artist);
-void   fetch_cover_image_from_name  (GooWindow  *window,
-				     const char *album,
-			  	     const char *artist);
-void   fetch_cover_image_from_asin  (GooWindow  *window,
-				     const char *asin);
+typedef enum {
+	FETCH_COVER_STAGE_0,
+	FETCH_COVER_STAGE_AFTER_ASIN,
+	FETCH_COVER_STAGE_AFTER_LIBCOVERART,
+	FETCH_COVER_STAGE_AFTER_WEB_SEARCH
+} FetchCoverStage;
+
+void   dlg_cover_chooser                 (GooWindow       *window,
+					  const char      *album,
+				       	  const char      *artist);
+void   fetch_cover_image_from_name       (GooWindow       *window,
+				      	  const char      *album,
+				          const char      *artist);
+void   fetch_cover_image_from_asin       (GooWindow       *window,
+					  const char      *asin);
+void   fetch_cover_image_from_album_info (GooWindow       *window,
+					  AlbumInfo       *album,
+					  FetchCoverStage  stage);
 
 #endif /* DLG_COVER_CHOOSER_H */
diff --git a/src/goo-window.c b/src/goo-window.c
index f0bae0b..35e6fea 100644
--- a/src/goo-window.c
+++ b/src/goo-window.c
@@ -898,7 +898,6 @@ static void
 goo_window_show (GtkWidget *widget)
 {
 	GooWindow *window = GOO_WINDOW (widget);
-	gboolean   view_foobar;
 
 	if (! arg_toggle_visibility)
 		GTK_WIDGET_CLASS (goo_window_parent_class)->show (widget);
@@ -1538,10 +1537,7 @@ auto_fetch_cover_image (GooWindow *window)
 	}
 	g_free (filename);
 
-	if ((window->priv->album->asin != NULL) && (strcmp (window->priv->album->asin, "") != 0))
-		fetch_cover_image_from_asin (window, window->priv->album->asin);
-	else if ((window->priv->album->title != NULL) && (window->priv->album->artist != NULL))
-		fetch_cover_image_from_name (window, window->priv->album->title, window->priv->album->artist);
+	fetch_cover_image_from_album_info (window, window->priv->album, FETCH_COVER_STAGE_0);
 }
 
 
diff --git a/src/gth-window.c b/src/gth-window.c
index 7c34d48..447ea32 100644
--- a/src/gth-window.c
+++ b/src/gth-window.c
@@ -315,9 +315,9 @@ gth_window_attach_get_content (GthWindow *window,
 {
 	GList *children;
 
-	g_return_if_fail (window != NULL);
-	g_return_if_fail (GTH_IS_WINDOW (window));
-	g_return_if_fail (page >= 0 && page < window->priv->n_pages);
+	g_return_val_if_fail (window != NULL, NULL);
+	g_return_val_if_fail (GTH_IS_WINDOW (window), NULL);
+	g_return_val_if_fail (page >= 0 && page < window->priv->n_pages, NULL);
 
 	children = gtk_container_get_children (GTK_CONTAINER (window->priv->contents[page]));
 	if (children != NULL)



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