[sound-juicer] Get country name ISO code.



commit d2d8da5bc8eabfb5e450fe216d7ff461346775a9
Author: Phillip Wood <phillip wood dunelm org uk>
Date:   Sat Jun 30 14:18:37 2012 +0100

    Get country name ISO code.
    
    Musicbrainz returns the country as a 2 letter ISO country code. Add
    functions based on totem's language code handling to convert that code
    to the country name and store the name, rather than the code in
    AlbumDetails::country.
    
    Adds a dependency on iso-codes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=674926

 configure.ac                         |    8 ++
 libjuicer/sj-metadata-musicbrainz5.c |    3 +-
 libjuicer/sj-metadata.c              |  128 ++++++++++++++++++++++++++++++++++
 libjuicer/sj-metadata.h              |    2 +
 src/sj-main.c                        |    2 +
 5 files changed, 142 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index a6f36bf..91ea529 100644
--- a/configure.ac
+++ b/configure.ac
@@ -80,6 +80,14 @@ else
         AC_MSG_ERROR([libmusicbrainz5 needs to be available for sound-juicer to build])
 fi
 
+# ISO-CODES
+PKG_CHECK_MODULES(ISO_CODES, [iso-codes])
+if $PKG_CONFIG --variable=domains iso-codes | grep 3166 >/dev/null ; then
+  AC_DEFINE_UNQUOTED([ISO_CODES_PREFIX],["`$PKG_CONFIG --variable=prefix iso-codes`"],[ISO codes prefix])
+else
+  AC_MSG_ERROR([iso-codes database does not support iso3166 country codes])
+fi
+
 # Find gio for the metadata extractor
 PKG_CHECK_MODULES(GIO, gio-2.0)
 AC_SUBST(GIO_CFLAGS)
diff --git a/libjuicer/sj-metadata-musicbrainz5.c b/libjuicer/sj-metadata-musicbrainz5.c
index 9ba09e1..8a3f18a 100644
--- a/libjuicer/sj-metadata-musicbrainz5.c
+++ b/libjuicer/sj-metadata-musicbrainz5.c
@@ -460,7 +460,8 @@ make_album_from_release (Mb5ReleaseGroup group,
   g_free (date);
 
   GET (album->asin, mb5_release_get_asin, release);
-  GET (album->country, mb5_release_get_country, release);
+  mb5_release_get_country (release, buffer, sizeof(buffer));
+  album->country = sj_metadata_helper_lookup_country_code (buffer);
   if (group) {
     GET (album->type, mb5_releasegroup_get_primarytype, group);
     if (g_str_has_suffix (album->type, "Spokenword")
diff --git a/libjuicer/sj-metadata.c b/libjuicer/sj-metadata.c
index 0a6497d..3324126 100644
--- a/libjuicer/sj-metadata.c
+++ b/libjuicer/sj-metadata.c
@@ -223,3 +223,131 @@ sj_metadata_helper_check_media (const char *cdrom, GError **error)
   return TRUE;
 }
 
+/* ISO-3166 helpers, these functions translate between a country code
+ * returned by MusicBrainz and the country name. Adapted from the
+ * totem language name lookup functions before it switched to using
+ * the GStreamer language helpers
+ */
+static GHashTable *country_table;
+
+void
+sj_metadata_helper_cleanup (void)
+{
+  if (country_table == NULL)
+    return;
+
+  g_hash_table_destroy (country_table);
+  country_table = NULL;
+}
+
+static void
+country_table_parse_start_tag (GMarkupParseContext *ctx,
+                               const gchar         *element_name,
+                               const gchar        **attr_names,
+                               const gchar        **attr_values,
+                               gpointer             data,
+                               GError             **error)
+{
+  const char *ccode, *country_name;
+
+  if (!g_str_equal (element_name, "iso_3166_entry")
+      || attr_names == NULL
+      || attr_values == NULL)
+    return;
+
+  ccode = NULL;
+  country_name = NULL;
+
+  while (*attr_names && *attr_values)
+    {
+      if (g_str_equal (*attr_names, "alpha_2_code"))
+        {
+          /* skip if empty */
+          if (**attr_values)
+            {
+              g_return_if_fail (strlen (*attr_values) == 2);
+              ccode = *attr_values;
+            }
+        } else if (g_str_equal (*attr_names, "name")) {
+        country_name = *attr_values;
+      }
+
+      ++attr_names;
+      ++attr_values;
+    }
+
+  if (country_name == NULL)
+    return;
+
+  if (ccode != NULL)
+    {
+      g_hash_table_insert (country_table,
+                           g_strdup (ccode),
+                           g_strdup (country_name));
+    }
+}
+
+#define ISO_CODES_DATADIR ISO_CODES_PREFIX"/share/xml/iso-codes"
+#define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX"/share/locale"
+
+static void
+country_table_init (void)
+{
+  GError *err = NULL;
+  char *buf;
+  gsize buf_len;
+
+  country_table = g_hash_table_new_full
+    (g_str_hash, g_str_equal, g_free, g_free);
+
+  bindtextdomain ("iso_3166", ISO_CODES_LOCALESDIR);
+  bind_textdomain_codeset ("iso_3166", "UTF-8");
+
+  if (g_file_get_contents (ISO_CODES_DATADIR "/iso_3166.xml",
+                           &buf, &buf_len, &err))
+    {
+      GMarkupParseContext *ctx;
+      GMarkupParser parser =
+        { country_table_parse_start_tag, NULL, NULL, NULL, NULL };
+
+      ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
+
+      if (!g_markup_parse_context_parse (ctx, buf, buf_len, &err))
+        {
+          g_warning ("Failed to parse '%s': %s\n",
+                     ISO_CODES_DATADIR"/iso_3166.xml",
+                     err->message);
+          g_error_free (err);
+        }
+
+      g_markup_parse_context_free (ctx);
+      g_free (buf);
+    } else {
+    g_warning ("Failed to load '%s': %s\n",
+               ISO_CODES_DATADIR"/iso_3166.xml", err->message);
+    g_error_free (err);
+  }
+}
+
+char *
+sj_metadata_helper_lookup_country_code (const char *code)
+{
+  const char *country_name;
+  int len;
+
+  g_return_val_if_fail (code != NULL, NULL);
+
+  len = strlen (code);
+  if (len != 2)
+    return NULL;
+  if (country_table == NULL)
+    country_table_init ();
+
+  country_name = (const gchar*) g_hash_table_lookup (country_table, code);
+
+  if (country_name)
+    return g_strdup (dgettext ("iso_3166", country_name));
+
+  g_warning ("Unknown country code: %s", code);
+  return NULL;
+}
diff --git a/libjuicer/sj-metadata.h b/libjuicer/sj-metadata.h
index f7a58d2..0ab2d44 100644
--- a/libjuicer/sj-metadata.h
+++ b/libjuicer/sj-metadata.h
@@ -53,6 +53,8 @@ GList * sj_metadata_list_albums (SjMetadata *metadata, char **url, GError **erro
 char * sj_metadata_helper_scan_disc_number (const char *album_title, int *disc_number);
 GDate * sj_metadata_helper_scan_date (const char *date);
 gboolean sj_metadata_helper_check_media (const char *cdrom, GError **error);
+char * sj_metadata_helper_lookup_country_code (const char *code);
+void sj_metadata_helper_cleanup (void);
 
 G_END_DECLS
 
diff --git a/src/sj-main.c b/src/sj-main.c
index a00ebd4..78f2159 100644
--- a/src/sj-main.c
+++ b/src/sj-main.c
@@ -44,6 +44,7 @@
 #include "gconf-bridge.h"
 #include "rb-gst-media-types.h"
 #include "sj-about.h"
+#include "sj-metadata.h"
 #include "sj-metadata-getter.h"
 #include "sj-extractor.h"
 #include "sj-structures.h"
@@ -2225,6 +2226,7 @@ int main (int argc, char **argv)
 
   g_object_unref (app);
 
+  sj_metadata_helper_cleanup ();
   g_object_unref (base_uri);
   g_object_unref (metadata);
   g_object_unref (extractor);


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