[grilo-plugins] local-metadata: Use libmediaart



commit eb546cbf2dbdd03becd25eb0b197455bd786b84d
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Feb 19 16:05:41 2014 +0100

    local-metadata: Use libmediaart
    
    Instead of our copy/paste code.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=724046

 configure.ac                            |   11 ++-
 src/local-metadata/grl-local-metadata.c |  188 ++-----------------------------
 2 files changed, 16 insertions(+), 183 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 0c642bf..23678e0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -160,6 +160,8 @@ PKG_CHECK_MODULES(GMIME, gmime-2.6,
 
 PKG_CHECK_MODULES(AVAHI, [avahi-gobject avahi-glib avahi-client], HAVE_AVAHI=yes, HAVE_AVAHI=no)
 
+PKG_CHECK_MODULES(MEDIAART, [libmediaart-1.0], HAVE_MEDIAART=yes, HAVE_MEDIAART=no)
+
 # Search available Tracker version
 trackerpkg="tracker-sparql-0.10"
 m4_foreach([VERSION], [[0.10], [0.11], [0.12], [0.14], [0.15], [0.16], [0.17], [0.18], [1.0]],
@@ -1082,11 +1084,14 @@ AC_ARG_ENABLE(localmetadata,
                         if test "x$HAVE_GIO" = "xno"; then
                            AC_MSG_ERROR([gio-2.0 not found, install it or use --disable-localmetadata])
                         fi
+                        if test "x$HAVE_MEDIAART" = "xno"; then
+                           AC_MSG_ERROR([libmediaart not found, install it or use --disable-localmetadata])
+                        fi
                         ;;
                 esac
         ],
         [
-                if test "x$HAVE_GIO" = "xyes"; then
+                if test "x$HAVE_GIO" = "xyes" -a "x$HAVE_MEDIAART" = "xyes"; then
                    enable_localmetadata=yes
                 else
                    enable_localmetadata=no
@@ -1104,9 +1109,9 @@ LOCALMETADATA_PLUGIN_ID="grl-local-metadata"
 AC_SUBST(LOCALMETADATA_PLUGIN_ID)
 AC_DEFINE_UNQUOTED([LOCALMETADATA_PLUGIN_ID], ["$LOCALMETADATA_PLUGIN_ID"], [Local metadata plugin ID])
 
-DEPS_LOCALMETADATA_CFLAGS="$DEPS_CFLAGS $GIO_CFLAGS"
+DEPS_LOCALMETADATA_CFLAGS="$DEPS_CFLAGS $GIO_CFLAGS $MEDIAART_CFLAGS"
 AC_SUBST(DEPS_LOCALMETADATA_CFLAGS)
-DEPS_LOCALMETADATA_LIBS="$DEPS_LIBS $GIO_LIBS"
+DEPS_LOCALMETADATA_LIBS="$DEPS_LIBS $GIO_LIBS $MEDIAART_LIBS"
 AC_SUBST(DEPS_LOCALMETADATA_LIBS)
 
 # ----------------------------------------------------------
diff --git a/src/local-metadata/grl-local-metadata.c b/src/local-metadata/grl-local-metadata.c
index e300433..9c6c7b4 100644
--- a/src/local-metadata/grl-local-metadata.c
+++ b/src/local-metadata/grl-local-metadata.c
@@ -30,6 +30,7 @@
 #include <grilo.h>
 #include <gio/gio.h>
 #include <glib/gi18n-lib.h>
+#include <libmediaart/mediaart.h>
 
 #include "grl-local-metadata.h"
 
@@ -604,198 +605,25 @@ resolve_image (GrlSource *source,
   return TRUE;
 }
 
-/* Taken from: http://live.gnome.org/MediaArtStorageSpec/SampleStripCodeInC */
-static gboolean
-strip_find_next_block (const gchar    *original,
-                       const gunichar  open_char,
-                       const gunichar  close_char,
-                       gint           *open_pos,
-                       gint           *close_pos)
-{
-  const gchar *p1, *p2;
-
-  if (open_pos)
-    *open_pos = -1;
-
-  if (close_pos)
-    *close_pos = -1;
-
-  p1 = g_utf8_strchr (original, -1, open_char);
-  if (p1) {
-    if (open_pos)
-      *open_pos = p1 - original;
-
-    p2 = g_utf8_strchr (g_utf8_next_char (p1), -1, close_char);
-    if (p2) {
-      if (close_pos)
-        *close_pos = p2 - original;
-
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-
-/* Taken from: http://live.gnome.org/MediaArtStorageSpec/SampleStripCodeInC
- * strips out invalid characters in a album name or artist name before md5sum
- * to get the unique identifier to find the album art.
- */
-static gchar *
-albumart_strip_invalid_entities (const gchar *original)
-{
-  GString         *str_no_blocks;
-  gchar          **strv;
-  gchar           *str, *res;
-  gboolean         blocks_done = FALSE;
-  const gchar     *p;
-  const gchar     *invalid_chars = "()[]<>{}_! #$^&*+=|\\/\"'?~";
-  const gchar     *invalid_chars_delimiter = "*";
-  const gchar     *convert_chars = "\t";
-  const gchar     *convert_chars_delimiter = " ";
-  const gunichar   blocks[5][2] = {
-      { '(', ')' },
-      { '{', '}' },
-      { '[', ']' },
-      { '<', '>' },
-      {  0,   0  }
-  };
-
-  str_no_blocks = g_string_new ("");
-
-  p = original;
-
-  while (!blocks_done) {
-    gint pos1, pos2, i;
-
-    pos1 = -1;
-    pos2 = -1;
-
-    for (i = 0; blocks[i][0] != 0; i++) {
-      gint start, end;
-
-      /* Go through blocks, find the earliest block we can */
-      if (strip_find_next_block (p, blocks[i][0], blocks[i][1], &start,
-                                 &end)) {
-        if (pos1 == -1 || start < pos1) {
-          pos1 = start;
-          pos2 = end;
-        }
-      }
-    }
-
-    /* If either are -1 we didn't find any */
-    if (pos1 == -1) {
-      /* This means no blocks were found */
-      g_string_append (str_no_blocks, p);
-      blocks_done = TRUE;
-    } else {
-      /* Append the test BEFORE the block */
-      if (pos1 > 0)
-        g_string_append_len (str_no_blocks, p, pos1);
-
-      p = g_utf8_next_char (p + pos2);
-
-      /* Do same again for position AFTER block */
-      if (*p == '\0')
-        blocks_done = TRUE;
-    }
-  }
-
-  str = g_string_free (str_no_blocks, FALSE);
-
-  /* Now strip invalid chars */
-  g_strdelimit (str, invalid_chars, *invalid_chars_delimiter);
-  strv = g_strsplit (str, invalid_chars_delimiter, -1);
-  g_free (str);
-  str = g_strjoinv (NULL, strv);
-  g_strfreev (strv);
-
-  /* Now convert chars */
-  g_strdelimit (str, convert_chars, *convert_chars_delimiter);
-  strv = g_strsplit (str, convert_chars_delimiter, -1);
-  g_free (str);
-  str = g_strjoinv (convert_chars_delimiter, strv);
-  g_strfreev (strv);
-
-  /* Now remove double spaces */
-  strv = g_strsplit (str, "  ", -1);
-  g_free (str);
-  str = g_strjoinv (" ", strv);
-  g_strfreev (strv);
-
-  /* Now strip leading/trailing white space */
-  g_strstrip (str);
-
-  res = g_utf8_strdown (str, -1);
-  g_free (str);
-
-  str = g_utf8_normalize (res, -1, G_NORMALIZE_NFKD);
-  g_free (res);
-
-  return str;
-}
-
 static gboolean
 resolve_album_art (GrlSource *source,
                    GrlSourceResolveSpec *rs,
                    resolution_flags_t flags)
 {
-  const gchar *artist_value, *album_value;
-  gchar *artist, *album, *artist_tmp, *album_tmp,
-        *artist_md5, *album_md5, *file_path;
-
-  GRegex *regex;
+  const gchar *artist, *album;
+  char *thumbnail_uri;
 
-  artist_value = grl_media_audio_get_artist (GRL_MEDIA_AUDIO (rs->media));
-  album_value = grl_media_audio_get_album (GRL_MEDIA_AUDIO (rs->media));
+  artist = grl_media_audio_get_artist (GRL_MEDIA_AUDIO (rs->media));
+  album = grl_media_audio_get_album (GRL_MEDIA_AUDIO (rs->media));
 
-  if (!artist_value || !album_value)
+  if (!artist || !album)
     return TRUE;
 
-  /* regex to find if we need to strip invalid chars
-   * ()[]<>{}_! #$^&*+=|\\/\"'?~" and 2 or more spaces
-   */
-
-  regex =
-    g_regex_new ("([\\(\\)\\[\\]\\<\\>\\{\\}_! #$\\^&\\*"
-                 "\\+=\\|\\\\/\\\"\\'\?~]|\\s{2,})",
-                 0, 0, NULL);
-
-  if ((g_regex_match (regex, artist_value, 0, NULL))) {
-    artist = albumart_strip_invalid_entities (artist_value);
-  } else {
-    artist_tmp = g_utf8_strdown (artist_value, -1);
-    artist = g_utf8_normalize (artist_tmp, -1, G_NORMALIZE_NFKD);
-    g_free (artist_tmp);
-  }
-
-  if (g_regex_match (regex, album_value, 0, NULL)) {
-    album = albumart_strip_invalid_entities (album_value);
-  } else {
-    album_tmp = g_utf8_strdown (album_value, -1);
-    album = g_utf8_normalize (album_tmp, -1, G_NORMALIZE_NFKD);
-    g_free (album_tmp);
-  }
-
-  g_regex_unref (regex);
-
-  artist_md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, artist, -1);
-  album_md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, album, -1);
-
-  file_path = g_strdup_printf ("%s/media-art/album-%s-%s.jpeg",
-                               g_get_user_cache_dir (),
-                               artist_md5,
-                               album_md5);
-  g_free (album_md5);
-  g_free (artist_md5);
+  media_art_get_path (artist, album, "album", NULL, NULL, &thumbnail_uri);
 
-  if (g_file_test (file_path, G_FILE_TEST_EXISTS)) {
-    gchar *thumbnail_uri = g_filename_to_uri (file_path, NULL, NULL);
+  if (thumbnail_uri) {
     grl_media_set_thumbnail (rs->media, thumbnail_uri);
     g_free (thumbnail_uri);
-    g_free (file_path);
   }
   rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, NULL);
 


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