[PATCH 1/2] core: Make get_sources return a GList



* GrlPluginRegistry's get_sources now returns a GList
* Added a compare_by_rank method to use with g_list_sort
* Updated grilo-test-ui, grl-inspect and tests to expect a GList

https://bugzilla.gnome.org/show_bug.cgi?id=629369
---
 src/grl-plugin-registry.c         |   50 ++++++++++++++++++++++++------------
 src/grl-plugin-registry.h         |    2 +-
 src/tests/registry.c              |   19 ++++++++------
 tools/grilo-inspect/grl-inspect.c |   17 ++++++++----
 tools/grilo-test-ui/main.c        |   14 ++++++---
 5 files changed, 65 insertions(+), 37 deletions(-)

diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index fac1e5e..4949833 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -200,6 +200,18 @@ grl_plugin_registry_setup_ranks (GrlPluginRegistry *registry)
   g_strfreev (rank_specs);
 }
 
+static gint
+compare_by_rank (gconstpointer a,
+                 gconstpointer b) {
+  gint rank_a;
+  gint rank_b;
+
+  rank_a = grl_media_plugin_get_rank (GRL_MEDIA_PLUGIN (a));
+  rank_b = grl_media_plugin_get_rank (GRL_MEDIA_PLUGIN (b));
+
+  return (rank_a > rank_b) - (rank_a < rank_b);
+}
+
 static void
 sort_by_rank (GrlMediaPlugin **source_list)
 {
@@ -551,27 +563,26 @@ grl_plugin_registry_lookup_source (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 (GrlPluginRegistry *registry,
 				 gboolean ranked)
 {
   GHashTableIter iter;
-  GrlMediaPlugin **source_list;
-  gint n;
+  GList *source_list = NULL;
+  GrlMediaPlugin *current_plugin;
 
   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 *) &source_list[n++]));
+  while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &current_plugin)) {
+    source_list = g_list_prepend (source_list, current_plugin);
+  }
 
   if (ranked) {
-    sort_by_rank (source_list);
+    source_list = g_list_sort (source_list, (GCompareFunc) compare_by_rank);
   }
 
   return source_list;
@@ -637,8 +648,8 @@ grl_plugin_registry_unload (GrlPluginRegistry *registry,
                             const gchar *plugin_id)
 {
   GrlPluginDescriptor *plugin;
-  GrlMediaPlugin **sources;
-  gint i;
+  GList *sources = NULL;
+  GList *sources_iter;
 
   GRL_DEBUG ("grl_plugin_registry_unload: %s", plugin_id);
 
@@ -655,14 +666,19 @@ grl_plugin_registry_unload (GrlPluginRegistry *registry,
   /* Second, shut down any sources spawned by this plugin */
   GRL_DEBUG ("Shutting down sources spawned by '%s'", plugin_id);
   sources = grl_plugin_registry_get_sources (registry, FALSE);
-  for (i=0; sources[i] != NULL; i++) {
-    const gchar *id; 
-    id = grl_media_plugin_get_id (sources[i]);
+
+  for (sources_iter = sources; sources_iter;
+      sources_iter = g_list_next (sources_iter)) {
+    const gchar *id;
+    GrlMediaPlugin *source;
+
+    source = GRL_MEDIA_PLUGIN (sources_iter->data);
+    id = grl_media_plugin_get_id (source);
     if (!strcmp (plugin_id, id)) {
-      grl_plugin_registry_unregister_source (registry, sources[i]);
+      grl_plugin_registry_unregister_source (registry, source);
     }
   }
-  g_free (sources);
+  g_list_free (sources);
 
   /* Third, shut down the plugin */
   GRL_DEBUG ("Unloading plugin '%s'", plugin_id);
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index a293b49..1230b3f 100644
--- a/src/grl-plugin-registry.h
+++ b/src/grl-plugin-registry.h
@@ -213,7 +213,7 @@ void grl_plugin_registry_unregister_source (GrlPluginRegistry *registry,
 GrlMediaPlugin *grl_plugin_registry_lookup_source (GrlPluginRegistry *registry,
                                                    const gchar *source_id);
 
-GrlMediaPlugin **grl_plugin_registry_get_sources (GrlPluginRegistry *registry,
+GList *grl_plugin_registry_get_sources (GrlPluginRegistry *registry,
 						  gboolean ranked);
 
 GrlMediaPlugin **grl_plugin_registry_get_sources_by_operations (GrlPluginRegistry *registry,
diff --git a/src/tests/registry.c b/src/tests/registry.c
index 54ba499..0306cde 100644
--- a/src/tests/registry.c
+++ b/src/tests/registry.c
@@ -92,27 +92,30 @@ registry_load (RegistryFixture *fixture, gconstpointer data)
 static void
 registry_unregister (RegistryFixture *fixture, gconstpointer data)
 {
-  GrlMediaPlugin **sources;
+  GList *sources = NULL;
+  GList *sources_iter;
   int i;
 
   g_test_bug ("627207");
 
   sources = grl_plugin_registry_get_sources (fixture->registry, FALSE);
 
-  i = 0;
-  while (sources[i]) {
-    grl_plugin_registry_unregister_source (fixture->registry, sources[i]);
-    i++;
+  for (sources_iter = sources, i = 0; sources_iter;
+      sources_iter = g_list_next (sources_iter), i++) {
+    GrlMediaPlugin *source = GRL_MEDIA_PLUGIN (sources_iter->data);
+
+    grl_plugin_registry_unregister_source (fixture->registry, source);
   }
-  g_free (sources);
+  g_list_free (sources);
 
   /* We expect to have loaded sources */
   g_assert_cmpint (i, !=, 0);
 
   sources = grl_plugin_registry_get_sources (fixture->registry, FALSE);
-  for (i = 0; sources[i]; i++)
+  for (sources_iter = sources, i = 0; sources_iter;
+      sources_iter = g_list_next (sources_iter), i++)
     ;
-  g_free (sources);
+  g_list_free (sources);
 
   /* After unregistering the sources, we don't expect any */
   g_assert_cmpint (i, ==, 0);
diff --git a/tools/grilo-inspect/grl-inspect.c b/tools/grilo-inspect/grl-inspect.c
index 97f7eb1..a4211e9 100644
--- a/tools/grilo-inspect/grl-inspect.c
+++ b/tools/grilo-inspect/grl-inspect.c
@@ -43,16 +43,21 @@ static GOptionEntry entries[] = {
 static void
 list_all_sources ()
 {
-  GrlMediaPlugin **source;
-  GrlMediaPlugin **sources;
+  GList *sources = NULL;
+  GList *sources_iter;
 
   sources = grl_plugin_registry_get_sources (registry, FALSE);
-  for (source = sources; *source; source++) {
+
+  for (sources_iter = sources; sources_iter;
+      sources_iter = g_list_next (sources_iter)) {
+    GrlMediaPlugin *source;
+
+    source = GRL_MEDIA_PLUGIN (sources_iter->data);
     g_print ("%s:  %s\n",
-             grl_media_plugin_get_id (*source),
-             grl_metadata_source_get_id (GRL_METADATA_SOURCE (*source)));
+             grl_media_plugin_get_id (source),
+             grl_metadata_source_get_id (GRL_METADATA_SOURCE (source)));
   }
-  g_free (sources);
+  g_list_free (sources);
 }
 
 static void
diff --git a/tools/grilo-test-ui/main.c b/tools/grilo-test-ui/main.c
index 0f31bb0..aee7aa4 100644
--- a/tools/grilo-test-ui/main.c
+++ b/tools/grilo-test-ui/main.c
@@ -1754,7 +1754,7 @@ load_plugins (void)
 static void
 shutdown_plugins (void)
 {
-  GrlMediaPlugin **sources;
+  GList *sources = NULL;
   GrlPluginRegistry *registry;
 
   /* Cancel previous operation, if any */
@@ -1772,14 +1772,18 @@ shutdown_plugins (void)
 
   /* Shut down the plugins now */
   sources = grl_plugin_registry_get_sources (registry, FALSE);
-  while (sources && sources[0]) {
+  while (sources) {
     const gchar *plugin_id;
-    plugin_id = grl_media_plugin_get_id (sources[0]);
+    GrlMediaPlugin *source;
+
+    source = GRL_MEDIA_PLUGIN (sources->data);
+    plugin_id = grl_media_plugin_get_id (source);
     grl_plugin_registry_unload (registry, plugin_id);
-    g_free (sources);
+
+    g_list_free (sources);
     sources = grl_plugin_registry_get_sources (registry, FALSE);
   }
-  g_free (sources);
+  g_list_free (sources);
 
   /* Re-enable "source-removed" handler */
   g_signal_handlers_unblock_by_func (G_OBJECT (registry), source_removed_cb,
-- 
1.7.0.4



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