[PATCH 2/2] core: Make get_sources_by_operations return a GList



https://bugzilla.gnome.org/show_bug.cgi?id=629369
---
 src/grl-metadata-source.c  |   28 +++++++++++++----------
 src/grl-multiple.c         |   24 ++++---------------
 src/grl-plugin-registry.c  |   45 +++++-------------------------------
 src/grl-plugin-registry.h  |    2 +-
 tools/grilo-test-ui/main.c |   53 +++++++++++++++++++++++--------------------
 5 files changed, 57 insertions(+), 95 deletions(-)

diff --git a/src/grl-metadata-source.c b/src/grl-metadata-source.c
index ce7d6e1..5522a83 100644
--- a/src/grl-metadata-source.c
+++ b/src/grl-metadata-source.c
@@ -437,10 +437,11 @@ analyze_keys_to_write (GrlMetadataSource *source,
   GList *maps = NULL;
   struct SourceKeyMap *map;
   GrlPluginRegistry *registry;
-  GrlMediaPlugin **source_list;
+  GList *sources = NULL;
+  GList *sources_iter;
 
   /* 'supported_keys' holds keys that can be written by this source
-     'key_list' holds those that must be hadled by other sources */
+     'key_list' holds those that must be handled by other sources */
   GList *key_list = g_list_copy (keys);
   GList *supported_keys =
     grl_metadata_source_filter_writable (source, &key_list, TRUE);
@@ -464,15 +465,16 @@ analyze_keys_to_write (GrlMetadataSource *source,
 
   /* Check if other sources can write the missing keys */
   registry = grl_plugin_registry_get_default ();
-  source_list =
+  sources =
     grl_plugin_registry_get_sources_by_operations (registry,
                                                    GRL_OP_SET_METADATA,
                                                    TRUE);
-  while (key_list && *source_list) {
+
+  for (sources_iter = sources; key_list && sources_iter;
+      sources_iter = g_list_next (sources_iter)) {
     GrlMetadataSource *_source;
 
-    _source = GRL_METADATA_SOURCE (*source_list);
-    source_list++;
+    _source = GRL_METADATA_SOURCE (sources_iter->data);
     if (_source == source) {
       continue;
     }
@@ -491,6 +493,7 @@ analyze_keys_to_write (GrlMetadataSource *source,
 
  done:
   *failed_keys = key_list;
+  g_list_free (sources);
   return maps;
 }
 
@@ -938,21 +941,21 @@ grl_metadata_source_setup_full_resolution_mode (GrlMetadataSource *source,
   /* Find which sources resolve which keys */
   GList *supported_keys;
   GrlMetadataSource *_source;
-  GrlMediaPlugin **source_list;
+  GList *sources;
+  GList *sources_iter;
   GList *iter;
   GrlPluginRegistry *registry;
 
   registry = grl_plugin_registry_get_default ();
-  source_list = grl_plugin_registry_get_sources_by_operations (registry,
+  sources = grl_plugin_registry_get_sources_by_operations (registry,
                                                                GRL_OP_RESOLVE,
                                                                TRUE);
 
-  while (*source_list && key_list) {
+  for (sources_iter = sources; sources_iter && key_list;
+      sources_iter = g_list_next(sources_iter)) {
     gchar *name;
 
-    _source = GRL_METADATA_SOURCE (*source_list);
-
-    source_list++;
+    _source = GRL_METADATA_SOURCE (sources_iter->data);
 
     /* Interested in sources other than this  */
     if (_source == source) {
@@ -1058,6 +1061,7 @@ grl_metadata_source_setup_full_resolution_mode (GrlMetadataSource *source,
     GRL_DEBUG ("No key mapping for other sources, can't resolve more metadata");
   }
  done:
+  g_list_free (sources);
   return;
 }
 
diff --git a/src/grl-multiple.c b/src/grl-multiple.c
index e32ed35..e121e2b 100644
--- a/src/grl-multiple.c
+++ b/src/grl-multiple.c
@@ -119,18 +119,6 @@ handle_no_searchable_sources (GrlMediaSourceResultCb callback, gpointer user_dat
   g_idle_add (handle_no_searchable_sources_idle, callback_data);
 }
 
-static GList *
-source_array_to_list (GrlMediaPlugin **sources)
-{
-  GList *list = NULL;
-  gint n = 0;
-  while (sources[n]) {
-    list = g_list_prepend (list, sources[n]);
-    n++;
-  }
-  return list;
-}
-
 static struct MultipleSearchData *
 start_multiple_search_operation (guint search_id,
 				 const GList *sources,
@@ -456,7 +444,7 @@ grl_multiple_search (const GList *sources,
 		     gpointer user_data)
 {
   GrlPluginRegistry *registry;
-  GrlMediaPlugin **sources_array;
+  GList *sources_list;
   struct MultipleSearchData *msd;
   gboolean allocated_sources_list = FALSE;
 
@@ -478,23 +466,21 @@ grl_multiple_search (const GList *sources,
      searchable sources from the registry */
   if (!sources) {
     registry = grl_plugin_registry_get_default ();
-    sources_array =
+    sources_list =
       grl_plugin_registry_get_sources_by_operations (registry,
 						     GRL_OP_SEARCH,
 						     TRUE);
-    if (sources_array[0] == NULL) {
+    if (sources_list == NULL) {
       /* No searchable sources? Raise error and bail out */
-      g_free (sources_array);
+      g_list_free (sources_list);
       handle_no_searchable_sources (callback, user_data);
       return 0;
     } else {
-      sources = source_array_to_list (sources_array);
+      sources = sources_list;
       allocated_sources_list = TRUE;
-      g_free (sources_array);
     }
   }
 
-
   /* Start multiple search operation */
   multiple_search_id++;
   msd = start_multiple_search_operation (multiple_search_id,
diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index 4949833..238e337 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -212,32 +212,6 @@ compare_by_rank (gconstpointer a,
   return (rank_a > rank_b) - (rank_a < rank_b);
 }
 
-static void
-sort_by_rank (GrlMediaPlugin **source_list)
-{
-  GrlMediaPlugin *plugin;
-  gint index, i, top_rank, top_index;
-
-  index = 0;
-  while (source_list[index]) {
-    top_rank = grl_media_plugin_get_rank (source_list[index]);
-    top_index = index;
-    i = index + 1;
-    while (source_list[i]) {
-      gint rank = grl_media_plugin_get_rank (source_list[i]);
-      if (rank > top_rank) {
-	top_rank = rank;
-	top_index = i;
-      }
-      i++;
-    }
-    plugin = source_list[index];
-    source_list[index] = source_list[top_index];
-    source_list[top_index] = plugin;
-    index++;
-  }
-}
-
 static GHashTable *
 get_info_from_plugin_xml (const gchar *xml_path)
 {
@@ -599,40 +573,35 @@ grl_plugin_registry_get_sources (GrlPluginRegistry *registry,
  *
  * If @ranked is %TRUE, the source list will be ordered by rank.
  *
- * Returns: (array zero-terminated=1) (transfer container): an array of available sources
+ * Returns: (element-type Grl.MediaPlugin) (transfer container):  a list of available sources.
+ * Use g_list_free() when done using the list.
  */
-GrlMediaPlugin **
+GList *
 grl_plugin_registry_get_sources_by_operations (GrlPluginRegistry *registry,
                                                GrlSupportedOps ops,
                                                gboolean ranked)
 {
   GHashTableIter iter;
-  GrlMediaPlugin **source_list;
+  GList *source_list = NULL;
   GrlMediaPlugin *p;
-  gint n;
 
   g_return_val_if_fail (GRL_IS_PLUGIN_REGISTRY (registry), NULL);
 
-  n = g_hash_table_size (registry->priv->sources);
-  source_list = (GrlMediaPlugin **) g_new0 (GrlMediaPlugin *, n + 1);
-
-  n = 0;
   g_hash_table_iter_init (&iter, registry->priv->sources);
   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &p)) {
     GrlSupportedOps source_ops;
     source_ops =
       grl_metadata_source_supported_operations (GRL_METADATA_SOURCE (p));
     if ((source_ops & ops) == ops) {
-      source_list[n++] = p;
+      source_list = g_list_prepend (source_list, p);
     }
   }
-  source_list[n] = NULL;
 
   if (ranked) {
-    sort_by_rank (source_list);
+    source_list = g_list_sort (source_list, compare_by_rank);
   }
 
-  return source_list;  
+  return source_list;
 }
 
 /**
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index 1230b3f..99e5844 100644
--- a/src/grl-plugin-registry.h
+++ b/src/grl-plugin-registry.h
@@ -216,7 +216,7 @@ GrlMediaPlugin *grl_plugin_registry_lookup_source (GrlPluginRegistry *registry,
 GList *grl_plugin_registry_get_sources (GrlPluginRegistry *registry,
 						  gboolean ranked);
 
-GrlMediaPlugin **grl_plugin_registry_get_sources_by_operations (GrlPluginRegistry *registry,
+GList *grl_plugin_registry_get_sources_by_operations (GrlPluginRegistry *registry,
                                                                 GrlSupportedOps ops,
                                                                 gboolean ranked);
 
diff --git a/tools/grilo-test-ui/main.c b/tools/grilo-test-ui/main.c
index aee7aa4..73ab59c 100644
--- a/tools/grilo-test-ui/main.c
+++ b/tools/grilo-test-ui/main.c
@@ -1159,9 +1159,9 @@ static void
 query_combo_setup (void)
 {
   GrlPluginRegistry *registry;
-  GrlMediaPlugin **sources;
+  GList *sources = NULL;
+  GList *sources_iter;
   GtkTreeIter iter;
-  guint i = 0;
 
   clear_query_combo ();
 
@@ -1169,18 +1169,19 @@ query_combo_setup (void)
   sources = grl_plugin_registry_get_sources_by_operations (registry,
                                                            GRL_OP_QUERY,
                                                            FALSE);
-  while (sources[i]) {
-    const gchar *name =
-      grl_metadata_source_get_name (GRL_METADATA_SOURCE (sources[i]));
+  for (sources_iter = sources; sources_iter;
+      sources_iter = g_list_next (sources_iter)) {
+    GrlMetadataSource *source = GRL_METADATA_SOURCE (sources_iter->data);
+    const gchar *name = grl_metadata_source_get_name (source);
+
     gtk_list_store_append (GTK_LIST_STORE (view->query_combo_model), &iter);
     gtk_list_store_set (GTK_LIST_STORE (view->query_combo_model),
 			&iter,
-			QUERY_MODEL_SOURCE, sources[i],
+			QUERY_MODEL_SOURCE, source,
 			QUERY_MODEL_NAME, name,
 			-1);
-    i++;
   }
-  g_free (sources);
+  g_list_free (sources);
 
   gtk_combo_box_set_active (GTK_COMBO_BOX (view->query_combo), 0);
 }
@@ -1189,9 +1190,9 @@ static void
 search_combo_setup (void)
 {
   GrlPluginRegistry *registry;
-  GrlMediaPlugin **sources;
+  GList *sources = NULL;
+  GList *sources_iter;
   GtkTreeIter iter;
-  guint i = 0;
 
   clear_search_combo ();
 
@@ -1199,18 +1200,19 @@ search_combo_setup (void)
   sources = grl_plugin_registry_get_sources_by_operations (registry,
                                                            GRL_OP_SEARCH,
                                                            FALSE);
-  while (sources[i]) {
-    const gchar *name =
-      grl_metadata_source_get_name (GRL_METADATA_SOURCE (sources[i]));
+  for (sources_iter = sources; sources_iter;
+      sources_iter = g_list_next (sources_iter)) {
+    GrlMetadataSource *source = GRL_METADATA_SOURCE (sources_iter->data);
+    const gchar *name = grl_metadata_source_get_name (source);
+
     gtk_list_store_append (GTK_LIST_STORE (view->search_combo_model), &iter);
     gtk_list_store_set (GTK_LIST_STORE (view->search_combo_model),
 			&iter,
-			SEARCH_MODEL_SOURCE, sources[i],
+			SEARCH_MODEL_SOURCE, source,
 			SEARCH_MODEL_NAME, name,
 			-1);
-    i++;
   }
-  g_free (sources);
+  g_list_free (sources);
 
   /* Add "All" option */
   gtk_list_store_append (GTK_LIST_STORE (view->search_combo_model), &iter);
@@ -1622,8 +1624,8 @@ ui_setup (void)
 static void
 show_plugins ()
 {
-  GrlMediaPlugin **sources;
-  guint i;
+  GList *sources;
+  GList *sources_iter;
   GtkTreeIter iter;
   GrlPluginRegistry *registry;
 
@@ -1631,29 +1633,30 @@ show_plugins ()
 
   clear_panes ();
 
-  i = 0;
   sources = grl_plugin_registry_get_sources_by_operations (registry,
                                                            GRL_OP_BROWSE,
                                                            FALSE);
-  while (sources[i]) {
+  for (sources_iter = sources; sources_iter;
+      sources_iter = g_list_next (sources_iter)) {
+    GrlMetadataSource *source;
     const gchar *name;
     GdkPixbuf *icon;
+
+    source = GRL_METADATA_SOURCE (sources_iter->data);
     icon = load_icon (GTK_STOCK_DIRECTORY);
-    name = grl_metadata_source_get_name (GRL_METADATA_SOURCE (sources[i]));
+    name = grl_metadata_source_get_name (source);
     GRL_DEBUG ("Loaded source: '%s'", name);
     gtk_list_store_append (GTK_LIST_STORE (view->browser_model), &iter);
     gtk_list_store_set (GTK_LIST_STORE (view->browser_model),
 			&iter,
-			BROWSER_MODEL_SOURCE, sources[i],
+			BROWSER_MODEL_SOURCE, source,
 			BROWSER_MODEL_CONTENT, NULL,
 			BROWSER_MODEL_TYPE, OBJECT_TYPE_SOURCE,
 			BROWSER_MODEL_NAME, name,
 			BROWSER_MODEL_ICON, icon,
 			-1);
-    i++;
   }
-  g_free (sources);
-
+  g_list_free (sources);
 }
 
 static void
-- 
1.7.0.4



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