[gnome-builder] plugins: load matching .gresources file bundled with plugins



commit 841794b3e4e3c16c70242dbddadce6c5d45378fb
Author: Christian Hergert <chergert redhat com>
Date:   Wed Mar 22 13:51:30 2017 -0700

    plugins: load matching .gresources file bundled with plugins
    
    If the plugin ships a .gresources file alongside the plugin, we can load
    that before loading the plugin. This simplifies plugins to be able to use
    GResources even when using a language such as Python.

 libide/application/ide-application-plugins.c |   72 ++++++++++++++++++++++++++
 libide/application/ide-application-private.h |    1 +
 libide/application/ide-application.c         |    1 +
 3 files changed, 74 insertions(+), 0 deletions(-)
---
diff --git a/libide/application/ide-application-plugins.c b/libide/application/ide-application-plugins.c
index 34b4928..73e6831 100644
--- a/libide/application/ide-application-plugins.c
+++ b/libide/application/ide-application-plugins.c
@@ -215,6 +215,65 @@ _ide_application_plugin_get_settings (IdeApplication *self,
   return settings;
 }
 
+static void
+ide_application_plugins_load_plugin_gresources (IdeApplication *self,
+                                                PeasPluginInfo *plugin_info,
+                                                PeasEngine     *engine)
+{
+  g_autofree gchar *gresources_path = NULL;
+  g_autofree gchar *gresources_basename = NULL;
+  const gchar *module_dir;
+  const gchar *module_name;
+
+  g_assert (IDE_IS_APPLICATION (self));
+  g_assert (plugin_info != NULL);
+  g_assert (PEAS_IS_ENGINE (engine));
+
+  module_dir = peas_plugin_info_get_module_dir (plugin_info);
+  module_name = peas_plugin_info_get_module_name (plugin_info);
+  gresources_basename = g_strdup_printf ("%s.gresources", module_name);
+  gresources_path = g_build_filename (module_dir, gresources_basename, NULL);
+
+  if (g_file_test (gresources_path, G_FILE_TEST_IS_REGULAR))
+    {
+      g_autoptr(GError) error = NULL;
+      GResource *resource;
+
+      resource = g_resource_load (gresources_path, &error);
+
+      if (resource == NULL)
+        {
+          g_warning ("Failed to load gresources: %s", error->message);
+          return;
+        }
+
+      g_hash_table_insert (self->plugin_gresources, g_strdup (module_name), resource);
+      g_resources_register (resource);
+    }
+}
+
+static void
+ide_application_plugins_unload_plugin_gresources (IdeApplication *self,
+                                                  PeasPluginInfo *plugin_info,
+                                                  PeasEngine     *engine)
+{
+  const gchar *module_name;
+  GResource *resources;
+
+  g_assert (IDE_IS_APPLICATION (self));
+  g_assert (plugin_info != NULL);
+  g_assert (PEAS_IS_ENGINE (engine));
+
+  module_name = peas_plugin_info_get_module_name (plugin_info);
+  resources = g_hash_table_lookup (self->plugin_gresources, module_name);
+
+  if (resources != NULL)
+    {
+      g_resources_unregister (resources);
+      g_hash_table_remove (self->plugin_gresources, module_name);
+    }
+}
+
 void
 ide_application_load_plugins (IdeApplication *self)
 {
@@ -224,6 +283,7 @@ ide_application_load_plugins (IdeApplication *self)
   g_return_if_fail (IDE_IS_APPLICATION (self));
 
   engine = peas_engine_get_default ();
+
   list = peas_engine_get_plugin_list (engine);
 
   for (; list; list = list->next)
@@ -433,6 +493,8 @@ ide_application_init_plugin_accessories (IdeApplication *self)
   g_assert (IDE_IS_APPLICATION (self));
 
   self->merge_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+  self->plugin_gresources = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
+                                                   (GDestroyNotify)g_resource_unref);
 
   engine = peas_engine_get_default ();
 
@@ -446,6 +508,11 @@ ide_application_init_plugin_accessories (IdeApplication *self)
                            G_CALLBACK (ide_application_load_plugin_css),
                            self,
                            G_CONNECT_AFTER | G_CONNECT_SWAPPED);
+  g_signal_connect_object (engine,
+                           "load-plugin",
+                           G_CALLBACK (ide_application_plugins_load_plugin_gresources),
+                           self,
+                           G_CONNECT_SWAPPED);
 
   g_signal_connect_object (engine,
                            "unload-plugin",
@@ -457,6 +524,11 @@ ide_application_init_plugin_accessories (IdeApplication *self)
                            G_CALLBACK (ide_application_unload_plugin_css),
                            self,
                            G_CONNECT_SWAPPED);
+  g_signal_connect_object (engine,
+                           "unload-plugin",
+                           G_CALLBACK (ide_application_plugins_unload_plugin_gresources),
+                           self,
+                           G_CONNECT_AFTER | G_CONNECT_SWAPPED);
 
   list = peas_engine_get_plugin_list (engine);
 
diff --git a/libide/application/ide-application-private.h b/libide/application/ide-application-private.h
index 2da33ca..044fb37 100644
--- a/libide/application/ide-application-private.h
+++ b/libide/application/ide-application-private.h
@@ -59,6 +59,7 @@ struct _IdeApplication
   GHashTable          *merge_ids;
 
   GHashTable          *plugin_css;
+  GHashTable          *plugin_gresources;
 
   GList               *test_funcs;
 
diff --git a/libide/application/ide-application.c b/libide/application/ide-application.c
index 4e8ac76..425d94a 100644
--- a/libide/application/ide-application.c
+++ b/libide/application/ide-application.c
@@ -499,6 +499,7 @@ ide_application_finalize (GObject *object)
   g_clear_pointer (&self->plugin_css, g_hash_table_unref);
   g_clear_pointer (&self->plugin_settings, g_hash_table_unref);
   g_clear_pointer (&self->reapers, g_ptr_array_unref);
+  g_clear_pointer (&self->plugin_gresources, g_hash_table_unref);
   g_clear_object (&self->worker_manager);
   g_clear_object (&self->keybindings);
   g_clear_object (&self->recent_projects);


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