#include <grilo.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "example"

static void
source_added_cb (GrlPluginRegistry *registry, gpointer user_data)
{
  g_debug ("Detected new source available: '%s'",
	   grl_metadata_source_get_name (GRL_METADATA_SOURCE (user_data)));

  /* Usually you may add the new service to the user interface so the user
     can interact with it (browse, search, etc) */
}

static void
source_removed_cb (GrlPluginRegistry *registry, gpointer user_data)
{
  g_debug ("Source '%s' is gone",
	   grl_metadata_source_get_name (GRL_METADATA_SOURCE (user_data)));

  /* Usually you would inform the user that this service is no longer
     available (for example a UPnP server was shutdown) and remove it
     from the user interface. */
}

static void
load_plugins (void)
{
  GrlPluginRegistry *registry;

  registry = grl_plugin_registry_get_default ();

  /* These callback will be invoked when media providers
     are loaded/unloaded */
  g_signal_connect (registry, "source-added",
		    G_CALLBACK (source_added_cb), NULL);
  g_signal_connect (registry, "source-removed",
		    G_CALLBACK (source_removed_cb), NULL);

  /* Command the registry to load all available plugins.
     The registry will look for plugins in the default
     plugin path and directories specified using the
     GRL_PLUGIN_PATH environment variable */
  if (!grl_plugin_registry_load_all (registry)) {
    g_error ("Failed to load plugins.");
  }

  GrlMediaPlugin **sources = grl_plugin_registry_get_sources (registry, FALSE);
  int i = 0;
  while (sources[i]) {
    grl_plugin_registry_unload (registry, grl_media_plugin_get_id (GRL_MEDIA_PLUGIN (sources[i])));
    grl_plugin_registry_unregister_source (registry, sources[i]);
    i ++;
  }
  g_free (sources);

  i = 0;
  sources = grl_plugin_registry_get_sources (registry, FALSE);
  while (sources[i]) {
    g_print ("blabla");
    i ++;
  }
  g_free (sources);
}

gint
main (int argc, gchar *argv[])
{
  GMainLoop *loop;

  grl_init (&argc, &argv);
  grl_log_init ("example:*"); /* Filter debug output */
  load_plugins ();            /* Load Grilo plugins */

  /* Run the main loop */
  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  return 0;
}
