[sound-juicer] Factor code iterating over relations



commit d674cddb00e8c086574730d75a3e4208b8713dc0
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Sun May 20 11:36:00 2012 +0200

    Factor code iterating over relations
    
    fill_work_relations and fill_recording_relations are using the
    same code to iterate over the relations contained in a
    RelationListList. This commit moves the common code in a helper
    function and changes these 2 functions to use it.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=661646

 libjuicer/sj-metadata-musicbrainz4.c |   88 +++++++++++++++-------------------
 1 files changed, 39 insertions(+), 49 deletions(-)
---
diff --git a/libjuicer/sj-metadata-musicbrainz4.c b/libjuicer/sj-metadata-musicbrainz4.c
index e6b7485..087f79a 100644
--- a/libjuicer/sj-metadata-musicbrainz4.c
+++ b/libjuicer/sj-metadata-musicbrainz4.c
@@ -228,93 +228,83 @@ fill_relations (Mb4RelationList relations, AlbumDetails *album)
   }
 }
 
-static void
-fill_work_relations (Mb4Work work, TrackDetails *track)
+typedef void (*RelationForeachFunc)(Mb4Relation relation, gpointer user_data);
+
+static void relationlist_list_foreach_relation(Mb4RelationListList relation_lists,
+                                               const char *targettype,
+                                               const char *relation_type,
+                                               RelationForeachFunc callback,
+                                               gpointer user_data)
 {
-  Mb4RelationListList relation_lists;
   unsigned int j;
 
-  if (work == NULL)
-    return;
-
-  relation_lists = mb4_work_get_relationlistlist (work);
   if (relation_lists == NULL)
     return;
 
   for (j = 0; j < mb4_relationlist_list_size (relation_lists); j++) {
     Mb4RelationList relations;
-    char buffer[512]; /* for the GET() macro */
+    char type[512]; /* To hold relationlist target-type and relation type */
     unsigned int i;
 
     relations = mb4_relationlist_list_item (relation_lists, j);
     if (relations == NULL)
       return;
 
-    mb4_relation_list_get_targettype (relations, buffer, sizeof (buffer));
-    if (!g_str_equal (buffer, "artist"))
+    mb4_relation_list_get_targettype (relations, type, sizeof (type));
+    if (!g_str_equal (type, targettype))
       continue;
 
     for (i = 0; i < mb4_relation_list_size (relations); i++) {
       Mb4Relation relation;
-      Mb4Artist composer;
 
       relation = mb4_relation_list_item (relations, i);
       if (relation == NULL)
         continue;
 
-      mb4_relation_get_type (relation, buffer, sizeof (buffer));
-      if (!g_str_equal (buffer, "composer"))
+      mb4_relation_get_type (relation, type, sizeof (type));
+      if (!g_str_equal (type, relation_type))
         continue;
 
+      callback(relation, user_data);
+    }
+  }
+}
+
+static void composer_cb (Mb4Relation relation, gpointer user_data)
+{
+      Mb4Artist composer;
+      TrackDetails *track = (TrackDetails *)user_data;
+      char buffer[512]; /* for the GET() macro */
+
       composer = mb4_relation_get_artist (relation);
       if (composer == NULL)
-        continue;
+        return;
 
       GET (track->composer, mb4_artist_get_name, composer);
       GET (track->composer_sortname, mb4_artist_get_sortname, composer);
-    }
-  }
+}
+
+static void work_cb (Mb4Relation relation, gpointer user_data)
+{
+    Mb4RelationListList relation_lists;
+    Mb4Work work;
+
+    work = mb4_relation_get_work (relation);
+    if (work == NULL)
+        return;
+    relation_lists = mb4_work_get_relationlistlist (work);
+    relationlist_list_foreach_relation (relation_lists, "artist", "composer",
+                                        composer_cb, user_data);
 }
 
 static void
 fill_recording_relations (Mb4Recording recording, TrackDetails *track)
 {
   Mb4RelationListList relation_lists;
-  unsigned int j;
 
   relation_lists = mb4_recording_get_relationlistlist (recording);
-  if (relation_lists == NULL)
-    return;
-
-  for (j = 0; j < mb4_relationlist_list_size (relation_lists); j++) {
-    Mb4RelationList relations;
-    char type[512]; /* To hold relationlist target-type and relation type */
-    unsigned int i;
-
-    relations = mb4_relationlist_list_item (relation_lists, j);
-    if (relations == NULL)
-      return;
-
-    mb4_relation_list_get_targettype (relations, type, sizeof (type));
-    if (!g_str_equal (type, "work"))
-      continue;
-
-    for (i = 0; i < mb4_relation_list_size (relations); i++) {
-      Mb4Relation relation;
-      Mb4Work work;
-
-      relation = mb4_relation_list_item (relations, i);
-      if (relation == NULL)
-        continue;
-
-      mb4_relation_get_type (relation, type, sizeof (type));
-      if (!g_str_equal (type, "performance"))
-        continue;
-
-      work = mb4_relation_get_work (relation);
-      fill_work_relations (work, track);
-    }
-  }
+  relationlist_list_foreach_relation(relation_lists, "work", "performance",
+                                     work_cb, track);
 }
 
 static void



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